package com.ruoyi.web.modules.news.controller;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.Collections;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.framework.web.exception.GlobalExceptionHandler;
import com.ruoyi.web.modules.farming.domain.BizTechResource;
import com.ruoyi.web.modules.news.LivestockResourceTestSamples;
import com.ruoyi.web.modules.news.service.ILivestockResourceService;
import com.ruoyi.web.modules.news.support.LivestockResourceRules;
import com.ruoyi.web.modules.news.support.LivestockResourceSourceTypeValidator;
import com.ruoyi.web.modules.news.support.LivestockResourceTypeValidator;

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
@DisplayName("畜牧资源移动端 HTTP MockMvc")
class BizAppLivestockResourceControllerApiTest
{
    @Mock
    private ILivestockResourceService livestockResourceService;

    @Mock
    private LivestockResourceTypeValidator livestockResourceTypeValidator;

    @Mock
    private LivestockResourceSourceTypeValidator livestockResourceSourceTypeValidator;

    @InjectMocks
    private BizAppLivestockResourceController controller;

    private MockMvc mockMvc;

    @BeforeEach
    void setUp()
    {
        mockMvc = MockMvcBuilders.standaloneSetup(controller)
                .setControllerAdvice(new GlobalExceptionHandler())
                .build();
        SysUser u = new SysUser();
        u.setUserId(50L);
        u.setUserName("herder");
        LoginUser lu = new LoginUser(50L, 200L, u, Collections.emptySet());
        SecurityContextHolder.getContext().setAuthentication(
                new UsernamePasswordAuthenticationToken(lu, "", lu.getAuthorities()));
    }

    @AfterEach
    void tearDown()
    {
        SecurityContextHolder.clearContext();
        PageHelper.clearPage();
    }

    @Test
    @DisplayName("GET /app/livestockResource/list?type=004006")
    void listByType() throws Exception
    {
        when(livestockResourceService.selectLivestockResourceList(any()))
                .thenReturn(Collections.singletonList(LivestockResourceTestSamples.sampleListVo()));
        mockMvc.perform(get("/app/livestockResource/list").param("type", LivestockResourceRules.TYPE_ACHIEVEMENT))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.rows[0].title").value("高原牦牛繁育技术成果"))
                .andExpect(jsonPath("$.rows[0].id").value(101))
                .andExpect(jsonPath("$.rows[0].sourceType").value(LivestockResourceRules.SOURCE_TECH))
                .andExpect(jsonPath("$.rows[0].type").value(LivestockResourceRules.TYPE_ACHIEVEMENT));
    }

    @Test
    @DisplayName("GET /app/livestockResource/list 分页参数")
    void listWithPagination() throws Exception
    {
        when(livestockResourceService.selectLivestockResourceList(any()))
                .thenReturn(Collections.singletonList(LivestockResourceTestSamples.sampleListVo()));
        mockMvc.perform(get("/app/livestockResource/list")
                .param("type", LivestockResourceRules.TYPE_INSTRUMENT)
                .param("pageNum", "2")
                .param("pageSize", "20"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.total").exists());
    }

    @Test
    @DisplayName("GET /app/livestockResource/{id}?type=2")
    void getInfoTech() throws Exception
    {
        BizTechResource detail = LivestockResourceTestSamples.sampleTechDetail();
        when(livestockResourceService.selectLivestockResourceById(22L, LivestockResourceRules.SOURCE_TECH))
                .thenReturn(detail);
        mockMvc.perform(get("/app/livestockResource/22").param("type", "2"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.id").value(22))
                .andExpect(jsonPath("$.data.resourceName").value("高原牦牛繁育技术成果"))
                .andExpect(jsonPath("$.data.resourceType").value(LivestockResourceRules.TYPE_ACHIEVEMENT));
    }

    @Test
    @DisplayName("GET /app/livestockResource/{id}?type=1")
    void getInfoMedical() throws Exception
    {
        when(livestockResourceService.selectLivestockResourceById(11L, LivestockResourceRules.SOURCE_MEDICAL))
                .thenReturn(LivestockResourceTestSamples.sampleMedicalDetail());
        mockMvc.perform(get("/app/livestockResource/11").param("type", "1"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.resourceName").value("高原牦牛专家团队"));
    }
}
