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.AgriculturalCourseTestSamples;
import com.ruoyi.web.modules.news.service.IAgriculturalCourseService;
import com.ruoyi.web.modules.news.support.AgriculturalCourseTypeValidator;

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
@DisplayName("农技课堂移动端 HTTP MockMvc")
class BizAppAgriculturalCourseControllerApiTest
{
    @Mock
    private IAgriculturalCourseService agriculturalCourseService;

    @Mock
    private AgriculturalCourseTypeValidator agriculturalCourseTypeValidator;

    @InjectMocks
    private BizAppAgriculturalCourseController 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/agriculturalCourse/list?type=005001")
    void listByType() throws Exception
    {
        when(agriculturalCourseService.selectAgriculturalCourseList(any()))
                .thenReturn(Collections.singletonList(AgriculturalCourseTestSamples.sampleVideoListVo()));
        mockMvc.perform(get("/app/agriculturalCourse/list")
                .param("type", AgriculturalCourseTestSamples.TYPE_VIDEO_TOPIC))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.rows[0].id").value(101))
                .andExpect(jsonPath("$.rows[0].title").value("高原牦牛繁育视频课"))
                .andExpect(jsonPath("$.rows[0].type").value(AgriculturalCourseTestSamples.TYPE_VIDEO_TOPIC));
    }

    @Test
    @DisplayName("GET /app/agriculturalCourse/list 分页参数")
    void listWithPagination() throws Exception
    {
        when(agriculturalCourseService.selectAgriculturalCourseList(any()))
                .thenReturn(Collections.singletonList(AgriculturalCourseTestSamples.sampleVideoListVo()));
        mockMvc.perform(get("/app/agriculturalCourse/list")
                .param("type", AgriculturalCourseTestSamples.TYPE_TRAINING_ENROLLING)
                .param("pageNum", "2")
                .param("pageSize", "20"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.total").exists());
    }
}
