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

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsc.edu.gateway.MockMvcConfigBase;
import com.zsc.edu.gateway.domain.message.UserNoticeBuilder;
import com.zsc.edu.gateway.modules.message.controller.UserNoticeController;
import com.zsc.edu.gateway.modules.message.dto.UserNoticeDto;
import com.zsc.edu.gateway.modules.message.entity.NoticeType;
import com.zsc.edu.gateway.modules.message.entity.UserNotice;
import com.zsc.edu.gateway.modules.message.query.AdminNoticeQuery;
import com.zsc.edu.gateway.modules.message.service.UserNoticeService;
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.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
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.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(UserNoticeController.class)
public class UserNoticeControllerTest extends MockMvcConfigBase {
    @Spy
    private static UserNotice userNotice1;
    private static UserNotice userNotice2;
    @MockBean
    private UserNoticeService service;

    @BeforeAll
    static void beforeAll() {
        userNotice1 = UserNoticeBuilder.builder().setNoticeId(1L).setUserId(1L).setIsRead(true).build();
        userNotice2 = UserNoticeBuilder.builder().setNoticeId(1L).setUserId(1L).setIsRead(false).build();
    }

    @Test
    void list() throws Exception {
        List<UserNotice> userNotices = Lists.newArrayList(userNotice1, userNotice2);
        Page<UserNotice> pageResult = new Page<>();
        pageResult.setRecords(userNotices);
        pageResult.setTotal((long) userNotices.size());
        when(service.getAdminNoticePage(any(Page.class), any(AdminNoticeQuery.class))).thenReturn(pageResult);
        mockMvc.perform(get("/api/rest/message")
                        .with(user(userDetails))
                )
                .andExpect(status().isOk())
                .andDo(print());
        verify(service).getAdminNoticePage(any(Page.class), any(AdminNoticeQuery.class));
    }

    @Test
    void create() throws Exception {
        UserNoticeDto dto = new UserNoticeDto();
        dto.setTitle("测试创建消息");
        dto.setContent("测试创建消息");
        dto.setType(NoticeType.MESSAGE);
        when(service.createByAdmin(any(UserNoticeDto.class))).thenReturn(true);
        mockMvc.perform(post("/api/rest/message")
                        .with(csrf().asHeader())
                        .with(user(userDetails))
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(dto))
                )
                .andExpect(status().isOk())
                .andDo(print());
        verify(service).createByAdmin(any(UserNoticeDto.class));
    }
}
