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

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;


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

import cn.iocoder.yudao.module.iot.controller.admin.frps.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.frps.FrpsServerDO;
import cn.iocoder.yudao.module.iot.dal.mysql.frps.FrpsServerMapper;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import org.springframework.context.annotation.Import;

import javax.annotation.Resource;
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 FrpsServerServiceImpl} 的单元测试类
 *
 * @author 内蒙必硕
 */
@Import(FrpsServerServiceImpl.class)
public class FrpsServerServiceImplTest extends BaseDbUnitTest {

    @Resource
    private FrpsServerServiceImpl frpsServerService;

    @Resource
    private FrpsServerMapper frpsServerMapper;

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

        // 调用
        Long frpsServerId = frpsServerService.createFrpsServer(createReqVO);
        // 断言
        assertNotNull(frpsServerId);
        // 校验记录的属性是否正确
        FrpsServerDO frpsServer = frpsServerMapper.selectById(frpsServerId);
        assertPojoEquals(createReqVO, frpsServer, "id");
    }

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

        // 调用
        frpsServerService.updateFrpsServer(updateReqVO);
        // 校验是否更新正确
        FrpsServerDO frpsServer = frpsServerMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, frpsServer);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> frpsServerService.updateFrpsServer(updateReqVO), FRPS_SERVER_NOT_EXISTS);
    }

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

        // 调用
        frpsServerService.deleteFrpsServer(id);
       // 校验数据不存在了
       assertNull(frpsServerMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> frpsServerService.deleteFrpsServer(id), FRPS_SERVER_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetFrpsServerPage() {
       // mock 数据
       FrpsServerDO dbFrpsServer = randomPojo(FrpsServerDO.class, o -> { // 等会查询到
           o.setFrpsName(null);
           o.setHostMac(null);
           o.setHostOs(null);
           o.setHostIp(null);
           o.setHostPort(null);
           o.setFrpsPath(null);
           o.setFrpsState(null);
           o.setRemark(null);
           o.setIsEnable(null);
           o.setCreateTime(null);
       });
       frpsServerMapper.insert(dbFrpsServer);
       // 测试 frpsName 不匹配
       frpsServerMapper.insert(cloneIgnoreId(dbFrpsServer, o -> o.setFrpsName(null)));
       // 测试 hostMac 不匹配
       frpsServerMapper.insert(cloneIgnoreId(dbFrpsServer, o -> o.setHostMac(null)));
       // 测试 hostOs 不匹配
       frpsServerMapper.insert(cloneIgnoreId(dbFrpsServer, o -> o.setHostOs(null)));
       // 测试 hostIp 不匹配
       frpsServerMapper.insert(cloneIgnoreId(dbFrpsServer, o -> o.setHostIp(null)));
       // 测试 hostPort 不匹配
       frpsServerMapper.insert(cloneIgnoreId(dbFrpsServer, o -> o.setHostPort(null)));
       // 测试 frpsPath 不匹配
       frpsServerMapper.insert(cloneIgnoreId(dbFrpsServer, o -> o.setFrpsPath(null)));
       // 测试 frpsState 不匹配
       frpsServerMapper.insert(cloneIgnoreId(dbFrpsServer, o -> o.setFrpsState(null)));
       // 测试 remark 不匹配
       frpsServerMapper.insert(cloneIgnoreId(dbFrpsServer, o -> o.setRemark(null)));
       // 测试 isEnable 不匹配
       frpsServerMapper.insert(cloneIgnoreId(dbFrpsServer, o -> o.setIsEnable(null)));
       // 测试 createTime 不匹配
       frpsServerMapper.insert(cloneIgnoreId(dbFrpsServer, o -> o.setCreateTime(null)));
       // 准备参数
       FrpsServerPageReqVO reqVO = new FrpsServerPageReqVO();
       reqVO.setFrpsName(null);
       reqVO.setHostMac(null);
       reqVO.setHostOs(null);
       reqVO.setHostIp(null);
       reqVO.setHostPort(null);
       reqVO.setFrpsPath(null);
       reqVO.setFrpsState(null);
       reqVO.setRemark(null);
       reqVO.setIsEnable(null);
       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       PageResult<FrpsServerDO> pageResult = frpsServerService.getFrpsServerPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbFrpsServer, pageResult.getList().get(0));
    }

}