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

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.verify;
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.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
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.content.domain.BizMallServiceAgreement;
import com.ruoyi.web.modules.content.service.IMallServiceAgreementService;
import com.ruoyi.web.modules.content.vo.MallServiceAgreementConfigVO;

/**
 * 平台商城服务协议接口测试（MSA-API）
 */
@ExtendWith(MockitoExtension.class)
class MallServiceAgreementControllerTest
{
    @Mock
    private IMallServiceAgreementService agreementService;

    @Spy
    @InjectMocks
    private MallServiceAgreementController 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 getConfig_returns200() throws Exception
    {
        MallServiceAgreementConfigVO vo = new MallServiceAgreementConfigVO();
        vo.setAgreementTitle("农资商城用户服务协议");
        vo.setEnableFlag("1");
        when(agreementService.getConfig()).thenReturn(vo);

        mockMvc.perform(get("/agri/mallServiceAgreement"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.agreementTitle").value("农资商城用户服务协议"));
    }

    @Test
    void save_returns200() throws Exception
    {
        doNothing().when(agreementService).saveConfig(any(), eq("admin"));

        BizMallServiceAgreement body = new BizMallServiceAgreement();
        body.setAgreementTitle("农资商城用户服务协议");
        body.setContent("<p>正文</p>");
        body.setEnableFlag("1");

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

        verify(agreementService).saveConfig(any(), eq("admin"));
    }
}
