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.news.FarmingNewsTestSamples;
import com.ruoyi.web.modules.news.service.IFarmingNewsService;
import com.ruoyi.web.modules.news.support.FarmingNewsTypeValidator;

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
@DisplayName("养殖资讯移动端 HTTP MockMvc")
class BizAppFarmingNewsControllerApiTest
{
    @Mock
    private IFarmingNewsService farmingNewsService;

    @Mock
    private FarmingNewsTypeValidator farmingNewsTypeValidator;

    @InjectMocks
    private BizAppFarmingNewsController 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/farmingNews/list?type=001001")
    void listByType() throws Exception
    {
        when(farmingNewsService.selectFarmingNewsList(any()))
                .thenReturn(Collections.singletonList(FarmingNewsTestSamples.sampleListVo()));
        mockMvc.perform(get("/app/farmingNews/list").param("type", FarmingNewsTestSamples.TYPE_BREEDING))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.rows[0].id").value(1001))
                .andExpect(jsonPath("$.rows[0].title").value("繁育作业标准示例"))
                .andExpect(jsonPath("$.rows[0].type").value(FarmingNewsTestSamples.TYPE_BREEDING))
                .andExpect(jsonPath("$.rows[0].coverFileUrl").value("/profile/upload/cover.png"))
                .andExpect(jsonPath("$.rows[0].contentFileUrl").value("/profile/upload/standard.pdf"));
    }

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

    @Test
    @DisplayName("GET /app/farmingNews/{id}?type=001001")
    void getInfoByTypeAndId() throws Exception
    {
        when(farmingNewsService.selectFarmingNewsById(1001L, FarmingNewsTestSamples.TYPE_BREEDING))
                .thenReturn(FarmingNewsTestSamples.sampleBreedingDetail());
        mockMvc.perform(get("/app/farmingNews/1001").param("type", FarmingNewsTestSamples.TYPE_BREEDING))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.id").value(1001))
                .andExpect(jsonPath("$.data.standardName").value("繁育作业标准示例"))
                .andExpect(jsonPath("$.data.standardType").value(FarmingNewsTestSamples.TYPE_BREEDING));
    }
}
