package cn.iocoder.yudao.module.configure.service.deviceenvtype;

import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.configure.controller.admin.deviceenvtype.vo.DeviceEnvTypeCreateReqVO;
import cn.iocoder.yudao.module.configure.controller.admin.deviceenvtype.vo.DeviceEnvTypeUpdateReqVO;
import cn.iocoder.yudao.module.configure.dal.dataobject.deviceenvtype.DeviceEnvTypeDO;
import cn.iocoder.yudao.module.configure.dal.mysql.deviceenvtype.DeviceEnvTypeMapper;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Import;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
import static cn.iocoder.yudao.module.configure.enums.ErrorCodeConstants.DEVICE_ENV_TYPE_NOT_EXISTS;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

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

    @Resource
    private DeviceEnvTypeServiceImpl deviceEnvTypeService;

    @Resource
    private DeviceEnvTypeMapper deviceEnvTypeMapper;

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

        // 调用
        Long deviceEnvTypeId = deviceEnvTypeService.createDeviceEnvType(reqVO);
        // 断言
        assertNotNull(deviceEnvTypeId);
        // 校验记录的属性是否正确
        DeviceEnvTypeDO deviceEnvType = deviceEnvTypeMapper.selectById(deviceEnvTypeId);
        assertPojoEquals(reqVO, deviceEnvType);
    }

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

        // 调用
        deviceEnvTypeService.updateDeviceEnvType(reqVO);
        // 校验是否更新正确
        DeviceEnvTypeDO deviceEnvType = deviceEnvTypeMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, deviceEnvType);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> deviceEnvTypeService.updateDeviceEnvType(reqVO), DEVICE_ENV_TYPE_NOT_EXISTS);
    }

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

        // 调用
        deviceEnvTypeService.deleteDeviceEnvType(id);
       // 校验数据不存在了
       assertNull(deviceEnvTypeMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> deviceEnvTypeService.deleteDeviceEnvType(id), DEVICE_ENV_TYPE_NOT_EXISTS);
    }
}
