package cn.iocoder.yudao.module.identity.service.businessrule;

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 cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;

import cn.iocoder.yudao.module.identity.controller.admin.businessrule.vo.*;
import cn.iocoder.yudao.module.identity.dal.dataobject.businessrule.BusinessRuleDO;
import cn.iocoder.yudao.module.identity.dal.mysql.businessrule.BusinessRuleMapper;
import cn.iocoder.yudao.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 cn.iocoder.yudao.module.identity.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.*;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.*;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

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

    @Resource
    private BusinessRuleServiceImpl businessRuleService;

    @Resource
    private BusinessRuleMapper businessRuleMapper;

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

        // 调用
        Long businessRuleId = businessRuleService.createBusinessRule(reqVO);
        // 断言
        assertNotNull(businessRuleId);
        // 校验记录的属性是否正确
        BusinessRuleDO businessRule = businessRuleMapper.selectById(businessRuleId);
        assertPojoEquals(reqVO, businessRule);
    }

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

        // 调用
        businessRuleService.updateBusinessRule(reqVO);
        // 校验是否更新正确
        BusinessRuleDO businessRule = businessRuleMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, businessRule);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> businessRuleService.updateBusinessRule(reqVO), BUSINESS_RULE_NOT_EXISTS);
    }

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

        // 调用
        businessRuleService.deleteBusinessRule(id);
       // 校验数据不存在了
       assertNull(businessRuleMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> businessRuleService.deleteBusinessRule(id), BUSINESS_RULE_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetBusinessRulePage() {
       // mock 数据
       BusinessRuleDO dbBusinessRule = randomPojo(BusinessRuleDO.class, o -> { // 等会查询到
           o.setBusinessId(null);
           o.setBusinessName(null);
           o.setCodeRuleId(null);
           o.setCodeRuleName(null);
           o.setCodeType(null);
           o.setCodeRuleFormat(null);
           o.setCreateTime(null);
       });
       businessRuleMapper.insert(dbBusinessRule);
       // 测试 businessId 不匹配
       businessRuleMapper.insert(cloneIgnoreId(dbBusinessRule, o -> o.setBusinessId(null)));
       // 测试 businessName 不匹配
       businessRuleMapper.insert(cloneIgnoreId(dbBusinessRule, o -> o.setBusinessName(null)));
       // 测试 codeRuleId 不匹配
       businessRuleMapper.insert(cloneIgnoreId(dbBusinessRule, o -> o.setCodeRuleId(null)));
       // 测试 codeRuleName 不匹配
       businessRuleMapper.insert(cloneIgnoreId(dbBusinessRule, o -> o.setCodeRuleName(null)));
       // 测试 codeType 不匹配
       businessRuleMapper.insert(cloneIgnoreId(dbBusinessRule, o -> o.setCodeType(null)));
       // 测试 codeRuleFormat 不匹配
       businessRuleMapper.insert(cloneIgnoreId(dbBusinessRule, o -> o.setCodeRuleFormat(null)));
       // 测试 createTime 不匹配
       businessRuleMapper.insert(cloneIgnoreId(dbBusinessRule, o -> o.setCreateTime(null)));
       // 准备参数
       BusinessRulePageReqVO reqVO = new BusinessRulePageReqVO();
       reqVO.setBusinessId(null);
       reqVO.setBusinessName(null);
       reqVO.setCodeRuleId(null);
       reqVO.setCodeRuleName(null);
       reqVO.setCodeType(null);
       reqVO.setCodeRuleFormat(null);
//       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       PageResult<BusinessRuleDO> pageResult = businessRuleService.getBusinessRulePage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbBusinessRule, pageResult.getList().get(0));
    }
}
