package com.cf.imes.module.system.service.process;

import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.module.system.controller.admin.process.vo.group.ProcessGroupPageReqVO;
import com.cf.imes.module.system.controller.admin.process.vo.group.ProcessGroupSaveReqVO;
import com.cf.imes.module.system.dal.dataobject.process.ProcessGroupDO;
import com.cf.imes.module.system.dal.mysql.process.ProcessGroupMapper;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;


import javax.annotation.Resource;

import com.cf.imes.framework.test.core.ut.BaseDbUnitTest;


import org.springframework.context.annotation.Import;
import java.util.*;
import java.time.LocalDateTime;

import static cn.hutool.core.util.RandomUtil.*;
import static com.cf.imes.framework.test.core.util.AssertUtils.*;
import static com.cf.imes.framework.test.core.util.RandomUtils.*;
import static com.cf.imes.framework.common.util.date.LocalDateTimeUtils.*;
import static com.cf.imes.framework.common.util.object.ObjectUtils.*;
import static com.cf.imes.framework.common.util.date.DateUtils.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import static com.cf.imes.module.system.enums.ErrorCodeConstants.*;
/**
 * {@link ProcessGroupServiceImpl} 的单元测试类
 *
 * @author 晨丰科技
 */
@Import(ProcessGroupServiceImpl.class)
public class GroupServiceImplTest extends BaseDbUnitTest {

    @Resource
    private ProcessGroupServiceImpl groupService;

    @Resource
    private ProcessGroupMapper groupMapper;

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

        // 调用
        Long groupId = groupService.createProcessGroup(createReqVO);
        // 断言
        assertNotNull(groupId);
        // 校验记录的属性是否正确
        ProcessGroupDO group = groupMapper.selectById(groupId);
        assertPojoEquals(createReqVO, group, "id");
    }
}


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

        // 调用
        //groupService.updateProcessGroup(updateReqVO);
        // 校验是否更新正确
        ProcessGroupDO group = groupMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, group);
    }*/

/*    @Test
    public void testUpdateGroup_notExists() {
        // 准备参数
        ProcessGroupSaveReqVO updateReqVO = randomPojo(ProcessGroupSaveReqVO.class);

        // 调用, 并断言异常
        //assertServiceException(() -> groupService.updateProcessGroup(updateReqVO), PROCESS_GROUP_NOT_EXISTS);
    }

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

        // 调用
        groupService.deleteProcessGroup(id);
       // 校验数据不存在了
       assertNull(groupMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> groupService.deleteProcessGroup(id), PROCESS_GROUP_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetGroupPage() {
       // mock 数据
        ProcessGroupDO dbGroup = randomPojo(ProcessGroupDO.class, o -> { // 等会查询到
           o.setName(null);
           o.setIsDefault(null);
           o.setSort(null);
           o.setItems(null);
           o.setDescription(null);
           o.setCreateTime(null);
       });
       groupMapper.insert(dbGroup);
       // 测试 name 不匹配
       groupMapper.insert(cloneIgnoreId(dbGroup, o -> o.setName(null)));
       // 测试 isDefault 不匹配
       groupMapper.insert(cloneIgnoreId(dbGroup, o -> o.setIsDefault(null)));
       // 测试 sort 不匹配
       groupMapper.insert(cloneIgnoreId(dbGroup, o -> o.setSort(null)));
       // 测试 items 不匹配
       groupMapper.insert(cloneIgnoreId(dbGroup, o -> o.setItems(null)));
       // 测试 description 不匹配
       groupMapper.insert(cloneIgnoreId(dbGroup, o -> o.setDescription(null)));
       // 测试 createTime 不匹配
       groupMapper.insert(cloneIgnoreId(dbGroup, o -> o.setCreateTime(null)));
       // 准备参数
        ProcessGroupPageReqVO reqVO = new ProcessGroupPageReqVO();
       reqVO.setName(null);
       reqVO.setIsDefault(null);
       reqVO.setSort(null);
       reqVO.setItems(null);
       reqVO.setDescription(null);
       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       PageResult<ProcessGroupDO> pageResult = groupService.getProcessGroupPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbGroup, pageResult.getList().get(0));
    }*/
