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

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.iot.controller.admin.mqttdatarecord.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.mqttdatarecord.MqttDataRecordDO;
import cn.iocoder.yudao.module.iot.dal.mysql.mqttdatarecord.MqttDataRecordMapper;
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.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 MqttDataRecordServiceImpl} 的单元测试类
 *
 * @author 内蒙必硕
 */
@Import(MqttDataRecordServiceImpl.class)
public class MqttDataRecordServiceImplTest extends BaseDbUnitTest {

    @Resource
    private MqttDataRecordServiceImpl mqttDataRecordService;

    @Resource
    private MqttDataRecordMapper mqttDataRecordMapper;

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

        // 调用
        Long mqttDataRecordId = mqttDataRecordService.createMqttDataRecord(createReqVO);
        // 断言
        assertNotNull(mqttDataRecordId);
        // 校验记录的属性是否正确
        MqttDataRecordDO mqttDataRecord = mqttDataRecordMapper.selectById(mqttDataRecordId);
        assertPojoEquals(createReqVO, mqttDataRecord, "id");
    }

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

        // 调用
        mqttDataRecordService.updateMqttDataRecord(updateReqVO);
        // 校验是否更新正确
        MqttDataRecordDO mqttDataRecord = mqttDataRecordMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, mqttDataRecord);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> mqttDataRecordService.updateMqttDataRecord(updateReqVO), MQTT_DATA_RECORD_NOT_EXISTS);
    }

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

        // 调用
        mqttDataRecordService.deleteMqttDataRecord(id);
       // 校验数据不存在了
       assertNull(mqttDataRecordMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> mqttDataRecordService.deleteMqttDataRecord(id), MQTT_DATA_RECORD_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetMqttDataRecordPage() {
       // mock 数据
       MqttDataRecordDO dbMqttDataRecord = randomPojo(MqttDataRecordDO.class, o -> { // 等会查询到
           o.setDeviceName(null);
           o.setAttribute(null);
           o.setAttrValue(null);
           o.setDeviceTime(null);
           o.setCreateTime(null);
       });
       mqttDataRecordMapper.insert(dbMqttDataRecord);
       // 测试 deviceName 不匹配
       mqttDataRecordMapper.insert(cloneIgnoreId(dbMqttDataRecord, o -> o.setDeviceName(null)));
       // 测试 attribute 不匹配
       mqttDataRecordMapper.insert(cloneIgnoreId(dbMqttDataRecord, o -> o.setAttribute(null)));
       // 测试 attrValue 不匹配
       mqttDataRecordMapper.insert(cloneIgnoreId(dbMqttDataRecord, o -> o.setAttrValue(null)));
       // 测试 deviceTime 不匹配
       mqttDataRecordMapper.insert(cloneIgnoreId(dbMqttDataRecord, o -> o.setDeviceTime(null)));
       // 测试 createTime 不匹配
       mqttDataRecordMapper.insert(cloneIgnoreId(dbMqttDataRecord, o -> o.setCreateTime(null)));
       // 准备参数
       MqttDataRecordPageReqVO reqVO = new MqttDataRecordPageReqVO();
       reqVO.setDeviceName(null);
       reqVO.setAttribute(null);
       reqVO.setAttrValue(null);
       reqVO.setDeviceTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       PageResult<MqttDataRecordDO> pageResult = mqttDataRecordService.getMqttDataRecordPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbMqttDataRecord, pageResult.getList().get(0));
    }

}