package com.cf.imes.module.system.controller.admin.sms;

import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.test.core.ut.BaseWebUnitTest;
import com.cf.imes.module.system.controller.admin.sms.vo.log.SmsLogPageReqVO;
import com.cf.imes.module.system.dal.dataobject.sms.SmsLogDO;
import com.cf.imes.module.system.service.sms.SmsLogService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;

import java.util.Collections;
import java.util.List;

import static org.mockito.ArgumentMatchers.any;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
 * @author Gqr
 * @since 2025/10/10 15:37
 */
@WebMvcTest(controllers = SmsLogController.class, excludeAutoConfiguration = SecurityAutoConfiguration.class)
class SmsLogControllerTest extends BaseWebUnitTest {
    @Autowired
    private MockMvc mockMvc;

    @MockitoBean
    private SmsLogService smsLogService;

    @Autowired
    private ObjectMapper objectMapper;
    /**
     * 测试分页查询接口
     */
    @Test
    void testGetSmsLogPage_success() throws Exception {
        // mock service 层返回分页结果
        SmsLogDO logDO = new SmsLogDO();
        logDO.setId(1L);
        logDO.setMobile("13800000000");
        logDO.setSendStatus(1);
        PageResult<SmsLogDO> pageResult = new PageResult<>(Collections.singletonList(logDO), 1L);
        Mockito.when(smsLogService.getSmsLogPage(any())).thenReturn(pageResult);

        // 构造查询参数
        SmsLogPageReqVO reqVO = new SmsLogPageReqVO();
        reqVO.setPageNo(1);
        reqVO.setPageSize(10);

        // 执行请求
        mockMvc.perform(get("/system/sms-log/page")
                        .contentType(MediaType.APPLICATION_JSON)
                        .param("pageNo", "1")
                        .param("pageSize", "10"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.list[0].mobile").value("13800000000"));
    }

    /**
     * 测试导出 Excel 接口
     */
    @Test
    void testExportSmsLogExcel_success() throws Exception {
        // mock service 层返回数据
        SmsLogDO logDO = new SmsLogDO();
        logDO.setId(2L);
        logDO.setMobile("13900000000");
        PageResult<SmsLogDO> pageResult = new PageResult<>(List.of(logDO), 1L);
        Mockito.when(smsLogService.getSmsLogPage(any())).thenReturn(pageResult);

        // 执行请求
        mockMvc.perform(get("/system/sms-log/export")
                        .contentType(MediaType.APPLICATION_JSON)
                        .param("pageNo", "1")
                        .param("pageSize", "10"))
                .andExpect(status().isOk())
                // 校验响应头是下载 Excel 文件
                .andExpect(header().string("Content-Disposition", org.hamcrest.Matchers.containsString("attachment")));
    }
}
