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

//import com.zsc.edu.gateway.MockMvcConfigBase;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsc.edu.gateway.MockMvcConfigBase;
import com.zsc.edu.gateway.domain.system.DeptBuilder;
import com.zsc.edu.gateway.modules.system.controller.DeptController;
import com.zsc.edu.gateway.modules.system.dto.DeptDto;
import com.zsc.edu.gateway.modules.system.entity.Dept;
import com.zsc.edu.gateway.modules.system.service.DeptService;
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.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(DeptController.class)
public class DeptControllerTest extends MockMvcConfigBase {

    @Spy
    private static Dept dept1;

    private static Dept dept2;

    @MockBean
    private DeptService service;



    @BeforeAll
    static void beforeAll() {
        dept1 = DeptBuilder.aDept().name("测试部门1").build();
        dept1.setId(1L);
        dept2 = DeptBuilder.aDept().name("测试部门2").pid(dept1.id).build();
    }

    @Test
    void create() throws Exception{
        DeptDto dto = new DeptDto();
        dto.name  = dept1.getName();
        when(service.create(any())).thenReturn(dept1);
        mockMvc.perform(post("/api/rest/dept")
                .with(csrf().asHeader())
                .with(user(userDetails))
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(dto))
            ).andExpect(status().isOk()).andDo(print());
        verify(service).create(any());
    }

    @Test
    void list() throws Exception {
        List<Dept> depts = Lists.newArrayList(dept1, dept2);
        Page<Dept> pageResult = new Page<>();
        pageResult.setRecords(depts);
        pageResult.setTotal(depts.size());

        when(service.page(any(Page.class), any())).thenReturn(pageResult);
        mockMvc.perform(get("/api/rest/dept")
                        .with(user(userDetails))
                ).andExpect(status().isOk())
                .andDo(print());
        verify(service).page(any(Page.class), any());
    }

    @Test
    void update() throws Exception {
        DeptDto dto = new DeptDto();
        dto.name  = dept1.getName();
        when(service.edit(any(), any())).thenReturn(true);
        mockMvc.perform(patch("/api/rest/dept/1")
                    .with(csrf().asHeader())
                    .with(user(userDetails))
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(objectMapper.writeValueAsString(dto))
                )
                .andExpect(status().isOk()).andDo(print());
        verify(service).edit(any(), any());
    }
}
