package com.zsc.edu.gateway.rest.message;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsc.edu.gateway.MockMvcConfigBase;
import com.zsc.edu.gateway.domain.message.BulletinBuilder;
import com.zsc.edu.gateway.framework.security.UserDetailsImpl;
import com.zsc.edu.gateway.modules.message.controller.BulletinController;
import com.zsc.edu.gateway.modules.message.dto.BulletinDto;
import com.zsc.edu.gateway.modules.message.entity.Bulletin;
import com.zsc.edu.gateway.modules.message.service.BulletinService;
import org.assertj.core.util.Lists;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Spy;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;

import java.util.List;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(BulletinController.class)
public class BulletinControllerTest extends MockMvcConfigBase {
    @Spy
    private static Bulletin bulletin1;
    private static Bulletin bulletin2;
    @MockBean
    private BulletinService service;

    @BeforeAll
    static void beforeAll() {
        bulletin1 = BulletinBuilder.bBulletin().title("title1").content("content1").top(true).build();
        bulletin1.setId(1L);
        bulletin2 = BulletinBuilder.bBulletin().title("title2").content("content2").top(false).build();
    }

    @Test
    void create() throws Exception {
        BulletinDto dto = new BulletinDto();
        dto.setTitle(bulletin1.getTitle());
        dto.setContent(bulletin1.getContent());
        dto.setTop(bulletin1.getTop());
        when(service.create(any(UserDetailsImpl.class), any(BulletinDto.class))).thenReturn(bulletin1);
        mockMvc.perform(post("/api/rest/bulletin")
                        .with(csrf().asHeader())
                        .with(user(userDetails))
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(dto))
                )
                .andExpect(status().isOk())
                .andDo(print());
        verify(service).create(any(UserDetailsImpl.class), any(BulletinDto.class));
    }

    @Test
    void list() throws Exception {
        List<Bulletin> bulletins = Lists.newArrayList(bulletin1, bulletin2);
        when(service.list()).thenReturn(bulletins);
        mockMvc.perform(get("/api/rest/bulletin").with(user(userDetails))
        ).andExpect(status().isOk()).andDo(print());
        verify(service).page(any(Page.class), any(LambdaQueryWrapper.class));
    }

    @Test
    void update() throws Exception {
        BulletinDto dto = new BulletinDto();
        dto.setTitle(bulletin1.getTitle());
        dto.setContent(bulletin1.getContent());
        dto.setTop(bulletin1.getTop());
        when(service.update(any(UserDetailsImpl.class), any(BulletinDto.class), anyLong())).thenReturn(true);
        mockMvc.perform(patch("/api/rest/bulletin/{id}", bulletin1.getId())
                        .with(csrf().asHeader())
                        .with(user(userDetails))
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(dto))
                )
                .andExpect(status().isOk())
                .andDo(print());
        verify(service).update(any(UserDetailsImpl.class), any(BulletinDto.class), anyLong());
    }


}
