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

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

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

import cn.iocoder.yudao.module.iot.controller.admin.mqttrecord.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.mqttrecord.MqttRecordDO;
import cn.iocoder.yudao.module.iot.dal.mysql.mqttrecord.MqttRecordMapper;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import org.springframework.context.annotation.Import;

import javax.annotation.Resource;

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 MqttRecordServiceImpl} 的单元测试类
 *
 * @author 内蒙必硕
 */
@Import(MqttRecordServiceImpl.class)
public class MqttRecordServiceImplTest extends BaseDbUnitTest {

    @Resource
    private MqttRecordServiceImpl mqttRecordService;

    @Resource
    private MqttRecordMapper mqttRecordMapper;

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

        // 调用
        Long mqttRecordId = mqttRecordService.createMqttRecord(createReqVO);
        // 断言
        assertNotNull(mqttRecordId);
        // 校验记录的属性是否正确
        MqttRecordDO mqttRecord = mqttRecordMapper.selectById(mqttRecordId);
        assertPojoEquals(createReqVO, mqttRecord, "id");
    }

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

        // 调用
        mqttRecordService.updateMqttRecord(updateReqVO);
        // 校验是否更新正确
        MqttRecordDO mqttRecord = mqttRecordMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, mqttRecord);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> mqttRecordService.updateMqttRecord(updateReqVO), MQTT_RECORD_NOT_EXISTS);
    }

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

        // 调用
        mqttRecordService.deleteMqttRecord(id);
       // 校验数据不存在了
       assertNull(mqttRecordMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> mqttRecordService.deleteMqttRecord(id), MQTT_RECORD_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetMqttRecordPage() {
       // mock 数据
       MqttRecordDO dbMqttRecord = randomPojo(MqttRecordDO.class, o -> { // 等会查询到
           o.setMachineName(null);
           o.setDeviceDataTime(null);
           o.setDeviceData(null);
           o.setDeviceCode(null);
           o.setDeviceName(null);
           o.setWorkerName(null);
           o.setGatewayCode(null);
           o.setCreateTime(null);
       });
       mqttRecordMapper.insert(dbMqttRecord);
       // 测试 machineName 不匹配
       mqttRecordMapper.insert(cloneIgnoreId(dbMqttRecord, o -> o.setMachineName(null)));
       // 测试 deviceDataTime 不匹配
       mqttRecordMapper.insert(cloneIgnoreId(dbMqttRecord, o -> o.setDeviceDataTime(null)));
       // 测试 deviceData 不匹配
       mqttRecordMapper.insert(cloneIgnoreId(dbMqttRecord, o -> o.setDeviceData(null)));
       // 测试 deviceCode 不匹配
       mqttRecordMapper.insert(cloneIgnoreId(dbMqttRecord, o -> o.setDeviceCode(null)));
       // 测试 deviceName 不匹配
       mqttRecordMapper.insert(cloneIgnoreId(dbMqttRecord, o -> o.setDeviceName(null)));
       // 测试 workerName 不匹配
       mqttRecordMapper.insert(cloneIgnoreId(dbMqttRecord, o -> o.setWorkerName(null)));
       // 测试 gatewayCode 不匹配
       mqttRecordMapper.insert(cloneIgnoreId(dbMqttRecord, o -> o.setGatewayCode(null)));
       // 测试 createTime 不匹配
       mqttRecordMapper.insert(cloneIgnoreId(dbMqttRecord, o -> o.setCreateTime(null)));
       // 准备参数
       MqttRecordPageReqVO reqVO = new MqttRecordPageReqVO();
       reqVO.setMachineName(null);
       reqVO.setDeviceDataTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setDeviceData(null);
       reqVO.setDeviceCode(null);
       reqVO.setDeviceName(null);
       reqVO.setWorkerName(null);
       reqVO.setGatewayCode(null);
       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       PageResult<MqttRecordDO> pageResult = mqttRecordService.getMqttRecordPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbMqttRecord, pageResult.getList().get(0));
    }

}