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

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
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.request.MockMvcRequestBuilders.put;
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.goodsservice.domain.BizGoodsService;
import com.ruoyi.web.modules.goodsservice.exception.GoodsServiceBatchDeleteException;
import com.ruoyi.web.modules.goodsservice.service.IGoodsServiceCatalogService;
/**
 * 平台商品服务接口测试（GSV-API）
 */
@ExtendWith(MockitoExtension.class)
class GoodsServiceControllerTest
{
    @Mock
    private IGoodsServiceCatalogService goodsServiceCatalogService;

    @Spy
    @InjectMocks
    private GoodsServiceController controller;

    private MockMvc mockMvc;

    private final ObjectMapper objectMapper = new ObjectMapper();

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

    @Test
    void getInfo_returns200() throws Exception
    {
        BizGoodsService row = new BizGoodsService();
        row.setServiceId(1L);
        row.setServiceName("正品保证");
        when(goodsServiceCatalogService.selectGoodsServiceById(1L)).thenReturn(row);

        mockMvc.perform(get("/agri/goodsService/1"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.serviceName").value("正品保证"));
    }

    @Test
    void add_returns200() throws Exception
    {
        when(goodsServiceCatalogService.insertGoodsService(any())).thenReturn(1);

        BizGoodsService body = new BizGoodsService();
        body.setServiceName("正品保证");
        body.setServiceIntro("平台认证正品");
        body.setServiceIcon("/profile/upload/s1.png");
        body.setDefaultShowFlag("1");
        body.setSortNo(0);

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

    @Test
    void edit_returns200() throws Exception
    {
        when(goodsServiceCatalogService.updateGoodsService(any())).thenReturn(1);

        BizGoodsService body = new BizGoodsService();
        body.setServiceId(1L);
        body.setServiceName("正品保证");
        body.setServiceIntro("更新简介");
        body.setServiceIcon("/profile/upload/s1.png");
        body.setDefaultShowFlag("0");
        body.setSortNo(1);

        mockMvc.perform(put("/agri/goodsService")
                .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 GoodsServiceBatchDeleteException("整批操作已取消",
                Collections.singletonList("服务「正品保证」已被商品使用，无法删除")))
                .when(goodsServiceCatalogService).deleteGoodsServiceByIds(any(), any());

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