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

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
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.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.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.ruoyi.framework.web.exception.GlobalExceptionHandler;
import com.ruoyi.web.modules.category.domain.vo.InformationCategoryItemVo;
import com.ruoyi.web.modules.category.domain.vo.InformationCategoryTreeVo;
import com.ruoyi.web.modules.category.service.IBizInformationCategoryService;

@ExtendWith(MockitoExtension.class)
@DisplayName("资讯类别 HTTP MockMvc")
class BizInformationCategoryControllerApiTest
{
    @Mock
    private IBizInformationCategoryService bizInformationCategoryService;

    @InjectMocks
    private BizInformationCategoryController controller;

    private MockMvc mockMvc;

    @BeforeEach
    void setUp()
    {
        mockMvc = MockMvcBuilders.standaloneSetup(controller)
                .setControllerAdvice(new GlobalExceptionHandler())
                .build();
    }

    @Test
    @DisplayName("GET /children 返回 code、name")
    void children_englishFieldNames() throws Exception
    {
        when(bizInformationCategoryService.selectChildren(eq("001"), eq("1")))
                .thenReturn(Collections.singletonList(new InformationCategoryItemVo("001001", "繁育作业")));
        mockMvc.perform(get("/category/informationCategory/children")
                .param("parentCode", "001")
                .param("categoryType", "1"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data[0].code").value("001001"))
                .andExpect(jsonPath("$.data[0].name").value("繁育作业"));
    }

    @Test
    @DisplayName("GET /tree 返回 code、name、children")
    void tree_englishFieldNames() throws Exception
    {
        InformationCategoryTreeVo root = new InformationCategoryTreeVo();
        root.setCode("001");
        root.setName("养殖标准");
        InformationCategoryTreeVo child = new InformationCategoryTreeVo();
        child.setCode("001001");
        child.setName("繁育作业");
        root.setChildren(Collections.singletonList(child));
        when(bizInformationCategoryService.selectCategoryTree(any()))
                .thenReturn(Collections.singletonList(root));
        mockMvc.perform(get("/category/informationCategory/tree"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data[0].code").value("001"))
                .andExpect(jsonPath("$.data[0].name").value("养殖标准"))
                .andExpect(jsonPath("$.data[0].children[0].code").value("001001"))
                .andExpect(jsonPath("$.data[0].children[0].name").value("繁育作业"));
    }

    @Test
    @DisplayName("GET /children 无 categoryType")
    void children_withoutCategoryType() throws Exception
    {
        when(bizInformationCategoryService.selectChildren(eq("002"), isNull()))
                .thenReturn(Collections.singletonList(new InformationCategoryItemVo("002001", "高新技术")));
        mockMvc.perform(get("/category/informationCategory/children").param("parentCode", "002"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data[0].code").value("002001"));
    }
}
