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

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.tracelinkitem.vo.TraceLinkItemPageReqVO;
import cn.iocoder.yudao.module.configure.dal.dataobject.tracelinkitem.TraceLinkItemDO;
import cn.iocoder.yudao.module.configure.dal.mysql.tracelinkitem.TraceLinkItemMapper;
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 TraceLinkItemServiceImpl} 的单元测试类
*
* @author 芋道源码
*/
@Import(TraceLinkItemServiceImpl.class)
public class TraceLinkItemServiceImplTest extends BaseDbUnitTest {

    @Resource
    private TraceLinkItemServiceImpl traceLinkItemService;

    @Resource
    private TraceLinkItemMapper traceLinkItemMapper;

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

        // 调用
        traceLinkItemService.deleteTraceLinkItem(id);
       // 校验数据不存在了
       assertNull(traceLinkItemMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> traceLinkItemService.deleteTraceLinkItem(id), ErrorCodeConstants.TRACE_LINK_ITEM_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetTraceLinkItemPage() {
       // mock 数据
       TraceLinkItemDO dbTraceLinkItem = randomPojo(TraceLinkItemDO.class, o -> { // 等会查询到
           o.setTitle(null);
           o.setType(null);
           o.setContent(null);
           o.setAddTime(null);
           o.setLinkName(null);
           o.setLinkId(null);
           o.setBelongBusinessId(null);
           o.setBelongBusinessName(null);
           o.setCreateTime(null);
       });
       traceLinkItemMapper.insert(dbTraceLinkItem);
       // 测试 title 不匹配
       traceLinkItemMapper.insert(cloneIgnoreId(dbTraceLinkItem, o -> o.setTitle(null)));
       // 测试 type 不匹配
       traceLinkItemMapper.insert(cloneIgnoreId(dbTraceLinkItem, o -> o.setType(null)));
       // 测试 content 不匹配
       traceLinkItemMapper.insert(cloneIgnoreId(dbTraceLinkItem, o -> o.setContent(null)));
       // 测试 addTime 不匹配
       traceLinkItemMapper.insert(cloneIgnoreId(dbTraceLinkItem, o -> o.setAddTime(null)));
       // 测试 linkName 不匹配
       traceLinkItemMapper.insert(cloneIgnoreId(dbTraceLinkItem, o -> o.setLinkName(null)));
       // 测试 linkId 不匹配
       traceLinkItemMapper.insert(cloneIgnoreId(dbTraceLinkItem, o -> o.setLinkId(null)));
       // 测试 belongBusinessId 不匹配
       traceLinkItemMapper.insert(cloneIgnoreId(dbTraceLinkItem, o -> o.setBelongBusinessId(null)));
       // 测试 belongBusinessName 不匹配
       traceLinkItemMapper.insert(cloneIgnoreId(dbTraceLinkItem, o -> o.setBelongBusinessName(null)));
       // 测试 createTime 不匹配
       traceLinkItemMapper.insert(cloneIgnoreId(dbTraceLinkItem, o -> o.setCreateTime(null)));
       // 准备参数
       TraceLinkItemPageReqVO reqVO = new TraceLinkItemPageReqVO();
        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.setTitle(null);
       reqVO.setType(null);
       reqVO.setContent(null);
       reqVO.setAddTime(dates);
       reqVO.setLinkName(null);
       reqVO.setLinkId(null);
       reqVO.setBelongBusinessId(null);
       reqVO.setBelongBusinessName(null);
       reqVO.setCreateTime(dates);

       // 调用
       PageResult<TraceLinkItemDO> pageResult = traceLinkItemService.getTraceLinkItemPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbTraceLinkItem, pageResult.getList().get(0));
    }

}
