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

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
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 java.util.List;
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.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.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.technology.domain.PracticalTrainingMyEnrollDetailVo;
import com.ruoyi.web.modules.technology.domain.PracticalTrainingMyEnrollListVo;
import com.ruoyi.web.modules.technology.service.IBizPracticalTrainingAppService;

@ExtendWith(MockitoExtension.class)
@DisplayName("实战培训移动端 HTTP MockMvc")
class BizAppPracticalTrainingControllerApiTest
{
    @Mock
    private IBizPracticalTrainingAppService bizPracticalTrainingAppService;

    @InjectMocks
    private BizAppPracticalTrainingController controller;

    private MockMvc mockMvc;

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

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

    @Test
    @DisplayName("GET /app/practicalTraining/myEnroll/list")
    void myEnrollList() throws Exception
    {
        PracticalTrainingMyEnrollListVo item = new PracticalTrainingMyEnrollListVo();
        item.setId(1L);
        item.setTrainingTopic("牧业实战");
        when(bizPracticalTrainingAppService.selectMyEnrollList(eq(9L))).thenReturn(Collections.singletonList(item));

        mockMvc.perform(get("/app/practicalTraining/myEnroll/list"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.rows[0].trainingTopic").value("牧业实战"));
    }

    @Test
    @DisplayName("GET /app/practicalTraining/myEnroll/{id}")
    void myEnrollDetail() throws Exception
    {
        PracticalTrainingMyEnrollDetailVo vo = new PracticalTrainingMyEnrollDetailVo();
        vo.setId(1L);
        vo.setTrainingTopic("牧业实战");
        vo.setCanCancel(true);
        when(bizPracticalTrainingAppService.selectMyEnrollDetail(eq(1L), eq(9L))).thenReturn(vo);

        mockMvc.perform(get("/app/practicalTraining/myEnroll/1"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.canCancel").value(true));
    }

    @Test
    @DisplayName("POST /app/practicalTraining/{id}/enroll")
    void enroll() throws Exception
    {
        when(bizPracticalTrainingAppService.enroll(eq(2L), eq(9L), eq("牧民甲"), eq("herder"))).thenReturn(1);
        mockMvc.perform(post("/app/practicalTraining/2/enroll"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200));
    }

    @Test
    @DisplayName("POST /app/practicalTraining/{id}/cancelEnroll")
    void cancelEnroll() throws Exception
    {
        when(bizPracticalTrainingAppService.cancelEnroll(eq(3L), eq(9L), eq("herder"))).thenReturn(1);
        mockMvc.perform(post("/app/practicalTraining/3/cancelEnroll"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200));
    }
}
