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

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.iotorganization.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.iotorganization.IotOrganizationDO;
import cn.iocoder.yudao.module.iot.dal.mysql.iotorganization.IotOrganizationMapper;
import cn.iocoder.yudao.framework.common.pojo.PageResult;


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

    @Resource
    private IotOrganizationServiceImpl organizationService;

    @Resource
    private IotOrganizationMapper organizationMapper;

    @Test
    public void testCreateOrganization_success() {
        // 准备参数
        IotOrganizationSaveReqVO createReqVO = randomPojo(IotOrganizationSaveReqVO.class).setId(null);
        createReqVO.setParentId(0L);
        // 调用
        Long organizationId = organizationService.createOrganization(createReqVO);
        // 断言
        assertNotNull(organizationId);
        // 校验记录的属性是否正确
        IotOrganizationDO organization = organizationMapper.selectById(organizationId);
        assertPojoEquals(createReqVO, organization, "id");
    }

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

        updateReqVO.setParentId(0L);
        // 调用
        organizationService.updateOrganization(updateReqVO);
        // 校验是否更新正确
        IotOrganizationDO organization = organizationMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, organization);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> organizationService.updateOrganization(updateReqVO), ORGANIZATION_NOT_EXISTS);
    }

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

        // 调用
        organizationService.deleteOrganization(id);
       // 校验数据不存在了
       assertNull(organizationMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> organizationService.deleteOrganization(id), ORGANIZATION_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetOrganizationList() {
       // mock 数据
       IotOrganizationDO dbOrganization = randomPojo(IotOrganizationDO.class, o -> { // 等会查询到
           o.setSort(null);
           o.setWorkerUserId(null);
           o.setOrgId(null);
           o.setCreateTime(null);
           o.setName(null);
           o.setParentId(null);
           o.setStatus(null);
           o.setDeviceType(null);
           o.setOrgClass(null);
           o.setMachineType(null);
       });
       organizationMapper.insert(dbOrganization);
       // 测试 sort 不匹配
       organizationMapper.insert(cloneIgnoreId(dbOrganization, o -> o.setSort(null)));
       // 测试 workerUserId 不匹配
       organizationMapper.insert(cloneIgnoreId(dbOrganization, o -> o.setWorkerUserId(null)));
       // 测试 orgId 不匹配
       organizationMapper.insert(cloneIgnoreId(dbOrganization, o -> o.setOrgId(null)));
       // 测试 createTime 不匹配
       organizationMapper.insert(cloneIgnoreId(dbOrganization, o -> o.setCreateTime(null)));
       // 测试 name 不匹配
       organizationMapper.insert(cloneIgnoreId(dbOrganization, o -> o.setName(null)));
       // 测试 parentId 不匹配
       organizationMapper.insert(cloneIgnoreId(dbOrganization, o -> o.setParentId(null)));
       // 测试 status 不匹配
       organizationMapper.insert(cloneIgnoreId(dbOrganization, o -> o.setStatus(null)));
       // 测试 deviceType 不匹配
       organizationMapper.insert(cloneIgnoreId(dbOrganization, o -> o.setDeviceType(null)));
       // 测试 orgClass 不匹配
       organizationMapper.insert(cloneIgnoreId(dbOrganization, o -> o.setOrgClass(null)));
       // 测试 machineType 不匹配
       organizationMapper.insert(cloneIgnoreId(dbOrganization, o -> o.setMachineType(null)));
       // 准备参数
       IotOrganizationListReqVO reqVO = new IotOrganizationListReqVO();
       reqVO.setSort(null);
       reqVO.setWorkerUserId(null);
       reqVO.setOrgId(null);
       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setName(null);
       reqVO.setParentId(null);
       reqVO.setStatus(null);
       reqVO.setDeviceType(null);
       reqVO.setOrgClass(null);
       reqVO.setMachineType(null);

       // 调用
       List<IotOrganizationDO> list = organizationService.getOrganizationList(reqVO);
       // 断言
       assertEquals(1, list.size());
       assertPojoEquals(dbOrganization, list.get(0));
    }

}