1. 【修复】单元测试报错
2. 【优化】yudao-module-xx-impl 调整成 yudao-module-xx-biz,更加符合定位
This commit is contained in:
@@ -0,0 +1,252 @@
|
||||
package cn.iocoder.yudao.module.infra.service.config;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils;
|
||||
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
import cn.iocoder.yudao.framework.test.core.util.RandomUtils;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.config.vo.ConfigCreateReqVO;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.config.vo.ConfigExportReqVO;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.config.vo.ConfigPageReqVO;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.config.vo.ConfigUpdateReqVO;
|
||||
import cn.iocoder.yudao.module.infra.dal.dataobject.config.ConfigDO;
|
||||
import cn.iocoder.yudao.module.infra.dal.mysql.config.ConfigMapper;
|
||||
import cn.iocoder.yudao.module.infra.enums.config.ConfigTypeEnum;
|
||||
import cn.iocoder.yudao.module.infra.mq.producer.config.ConfigProducer;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static cn.hutool.core.util.RandomUtil.randomEle;
|
||||
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.buildTime;
|
||||
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.*;
|
||||
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@Import(ConfigServiceImpl.class)
|
||||
public class ConfigServiceTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private ConfigServiceImpl configService;
|
||||
|
||||
@Resource
|
||||
private ConfigMapper configMapper;
|
||||
@MockBean
|
||||
private ConfigProducer configProducer;
|
||||
|
||||
@Test
|
||||
public void testCreateConfig_success() {
|
||||
// 准备参数
|
||||
ConfigCreateReqVO reqVO = randomPojo(ConfigCreateReqVO.class);
|
||||
|
||||
// 调用
|
||||
Long configId = configService.createConfig(reqVO);
|
||||
// 断言
|
||||
assertNotNull(configId);
|
||||
// 校验记录的属性是否正确
|
||||
ConfigDO config = configMapper.selectById(configId);
|
||||
assertPojoEquals(reqVO, config);
|
||||
Assertions.assertEquals(ConfigTypeEnum.CUSTOM.getType(), config.getType());
|
||||
// 校验调用
|
||||
verify(configProducer, times(1)).sendConfigRefreshMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateConfig_success() {
|
||||
// mock 数据
|
||||
ConfigDO dbConfig = randomConfigDO();
|
||||
configMapper.insert(dbConfig);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
ConfigUpdateReqVO reqVO = randomPojo(ConfigUpdateReqVO.class, o -> {
|
||||
o.setId(dbConfig.getId()); // 设置更新的 ID
|
||||
});
|
||||
|
||||
// 调用
|
||||
configService.updateConfig(reqVO);
|
||||
// 校验是否更新正确
|
||||
ConfigDO config = configMapper.selectById(reqVO.getId()); // 获取最新的
|
||||
assertPojoEquals(reqVO, config);
|
||||
// 校验调用
|
||||
verify(configProducer, times(1)).sendConfigRefreshMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteConfig_success() {
|
||||
// mock 数据
|
||||
ConfigDO dbConfig = randomConfigDO(o -> {
|
||||
o.setType(ConfigTypeEnum.CUSTOM.getType()); // 只能删除 CUSTOM 类型
|
||||
});
|
||||
configMapper.insert(dbConfig);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
Long id = dbConfig.getId();
|
||||
|
||||
// 调用
|
||||
configService.deleteConfig(id);
|
||||
// 校验数据不存在了
|
||||
assertNull(configMapper.selectById(id));
|
||||
// 校验调用
|
||||
verify(configProducer, times(1)).sendConfigRefreshMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteConfig_canNotDeleteSystemType() {
|
||||
// mock 数据
|
||||
ConfigDO dbConfig = randomConfigDO(o -> {
|
||||
o.setType(ConfigTypeEnum.SYSTEM.getType()); // SYSTEM 不允许删除
|
||||
});
|
||||
configMapper.insert(dbConfig);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
Long id = dbConfig.getId();
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> configService.deleteConfig(id), CONFIG_CAN_NOT_DELETE_SYSTEM_TYPE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckConfigExists_success() {
|
||||
// mock 数据
|
||||
ConfigDO dbConfigDO = randomConfigDO();
|
||||
configMapper.insert(dbConfigDO);// @Sql: 先插入出一条存在的数据
|
||||
|
||||
// 调用成功
|
||||
configService.checkConfigExists(dbConfigDO.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckConfigExist_notExists() {
|
||||
assertServiceException(() -> configService.checkConfigExists(randomLongId()), CONFIG_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckConfigKeyUnique_success() {
|
||||
// 调用,成功
|
||||
configService.checkConfigKeyUnique(randomLongId(), randomString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckConfigKeyUnique_keyDuplicateForCreate() {
|
||||
// 准备参数
|
||||
String key = randomString();
|
||||
// mock 数据
|
||||
configMapper.insert(randomConfigDO(o -> o.setKey(key)));
|
||||
|
||||
// 调用,校验异常
|
||||
assertServiceException(() -> configService.checkConfigKeyUnique(null, key),
|
||||
CONFIG_KEY_DUPLICATE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckConfigKeyUnique_keyDuplicateForUpdate() {
|
||||
// 准备参数
|
||||
Long id = randomLongId();
|
||||
String key = randomString();
|
||||
// mock 数据
|
||||
configMapper.insert(randomConfigDO(o -> o.setKey(key)));
|
||||
|
||||
// 调用,校验异常
|
||||
assertServiceException(() -> configService.checkConfigKeyUnique(id, key),
|
||||
CONFIG_KEY_DUPLICATE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetConfigPage() {
|
||||
// mock 数据
|
||||
ConfigDO dbConfig = randomConfigDO(o -> { // 等会查询到
|
||||
o.setName("芋艿");
|
||||
o.setKey("yunai");
|
||||
o.setType(ConfigTypeEnum.SYSTEM.getType());
|
||||
o.setCreateTime(buildTime(2021, 2, 1));
|
||||
});
|
||||
configMapper.insert(dbConfig);
|
||||
// 测试 name 不匹配
|
||||
configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setName("土豆")));
|
||||
// 测试 key 不匹配
|
||||
configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setKey("tudou")));
|
||||
// 测试 type 不匹配
|
||||
configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setType(ConfigTypeEnum.CUSTOM.getType())));
|
||||
// 测试 createTime 不匹配
|
||||
configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setCreateTime(buildTime(2021, 1, 1))));
|
||||
// 准备参数
|
||||
ConfigPageReqVO reqVO = new ConfigPageReqVO();
|
||||
reqVO.setName("艿");
|
||||
reqVO.setKey("nai");
|
||||
reqVO.setType(ConfigTypeEnum.SYSTEM.getType());
|
||||
reqVO.setBeginTime(buildTime(2021, 1, 15));
|
||||
reqVO.setEndTime(buildTime(2021, 2, 15));
|
||||
|
||||
// 调用
|
||||
PageResult<ConfigDO> pageResult = configService.getConfigPage(reqVO);
|
||||
// 断言
|
||||
assertEquals(1, pageResult.getTotal());
|
||||
assertEquals(1, pageResult.getList().size());
|
||||
assertPojoEquals(dbConfig, pageResult.getList().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetConfigList() {
|
||||
// mock 数据
|
||||
ConfigDO dbConfig = randomConfigDO(o -> { // 等会查询到
|
||||
o.setName("芋艿");
|
||||
o.setKey("yunai");
|
||||
o.setType(ConfigTypeEnum.SYSTEM.getType());
|
||||
o.setCreateTime(buildTime(2021, 2, 1));
|
||||
});
|
||||
configMapper.insert(dbConfig);
|
||||
// 测试 name 不匹配
|
||||
configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setName("土豆")));
|
||||
// 测试 key 不匹配
|
||||
configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setKey("tudou")));
|
||||
// 测试 type 不匹配
|
||||
configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setType(ConfigTypeEnum.CUSTOM.getType())));
|
||||
// 测试 createTime 不匹配
|
||||
configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setCreateTime(buildTime(2021, 1, 1))));
|
||||
// 准备参数
|
||||
ConfigExportReqVO reqVO = new ConfigExportReqVO();
|
||||
reqVO.setName("艿");
|
||||
reqVO.setKey("nai");
|
||||
reqVO.setType(ConfigTypeEnum.SYSTEM.getType());
|
||||
reqVO.setBeginTime(buildTime(2021, 1, 15));
|
||||
reqVO.setEndTime(buildTime(2021, 2, 15));
|
||||
|
||||
// 调用
|
||||
List<ConfigDO> list = configService.getConfigList(reqVO);
|
||||
// 断言
|
||||
assertEquals(1, list.size());
|
||||
assertPojoEquals(dbConfig, list.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetConfigByKey() {
|
||||
// mock 数据
|
||||
ConfigDO dbConfig = randomConfigDO();
|
||||
configMapper.insert(dbConfig);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
String key = dbConfig.getKey();
|
||||
|
||||
// 调用
|
||||
ConfigDO config = configService.getConfigByKey(key);
|
||||
// 断言
|
||||
assertNotNull(config);
|
||||
assertPojoEquals(dbConfig, config);
|
||||
}
|
||||
|
||||
// ========== 随机对象 ==========
|
||||
|
||||
@SafeVarargs
|
||||
private static ConfigDO randomConfigDO(Consumer<ConfigDO>... consumers) {
|
||||
Consumer<ConfigDO> consumer = (o) -> {
|
||||
o.setType(randomEle(ConfigTypeEnum.values()).getType()); // 保证 key 的范围
|
||||
};
|
||||
return RandomUtils.randomPojo(ConfigDO.class, ArrayUtils.append(consumer, consumers));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,256 @@
|
||||
package cn.iocoder.yudao.module.infra.service.file;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.file.core.client.FileClient;
|
||||
import cn.iocoder.yudao.framework.file.core.client.FileClientConfig;
|
||||
import cn.iocoder.yudao.framework.file.core.client.FileClientFactory;
|
||||
import cn.iocoder.yudao.framework.file.core.client.local.LocalFileClientConfig;
|
||||
import cn.iocoder.yudao.framework.file.core.enums.FileStorageEnum;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.file.vo.config.FileConfigCreateReqVO;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.file.vo.config.FileConfigPageReqVO;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.file.vo.config.FileConfigUpdateReqVO;
|
||||
import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileConfigDO;
|
||||
import cn.iocoder.yudao.module.infra.dal.mysql.file.FileConfigMapper;
|
||||
import cn.iocoder.yudao.module.infra.mq.producer.file.FileConfigProducer;
|
||||
import lombok.Data;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.validation.Validator;
|
||||
import java.io.Serializable;
|
||||
import java.util.Map;
|
||||
|
||||
import static cn.hutool.core.util.RandomUtil.randomEle;
|
||||
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.buildTime;
|
||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
|
||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.max;
|
||||
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.infra.enums.ErrorCodeConstants.FILE_CONFIG_DELETE_FAIL_MASTER;
|
||||
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_CONFIG_NOT_EXISTS;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* {@link FileConfigServiceImpl} 的单元测试类
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Import(FileConfigServiceImpl.class)
|
||||
public class FileConfigServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private FileConfigServiceImpl fileConfigService;
|
||||
|
||||
@Resource
|
||||
private FileConfigMapper fileConfigMapper;
|
||||
|
||||
@MockBean
|
||||
private FileConfigProducer fileConfigProducer;
|
||||
@MockBean
|
||||
private Validator validator;
|
||||
@MockBean
|
||||
private FileClientFactory fileClientFactory;
|
||||
|
||||
@Test
|
||||
public void testInitLocalCache() {
|
||||
// mock 数据
|
||||
FileConfigDO configDO1 = randomFileConfigDO().setId(1L).setMaster(true);
|
||||
fileConfigMapper.insert(configDO1);
|
||||
FileConfigDO configDO2 = randomFileConfigDO().setId(2L).setMaster(false);
|
||||
fileConfigMapper.insert(configDO2);
|
||||
// mock fileClientFactory 获得 master
|
||||
FileClient masterFileClient = mock(FileClient.class);
|
||||
when(fileClientFactory.getFileClient(eq(1L))).thenReturn(masterFileClient);
|
||||
|
||||
// 调用
|
||||
fileConfigService.initFileClients();
|
||||
// 断言 fileClientFactory 调用
|
||||
verify(fileClientFactory).createOrUpdateFileClient(eq(1L),
|
||||
eq(configDO1.getStorage()), eq(configDO1.getConfig()));
|
||||
verify(fileClientFactory).createOrUpdateFileClient(eq(2L),
|
||||
eq(configDO2.getStorage()), eq(configDO2.getConfig()));
|
||||
assertSame(masterFileClient, fileConfigService.getMasterFileClient());
|
||||
// 断言 maxUpdateTime 缓存
|
||||
assertEquals(max(configDO1.getUpdateTime(), configDO2.getUpdateTime()),
|
||||
fileConfigService.getMaxUpdateTime());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateFileConfig_success() {
|
||||
// 准备参数
|
||||
Map<String, Object> config = MapUtil.<String, Object>builder().put("basePath", "/yunai")
|
||||
.put("domain", "https://www.iocoder.cn").build();
|
||||
FileConfigCreateReqVO reqVO = randomPojo(FileConfigCreateReqVO.class,
|
||||
o -> o.setStorage(FileStorageEnum.LOCAL.getStorage()).setConfig(config));
|
||||
|
||||
// 调用
|
||||
Long fileConfigId = fileConfigService.createFileConfig(reqVO);
|
||||
// 断言
|
||||
assertNotNull(fileConfigId);
|
||||
// 校验记录的属性是否正确
|
||||
FileConfigDO fileConfig = fileConfigMapper.selectById(fileConfigId);
|
||||
assertPojoEquals(reqVO, fileConfig, "config");
|
||||
assertFalse(fileConfig.getMaster());
|
||||
assertEquals("/yunai", ((LocalFileClientConfig) fileConfig.getConfig()).getBasePath());
|
||||
assertEquals("https://www.iocoder.cn", ((LocalFileClientConfig) fileConfig.getConfig()).getDomain());
|
||||
// verify 调用
|
||||
verify(fileConfigProducer).sendFileConfigRefreshMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateFileConfig_success() {
|
||||
// mock 数据
|
||||
FileConfigDO dbFileConfig = randomPojo(FileConfigDO.class, o -> o.setStorage(FileStorageEnum.LOCAL.getStorage())
|
||||
.setConfig(new LocalFileClientConfig().setBasePath("/yunai").setDomain("https://www.iocoder.cn")));
|
||||
fileConfigMapper.insert(dbFileConfig);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
FileConfigUpdateReqVO reqVO = randomPojo(FileConfigUpdateReqVO.class, o -> {
|
||||
o.setId(dbFileConfig.getId()); // 设置更新的 ID
|
||||
Map<String, Object> config = MapUtil.<String, Object>builder().put("basePath", "/yunai2")
|
||||
.put("domain", "https://doc.iocoder.cn").build();
|
||||
o.setConfig(config);
|
||||
});
|
||||
|
||||
// 调用
|
||||
fileConfigService.updateFileConfig(reqVO);
|
||||
// 校验是否更新正确
|
||||
FileConfigDO fileConfig = fileConfigMapper.selectById(reqVO.getId()); // 获取最新的
|
||||
assertPojoEquals(reqVO, fileConfig, "config");
|
||||
assertEquals("/yunai2", ((LocalFileClientConfig) fileConfig.getConfig()).getBasePath());
|
||||
assertEquals("https://doc.iocoder.cn", ((LocalFileClientConfig) fileConfig.getConfig()).getDomain());
|
||||
// verify 调用
|
||||
verify(fileConfigProducer).sendFileConfigRefreshMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateFileConfig_notExists() {
|
||||
// 准备参数
|
||||
FileConfigUpdateReqVO reqVO = randomPojo(FileConfigUpdateReqVO.class);
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> fileConfigService.updateFileConfig(reqVO), FILE_CONFIG_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateFileConfigMaster_success() {
|
||||
// mock 数据
|
||||
FileConfigDO dbFileConfig = randomFileConfigDO().setMaster(false);
|
||||
fileConfigMapper.insert(dbFileConfig);// @Sql: 先插入出一条存在的数据
|
||||
FileConfigDO masterFileConfig = randomFileConfigDO().setMaster(true);
|
||||
fileConfigMapper.insert(masterFileConfig);// @Sql: 先插入出一条存在的数据
|
||||
|
||||
// 调用
|
||||
fileConfigService.updateFileConfigMaster(dbFileConfig.getId());
|
||||
// 断言数据
|
||||
assertTrue(fileConfigMapper.selectById(dbFileConfig.getId()).getMaster());
|
||||
assertFalse(fileConfigMapper.selectById(masterFileConfig.getId()).getMaster());
|
||||
// verify 调用
|
||||
verify(fileConfigProducer).sendFileConfigRefreshMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateFileConfigMaster_notExists() {
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> fileConfigService.updateFileConfigMaster(randomLongId()), FILE_CONFIG_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteFileConfig_success() {
|
||||
// mock 数据
|
||||
FileConfigDO dbFileConfig = randomFileConfigDO().setMaster(false);
|
||||
fileConfigMapper.insert(dbFileConfig);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
Long id = dbFileConfig.getId();
|
||||
|
||||
// 调用
|
||||
fileConfigService.deleteFileConfig(id);
|
||||
// 校验数据不存在了
|
||||
assertNull(fileConfigMapper.selectById(id));
|
||||
// verify 调用
|
||||
verify(fileConfigProducer).sendFileConfigRefreshMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteFileConfig_notExists() {
|
||||
// 准备参数
|
||||
Long id = randomLongId();
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> fileConfigService.deleteFileConfig(id), FILE_CONFIG_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteFileConfig_master() {
|
||||
// mock 数据
|
||||
FileConfigDO dbFileConfig = randomFileConfigDO().setMaster(true);
|
||||
fileConfigMapper.insert(dbFileConfig);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
Long id = dbFileConfig.getId();
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> fileConfigService.deleteFileConfig(id), FILE_CONFIG_DELETE_FAIL_MASTER);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFileConfigPage() {
|
||||
// mock 数据
|
||||
FileConfigDO dbFileConfig = randomFileConfigDO().setName("芋道源码")
|
||||
.setStorage(FileStorageEnum.LOCAL.getStorage());
|
||||
dbFileConfig.setCreateTime(buildTime(2022, 11, 11));// 等会查询到
|
||||
fileConfigMapper.insert(dbFileConfig);
|
||||
// 测试 name 不匹配
|
||||
fileConfigMapper.insert(cloneIgnoreId(dbFileConfig, o -> o.setName("源码")));
|
||||
// 测试 storage 不匹配
|
||||
fileConfigMapper.insert(cloneIgnoreId(dbFileConfig, o -> o.setStorage(FileStorageEnum.DB.getStorage())));
|
||||
// 测试 createTime 不匹配
|
||||
fileConfigMapper.insert(cloneIgnoreId(dbFileConfig, o -> o.setCreateTime(buildTime(2022, 12, 12))));
|
||||
// 准备参数
|
||||
FileConfigPageReqVO reqVO = new FileConfigPageReqVO();
|
||||
reqVO.setName("芋道");
|
||||
reqVO.setStorage(FileStorageEnum.LOCAL.getStorage());
|
||||
reqVO.setBeginCreateTime(buildTime(2022, 11, 10));
|
||||
reqVO.setEndCreateTime(buildTime(2022, 11, 12));
|
||||
|
||||
// 调用
|
||||
PageResult<FileConfigDO> pageResult = fileConfigService.getFileConfigPage(reqVO);
|
||||
// 断言
|
||||
assertEquals(1, pageResult.getTotal());
|
||||
assertEquals(1, pageResult.getList().size());
|
||||
assertPojoEquals(dbFileConfig, pageResult.getList().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFileConfig() throws Exception {
|
||||
// mock 数据
|
||||
FileConfigDO dbFileConfig = randomFileConfigDO().setMaster(false);
|
||||
fileConfigMapper.insert(dbFileConfig);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
Long id = dbFileConfig.getId();
|
||||
// mock 获得 Client
|
||||
FileClient fileClient = mock(FileClient.class);
|
||||
when(fileClientFactory.getFileClient(eq(id))).thenReturn(fileClient);
|
||||
when(fileClient.upload(any(), any())).thenReturn("https://www.iocoder.cn");
|
||||
|
||||
// 调用,并断言
|
||||
assertEquals("https://www.iocoder.cn", fileConfigService.testFileConfig(id));
|
||||
}
|
||||
|
||||
private FileConfigDO randomFileConfigDO() {
|
||||
return randomPojo(FileConfigDO.class).setStorage(randomEle(FileStorageEnum.values()).getStorage())
|
||||
.setConfig(new EmptyFileClientConfig());
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class EmptyFileClientConfig implements FileClientConfig, Serializable {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
package cn.iocoder.yudao.module.infra.service.file;
|
||||
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
|
||||
import cn.iocoder.yudao.framework.file.core.client.FileClient;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
import cn.iocoder.yudao.framework.test.core.util.AssertUtils;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO;
|
||||
import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO;
|
||||
import cn.iocoder.yudao.module.infra.dal.mysql.file.FileMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.buildTime;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
|
||||
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_NOT_EXISTS;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.same;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@Import({FileServiceImpl.class})
|
||||
public class FileServiceTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private FileService fileService;
|
||||
|
||||
@Resource
|
||||
private FileMapper fileMapper;
|
||||
|
||||
@MockBean
|
||||
private FileConfigService fileConfigService;
|
||||
|
||||
@Test
|
||||
public void testGetFilePage() {
|
||||
// mock 数据
|
||||
FileDO dbFile = randomPojo(FileDO.class, o -> { // 等会查询到
|
||||
o.setPath("yunai");
|
||||
o.setType("jpg");
|
||||
o.setCreateTime(buildTime(2021, 1, 15));
|
||||
});
|
||||
fileMapper.insert(dbFile);
|
||||
// 测试 path 不匹配
|
||||
fileMapper.insert(ObjectUtils.cloneIgnoreId(dbFile, o -> o.setPath("tudou")));
|
||||
// 测试 type 不匹配
|
||||
fileMapper.insert(ObjectUtils.cloneIgnoreId(dbFile, o -> {
|
||||
o.setType("png");
|
||||
}));
|
||||
// 测试 createTime 不匹配
|
||||
fileMapper.insert(ObjectUtils.cloneIgnoreId(dbFile, o -> {
|
||||
o.setCreateTime(buildTime(2020, 1, 15));
|
||||
}));
|
||||
// 准备参数
|
||||
FilePageReqVO reqVO = new FilePageReqVO();
|
||||
reqVO.setPath("yunai");
|
||||
reqVO.setType("jp");
|
||||
reqVO.setBeginCreateTime(buildTime(2021, 1, 10));
|
||||
reqVO.setEndCreateTime(buildTime(2021, 1, 20));
|
||||
|
||||
// 调用
|
||||
PageResult<FileDO> pageResult = fileService.getFilePage(reqVO);
|
||||
// 断言
|
||||
assertEquals(1, pageResult.getTotal());
|
||||
assertEquals(1, pageResult.getList().size());
|
||||
AssertUtils.assertPojoEquals(dbFile, pageResult.getList().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateFile_success() throws Exception {
|
||||
// 准备参数
|
||||
String path = randomString();
|
||||
byte[] content = ResourceUtil.readBytes("file/erweima.jpg");
|
||||
// mock Master 文件客户端
|
||||
FileClient client = mock(FileClient.class);
|
||||
when(fileConfigService.getMasterFileClient()).thenReturn(client);
|
||||
String url = randomString();
|
||||
when(client.upload(same(content), same(path))).thenReturn(url);
|
||||
when(client.getId()).thenReturn(10L);
|
||||
|
||||
// 调用
|
||||
String result = fileService.createFile(path, content);
|
||||
// 断言
|
||||
assertEquals(result, url);
|
||||
// 校验数据
|
||||
FileDO file = fileMapper.selectOne(FileDO::getPath, path);
|
||||
assertEquals(10L, file.getConfigId());
|
||||
assertEquals(path, file.getPath());
|
||||
assertEquals(url, file.getUrl());
|
||||
assertEquals("jpg", file.getType());
|
||||
assertEquals(content.length, file.getSize());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteFile_success() throws Exception {
|
||||
// mock 数据
|
||||
FileDO dbFile = randomPojo(FileDO.class, o -> o.setConfigId(10L).setPath("tudou.jpg"));
|
||||
fileMapper.insert(dbFile);// @Sql: 先插入出一条存在的数据
|
||||
// mock Master 文件客户端
|
||||
FileClient client = mock(FileClient.class);
|
||||
when(fileConfigService.getFileClient(eq(10L))).thenReturn(client);
|
||||
// 准备参数
|
||||
Long id = dbFile.getId();
|
||||
|
||||
// 调用
|
||||
fileService.deleteFile(id);
|
||||
// 校验数据不存在了
|
||||
assertNull(fileMapper.selectById(id));
|
||||
// 校验调用
|
||||
verify(client).delete(eq("tudou.jpg"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteFile_notExists() {
|
||||
// 准备参数
|
||||
Long id = randomLongId();
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> fileService.deleteFile(id), FILE_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFileContent() throws Exception {
|
||||
// 准备参数
|
||||
Long configId = 10L;
|
||||
String path = "tudou.jpg";
|
||||
// mock 方法
|
||||
FileClient client = mock(FileClient.class);
|
||||
when(fileConfigService.getFileClient(eq(10L))).thenReturn(client);
|
||||
byte[] content = new byte[]{};
|
||||
when(client.getContent(eq("tudou.jpg"))).thenReturn(content);
|
||||
|
||||
// 调用
|
||||
byte[] result = fileService.getFileContent(configId, path);
|
||||
// 断言
|
||||
assertSame(result, content);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
package cn.iocoder.yudao.module.infra.service.job;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.job.vo.log.JobLogExportReqVO;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.job.vo.log.JobLogPageReqVO;
|
||||
import cn.iocoder.yudao.module.infra.dal.dataobject.job.JobLogDO;
|
||||
import cn.iocoder.yudao.module.infra.dal.mysql.job.JobLogMapper;
|
||||
import cn.iocoder.yudao.module.infra.enums.job.JobLogStatusEnum;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.hutool.core.util.RandomUtil.randomEle;
|
||||
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.buildTime;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
@Import(JobLogServiceImpl.class)
|
||||
public class JobLogServiceTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private JobLogServiceImpl jobLogService;
|
||||
@Resource
|
||||
private JobLogMapper jobLogMapper;
|
||||
|
||||
@Test
|
||||
public void testCreateJobLog_success() {
|
||||
// 准备参数
|
||||
JobLogDO reqVO = randomPojo(JobLogDO.class, o -> {
|
||||
o.setExecuteIndex(1);
|
||||
});
|
||||
// 调用
|
||||
Long jobLogId = jobLogService.createJobLog(reqVO.getJobId(), reqVO.getBeginTime(), reqVO.getHandlerName(), reqVO.getHandlerParam(), reqVO.getExecuteIndex());
|
||||
// 断言
|
||||
assertNotNull(jobLogId);
|
||||
// 校验记录的属性是否正确
|
||||
JobLogDO job = jobLogMapper.selectById(jobLogId);
|
||||
assertEquals(JobLogStatusEnum.RUNNING.getStatus(), job.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateJobLogResultAsync_success() {
|
||||
// 准备参数
|
||||
JobLogDO reqVO = randomPojo(JobLogDO.class, o -> {
|
||||
o.setExecuteIndex(1);
|
||||
});
|
||||
JobLogDO log = JobLogDO.builder().jobId(reqVO.getJobId()).handlerName(reqVO.getHandlerName()).handlerParam(reqVO.getHandlerParam()).executeIndex(reqVO.getExecuteIndex())
|
||||
.beginTime(reqVO.getBeginTime()).status(JobLogStatusEnum.RUNNING.getStatus()).build();
|
||||
jobLogMapper.insert(log);
|
||||
// 调用
|
||||
jobLogService.updateJobLogResultAsync(log.getId(), reqVO.getBeginTime(), reqVO.getDuration(), true,reqVO.getResult());
|
||||
// 校验记录的属性是否正确
|
||||
JobLogDO job = jobLogMapper.selectById(log.getId());
|
||||
assertEquals(JobLogStatusEnum.SUCCESS.getStatus(), job.getStatus());
|
||||
|
||||
// 调用
|
||||
jobLogService.updateJobLogResultAsync(log.getId(), reqVO.getBeginTime(), reqVO.getDuration(), false,reqVO.getResult());
|
||||
// 校验记录的属性是否正确
|
||||
JobLogDO job2 = jobLogMapper.selectById(log.getId());
|
||||
assertEquals(JobLogStatusEnum.FAILURE.getStatus(), job2.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJobLogListByIds_success() {
|
||||
// mock 数据
|
||||
JobLogDO dbJobLog = randomPojo(JobLogDO.class, o -> {
|
||||
o.setExecuteIndex(1);
|
||||
o.setStatus(randomEle(JobLogStatusEnum.values()).getStatus()); // 保证 status 的范围
|
||||
});
|
||||
JobLogDO cloneJobLog = ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setHandlerName(randomString()));
|
||||
jobLogMapper.insert(dbJobLog);
|
||||
// 测试 handlerName 不匹配
|
||||
jobLogMapper.insert(cloneJobLog);
|
||||
// 准备参数
|
||||
ArrayList ids = new ArrayList<>();
|
||||
ids.add(dbJobLog.getId());
|
||||
ids.add(cloneJobLog.getId());
|
||||
// 调用
|
||||
List<JobLogDO> list = jobLogService.getJobLogList(ids);
|
||||
// 断言
|
||||
assertEquals(2, list.size());
|
||||
assertPojoEquals(dbJobLog, list.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJobPage_success() {
|
||||
// mock 数据
|
||||
JobLogDO dbJobLog = randomPojo(JobLogDO.class, o -> {
|
||||
o.setExecuteIndex(1);
|
||||
o.setHandlerName("handlerName 单元测试");
|
||||
o.setStatus(JobLogStatusEnum.SUCCESS.getStatus());
|
||||
o.setBeginTime(buildTime(2021, 1, 8));
|
||||
o.setEndTime(buildTime(2021, 1, 8));
|
||||
});
|
||||
jobLogMapper.insert(dbJobLog);
|
||||
// 测试 jobId 不匹配
|
||||
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setJobId(randomLongId())));
|
||||
// 测试 handlerName 不匹配
|
||||
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setHandlerName(randomString())));
|
||||
// 测试 beginTime 不匹配
|
||||
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setBeginTime(buildTime(2021, 1, 7))));
|
||||
// 测试 endTime 不匹配
|
||||
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setEndTime(buildTime(2021, 1, 9))));
|
||||
// 测试 status 不匹配
|
||||
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setStatus(JobLogStatusEnum.FAILURE.getStatus())));
|
||||
// 准备参数
|
||||
JobLogPageReqVO reqVo = new JobLogPageReqVO();
|
||||
reqVo.setJobId(dbJobLog.getJobId());
|
||||
reqVo.setHandlerName("单元");
|
||||
reqVo.setBeginTime(dbJobLog.getBeginTime());
|
||||
reqVo.setEndTime(dbJobLog.getEndTime());
|
||||
reqVo.setStatus(JobLogStatusEnum.SUCCESS.getStatus());
|
||||
// 调用
|
||||
PageResult<JobLogDO> pageResult = jobLogService.getJobLogPage(reqVo);
|
||||
// 断言
|
||||
assertEquals(1, pageResult.getTotal());
|
||||
assertEquals(1, pageResult.getList().size());
|
||||
assertPojoEquals(dbJobLog, pageResult.getList().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJobListForExport_success() {
|
||||
// mock 数据
|
||||
JobLogDO dbJobLog = randomPojo(JobLogDO.class, o -> {
|
||||
o.setExecuteIndex(1);
|
||||
o.setHandlerName("handlerName 单元测试");
|
||||
o.setStatus(JobLogStatusEnum.SUCCESS.getStatus());
|
||||
o.setBeginTime(buildTime(2021, 1, 8));
|
||||
o.setEndTime(buildTime(2021, 1, 8));
|
||||
});
|
||||
jobLogMapper.insert(dbJobLog);
|
||||
// 测试 jobId 不匹配
|
||||
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setJobId(randomLongId())));
|
||||
// 测试 handlerName 不匹配
|
||||
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setHandlerName(randomString())));
|
||||
// 测试 beginTime 不匹配
|
||||
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setBeginTime(buildTime(2021, 1, 7))));
|
||||
// 测试 endTime 不匹配
|
||||
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setEndTime(buildTime(2021, 1, 9))));
|
||||
// 测试 status 不匹配
|
||||
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setStatus(JobLogStatusEnum.FAILURE.getStatus())));
|
||||
// 准备参数
|
||||
JobLogExportReqVO reqVo = new JobLogExportReqVO();
|
||||
reqVo.setJobId(dbJobLog.getJobId());
|
||||
reqVo.setHandlerName("单元");
|
||||
reqVo.setBeginTime(dbJobLog.getBeginTime());
|
||||
reqVo.setEndTime(dbJobLog.getEndTime());
|
||||
reqVo.setStatus(JobLogStatusEnum.SUCCESS.getStatus());
|
||||
// 调用
|
||||
List<JobLogDO> list = jobLogService.getJobLogList(reqVo);
|
||||
// 断言
|
||||
assertEquals(1, list.size());
|
||||
assertPojoEquals(dbJobLog, list.get(0));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,294 @@
|
||||
package cn.iocoder.yudao.module.infra.service.job;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
|
||||
import cn.iocoder.yudao.framework.quartz.core.scheduler.SchedulerManager;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.job.vo.job.JobCreateReqVO;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.job.vo.job.JobExportReqVO;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.job.vo.job.JobPageReqVO;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.job.vo.job.JobUpdateReqVO;
|
||||
import cn.iocoder.yudao.module.infra.convert.job.JobConvert;
|
||||
import cn.iocoder.yudao.module.infra.dal.dataobject.job.JobDO;
|
||||
import cn.iocoder.yudao.module.infra.dal.mysql.job.JobMapper;
|
||||
import cn.iocoder.yudao.module.infra.enums.job.JobStatusEnum;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.quartz.SchedulerException;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.hutool.core.util.RandomUtil.randomEle;
|
||||
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.randomPojo;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString;
|
||||
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@Import(JobServiceImpl.class)
|
||||
public class JobServiceTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private JobServiceImpl jobService;
|
||||
@Resource
|
||||
private JobMapper jobMapper;
|
||||
@MockBean
|
||||
private SchedulerManager schedulerManager;
|
||||
|
||||
@Test
|
||||
public void testCreateJob_cronExpressionValid() {
|
||||
// 准备参数。Cron 表达式为 String 类型,默认随机字符串。
|
||||
JobCreateReqVO reqVO = randomPojo(JobCreateReqVO.class);
|
||||
// 调用,并断言异常
|
||||
assertServiceException(() -> jobService.createJob(reqVO), JOB_CRON_EXPRESSION_VALID);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateJob_jobHandlerExists() throws SchedulerException {
|
||||
// 准备参数 指定 Cron 表达式
|
||||
JobCreateReqVO reqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
|
||||
// 调用
|
||||
jobService.createJob(reqVO);
|
||||
// 调用,并断言异常
|
||||
assertServiceException(() -> jobService.createJob(reqVO), JOB_HANDLER_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateJob_success() throws SchedulerException {
|
||||
// 准备参数 指定 Cron 表达式
|
||||
JobCreateReqVO reqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
|
||||
// 调用
|
||||
Long jobId = jobService.createJob(reqVO);
|
||||
// 断言
|
||||
assertNotNull(jobId);
|
||||
// 校验记录的属性是否正确
|
||||
JobDO job = jobMapper.selectById(jobId);
|
||||
assertPojoEquals(reqVO, job);
|
||||
assertEquals(JobStatusEnum.NORMAL.getStatus(), job.getStatus());
|
||||
// 校验调用
|
||||
verify(schedulerManager, times(1)).addJob(eq(job.getId()), eq(job.getHandlerName()), eq(job.getHandlerParam()), eq(job.getCronExpression()),
|
||||
eq(reqVO.getRetryCount()), eq(reqVO.getRetryInterval()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateJob_jobNotExists(){
|
||||
// 准备参数
|
||||
JobUpdateReqVO reqVO = randomPojo(JobUpdateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
|
||||
// 调用,并断言异常
|
||||
assertServiceException(() -> jobService.updateJob(reqVO), JOB_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateJob_onlyNormalStatus(){
|
||||
// mock 数据
|
||||
JobCreateReqVO createReqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
|
||||
JobDO job = JobConvert.INSTANCE.convert(createReqVO);
|
||||
job.setStatus(JobStatusEnum.INIT.getStatus());
|
||||
fillJobMonitorTimeoutEmpty(job);
|
||||
jobMapper.insert(job);
|
||||
// 准备参数
|
||||
JobUpdateReqVO updateReqVO = randomPojo(JobUpdateReqVO.class, o -> {
|
||||
o.setId(job.getId());
|
||||
o.setName(createReqVO.getName());
|
||||
o.setCronExpression(createReqVO.getCronExpression());
|
||||
});
|
||||
// 调用,并断言异常
|
||||
assertServiceException(() -> jobService.updateJob(updateReqVO), JOB_UPDATE_ONLY_NORMAL_STATUS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateJob_success() throws SchedulerException {
|
||||
// mock 数据
|
||||
JobCreateReqVO createReqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
|
||||
JobDO job = JobConvert.INSTANCE.convert(createReqVO);
|
||||
job.setStatus(JobStatusEnum.NORMAL.getStatus());
|
||||
fillJobMonitorTimeoutEmpty(job);
|
||||
jobMapper.insert(job);
|
||||
// 准备参数
|
||||
JobUpdateReqVO updateReqVO = randomPojo(JobUpdateReqVO.class, o -> {
|
||||
o.setId(job.getId());
|
||||
o.setName(createReqVO.getName());
|
||||
o.setCronExpression(createReqVO.getCronExpression());
|
||||
});
|
||||
// 调用
|
||||
jobService.updateJob(updateReqVO);
|
||||
// 校验记录的属性是否正确
|
||||
JobDO updateJob = jobMapper.selectById(updateReqVO.getId());
|
||||
assertPojoEquals(updateReqVO, updateJob);
|
||||
// 校验调用
|
||||
verify(schedulerManager, times(1)).updateJob(eq(job.getHandlerName()), eq(updateReqVO.getHandlerParam()), eq(updateReqVO.getCronExpression()),
|
||||
eq(updateReqVO.getRetryCount()), eq(updateReqVO.getRetryInterval()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateJobStatus_changeStatusInvalid() {
|
||||
// 调用,并断言异常
|
||||
assertServiceException(() -> jobService.updateJobStatus(1L, JobStatusEnum.INIT.getStatus()), JOB_CHANGE_STATUS_INVALID);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateJobStatus_changeStatusEquals() {
|
||||
// mock 数据
|
||||
JobCreateReqVO createReqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
|
||||
JobDO job = JobConvert.INSTANCE.convert(createReqVO);
|
||||
job.setStatus(JobStatusEnum.NORMAL.getStatus());
|
||||
fillJobMonitorTimeoutEmpty(job);
|
||||
jobMapper.insert(job);
|
||||
// 调用,并断言异常
|
||||
assertServiceException(() -> jobService.updateJobStatus(job.getId(), job.getStatus()), JOB_CHANGE_STATUS_EQUALS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateJobStatus_NormalToStop_success() throws SchedulerException {
|
||||
// mock 数据
|
||||
JobCreateReqVO createReqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
|
||||
JobDO job = JobConvert.INSTANCE.convert(createReqVO);
|
||||
job.setStatus(JobStatusEnum.NORMAL.getStatus());
|
||||
fillJobMonitorTimeoutEmpty(job);
|
||||
jobMapper.insert(job);
|
||||
// 调用
|
||||
jobService.updateJobStatus(job.getId(), JobStatusEnum.STOP.getStatus());
|
||||
// 校验记录的属性是否正确
|
||||
JobDO updateJob = jobMapper.selectById(job.getId());
|
||||
assertEquals(JobStatusEnum.STOP.getStatus(), updateJob.getStatus());
|
||||
// 校验调用
|
||||
verify(schedulerManager, times(1)).pauseJob(eq(job.getHandlerName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateJobStatus_StopToNormal_success() throws SchedulerException {
|
||||
// mock 数据
|
||||
JobCreateReqVO createReqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
|
||||
JobDO job = JobConvert.INSTANCE.convert(createReqVO);
|
||||
job.setStatus(JobStatusEnum.STOP.getStatus());
|
||||
fillJobMonitorTimeoutEmpty(job);
|
||||
jobMapper.insert(job);
|
||||
// 调用
|
||||
jobService.updateJobStatus(job.getId(), JobStatusEnum.NORMAL.getStatus());
|
||||
// 校验记录的属性是否正确
|
||||
JobDO updateJob = jobMapper.selectById(job.getId());
|
||||
assertEquals(JobStatusEnum.NORMAL.getStatus(), updateJob.getStatus());
|
||||
// 校验调用
|
||||
verify(schedulerManager, times(1)).resumeJob(eq(job.getHandlerName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTriggerJob_success() throws SchedulerException {
|
||||
// mock 数据
|
||||
JobCreateReqVO createReqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
|
||||
JobDO job = JobConvert.INSTANCE.convert(createReqVO);
|
||||
job.setStatus(JobStatusEnum.NORMAL.getStatus());
|
||||
fillJobMonitorTimeoutEmpty(job);
|
||||
jobMapper.insert(job);
|
||||
// 调用
|
||||
jobService.triggerJob(job.getId());
|
||||
// 校验调用
|
||||
verify(schedulerManager, times(1)).triggerJob(eq(job.getId()), eq(job.getHandlerName()), eq(job.getHandlerParam()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteJob_success() throws SchedulerException {
|
||||
// mock 数据
|
||||
JobCreateReqVO createReqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
|
||||
JobDO job = JobConvert.INSTANCE.convert(createReqVO);
|
||||
job.setStatus(JobStatusEnum.NORMAL.getStatus());
|
||||
fillJobMonitorTimeoutEmpty(job);
|
||||
jobMapper.insert(job);
|
||||
// 调用 UPDATE inf_job SET deleted=1 WHERE id=? AND deleted=0
|
||||
jobService.deleteJob(job.getId());
|
||||
// 校验数据不存在了 WHERE id=? AND deleted=0 查询为空正常
|
||||
assertNull(jobMapper.selectById(job.getId()));
|
||||
// 校验调用
|
||||
verify(schedulerManager, times(1)).deleteJob(eq(job.getHandlerName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJobListByIds_success() {
|
||||
// mock 数据
|
||||
JobDO dbJob = randomPojo(JobDO.class, o -> {
|
||||
o.setStatus(randomEle(JobStatusEnum.values()).getStatus()); // 保证 status 的范围
|
||||
});
|
||||
JobDO cloneJob = ObjectUtils.cloneIgnoreId(dbJob, o -> o.setHandlerName(randomString()));
|
||||
jobMapper.insert(dbJob);
|
||||
// 测试 handlerName 不匹配
|
||||
jobMapper.insert(cloneJob);
|
||||
// 准备参数
|
||||
ArrayList<Long> ids = new ArrayList<>();
|
||||
ids.add(dbJob.getId());
|
||||
ids.add(cloneJob.getId());
|
||||
// 调用
|
||||
List<JobDO> list = jobService.getJobList(ids);
|
||||
// 断言
|
||||
assertEquals(2, list.size());
|
||||
assertPojoEquals(dbJob, list.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJobPage_success() {
|
||||
// mock 数据
|
||||
JobDO dbJob = randomPojo(JobDO.class, o -> {
|
||||
o.setName("定时任务测试");
|
||||
o.setHandlerName("handlerName 单元测试");
|
||||
o.setStatus(JobStatusEnum.INIT.getStatus());
|
||||
});
|
||||
jobMapper.insert(dbJob);
|
||||
// 测试 name 不匹配
|
||||
jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setName("土豆")));
|
||||
// 测试 status 不匹配
|
||||
jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setStatus(JobStatusEnum.NORMAL.getStatus())));
|
||||
// 测试 handlerName 不匹配
|
||||
jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setHandlerName(randomString())));
|
||||
// 准备参数
|
||||
JobPageReqVO reqVo = new JobPageReqVO();
|
||||
reqVo.setName("定时");
|
||||
reqVo.setStatus(JobStatusEnum.INIT.getStatus());
|
||||
reqVo.setHandlerName("单元");
|
||||
// 调用
|
||||
PageResult<JobDO> pageResult = jobService.getJobPage(reqVo);
|
||||
// 断言
|
||||
assertEquals(1, pageResult.getTotal());
|
||||
assertEquals(1, pageResult.getList().size());
|
||||
assertPojoEquals(dbJob, pageResult.getList().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJobListForExport_success() {
|
||||
// mock 数据
|
||||
JobDO dbJob = randomPojo(JobDO.class, o -> {
|
||||
o.setName("定时任务测试");
|
||||
o.setHandlerName("handlerName 单元测试");
|
||||
o.setStatus(JobStatusEnum.INIT.getStatus());
|
||||
});
|
||||
jobMapper.insert(dbJob);
|
||||
// 测试 name 不匹配
|
||||
jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setName("土豆")));
|
||||
// 测试 status 不匹配
|
||||
jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setStatus(JobStatusEnum.NORMAL.getStatus())));
|
||||
// 测试 handlerName 不匹配
|
||||
jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setHandlerName(randomString())));
|
||||
// 准备参数
|
||||
JobExportReqVO reqVo = new JobExportReqVO();
|
||||
reqVo.setName("定时");
|
||||
reqVo.setStatus(JobStatusEnum.INIT.getStatus());
|
||||
reqVo.setHandlerName("单元");
|
||||
// 调用
|
||||
List<JobDO> list = jobService.getJobList(reqVo);
|
||||
// 断言
|
||||
assertEquals(1, list.size());
|
||||
assertPojoEquals(dbJob, list.get(0));
|
||||
}
|
||||
|
||||
private static void fillJobMonitorTimeoutEmpty(JobDO job) {
|
||||
if (job.getMonitorTimeout() == null) {
|
||||
job.setMonitorTimeout(0);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
package cn.iocoder.yudao.module.infra.service.logger;
|
||||
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.iocoder.yudao.framework.apilog.core.service.dto.ApiAccessLogCreateReqDTO;
|
||||
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
||||
import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
import cn.iocoder.yudao.framework.test.core.util.RandomUtils;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogExportReqVO;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogPageReqVO;
|
||||
import cn.iocoder.yudao.module.infra.dal.dataobject.logger.ApiAccessLogDO;
|
||||
import cn.iocoder.yudao.module.infra.dal.mysql.logger.ApiAccessLogMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.buildTime;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
@Import(ApiAccessLogServiceImpl.class)
|
||||
public class ApiAccessLogServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private ApiAccessLogService apiAccessLogService;
|
||||
|
||||
@Resource
|
||||
private ApiAccessLogMapper apiAccessLogMapper;
|
||||
|
||||
@Test
|
||||
public void testGetApiAccessLogPage() {
|
||||
// 构造测试数据
|
||||
long userId = 2233L;
|
||||
int userType = UserTypeEnum.ADMIN.getValue();
|
||||
String applicationName = "yudao-test";
|
||||
String requestUrl = "foo";
|
||||
Date beginTime = buildTime(2021, 3, 13);
|
||||
int duration = 1000;
|
||||
int resultCode = GlobalErrorCodeConstants.SUCCESS.getCode();
|
||||
|
||||
ApiAccessLogDO infApiAccessLogDO = RandomUtils.randomPojo(ApiAccessLogDO.class, dto -> {
|
||||
dto.setUserId(userId);
|
||||
dto.setUserType(userType);
|
||||
dto.setApplicationName(applicationName);
|
||||
dto.setRequestUrl(requestUrl);
|
||||
dto.setBeginTime(beginTime);
|
||||
dto.setDuration(duration);
|
||||
dto.setResultCode(resultCode);
|
||||
});
|
||||
apiAccessLogMapper.insert(infApiAccessLogDO);
|
||||
|
||||
// 下面几个都是不匹配的数据
|
||||
// userId 不同的
|
||||
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setUserId(3344L)));
|
||||
// userType
|
||||
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue())));
|
||||
// applicationName 不同的
|
||||
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setApplicationName("test")));
|
||||
// requestUrl 不同的
|
||||
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setRequestUrl("bar")));
|
||||
// 构造一个早期时间 2021-02-06 00:00:00
|
||||
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setBeginTime(buildTime(2021, 2, 6))));
|
||||
// duration 不同的
|
||||
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setDuration(100)));
|
||||
// resultCode 不同的
|
||||
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setResultCode(2)));
|
||||
|
||||
// 构造调用参数
|
||||
ApiAccessLogPageReqVO reqVO = new ApiAccessLogPageReqVO();
|
||||
reqVO.setUserId(userId);
|
||||
reqVO.setUserType(userType);
|
||||
reqVO.setApplicationName(applicationName);
|
||||
reqVO.setRequestUrl(requestUrl);
|
||||
reqVO.setBeginBeginTime(buildTime(2021, 3, 12));
|
||||
reqVO.setEndBeginTime(buildTime(2021, 3, 14));
|
||||
reqVO.setDuration(duration);
|
||||
reqVO.setResultCode(resultCode);
|
||||
|
||||
// 调用service方法
|
||||
PageResult<ApiAccessLogDO> pageResult = apiAccessLogService.getApiAccessLogPage(reqVO);
|
||||
|
||||
// 断言,只查到了一条符合条件的
|
||||
assertEquals(1, pageResult.getTotal());
|
||||
assertEquals(1, pageResult.getList().size());
|
||||
assertPojoEquals(infApiAccessLogDO, pageResult.getList().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetApiAccessLogList() {
|
||||
// 构造测试数据
|
||||
long userId = 2233L;
|
||||
int userType = UserTypeEnum.ADMIN.getValue();
|
||||
String applicationName = "yudao-test";
|
||||
String requestUrl = "foo";
|
||||
Date beginTime = buildTime(2021, 3, 13);
|
||||
int duration = 1000;
|
||||
int resultCode = GlobalErrorCodeConstants.SUCCESS.getCode();
|
||||
|
||||
ApiAccessLogDO infApiAccessLogDO = RandomUtils.randomPojo(ApiAccessLogDO.class, dto -> {
|
||||
dto.setUserId(userId);
|
||||
dto.setUserType(userType);
|
||||
dto.setApplicationName(applicationName);
|
||||
dto.setRequestUrl(requestUrl);
|
||||
dto.setBeginTime(beginTime);
|
||||
dto.setDuration(duration);
|
||||
dto.setResultCode(resultCode);
|
||||
});
|
||||
apiAccessLogMapper.insert(infApiAccessLogDO);
|
||||
|
||||
// 下面几个都是不匹配的数据
|
||||
// userId 不同的
|
||||
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setUserId(3344L)));
|
||||
// userType
|
||||
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue())));
|
||||
// applicationName 不同的
|
||||
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setApplicationName("test")));
|
||||
// requestUrl 不同的
|
||||
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setRequestUrl("bar")));
|
||||
// 构造一个早期时间 2021-02-06 00:00:00
|
||||
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setBeginTime(buildTime(2021, 2, 6))));
|
||||
// duration 不同的
|
||||
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setDuration(100)));
|
||||
// resultCode 不同的
|
||||
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setResultCode(2)));
|
||||
|
||||
// 构造调用参数
|
||||
ApiAccessLogExportReqVO reqVO = new ApiAccessLogExportReqVO();
|
||||
reqVO.setUserId(userId);
|
||||
reqVO.setUserType(userType);
|
||||
reqVO.setApplicationName(applicationName);
|
||||
reqVO.setRequestUrl(requestUrl);
|
||||
reqVO.setBeginBeginTime(buildTime(2021, 3, 12));
|
||||
reqVO.setEndBeginTime(buildTime(2021, 3, 14));
|
||||
reqVO.setDuration(duration);
|
||||
reqVO.setResultCode(resultCode);
|
||||
|
||||
// 调用service方法
|
||||
List<ApiAccessLogDO> list = apiAccessLogService.getApiAccessLogList(reqVO);
|
||||
|
||||
// 断言,只查到了一条符合条件的
|
||||
assertEquals(1, list.size());
|
||||
assertPojoEquals(infApiAccessLogDO, list.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateApiAccessLogAsync() {
|
||||
// 准备参数
|
||||
ApiAccessLogCreateReqDTO createDTO = RandomUtils.randomPojo(ApiAccessLogCreateReqDTO.class,
|
||||
dto -> dto.setUserType(RandomUtil.randomEle(UserTypeEnum.values()).getValue()));
|
||||
|
||||
// 调用
|
||||
apiAccessLogService.createApiAccessLogAsync(createDTO);
|
||||
// 断言
|
||||
ApiAccessLogDO infApiAccessLogDO = apiAccessLogMapper.selectOne(null);
|
||||
assertNotNull(infApiAccessLogDO);
|
||||
assertPojoEquals(createDTO, infApiAccessLogDO);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
package cn.iocoder.yudao.module.infra.service.logger;
|
||||
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.iocoder.yudao.framework.apilog.core.service.dto.ApiErrorLogCreateReqDTO;
|
||||
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
import cn.iocoder.yudao.framework.test.core.util.RandomUtils;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiErrorLogExportReqVO;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiErrorLogPageReqVO;
|
||||
import cn.iocoder.yudao.module.infra.dal.dataobject.logger.ApiErrorLogDO;
|
||||
import cn.iocoder.yudao.module.infra.dal.mysql.logger.ApiErrorLogMapper;
|
||||
import cn.iocoder.yudao.module.infra.enums.logger.ApiErrorLogProcessStatusEnum;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.buildTime;
|
||||
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.module.infra.enums.ErrorCodeConstants.API_ERROR_LOG_NOT_FOUND;
|
||||
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.API_ERROR_LOG_PROCESSED;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
@Import(ApiErrorLogServiceImpl.class)
|
||||
public class ApiErrorLogServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private ApiErrorLogServiceImpl apiErrorLogService;
|
||||
|
||||
@Resource
|
||||
private ApiErrorLogMapper infApiErrorLogMapper;
|
||||
|
||||
@Test
|
||||
public void testGetApiErrorLogPage() {
|
||||
// 构造测试数据
|
||||
long userId = 2233L;
|
||||
int userType = UserTypeEnum.ADMIN.getValue();
|
||||
String applicationName = "yudao-test";
|
||||
String requestUrl = "foo";
|
||||
Date beginTime = buildTime(2021, 3, 13);
|
||||
int progressStatus = ApiErrorLogProcessStatusEnum.INIT.getStatus();
|
||||
|
||||
ApiErrorLogDO infApiErrorLogDO = RandomUtils.randomPojo(ApiErrorLogDO.class, logDO -> {
|
||||
logDO.setUserId(userId);
|
||||
logDO.setUserType(userType);
|
||||
logDO.setApplicationName(applicationName);
|
||||
logDO.setRequestUrl(requestUrl);
|
||||
logDO.setExceptionTime(beginTime);
|
||||
logDO.setProcessStatus(progressStatus);
|
||||
});
|
||||
infApiErrorLogMapper.insert(infApiErrorLogDO);
|
||||
|
||||
// 下面几个都是不匹配的数据
|
||||
// userId 不同的
|
||||
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setUserId(3344L)));
|
||||
// userType
|
||||
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue())));
|
||||
// applicationName 不同的
|
||||
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setApplicationName("test")));
|
||||
// requestUrl 不同的
|
||||
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setRequestUrl("bar")));
|
||||
// 构造一个早期时间 2021-02-06 00:00:00
|
||||
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setExceptionTime(buildTime(2021, 2, 6))));
|
||||
// progressStatus 不同的
|
||||
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus())));
|
||||
|
||||
// 构造调用参数
|
||||
ApiErrorLogPageReqVO reqVO = new ApiErrorLogPageReqVO();
|
||||
reqVO.setUserId(userId);
|
||||
reqVO.setUserType(userType);
|
||||
reqVO.setApplicationName(applicationName);
|
||||
reqVO.setRequestUrl(requestUrl);
|
||||
reqVO.setBeginExceptionTime(buildTime(2021, 3, 12));
|
||||
reqVO.setEndExceptionTime(buildTime(2021, 3, 14));
|
||||
reqVO.setProcessStatus(progressStatus);
|
||||
|
||||
// 调用service方法
|
||||
PageResult<ApiErrorLogDO> pageResult = apiErrorLogService.getApiErrorLogPage(reqVO);
|
||||
|
||||
// 断言,只查到了一条符合条件的
|
||||
assertEquals(1, pageResult.getTotal());
|
||||
assertEquals(1, pageResult.getList().size());
|
||||
assertPojoEquals(infApiErrorLogDO, pageResult.getList().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetApiErrorLogList() {
|
||||
// 构造测试数据
|
||||
long userId = 2233L;
|
||||
int userType = UserTypeEnum.ADMIN.getValue();
|
||||
String applicationName = "yudao-test";
|
||||
String requestUrl = "foo";
|
||||
Date beginTime = buildTime(2021, 3, 13);
|
||||
int progressStatus = ApiErrorLogProcessStatusEnum.INIT.getStatus();
|
||||
|
||||
ApiErrorLogDO infApiErrorLogDO = RandomUtils.randomPojo(ApiErrorLogDO.class, logDO -> {
|
||||
logDO.setUserId(userId);
|
||||
logDO.setUserType(userType);
|
||||
logDO.setApplicationName(applicationName);
|
||||
logDO.setRequestUrl(requestUrl);
|
||||
logDO.setExceptionTime(beginTime);
|
||||
logDO.setProcessStatus(progressStatus);
|
||||
});
|
||||
infApiErrorLogMapper.insert(infApiErrorLogDO);
|
||||
|
||||
// 下面几个都是不匹配的数据
|
||||
// userId 不同的
|
||||
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setUserId(3344L)));
|
||||
// userType
|
||||
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue())));
|
||||
// applicationName 不同的
|
||||
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setApplicationName("test")));
|
||||
// requestUrl 不同的
|
||||
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setRequestUrl("bar")));
|
||||
// 构造一个早期时间 2021-02-06 00:00:00
|
||||
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setExceptionTime(buildTime(2021, 2, 6))));
|
||||
// progressStatus 不同的
|
||||
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus())));
|
||||
|
||||
// 构造调用参数
|
||||
ApiErrorLogExportReqVO reqVO = new ApiErrorLogExportReqVO();
|
||||
reqVO.setUserId(userId);
|
||||
reqVO.setUserType(userType);
|
||||
reqVO.setApplicationName(applicationName);
|
||||
reqVO.setRequestUrl(requestUrl);
|
||||
reqVO.setBeginExceptionTime(buildTime(2021, 3, 12));
|
||||
reqVO.setEndExceptionTime(buildTime(2021, 3, 14));
|
||||
reqVO.setProcessStatus(progressStatus);
|
||||
|
||||
// 调用service方法
|
||||
List<ApiErrorLogDO> list = apiErrorLogService.getApiErrorLogList(reqVO);
|
||||
|
||||
// 断言,只查到了一条符合条件的
|
||||
assertEquals(1, list.size());
|
||||
assertPojoEquals(infApiErrorLogDO, list.get(0));
|
||||
}
|
||||
|
||||
|
||||
// TODO 芋艿:单元测试,可以拆小一点
|
||||
@Test
|
||||
public void testUpdateApiErrorLogProcess() {
|
||||
// 先构造两条数据,第一条用于抛出异常,第二条用于正常的执行update操作
|
||||
Long processUserId = 2233L;
|
||||
|
||||
ApiErrorLogDO first = RandomUtils.randomPojo(ApiErrorLogDO.class, logDO -> {
|
||||
logDO.setProcessUserId(processUserId);
|
||||
logDO.setUserType(UserTypeEnum.ADMIN.getValue());
|
||||
logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus());
|
||||
});
|
||||
infApiErrorLogMapper.insert(first);
|
||||
|
||||
ApiErrorLogDO second = RandomUtils.randomPojo(ApiErrorLogDO.class, logDO -> {
|
||||
logDO.setProcessUserId(1122L);
|
||||
logDO.setUserType(UserTypeEnum.ADMIN.getValue());
|
||||
logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
|
||||
});
|
||||
infApiErrorLogMapper.insert(second);
|
||||
|
||||
Long firstId = first.getId();
|
||||
Long secondId = second.getId();
|
||||
|
||||
// 执行正常的 update 操作
|
||||
apiErrorLogService.updateApiErrorLogProcess(secondId, ApiErrorLogProcessStatusEnum.DONE.getStatus(), processUserId);
|
||||
ApiErrorLogDO secondSelect = infApiErrorLogMapper.selectOne("id", secondId);
|
||||
|
||||
// id 为 0 查询不到,应该抛出异常 API_ERROR_LOG_NOT_FOUND
|
||||
assertServiceException(() -> apiErrorLogService.updateApiErrorLogProcess(0L, ApiErrorLogProcessStatusEnum.DONE.getStatus(), processUserId), API_ERROR_LOG_NOT_FOUND);
|
||||
// id 为 first 的 progressStatus 为 DONE ,应该抛出 API_ERROR_LOG_PROCESSED
|
||||
assertServiceException(() -> apiErrorLogService.updateApiErrorLogProcess(firstId, ApiErrorLogProcessStatusEnum.DONE.getStatus(), processUserId), API_ERROR_LOG_PROCESSED);
|
||||
// 验证 progressStatus 是否修改成功
|
||||
Assertions.assertEquals(ApiErrorLogProcessStatusEnum.DONE.getStatus(), secondSelect.getProcessStatus());
|
||||
// 验证 progressUserId 是否修改成功
|
||||
Assertions.assertEquals(processUserId, secondSelect.getProcessUserId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateApiErrorLogAsync() {
|
||||
// 准备参数
|
||||
ApiErrorLogCreateReqDTO createDTO = RandomUtils.randomPojo(ApiErrorLogCreateReqDTO.class,
|
||||
dto -> dto.setUserType(RandomUtil.randomEle(UserTypeEnum.values()).getValue()));
|
||||
|
||||
// 调用
|
||||
apiErrorLogService.createApiErrorLogAsync(createDTO);
|
||||
// 断言
|
||||
ApiErrorLogDO infApiErrorLogDO = infApiErrorLogMapper.selectOne(null);
|
||||
assertNotNull(infApiErrorLogDO);
|
||||
assertPojoEquals(createDTO, infApiErrorLogDO);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
package cn.iocoder.yudao.module.infra.service.test;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.test.vo.TestDemoCreateReqVO;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.test.vo.TestDemoExportReqVO;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.test.vo.TestDemoPageReqVO;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.test.vo.TestDemoUpdateReqVO;
|
||||
import cn.iocoder.yudao.module.infra.dal.dataobject.test.TestDemoDO;
|
||||
import cn.iocoder.yudao.module.infra.dal.mysql.test.TestDemoMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
|
||||
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.infra.enums.ErrorCodeConstants.TEST_DEMO_NOT_EXISTS;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* {@link TestDemoServiceImpl} 的单元测试类
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Import(TestDemoServiceImpl.class)
|
||||
public class TestDemoServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private TestDemoServiceImpl testDemoService;
|
||||
|
||||
@Resource
|
||||
private TestDemoMapper testDemoMapper;
|
||||
|
||||
@Test
|
||||
public void testCreateTestDemo_success() {
|
||||
// 准备参数
|
||||
TestDemoCreateReqVO reqVO = randomPojo(TestDemoCreateReqVO.class);
|
||||
|
||||
// 调用
|
||||
Long testDemoId = testDemoService.createTestDemo(reqVO);
|
||||
// 断言
|
||||
assertNotNull(testDemoId);
|
||||
// 校验记录的属性是否正确
|
||||
TestDemoDO testDemo = testDemoMapper.selectById(testDemoId);
|
||||
assertPojoEquals(reqVO, testDemo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateTestDemo_success() {
|
||||
// mock 数据
|
||||
TestDemoDO dbTestDemo = randomPojo(TestDemoDO.class);
|
||||
testDemoMapper.insert(dbTestDemo);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
TestDemoUpdateReqVO reqVO = randomPojo(TestDemoUpdateReqVO.class, o -> {
|
||||
o.setId(dbTestDemo.getId()); // 设置更新的 ID
|
||||
});
|
||||
|
||||
// 调用
|
||||
testDemoService.updateTestDemo(reqVO);
|
||||
// 校验是否更新正确
|
||||
TestDemoDO testDemo = testDemoMapper.selectById(reqVO.getId()); // 获取最新的
|
||||
assertPojoEquals(reqVO, testDemo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateTestDemo_notExists() {
|
||||
// 准备参数
|
||||
TestDemoUpdateReqVO reqVO = randomPojo(TestDemoUpdateReqVO.class);
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> testDemoService.updateTestDemo(reqVO), TEST_DEMO_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteTestDemo_success() {
|
||||
// mock 数据
|
||||
TestDemoDO dbTestDemo = randomPojo(TestDemoDO.class);
|
||||
testDemoMapper.insert(dbTestDemo);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
Long id = dbTestDemo.getId();
|
||||
|
||||
// 调用
|
||||
testDemoService.deleteTestDemo(id);
|
||||
// 校验数据不存在了
|
||||
assertNull(testDemoMapper.selectById(id));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteTestDemo_notExists() {
|
||||
// 准备参数
|
||||
Long id = randomLongId();
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> testDemoService.deleteTestDemo(id), TEST_DEMO_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetTestDemoPage() {
|
||||
// mock 数据
|
||||
TestDemoDO dbTestDemo = randomPojo(TestDemoDO.class, o -> { // 等会查询到
|
||||
o.setName("芋道源码");
|
||||
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
o.setType(1);
|
||||
o.setCategory(2);
|
||||
o.setRemark("哈哈哈");
|
||||
o.setCreateTime(DateUtils.buildTime(2021, 11, 11));
|
||||
});
|
||||
testDemoMapper.insert(dbTestDemo);
|
||||
// 测试 name 不匹配
|
||||
testDemoMapper.insert(cloneIgnoreId(dbTestDemo, o -> o.setName("不匹配")));
|
||||
// 测试 status 不匹配
|
||||
testDemoMapper.insert(cloneIgnoreId(dbTestDemo, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
|
||||
// 测试 type 不匹配
|
||||
testDemoMapper.insert(cloneIgnoreId(dbTestDemo, o -> o.setType(2)));
|
||||
// 测试 category 不匹配
|
||||
testDemoMapper.insert(cloneIgnoreId(dbTestDemo, o -> o.setCategory(1)));
|
||||
// 测试 remark 不匹配
|
||||
testDemoMapper.insert(cloneIgnoreId(dbTestDemo, o -> o.setRemark("呵呵呵")));
|
||||
// 测试 createTime 不匹配
|
||||
testDemoMapper.insert(cloneIgnoreId(dbTestDemo, o -> o.setCreateTime(DateUtils.buildTime(2021, 12, 12))));
|
||||
// 准备参数
|
||||
TestDemoPageReqVO reqVO = new TestDemoPageReqVO();
|
||||
reqVO.setName("芋道");
|
||||
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
reqVO.setType(1);
|
||||
reqVO.setCategory(2);
|
||||
reqVO.setRemark("哈哈哈");
|
||||
reqVO.setBeginCreateTime(DateUtils.buildTime(2021, 11, 10));
|
||||
reqVO.setEndCreateTime(DateUtils.buildTime(2021, 11, 12));
|
||||
|
||||
// 调用
|
||||
PageResult<TestDemoDO> pageResult = testDemoService.getTestDemoPage(reqVO);
|
||||
// 断言
|
||||
assertEquals(1, pageResult.getTotal());
|
||||
assertEquals(1, pageResult.getList().size());
|
||||
assertPojoEquals(dbTestDemo, pageResult.getList().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetTestDemoList() {
|
||||
// mock 数据
|
||||
TestDemoDO dbTestDemo = randomPojo(TestDemoDO.class, o -> { // 等会查询到
|
||||
o.setName("芋道源码");
|
||||
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
o.setType(1);
|
||||
o.setCategory(2);
|
||||
o.setRemark("哈哈哈");
|
||||
o.setCreateTime(DateUtils.buildTime(2021, 11, 11));
|
||||
});
|
||||
testDemoMapper.insert(dbTestDemo);
|
||||
// 测试 name 不匹配
|
||||
testDemoMapper.insert(cloneIgnoreId(dbTestDemo, o -> o.setName("不匹配")));
|
||||
// 测试 status 不匹配
|
||||
testDemoMapper.insert(cloneIgnoreId(dbTestDemo, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
|
||||
// 测试 type 不匹配
|
||||
testDemoMapper.insert(cloneIgnoreId(dbTestDemo, o -> o.setType(2)));
|
||||
// 测试 category 不匹配
|
||||
testDemoMapper.insert(cloneIgnoreId(dbTestDemo, o -> o.setCategory(1)));
|
||||
// 测试 remark 不匹配
|
||||
testDemoMapper.insert(cloneIgnoreId(dbTestDemo, o -> o.setRemark("呵呵呵")));
|
||||
// 测试 createTime 不匹配
|
||||
testDemoMapper.insert(cloneIgnoreId(dbTestDemo, o -> o.setCreateTime(DateUtils.buildTime(2021, 12, 12))));
|
||||
// 准备参数
|
||||
TestDemoExportReqVO reqVO = new TestDemoExportReqVO();
|
||||
reqVO.setName("芋道");
|
||||
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
reqVO.setType(1);
|
||||
reqVO.setCategory(2);
|
||||
reqVO.setRemark("哈哈哈");
|
||||
reqVO.setBeginCreateTime(DateUtils.buildTime(2021, 11, 10));
|
||||
reqVO.setEndCreateTime(DateUtils.buildTime(2021, 11, 12));
|
||||
|
||||
// 调用
|
||||
List<TestDemoDO> list = testDemoService.getTestDemoList(reqVO);
|
||||
// 断言
|
||||
assertEquals(1, list.size());
|
||||
assertPojoEquals(dbTestDemo, list.get(0));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
spring:
|
||||
main:
|
||||
lazy-initialization: true # 开启懒加载,加快速度
|
||||
banner-mode: off # 单元测试,禁用 Banner
|
||||
|
||||
--- #################### 数据库相关配置 ####################
|
||||
|
||||
spring:
|
||||
# 数据源配置项
|
||||
datasource:
|
||||
name: ruoyi-vue-pro
|
||||
url: jdbc:h2:mem:testdb;MODE=MYSQL;DATABASE_TO_UPPER=false; # MODE 使用 MySQL 模式;DATABASE_TO_UPPER 配置表和字段使用小写
|
||||
driver-class-name: org.h2.Driver
|
||||
username: sa
|
||||
password:
|
||||
druid:
|
||||
async-init: true # 单元测试,异步初始化 Druid 连接池,提升启动速度
|
||||
initial-size: 1 # 单元测试,配置为 1,提升启动速度
|
||||
sql:
|
||||
init:
|
||||
schema-locations: classpath:/sql/create_tables.sql
|
||||
|
||||
# Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优
|
||||
redis:
|
||||
host: 127.0.0.1 # 地址
|
||||
port: 16379 # 端口(单元测试,使用 16379 端口)
|
||||
database: 0 # 数据库索引
|
||||
|
||||
mybatis-plus:
|
||||
lazy-initialization: true # 单元测试,设置 MyBatis Mapper 延迟加载,加速每个单元测试
|
||||
type-aliases-package: ${yudao.info.base-package}.module.*.dal.dataobject
|
||||
|
||||
--- #################### 定时任务相关配置 ####################
|
||||
|
||||
--- #################### 配置中心相关配置 ####################
|
||||
|
||||
--- #################### 服务保障相关配置 ####################
|
||||
|
||||
# Lock4j 配置项(单元测试,禁用 Lock4j)
|
||||
|
||||
# Resilience4j 配置项
|
||||
|
||||
--- #################### 监控相关配置 ####################
|
||||
|
||||
--- #################### 芋道相关配置 ####################
|
||||
|
||||
# 芋道配置项,设置当前项目所有自定义的配置
|
||||
yudao:
|
||||
info:
|
||||
base-package: cn.iocoder.yudao
|
||||
@@ -0,0 +1,4 @@
|
||||
<configuration>
|
||||
<!-- 引用 Spring Boot 的 logback 基础配置 -->
|
||||
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
|
||||
</configuration>
|
||||
@@ -0,0 +1,11 @@
|
||||
DELETE FROM "infra_config";
|
||||
DELETE FROM "infra_file";
|
||||
DELETE FROM "infra_job";
|
||||
DELETE FROM "infra_job_log";
|
||||
DELETE FROM "infra_api_access_log";
|
||||
DELETE FROM "infra_api_error_log";
|
||||
DELETE FROM "infra_api_access_log";
|
||||
DELETE FROM "infra_file";
|
||||
DELETE FROM "infra_api_error_log";
|
||||
DELETE FROM "infra_test_demo";
|
||||
DELETE FROM "infra_file_config";
|
||||
@@ -0,0 +1,170 @@
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "infra_config" (
|
||||
"id" bigint(20) NOT NULL GENERATED BY DEFAULT AS IDENTITY,
|
||||
"group" varchar(50) NOT NULL,
|
||||
"type" tinyint NOT NULL,
|
||||
"name" varchar(100) NOT NULL DEFAULT '',
|
||||
"key" varchar(100) NOT NULL DEFAULT '',
|
||||
"value" varchar(500) NOT NULL DEFAULT '',
|
||||
"sensitive" bit NOT NULL,
|
||||
"remark" varchar(500) DEFAULT NULL,
|
||||
"creator" varchar(64) DEFAULT '',
|
||||
"create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updater" varchar(64) DEFAULT '',
|
||||
"update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"deleted" bit NOT NULL DEFAULT FALSE,
|
||||
PRIMARY KEY ("id")
|
||||
) COMMENT '参数配置表';
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "infra_file_config" (
|
||||
"id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
|
||||
"name" varchar(63) NOT NULL,
|
||||
"storage" tinyint NOT NULL,
|
||||
"remark" varchar(255),
|
||||
"master" bit(1) NOT NULL,
|
||||
"config" varchar(4096) NOT NULL,
|
||||
"creator" varchar(64) DEFAULT '',
|
||||
"create_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updater" varchar(64) DEFAULT '',
|
||||
"update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
"deleted" bit NOT NULL DEFAULT FALSE,
|
||||
PRIMARY KEY ("id")
|
||||
) COMMENT '文件配置表';
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "infra_file" (
|
||||
"id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
|
||||
"config_id" bigint NOT NULL,
|
||||
"path" varchar(512),
|
||||
"url" varchar(1024),
|
||||
"type" varchar(63) DEFAULT NULL,
|
||||
"size" bigint NOT NULL,
|
||||
"creator" varchar(64) DEFAULT '',
|
||||
"create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updater" varchar(64) DEFAULT '',
|
||||
"update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"deleted" bit NOT NULL DEFAULT FALSE,
|
||||
"tenant_id" bigint not null default '0',
|
||||
PRIMARY KEY ("id")
|
||||
) COMMENT '文件表';
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "infra_job" (
|
||||
"id" bigint(20) NOT NULL GENERATED BY DEFAULT AS IDENTITY COMMENT '任务编号',
|
||||
"name" varchar(32) NOT NULL COMMENT '任务名称',
|
||||
"status" tinyint(4) NOT NULL COMMENT '任务状态',
|
||||
"handler_name" varchar(64) NOT NULL COMMENT '处理器的名字',
|
||||
"handler_param" varchar(255) DEFAULT NULL COMMENT '处理器的参数',
|
||||
"cron_expression" varchar(32) NOT NULL COMMENT 'CRON 表达式',
|
||||
"retry_count" int(11) NOT NULL DEFAULT '0' COMMENT '重试次数',
|
||||
"retry_interval" int(11) NOT NULL DEFAULT '0' COMMENT '重试间隔',
|
||||
"monitor_timeout" int(11) NOT NULL DEFAULT '0' COMMENT '监控超时时间',
|
||||
"creator" varchar(64) DEFAULT '' COMMENT '创建者',
|
||||
"create_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
||||
"updater" varchar(64) DEFAULT '' COMMENT '更新者',
|
||||
"update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
|
||||
"deleted" bit NOT NULL DEFAULT FALSE COMMENT '是否删除',
|
||||
PRIMARY KEY ("id")
|
||||
) COMMENT='定时任务表';
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "infra_job_log" (
|
||||
"id" bigint(20) NOT NULL GENERATED BY DEFAULT AS IDENTITY COMMENT '日志编号',
|
||||
"job_id" bigint(20) NOT NULL COMMENT '任务编号',
|
||||
"handler_name" varchar(64) NOT NULL COMMENT '处理器的名字',
|
||||
"handler_param" varchar(255) DEFAULT NULL COMMENT '处理器的参数',
|
||||
"execute_index" tinyint(4) NOT NULL DEFAULT '1' COMMENT '第几次执行',
|
||||
"begin_time" datetime NOT NULL COMMENT '开始执行时间',
|
||||
"end_time" datetime DEFAULT NULL COMMENT '结束执行时间',
|
||||
"duration" int(11) DEFAULT NULL COMMENT '执行时长',
|
||||
"status" tinyint(4) NOT NULL COMMENT '任务状态',
|
||||
"result" varchar(4000) DEFAULT '' COMMENT '结果数据',
|
||||
"creator" varchar(64) DEFAULT '' COMMENT '创建者',
|
||||
"create_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
||||
"updater" varchar(64) DEFAULT '' COMMENT '更新者',
|
||||
"update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
|
||||
"deleted" bit(1) NOT NULL DEFAULT FALSE COMMENT '是否删除',
|
||||
PRIMARY KEY ("id")
|
||||
)COMMENT='定时任务日志表';
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "inf_file" (
|
||||
"id" varchar(188) NOT NULL,
|
||||
"type" varchar(63) DEFAULT NULL,
|
||||
"content" blob NOT NULL,
|
||||
"creator" varchar(64) DEFAULT '',
|
||||
"create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updater" varchar(64) DEFAULT '',
|
||||
"update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"deleted" bit NOT NULL DEFAULT FALSE,
|
||||
"tenant_id" bigint not null default '0',
|
||||
PRIMARY KEY ("id")
|
||||
) COMMENT '文件表';
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "infra_api_access_log" (
|
||||
"id" bigint not null GENERATED BY DEFAULT AS IDENTITY,
|
||||
"trace_id" varchar(64) not null default '',
|
||||
"user_id" bigint not null default '0',
|
||||
"user_type" tinyint not null default '0',
|
||||
"application_name" varchar(50) not null,
|
||||
"request_method" varchar(16) not null default '',
|
||||
"request_url" varchar(255) not null default '',
|
||||
"request_params" varchar(8000) not null default '',
|
||||
"user_ip" varchar(50) not null,
|
||||
"user_agent" varchar(512) not null,
|
||||
"begin_time" timestamp not null,
|
||||
"end_time" timestamp not null,
|
||||
"duration" integer not null,
|
||||
"result_code" integer not null default '0',
|
||||
"result_msg" varchar(512) default '',
|
||||
"creator" varchar(64) default '',
|
||||
"create_time" timestamp not null default current_timestamp,
|
||||
"updater" varchar(64) default '',
|
||||
"update_time" timestamp not null default current_timestamp,
|
||||
"deleted" bit not null default false,
|
||||
"tenant_id" bigint not null default '0',
|
||||
primary key ("id")
|
||||
) COMMENT 'API 访问日志表';
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "infra_api_error_log" (
|
||||
"id" bigint not null GENERATED BY DEFAULT AS IDENTITY,
|
||||
"trace_id" varchar(64) not null,
|
||||
"user_id" bigint not null default '0',
|
||||
"user_type" tinyint not null default '0',
|
||||
"application_name" varchar(50) not null,
|
||||
"request_method" varchar(16) not null,
|
||||
"request_url" varchar(255) not null,
|
||||
"request_params" varchar(8000) not null,
|
||||
"user_ip" varchar(50) not null,
|
||||
"user_agent" varchar(512) not null,
|
||||
"exception_time" timestamp not null,
|
||||
"exception_name" varchar(128) not null default '',
|
||||
"exception_message" clob not null,
|
||||
"exception_root_cause_message" clob not null,
|
||||
"exception_stack_trace" clob not null,
|
||||
"exception_class_name" varchar(512) not null,
|
||||
"exception_file_name" varchar(512) not null,
|
||||
"exception_method_name" varchar(512) not null,
|
||||
"exception_line_number" integer not null,
|
||||
"process_status" tinyint not null,
|
||||
"process_time" timestamp default null,
|
||||
"process_user_id" bigint default '0',
|
||||
"creator" varchar(64) default '',
|
||||
"create_time" timestamp not null default current_timestamp,
|
||||
"updater" varchar(64) default '',
|
||||
"update_time" timestamp not null default current_timestamp,
|
||||
"deleted" bit not null default false,
|
||||
"tenant_id" bigint not null default '0',
|
||||
primary key ("id")
|
||||
) COMMENT '系统异常日志';
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "infra_test_demo" (
|
||||
"id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
|
||||
"name" varchar(100) NOT NULL,
|
||||
"status" tinyint NOT NULL,
|
||||
"type" tinyint NOT NULL,
|
||||
"category" tinyint NOT NULL,
|
||||
"remark" varchar(500),
|
||||
"creator" varchar(64) DEFAULT '''',
|
||||
"create_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updater" varchar(64) DEFAULT '''',
|
||||
"update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
"deleted" bit NOT NULL DEFAULT FALSE,
|
||||
PRIMARY KEY ("id")
|
||||
) COMMENT '字典类型表';
|
||||
Reference in New Issue
Block a user