package cn.iocoder.yudao.module.configure.service.tracelink;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.configure.controller.admin.tracelink.vo.TraceLinkPageReqVO;
import cn.iocoder.yudao.module.configure.dal.dataobject.tracelink.TraceLinkDO;
import cn.iocoder.yudao.module.configure.dal.mysql.tracelink.TraceLinkMapper;
import cn.iocoder.yudao.module.configure.enums.ErrorCodeConstants;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Import;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

/**
* {@link TraceLinkServiceImpl} 的单元测试类
*
* @author 芋道源码
*/
@Import(TraceLinkServiceImpl.class)
public class TraceLinkServiceImplTest extends BaseDbUnitTest {

    @Resource
    private TraceLinkServiceImpl traceLinkService;

    @Resource
    private TraceLinkMapper traceLinkMapper;

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

        // 调用
        traceLinkService.deleteTraceLink(id);
       // 校验数据不存在了
       assertNull(traceLinkMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> traceLinkService.deleteTraceLink(id), ErrorCodeConstants.TRACE_LINK_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetTraceLinkPage() {
       // mock 数据
       TraceLinkDO dbTraceLink = randomPojo(TraceLinkDO.class, o -> { // 等会查询到
           o.setTraceTemplateId(null);
           o.setTraceTemplateName(null);
           o.setLinkId(null);
           o.setLinkName(null);
           o.setBelongBusinessId(null);
           o.setBelongBusinessName(null);
           o.setCreateTime(null);
       });
       traceLinkMapper.insert(dbTraceLink);
       // 测试 traceTemplateId 不匹配
       traceLinkMapper.insert(cloneIgnoreId(dbTraceLink, o -> o.setTraceTemplateId(null)));
       // 测试 traceTemplateName 不匹配
       traceLinkMapper.insert(cloneIgnoreId(dbTraceLink, o -> o.setTraceTemplateName(null)));
       // 测试 linkId 不匹配
       traceLinkMapper.insert(cloneIgnoreId(dbTraceLink, o -> o.setLinkId(null)));
       // 测试 linkName 不匹配
       traceLinkMapper.insert(cloneIgnoreId(dbTraceLink, o -> o.setLinkName(null)));
       // 测试 belongBusinessId 不匹配
       traceLinkMapper.insert(cloneIgnoreId(dbTraceLink, o -> o.setBelongBusinessId(null)));
       // 测试 belongBusinessName 不匹配
       traceLinkMapper.insert(cloneIgnoreId(dbTraceLink, o -> o.setBelongBusinessName(null)));
       // 测试 createTime 不匹配
       traceLinkMapper.insert(cloneIgnoreId(dbTraceLink, o -> o.setCreateTime(null)));
       // 准备参数
       TraceLinkPageReqVO reqVO = new TraceLinkPageReqVO();
        LocalDateTime[] localDateTimes = LocalDateTimeUtils.buildBetweenTime(2023, 2, 1, 2023, 2, 28);
        Date[] dates = new Date[localDateTimes.length];
        for(int i=0; i<localDateTimes.length; i++) {
            dates[i] = Date.from(localDateTimes[i].atZone(ZoneId.systemDefault()).toInstant());
        }
       reqVO.setTraceTemplateId(null);
       reqVO.setTraceTemplateName(null);
       reqVO.setLinkId(null);
       reqVO.setLinkName(null);
       reqVO.setLinkType(null);
       reqVO.setBelongBusinessId(null);
       reqVO.setBelongBusinessName(null);
       reqVO.setCreateTime(dates);

       // 调用
       PageResult<TraceLinkDO> pageResult = traceLinkService.getTraceLinkPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbTraceLink, pageResult.getList().get(0));
    }

}
