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

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
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 org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.web.modules.category.constant.CategoryConstants;
import com.ruoyi.web.modules.category.domain.BizGoodsCategory;
import com.ruoyi.web.modules.category.exception.CategoryBatchDeleteException;
import com.ruoyi.web.modules.category.service.IPlatformCategoryService;
import com.ruoyi.web.modules.category.vo.CategoryRowVO;
import com.ruoyi.web.modules.category.vo.CategoryTreeResultVO;
import com.ruoyi.web.modules.category.vo.CategoryTreeVO;

/**
 * 平台端分类接口测试
 */
@ExtendWith(MockitoExtension.class)
class PlatformCategoryControllerTest
{
    @Mock
    private IPlatformCategoryService platformCategoryService;

    @Spy
    @InjectMocks
    private PlatformCategoryController controller;

    private MockMvc mockMvc;

    private final ObjectMapper objectMapper = new ObjectMapper();

    @BeforeEach
    void setUp() throws Exception
    {
        mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
        lenient().doReturn("admin").when(controller).getUsername();
    }

    @Test
    void list_returns200() throws Exception
    {
        CategoryRowVO row = new CategoryRowVO();
        row.setCategoryId(1L);
        row.setCategoryName("肥料");
        when(platformCategoryService.selectPlatformCategoryRowList(null)).thenReturn(Collections.singletonList(row));

        mockMvc.perform(get("/agri/category/list"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.rows[0].categoryName").value("肥料"));
    }

    @Test
    void tree_returns200() throws Exception
    {
        CategoryTreeResultVO vo = new CategoryTreeResultVO();
        vo.setShopName(CategoryConstants.PLATFORM_SCOPE_NAME);
        CategoryTreeVO node = new CategoryTreeVO();
        node.setCategoryId(1L);
        node.setCategoryName("肥料");
        vo.setTree(Collections.singletonList(node));
        when(platformCategoryService.selectPlatformCategoryTree(null)).thenReturn(vo);

        mockMvc.perform(get("/agri/category/tree"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.shopName").value(CategoryConstants.PLATFORM_SCOPE_NAME))
                .andExpect(jsonPath("$.data.tree[0].categoryName").value("肥料"));
    }

    @Test
    void add_returns200() throws Exception
    {
        when(platformCategoryService.insertPlatformCategory(any())).thenReturn(1);

        BizGoodsCategory body = new BizGoodsCategory();
        body.setCategoryLevel("1");
        body.setCategoryName("肥料");
        body.setCategoryPic("/profile/upload/c.jpg");
        body.setSortNo(0);
        body.setShowFlag("1");
        body.setHotFlag("0");

        mockMvc.perform(post("/agri/category")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(body)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200));
    }

    @Test
    void remove_batchFail_returnsReasons() throws Exception
    {
        doThrow(new CategoryBatchDeleteException("部分分类不满足删除条件，整批操作已取消",
                Collections.singletonList("分类「复合肥」下存在商品，请先处理商品")))
                .when(platformCategoryService).deletePlatformCategoryByIds(any(), any());

        mockMvc.perform(delete("/agri/category/10"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(500))
                .andExpect(jsonPath("$.data.reasons[0]").exists());
    }
}
