package cn.newfeifan.mall.module.trade.service.ordercallbacklogs;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import javax.annotation.Resource;

import cn.newfeifan.mall.framework.test.core.ut.BaseDbUnitTest;

import cn.newfeifan.mall.module.trade.controller.admin.ordercallbacklogs.vo.*;
import cn.newfeifan.mall.module.trade.dal.dataobject.ordercallbacklogs.OrderCallbackLogsDO;
import cn.newfeifan.mall.module.trade.dal.mysql.ordercallbacklogs.OrderCallbackLogsMapper;
import cn.newfeifan.mall.framework.common.pojo.PageResult;

import org.springframework.context.annotation.Import;

import static cn.newfeifan.mall.module.trade.enums.ErrorCodeConstants.*;
import static cn.newfeifan.mall.framework.test.core.util.AssertUtils.*;
import static cn.newfeifan.mall.framework.test.core.util.RandomUtils.*;
import static cn.newfeifan.mall.framework.common.util.date.LocalDateTimeUtils.*;
import static cn.newfeifan.mall.framework.common.util.object.ObjectUtils.*;
import static org.junit.jupiter.api.Assertions.*;

/**
 * {@link OrderCallbackLogsServiceImpl} 的单元测试类
 *
 * @author 非繁人
 */
@Import(OrderCallbackLogsServiceImpl.class)
public class OrderCallbackLogsServiceImplTest extends BaseDbUnitTest {

    @Resource
    private OrderCallbackLogsServiceImpl orderCallbackLogsService;

    @Resource
    private OrderCallbackLogsMapper orderCallbackLogsMapper;

    @Test
    public void testCreateOrderCallbackLogs_success() {
        // 准备参数
        OrderCallbackLogsSaveReqVO createReqVO = randomPojo(OrderCallbackLogsSaveReqVO.class).setId(null);

        // 调用
        Long orderCallbackLogsId = orderCallbackLogsService.createOrderCallbackLogs(createReqVO);
        // 断言
        assertNotNull(orderCallbackLogsId);
        // 校验记录的属性是否正确
        OrderCallbackLogsDO orderCallbackLogs = orderCallbackLogsMapper.selectById(orderCallbackLogsId);
        assertPojoEquals(createReqVO, orderCallbackLogs, "id");
    }

    @Test
    public void testUpdateOrderCallbackLogs_success() {
        // mock 数据
        OrderCallbackLogsDO dbOrderCallbackLogs = randomPojo(OrderCallbackLogsDO.class);
        orderCallbackLogsMapper.insert(dbOrderCallbackLogs);// @Sql: 先插入出一条存在的数据
        // 准备参数
        OrderCallbackLogsSaveReqVO updateReqVO = randomPojo(OrderCallbackLogsSaveReqVO.class, o -> {
            o.setId(dbOrderCallbackLogs.getId()); // 设置更新的 ID
        });

        // 调用
        orderCallbackLogsService.updateOrderCallbackLogs(updateReqVO);
        // 校验是否更新正确
        OrderCallbackLogsDO orderCallbackLogs = orderCallbackLogsMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, orderCallbackLogs);
    }

    @Test
    public void testUpdateOrderCallbackLogs_notExists() {
        // 准备参数
        OrderCallbackLogsSaveReqVO updateReqVO = randomPojo(OrderCallbackLogsSaveReqVO.class);

        // 调用, 并断言异常
        assertServiceException(() -> orderCallbackLogsService.updateOrderCallbackLogs(updateReqVO), ORDER_CALLBACK_LOGS_NOT_EXISTS);
    }

    @Test
    public void testDeleteOrderCallbackLogs_success() {
        // mock 数据
        OrderCallbackLogsDO dbOrderCallbackLogs = randomPojo(OrderCallbackLogsDO.class);
        orderCallbackLogsMapper.insert(dbOrderCallbackLogs);// @Sql: 先插入出一条存在的数据
        // 准备参数
        Long id = dbOrderCallbackLogs.getId();

        // 调用
        orderCallbackLogsService.deleteOrderCallbackLogs(id);
       // 校验数据不存在了
       assertNull(orderCallbackLogsMapper.selectById(id));
    }

    @Test
    public void testDeleteOrderCallbackLogs_notExists() {
        // 准备参数
        Long id = randomLongId();

        // 调用, 并断言异常
        assertServiceException(() -> orderCallbackLogsService.deleteOrderCallbackLogs(id), ORDER_CALLBACK_LOGS_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetOrderCallbackLogsPage() {
       // mock 数据
       OrderCallbackLogsDO dbOrderCallbackLogs = randomPojo(OrderCallbackLogsDO.class, o -> { // 等会查询到
           o.setOrderId(null);
           o.setRequestParam(null);
           o.setCreateTime(null);
           o.setResponseBody(null);
           o.setStatus(null);
       });
       orderCallbackLogsMapper.insert(dbOrderCallbackLogs);
       // 测试 orderId 不匹配
       orderCallbackLogsMapper.insert(cloneIgnoreId(dbOrderCallbackLogs, o -> o.setOrderId(null)));
       // 测试 requestParam 不匹配
       orderCallbackLogsMapper.insert(cloneIgnoreId(dbOrderCallbackLogs, o -> o.setRequestParam(null)));
       // 测试 createTime 不匹配
       orderCallbackLogsMapper.insert(cloneIgnoreId(dbOrderCallbackLogs, o -> o.setCreateTime(null)));
       // 测试 responseBody 不匹配
       orderCallbackLogsMapper.insert(cloneIgnoreId(dbOrderCallbackLogs, o -> o.setResponseBody(null)));
       // 测试 status 不匹配
       orderCallbackLogsMapper.insert(cloneIgnoreId(dbOrderCallbackLogs, o -> o.setStatus(null)));
       // 准备参数
       OrderCallbackLogsPageReqVO reqVO = new OrderCallbackLogsPageReqVO();
       reqVO.setOrderId(null);
       reqVO.setRequestParam(null);
       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setResponseBody(null);
       reqVO.setStatus(null);

       // 调用
       PageResult<OrderCallbackLogsDO> pageResult = orderCallbackLogsService.getOrderCallbackLogsPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbOrderCallbackLogs, pageResult.getList().get(0));
    }

}