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.BizMerchantEntryAgreement;
import com.ruoyi.web.modules.content.service.IMerchantEntryAgreementService;
import com.ruoyi.web.modules.content.vo.MerchantEntryAgreementConfigVO;

/**
 * 平台入驻协议接口测试（MEA-API）
 */
@ExtendWith(MockitoExtension.class)
class MerchantEntryAgreementControllerTest
{
    @Mock
    private IMerchantEntryAgreementService agreementService;

    @Spy
    @InjectMocks
    private MerchantEntryAgreementController 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
    {
        MerchantEntryAgreementConfigVO vo = new MerchantEntryAgreementConfigVO();
        vo.setConfigId(1L);
        vo.setAgreementTitle("农资商城商家入驻协议");
        vo.setEnableFlag("1");
        when(agreementService.getConfig()).thenReturn(vo);

        mockMvc.perform(get("/agri/merchantEntryAgreement"))
                .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"));

        BizMerchantEntryAgreement body = new BizMerchantEntryAgreement();
        body.setAgreementTitle("农资商城商家入驻协议");
        body.setVersionLabel("v1.0");
        body.setContent("<p>正文</p>");
        body.setEnableFlag("1");

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

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