package cn.iocoder.yudao.module.iot.service.gateway;

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



import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;

import cn.iocoder.yudao.module.iot.controller.admin.gateway.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.gateway.GatewayDO;
import cn.iocoder.yudao.module.iot.dal.mysql.gateway.GatewayMapper;
import cn.iocoder.yudao.framework.common.pojo.PageResult;


import org.springframework.context.annotation.Import;

import javax.annotation.Resource;
import java.util.*;
import java.time.LocalDateTime;

import static cn.hutool.core.util.RandomUtil.*;
import static cn.iocoder.yudao.module.iot.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.date.LocalDateTimeUtils.*;
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 GatewayServiceImpl} 的单元测试类
 *
 * @author 内蒙必硕
 */
@Import(GatewayServiceImpl.class)
public class GatewayServiceImplTest extends BaseDbUnitTest {

    @Resource
    private GatewayServiceImpl gatewayService;

    @Resource
    private GatewayMapper gatewayMapper;

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

        // 调用
        Long gatewayId = gatewayService.createGateway(createReqVO);
        // 断言
        assertNotNull(gatewayId);
        // 校验记录的属性是否正确
        GatewayDO gateway = gatewayMapper.selectById(gatewayId);
        assertPojoEquals(createReqVO, gateway, "id");
    }

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

        // 调用
        gatewayService.updateGateway(updateReqVO);
        // 校验是否更新正确
        GatewayDO gateway = gatewayMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, gateway);
    }

    @Test
    public void testUpdateGateway_notExists() {
        // 准备参数
        GatewaySaveReqVO updateReqVO = randomPojo(GatewaySaveReqVO.class);

        // 调用, 并断言异常
        assertServiceException(() -> gatewayService.updateGateway(updateReqVO), GATEWAY_NOT_EXISTS);
    }

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

        // 调用
        gatewayService.deleteGateway(id);
       // 校验数据不存在了
       assertNull(gatewayMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> gatewayService.deleteGateway(id), GATEWAY_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetGatewayPage() {
       // mock 数据
       GatewayDO dbGateway = randomPojo(GatewayDO.class, o -> { // 等会查询到
           o.setAdminIp(null);
           o.setUsername(null);
           o.setPassword(null);
           o.setGatewayName(null);
           o.setRemark(null);
           o.setIsEnable(null);
           o.setCreateTime(null);
           o.setGatewayCode(null);
           o.setGatewayStatus(null);
           o.setTopic(null);
       });
       gatewayMapper.insert(dbGateway);
       // 测试 adminIp 不匹配
       gatewayMapper.insert(cloneIgnoreId(dbGateway, o -> o.setAdminIp(null)));
       // 测试 username 不匹配
       gatewayMapper.insert(cloneIgnoreId(dbGateway, o -> o.setUsername(null)));
       // 测试 password 不匹配
       gatewayMapper.insert(cloneIgnoreId(dbGateway, o -> o.setPassword(null)));
       // 测试 gatewayName 不匹配
       gatewayMapper.insert(cloneIgnoreId(dbGateway, o -> o.setGatewayName(null)));
       // 测试 remark 不匹配
       gatewayMapper.insert(cloneIgnoreId(dbGateway, o -> o.setRemark(null)));
       // 测试 isEnable 不匹配
       gatewayMapper.insert(cloneIgnoreId(dbGateway, o -> o.setIsEnable(null)));
       // 测试 createTime 不匹配
       gatewayMapper.insert(cloneIgnoreId(dbGateway, o -> o.setCreateTime(null)));
       // 测试 gatewayCode 不匹配
       gatewayMapper.insert(cloneIgnoreId(dbGateway, o -> o.setGatewayCode(null)));
       // 测试 gatewayStatus 不匹配
       gatewayMapper.insert(cloneIgnoreId(dbGateway, o -> o.setGatewayStatus(null)));
       // 测试 topic 不匹配
       gatewayMapper.insert(cloneIgnoreId(dbGateway, o -> o.setTopic(null)));
       // 准备参数
       GatewayPageReqVO reqVO = new GatewayPageReqVO();
       reqVO.setAdminIp(null);
       reqVO.setUsername(null);
       reqVO.setPassword(null);
       reqVO.setGatewayName(null);
       reqVO.setRemark(null);
       reqVO.setIsEnable(null);
       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setGatewayCode(null);
       reqVO.setGatewayStatus(null);
       reqVO.setTopic(null);

       // 调用
       PageResult<GatewayDO> pageResult = gatewayService.getGatewayPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbGateway, pageResult.getList().get(0));
    }

}