package co.yixiang.yshop.module.member.service.burypoint;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;

import javax.annotation.Resource;

import co.yixiang.yshop.framework.test.core.ut.BaseDbUnitTest;

import co.yixiang.yshop.module.member.controller.admin.burypoint.vo.*;
import co.yixiang.yshop.module.member.dal.dataobject.burypoint.BuryPointDO;
import co.yixiang.yshop.module.member.dal.mysql.burypoint.BuryPointMapper;
import co.yixiang.yshop.framework.common.pojo.PageResult;

import javax.annotation.Resource;

import org.springframework.context.annotation.Import;

import java.util.*;
import java.time.LocalDateTime;

import static cn.hutool.core.util.RandomUtil.*;
import static co.yixiang.yshop.module.member.enums.ErrorCodeConstants.*;
import static co.yixiang.yshop.framework.test.core.util.AssertUtils.*;
import static co.yixiang.yshop.framework.test.core.util.RandomUtils.*;
import static co.yixiang.yshop.framework.common.util.date.LocalDateTimeUtils.*;
import static co.yixiang.yshop.framework.common.util.object.ObjectUtils.*;
import static co.yixiang.yshop.framework.common.util.date.DateUtils.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

/**
 * {@link BuryPointServiceImpl} 的单元测试类
 *
 * @author admin
 */
@Import(BuryPointServiceImpl.class)
public class BuryPointServiceImplTest extends BaseDbUnitTest {

    @Resource
    private BuryPointServiceImpl buryPointService;

    @Resource
    private BuryPointMapper buryPointMapper;

    @Test
    public void testCreateBuryPoint_success() {
        // 准备参数
        BuryPointCreateReqVO reqVO = randomPojo(BuryPointCreateReqVO.class);

        // 调用
        Long buryPointId = buryPointService.createBuryPoint(reqVO);
        // 断言
        assertNotNull(buryPointId);
        // 校验记录的属性是否正确
        BuryPointDO buryPoint = buryPointMapper.selectById(buryPointId);
        assertPojoEquals(reqVO, buryPoint);
    }

    @Test
    public void testUpdateBuryPoint_success() {
        // mock 数据
        BuryPointDO dbBuryPoint = randomPojo(BuryPointDO.class);
        buryPointMapper.insert(dbBuryPoint);// @Sql: 先插入出一条存在的数据
        // 准备参数
        BuryPointUpdateReqVO reqVO = randomPojo(BuryPointUpdateReqVO.class, o -> {
            o.setId(dbBuryPoint.getId()); // 设置更新的 ID
        });

        // 调用
        buryPointService.updateBuryPoint(reqVO);
        // 校验是否更新正确
        BuryPointDO buryPoint = buryPointMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, buryPoint);
    }

    @Test
    public void testUpdateBuryPoint_notExists() {
        // 准备参数
        BuryPointUpdateReqVO reqVO = randomPojo(BuryPointUpdateReqVO.class);

        // 调用, 并断言异常
        assertServiceException(() -> buryPointService.updateBuryPoint(reqVO), BURY_POINT_NOT_EXISTS);
    }

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

        // 调用
        buryPointService.deleteBuryPoint(id);
        // 校验数据不存在了
        assertNull(buryPointMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> buryPointService.deleteBuryPoint(id), BURY_POINT_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetBuryPointPage() {
        // mock 数据
        BuryPointDO dbBuryPoint = randomPojo(BuryPointDO.class, o -> { // 等会查询到
            o.setUserId(null);
            o.setEvent(null);
            o.setEventParameter(null);
            o.setCreateTime(null);
            o.setIp(null);
        });
        buryPointMapper.insert(dbBuryPoint);
        // 测试 userId 不匹配
        buryPointMapper.insert(cloneIgnoreId(dbBuryPoint, o -> o.setUserId(null)));
        // 测试 event 不匹配
        buryPointMapper.insert(cloneIgnoreId(dbBuryPoint, o -> o.setEvent(null)));
        // 测试 eventParameter 不匹配
        buryPointMapper.insert(cloneIgnoreId(dbBuryPoint, o -> o.setEventParameter(null)));
        // 测试 createTime 不匹配
        buryPointMapper.insert(cloneIgnoreId(dbBuryPoint, o -> o.setCreateTime(null)));
        // 测试 ip 不匹配
        buryPointMapper.insert(cloneIgnoreId(dbBuryPoint, o -> o.setIp(null)));
        // 准备参数
        BuryPointPageReqVO reqVO = new BuryPointPageReqVO();
        reqVO.setUserId(null);
        reqVO.setEvent(null);
        reqVO.setEventParameter(null);
        reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
        reqVO.setIp(null);

        // 调用
        PageResult<BuryPointDO> pageResult = buryPointService.getBuryPointPage(reqVO);
        // 断言
        assertEquals(1, pageResult.getTotal());
        assertEquals(1, pageResult.getList().size());
        assertPojoEquals(dbBuryPoint, pageResult.getList().get(0));
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetBuryPointList() {
        // mock 数据
        BuryPointDO dbBuryPoint = randomPojo(BuryPointDO.class, o -> { // 等会查询到
            o.setUserId(null);
            o.setEvent(null);
            o.setEventParameter(null);
            o.setCreateTime(null);
            o.setIp(null);
        });
        buryPointMapper.insert(dbBuryPoint);
        // 测试 userId 不匹配
        buryPointMapper.insert(cloneIgnoreId(dbBuryPoint, o -> o.setUserId(null)));
        // 测试 event 不匹配
        buryPointMapper.insert(cloneIgnoreId(dbBuryPoint, o -> o.setEvent(null)));
        // 测试 eventParameter 不匹配
        buryPointMapper.insert(cloneIgnoreId(dbBuryPoint, o -> o.setEventParameter(null)));
        // 测试 createTime 不匹配
        buryPointMapper.insert(cloneIgnoreId(dbBuryPoint, o -> o.setCreateTime(null)));
        // 测试 ip 不匹配
        buryPointMapper.insert(cloneIgnoreId(dbBuryPoint, o -> o.setIp(null)));
        // 准备参数
        BuryPointExportReqVO reqVO = new BuryPointExportReqVO();
        reqVO.setUserId(null);
        reqVO.setEvent(null);
        reqVO.setEventParameter(null);
        reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
        reqVO.setIp(null);

        // 调用
        List<BuryPointDO> list = buryPointService.getBuryPointList(reqVO);
        // 断言
        assertEquals(1, list.size());
        assertPojoEquals(dbBuryPoint, list.get(0));
    }

}
