package cn.iocoder.yudao.module.product.service.slavedevice;

import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils;
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.product.controller.admin.slavedevice.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.slavedevice.SlaveDeviceDO;
import cn.iocoder.yudao.module.product.dal.mysql.slavedevice.SlaveDeviceMapper;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import javax.annotation.Resource;
import org.springframework.context.annotation.Import;

import java.time.ZoneId;
import java.util.*;
import java.time.LocalDateTime;

import static cn.hutool.core.util.RandomUtil.*;
import static cn.iocoder.yudao.module.product.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 SlaveDeviceServiceImpl} 的单元测试类
*
* @author 芋道源码
*/
@Import(SlaveDeviceServiceImpl.class)
public class SlaveDeviceServiceImplTest extends BaseDbUnitTest {

    @Resource
    private SlaveDeviceServiceImpl slaveDeviceService;

    @Resource
    private SlaveDeviceMapper slaveDeviceMapper;

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

        // 调用
        slaveDeviceService.deleteSlaveDevice(id);
       // 校验数据不存在了
       assertNull(slaveDeviceMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> slaveDeviceService.deleteSlaveDevice(id), SLAVE_DEVICE_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetSlaveDevicePage() {
       // mock 数据
       SlaveDeviceDO dbSlaveDevice = randomPojo(SlaveDeviceDO.class, o -> { // 等会查询到
           o.setDeviceName(null);
           o.setEnvType(null);
           o.setBelongBusinessId(null);
           o.setBelongBusinessName(null);
           o.setCreateTime(null);
       });
       slaveDeviceMapper.insert(dbSlaveDevice);
       // 测试 deviceName 不匹配
       slaveDeviceMapper.insert(cloneIgnoreId(dbSlaveDevice, o -> o.setDeviceName(null)));
       // 测试 envType 不匹配
       slaveDeviceMapper.insert(cloneIgnoreId(dbSlaveDevice, o -> o.setEnvType(null)));
       // 测试 belongBusinessId 不匹配
       slaveDeviceMapper.insert(cloneIgnoreId(dbSlaveDevice, o -> o.setBelongBusinessId(null)));
       // 测试 belongBusinessName 不匹配
       slaveDeviceMapper.insert(cloneIgnoreId(dbSlaveDevice, o -> o.setBelongBusinessName(null)));
       // 测试 createTime 不匹配
       slaveDeviceMapper.insert(cloneIgnoreId(dbSlaveDevice, o -> o.setCreateTime(null)));
       // 准备参数
       SlaveDevicePageReqVO reqVO = new SlaveDevicePageReqVO();
       reqVO.setDeviceName(null);
       reqVO.setEnvType(null);
       reqVO.setBelongBusinessId(null);

       // 调用
       PageResult<SlaveDeviceDO> pageResult = slaveDeviceService.getSlaveDevicePage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbSlaveDevice, pageResult.getList().get(0));
    }
}
