Commit 0a358d08630e85a7a7c216554dbd33ec47178fa7

Authored by 芋道源码
Committed by Gitee
2 parents 045c60fc ba4e90b5

!393 完善 infra 模块的 Service 逻辑的单元测试,单测数量 81,方法行覆盖率 63%,行覆盖率 47%

Merge pull request !393 from 芋道源码/feature/dev-yunai
Showing 29 changed files with 665 additions and 480 deletions
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/config/ConfigController.java
1 1 package cn.iocoder.yudao.module.infra.controller.admin.config;
2 2  
3   -import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
4 3 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
5 4 import cn.iocoder.yudao.framework.common.pojo.PageResult;
6 5 import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
... ... @@ -8,8 +7,8 @@ import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
8 7 import cn.iocoder.yudao.module.infra.controller.admin.config.vo.*;
9 8 import cn.iocoder.yudao.module.infra.convert.config.ConfigConvert;
10 9 import cn.iocoder.yudao.module.infra.dal.dataobject.config.ConfigDO;
11   -import cn.iocoder.yudao.module.infra.service.config.ConfigService;
12 10 import cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants;
  11 +import cn.iocoder.yudao.module.infra.service.config.ConfigService;
13 12 import io.swagger.annotations.Api;
14 13 import io.swagger.annotations.ApiImplicitParam;
15 14 import io.swagger.annotations.ApiOperation;
... ... @@ -23,6 +22,7 @@ import javax.validation.Valid;
23 22 import java.io.IOException;
24 23 import java.util.List;
25 24  
  25 +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
26 26 import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
27 27 import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
28 28  
... ... @@ -76,7 +76,7 @@ public class ConfigController {
76 76 return null;
77 77 }
78 78 if (!config.getVisible()) {
79   - throw ServiceExceptionUtil.exception(ErrorCodeConstants.CONFIG_GET_VALUE_ERROR_IF_VISIBLE);
  79 + throw exception(ErrorCodeConstants.CONFIG_GET_VALUE_ERROR_IF_VISIBLE);
80 80 }
81 81 return success(config.getValue());
82 82 }
... ... @@ -93,8 +93,8 @@ public class ConfigController {
93 93 @ApiOperation("导出参数配置")
94 94 @PreAuthorize("@ss.hasPermission('infra:config:export')")
95 95 @OperateLog(type = EXPORT)
96   - public void exportSysConfig(@Valid ConfigExportReqVO reqVO,
97   - HttpServletResponse response) throws IOException {
  96 + public void exportConfig(@Valid ConfigExportReqVO reqVO,
  97 + HttpServletResponse response) throws IOException {
98 98 List<ConfigDO> list = configService.getConfigList(reqVO);
99 99 // 拼接数据
100 100 List<ConfigExcelVO> datas = ConfigConvert.INSTANCE.convertList(list);
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/codegen/CodegenColumnDO.java
... ... @@ -39,27 +39,38 @@ public class CodegenColumnDO extends BaseDO {
39 39  
40 40 /**
41 41 * 字段名
  42 + *
  43 + * 关联 {@link TableField#getName()}
42 44 */
43 45 private String columnName;
44 46 /**
45 47 * 数据库字段类型
  48 + *
46 49 * 关联 {@link TableField.MetaInfo#getJdbcType()}
47 50 */
48 51 private String dataType;
49 52 /**
50 53 * 字段描述
  54 + *
  55 + * 关联 {@link TableField#getComment()}
51 56 */
52 57 private String columnComment;
53 58 /**
54 59 * 是否允许为空
  60 + *
  61 + * 关联 {@link TableField.MetaInfo#isNullable()}
55 62 */
56 63 private Boolean nullable;
57 64 /**
58 65 * 是否主键
  66 + *
  67 + * 关联 {@link TableField#isKeyFlag()}
59 68 */
60 69 private Boolean primaryKey;
61 70 /**
62 71 * 是否自增
  72 + *
  73 + * 关联 {@link TableField#isKeyIdentityFlag()}
63 74 */
64 75 private Boolean autoIncrement;
65 76 /**
... ... @@ -71,12 +82,16 @@ public class CodegenColumnDO extends BaseDO {
71 82  
72 83 /**
73 84 * Java 属性类型
74   - * <p>
  85 + *
75 86 * 例如说 String、Boolean 等等
  87 + *
  88 + * 关联 {@link TableField#getColumnType()}
76 89 */
77 90 private String javaType;
78 91 /**
79 92 * Java 属性名
  93 + *
  94 + * 关联 {@link TableField#getPropertyName()}
80 95 */
81 96 private String javaField;
82 97 /**
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/codegen/CodegenTableDO.java
... ... @@ -6,6 +6,7 @@ import cn.iocoder.yudao.module.infra.enums.codegen.CodegenSceneEnum;
6 6 import cn.iocoder.yudao.module.infra.enums.codegen.CodegenTemplateTypeEnum;
7 7 import com.baomidou.mybatisplus.annotation.KeySequence;
8 8 import com.baomidou.mybatisplus.annotation.TableName;
  9 +import com.baomidou.mybatisplus.generator.config.po.TableInfo;
9 10 import lombok.Data;
10 11 import lombok.EqualsAndHashCode;
11 12 import lombok.experimental.Accessors;
... ... @@ -44,10 +45,14 @@ public class CodegenTableDO extends BaseDO {
44 45  
45 46 /**
46 47 * 表名称
  48 + *
  49 + * 关联 {@link TableInfo#getName()}
47 50 */
48 51 private String tableName;
49 52 /**
50 53 * 表描述
  54 + *
  55 + * 关联 {@link TableInfo#getComment()}
51 56 */
52 57 private String tableComment;
53 58 /**
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/job/JobLogMapper.java
... ... @@ -2,7 +2,7 @@ package cn.iocoder.yudao.module.infra.dal.mysql.job;
2 2  
3 3 import cn.iocoder.yudao.framework.common.pojo.PageResult;
4 4 import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
5   -import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX;
  5 +import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
6 6 import cn.iocoder.yudao.module.infra.controller.admin.job.vo.log.JobLogExportReqVO;
7 7 import cn.iocoder.yudao.module.infra.controller.admin.job.vo.log.JobLogPageReqVO;
8 8 import cn.iocoder.yudao.module.infra.dal.dataobject.job.JobLogDO;
... ... @@ -19,24 +19,24 @@ import java.util.List;
19 19 public interface JobLogMapper extends BaseMapperX<JobLogDO> {
20 20  
21 21 default PageResult<JobLogDO> selectPage(JobLogPageReqVO reqVO) {
22   - return selectPage(reqVO, new QueryWrapperX<JobLogDO>()
23   - .eqIfPresent("job_id", reqVO.getJobId())
24   - .likeIfPresent("handler_name", reqVO.getHandlerName())
25   - .geIfPresent("begin_time", reqVO.getBeginTime())
26   - .leIfPresent("end_time", reqVO.getEndTime())
27   - .eqIfPresent("status", reqVO.getStatus())
28   - .orderByDesc("id") // ID 倒序
  22 + return selectPage(reqVO, new LambdaQueryWrapperX<JobLogDO>()
  23 + .eqIfPresent(JobLogDO::getJobId, reqVO.getJobId())
  24 + .likeIfPresent(JobLogDO::getHandlerName, reqVO.getHandlerName())
  25 + .geIfPresent(JobLogDO::getBeginTime, reqVO.getBeginTime())
  26 + .leIfPresent(JobLogDO::getEndTime, reqVO.getEndTime())
  27 + .eqIfPresent(JobLogDO::getStatus, reqVO.getStatus())
  28 + .orderByDesc(JobLogDO::getId) // ID 倒序
29 29 );
30 30 }
31 31  
32 32 default List<JobLogDO> selectList(JobLogExportReqVO reqVO) {
33   - return selectList(new QueryWrapperX<JobLogDO>()
34   - .eqIfPresent("job_id", reqVO.getJobId())
35   - .likeIfPresent("handler_name", reqVO.getHandlerName())
36   - .geIfPresent("begin_time", reqVO.getBeginTime())
37   - .leIfPresent("end_time", reqVO.getEndTime())
38   - .eqIfPresent("status", reqVO.getStatus())
39   - .orderByDesc("id") // ID 倒序
  33 + return selectList(new LambdaQueryWrapperX<JobLogDO>()
  34 + .eqIfPresent(JobLogDO::getJobId, reqVO.getJobId())
  35 + .likeIfPresent(JobLogDO::getHandlerName, reqVO.getHandlerName())
  36 + .geIfPresent(JobLogDO::getBeginTime, reqVO.getBeginTime())
  37 + .leIfPresent(JobLogDO::getEndTime, reqVO.getEndTime())
  38 + .eqIfPresent(JobLogDO::getStatus, reqVO.getStatus())
  39 + .orderByDesc(JobLogDO::getId) // ID 倒序
40 40 );
41 41 }
42 42  
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/CodegenServiceImpl.java
... ... @@ -76,7 +76,7 @@ public class CodegenServiceImpl implements CodegenService {
76 76  
77 77 private Long createCodegen0(Long userId, Long dataSourceConfigId, TableInfo tableInfo) {
78 78 // 校验导入的表和字段非空
79   - checkTableInfo(tableInfo);
  79 + validateTableInfo(tableInfo);
80 80 // 校验是否已经存在
81 81 if (codegenTableMapper.selectByTableNameAndDataSourceConfigId(tableInfo.getName(),
82 82 dataSourceConfigId) != null) {
... ... @@ -100,7 +100,7 @@ public class CodegenServiceImpl implements CodegenService {
100 100 return table.getId();
101 101 }
102 102  
103   - private void checkTableInfo(TableInfo tableInfo) {
  103 + private void validateTableInfo(TableInfo tableInfo) {
104 104 if (tableInfo == null) {
105 105 throw exception(CODEGEN_IMPORT_TABLE_NULL);
106 106 }
... ... @@ -149,7 +149,7 @@ public class CodegenServiceImpl implements CodegenService {
149 149  
150 150 private void syncCodegen0(Long tableId, TableInfo tableInfo) {
151 151 // 校验导入的表和字段非空
152   - checkTableInfo(tableInfo);
  152 + validateTableInfo(tableInfo);
153 153 List<TableField> tableFields = tableInfo.getFields();
154 154  
155 155 // 构建 CodegenColumnDO 数组,只同步新增的字段
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/config/ConfigServiceImpl.java
... ... @@ -9,7 +9,6 @@ import cn.iocoder.yudao.module.infra.controller.admin.config.vo.ConfigUpdateReqV
9 9 import cn.iocoder.yudao.module.infra.convert.config.ConfigConvert;
10 10 import cn.iocoder.yudao.module.infra.dal.dataobject.config.ConfigDO;
11 11 import cn.iocoder.yudao.module.infra.dal.mysql.config.ConfigMapper;
12   -import cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants;
13 12 import cn.iocoder.yudao.module.infra.enums.config.ConfigTypeEnum;
14 13 import com.google.common.annotations.VisibleForTesting;
15 14 import lombok.extern.slf4j.Slf4j;
... ... @@ -20,6 +19,7 @@ import javax.annotation.Resource;
20 19 import java.util.List;
21 20  
22 21 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
  22 +import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.*;
23 23  
24 24 /**
25 25 * 参数配置 Service 实现类
... ... @@ -35,7 +35,7 @@ public class ConfigServiceImpl implements ConfigService {
35 35 @Override
36 36 public Long createConfig(ConfigCreateReqVO reqVO) {
37 37 // 校验正确性
38   - checkCreateOrUpdate(null, reqVO.getKey());
  38 + validateConfigForCreateOrUpdate(null, reqVO.getKey());
39 39 // 插入参数配置
40 40 ConfigDO config = ConfigConvert.INSTANCE.convert(reqVO);
41 41 config.setType(ConfigTypeEnum.CUSTOM.getType());
... ... @@ -46,19 +46,19 @@ public class ConfigServiceImpl implements ConfigService {
46 46 @Override
47 47 public void updateConfig(ConfigUpdateReqVO reqVO) {
48 48 // 校验正确性
49   - checkCreateOrUpdate(reqVO.getId(), null); // 不允许更新 key
  49 + validateConfigForCreateOrUpdate(reqVO.getId(), null); // 不允许更新 key
50 50 // 更新参数配置
51 51 ConfigDO updateObj = ConfigConvert.INSTANCE.convert(reqVO);
52   - configMapper.updateById(updateObj);;
  52 + configMapper.updateById(updateObj);
53 53 }
54 54  
55 55 @Override
56 56 public void deleteConfig(Long id) {
57 57 // 校验配置存在
58   - ConfigDO config = checkConfigExists(id);
  58 + ConfigDO config = validateConfigExists(id);
59 59 // 内置配置,不允许删除
60 60 if (ConfigTypeEnum.SYSTEM.getType().equals(config.getType())) {
61   - throw exception(ErrorCodeConstants.CONFIG_CAN_NOT_DELETE_SYSTEM_TYPE);
  61 + throw exception(CONFIG_CAN_NOT_DELETE_SYSTEM_TYPE);
62 62 }
63 63 // 删除
64 64 configMapper.deleteById(id);
... ... @@ -84,39 +84,39 @@ public class ConfigServiceImpl implements ConfigService {
84 84 return configMapper.selectList(reqVO);
85 85 }
86 86  
87   - private void checkCreateOrUpdate(Long id, String key) {
  87 + private void validateConfigForCreateOrUpdate(Long id, String key) {
88 88 // 校验自己存在
89   - checkConfigExists(id);
  89 + validateConfigExists(id);
90 90 // 校验参数配置 key 的唯一性
91 91 if (StrUtil.isNotEmpty(key)) {
92   - checkConfigKeyUnique(id, key);
  92 + validateConfigKeyUnique(id, key);
93 93 }
94 94 }
95 95  
96 96 @VisibleForTesting
97   - public ConfigDO checkConfigExists(Long id) {
  97 + public ConfigDO validateConfigExists(Long id) {
98 98 if (id == null) {
99 99 return null;
100 100 }
101 101 ConfigDO config = configMapper.selectById(id);
102 102 if (config == null) {
103   - throw exception(ErrorCodeConstants.CONFIG_NOT_EXISTS);
  103 + throw exception(CONFIG_NOT_EXISTS);
104 104 }
105 105 return config;
106 106 }
107 107  
108 108 @VisibleForTesting
109   - public void checkConfigKeyUnique(Long id, String key) {
  109 + public void validateConfigKeyUnique(Long id, String key) {
110 110 ConfigDO config = configMapper.selectByKey(key);
111 111 if (config == null) {
112 112 return;
113 113 }
114 114 // 如果 id 为空,说明不用比较是否为相同 id 的参数配置
115 115 if (id == null) {
116   - throw exception(ErrorCodeConstants.CONFIG_KEY_DUPLICATE);
  116 + throw exception(CONFIG_KEY_DUPLICATE);
117 117 }
118 118 if (!config.getId().equals(id)) {
119   - throw exception(ErrorCodeConstants.CONFIG_KEY_DUPLICATE);
  119 + throw exception(CONFIG_KEY_DUPLICATE);
120 120 }
121 121 }
122 122  
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/db/DataSourceConfigServiceImpl.java
... ... @@ -37,7 +37,7 @@ public class DataSourceConfigServiceImpl implements DataSourceConfigService {
37 37 @Override
38 38 public Long createDataSourceConfig(DataSourceConfigCreateReqVO createReqVO) {
39 39 DataSourceConfigDO dataSourceConfig = DataSourceConfigConvert.INSTANCE.convert(createReqVO);
40   - checkConnectionOK(dataSourceConfig);
  40 + validateConnectionOK(dataSourceConfig);
41 41  
42 42 // 插入
43 43 dataSourceConfigMapper.insert(dataSourceConfig);
... ... @@ -50,7 +50,7 @@ public class DataSourceConfigServiceImpl implements DataSourceConfigService {
50 50 // 校验存在
51 51 validateDataSourceConfigExists(updateReqVO.getId());
52 52 DataSourceConfigDO updateObj = DataSourceConfigConvert.INSTANCE.convert(updateReqVO);
53   - checkConnectionOK(updateObj);
  53 + validateConnectionOK(updateObj);
54 54  
55 55 // 更新
56 56 dataSourceConfigMapper.updateById(updateObj);
... ... @@ -88,7 +88,7 @@ public class DataSourceConfigServiceImpl implements DataSourceConfigService {
88 88 return result;
89 89 }
90 90  
91   - private void checkConnectionOK(DataSourceConfigDO config) {
  91 + private void validateConnectionOK(DataSourceConfigDO config) {
92 92 boolean success = JdbcUtils.isConnectionOK(config.getUrl(), config.getUsername(), config.getPassword());
93 93 if (!success) {
94 94 throw exception(DATA_SOURCE_CONFIG_NOT_OK);
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/db/DatabaseTableServiceImpl.java
... ... @@ -41,7 +41,7 @@ public class DatabaseTableServiceImpl implements DatabaseTableService {
41 41 return CollUtil.getFirst(getTableList0(dataSourceConfigId, name));
42 42 }
43 43  
44   - public List<TableInfo> getTableList0(Long dataSourceConfigId, String name) {
  44 + private List<TableInfo> getTableList0(Long dataSourceConfigId, String name) {
45 45 // 获得数据源配置
46 46 DataSourceConfigDO config = dataSourceConfigService.getDataSourceConfig(dataSourceConfigId);
47 47 Assert.notNull(config, "数据源({}) 不存在!", dataSourceConfigId);
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileConfigService.java
... ... @@ -8,8 +8,6 @@ import cn.iocoder.yudao.module.infra.controller.admin.file.vo.config.FileConfigU
8 8 import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileConfigDO;
9 9  
10 10 import javax.validation.Valid;
11   -import java.util.Collection;
12   -import java.util.List;
13 11  
14 12 /**
15 13 * 文件配置 Service 接口
... ... @@ -61,14 +59,6 @@ public interface FileConfigService {
61 59 FileConfigDO getFileConfig(Long id);
62 60  
63 61 /**
64   - * 获得文件配置列表
65   - *
66   - * @param ids 编号
67   - * @return 文件配置列表
68   - */
69   - List<FileConfigDO> getFileConfigList(Collection<Long> ids);
70   -
71   - /**
72 62 * 获得文件配置分页
73 63 *
74 64 * @param pageReqVO 分页查询
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileConfigServiceImpl.java
... ... @@ -27,7 +27,6 @@ import org.springframework.validation.annotation.Validated;
27 27 import javax.annotation.PostConstruct;
28 28 import javax.annotation.Resource;
29 29 import javax.validation.Validator;
30   -import java.util.Collection;
31 30 import java.util.List;
32 31 import java.util.Map;
33 32  
... ... @@ -95,7 +94,7 @@ public class FileConfigServiceImpl implements FileConfigService {
95 94 @Override
96 95 public void updateFileConfig(FileConfigUpdateReqVO updateReqVO) {
97 96 // 校验存在
98   - FileConfigDO config = this.validateFileConfigExists(updateReqVO.getId());
  97 + FileConfigDO config = validateFileConfigExists(updateReqVO.getId());
99 98 // 更新
100 99 FileConfigDO updateObj = FileConfigConvert.INSTANCE.convert(updateReqVO)
101 100 .setConfig(parseClientConfig(config.getStorage(), updateReqVO.getConfig()));
... ... @@ -108,7 +107,7 @@ public class FileConfigServiceImpl implements FileConfigService {
108 107 @Transactional(rollbackFor = Exception.class)
109 108 public void updateFileConfigMaster(Long id) {
110 109 // 校验存在
111   - this.validateFileConfigExists(id);
  110 + validateFileConfigExists(id);
112 111 // 更新其它为非 master
113 112 fileConfigMapper.updateBatch(new FileConfigDO().setMaster(false));
114 113 // 更新
... ... @@ -138,7 +137,7 @@ public class FileConfigServiceImpl implements FileConfigService {
138 137 @Override
139 138 public void deleteFileConfig(Long id) {
140 139 // 校验存在
141   - FileConfigDO config = this.validateFileConfigExists(id);
  140 + FileConfigDO config = validateFileConfigExists(id);
142 141 if (Boolean.TRUE.equals(config.getMaster())) {
143 142 throw exception(FILE_CONFIG_DELETE_FAIL_MASTER);
144 143 }
... ... @@ -162,11 +161,6 @@ public class FileConfigServiceImpl implements FileConfigService {
162 161 }
163 162  
164 163 @Override
165   - public List<FileConfigDO> getFileConfigList(Collection<Long> ids) {
166   - return fileConfigMapper.selectBatchIds(ids);
167   - }
168   -
169   - @Override
170 164 public PageResult<FileConfigDO> getFileConfigPage(FileConfigPageReqVO pageReqVO) {
171 165 return fileConfigMapper.selectPage(pageReqVO);
172 166 }
... ... @@ -174,7 +168,7 @@ public class FileConfigServiceImpl implements FileConfigService {
174 168 @Override
175 169 public String testFileConfig(Long id) throws Exception {
176 170 // 校验存在
177   - this.validateFileConfigExists(id);
  171 + validateFileConfigExists(id);
178 172 // 上传文件
179 173 byte[] content = ResourceUtil.readBytes("file/erweima.jpg");
180 174 return fileClientFactory.getFileClient(id).upload(content, IdUtil.fastSimpleUUID() + ".jpg", "image/jpeg");
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java
... ... @@ -69,7 +69,7 @@ public class FileServiceImpl implements FileService {
69 69 @Override
70 70 public void deleteFile(Long id) throws Exception {
71 71 // 校验存在
72   - FileDO file = this.validateFileExists(id);
  72 + FileDO file = validateFileExists(id);
73 73  
74 74 // 从文件存储器中删除
75 75 FileClient client = fileConfigService.getFileClient(file.getConfigId());
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/job/JobServiceImpl.java
... ... @@ -69,7 +69,7 @@ public class JobServiceImpl implements JobService {
69 69 public void updateJob(JobUpdateReqVO updateReqVO) throws SchedulerException {
70 70 validateCronExpression(updateReqVO.getCronExpression());
71 71 // 校验存在
72   - JobDO job = this.validateJobExists(updateReqVO.getId());
  72 + JobDO job = validateJobExists(updateReqVO.getId());
73 73 // 只有开启状态,才可以修改.原因是,如果出暂停状态,修改 Quartz Job 时,会导致任务又开始执行
74 74 if (!job.getStatus().equals(JobStatusEnum.NORMAL.getStatus())) {
75 75 throw exception(JOB_UPDATE_ONLY_NORMAL_STATUS);
... ... @@ -92,7 +92,7 @@ public class JobServiceImpl implements JobService {
92 92 throw exception(JOB_CHANGE_STATUS_INVALID);
93 93 }
94 94 // 校验存在
95   - JobDO job = this.validateJobExists(id);
  95 + JobDO job = validateJobExists(id);
96 96 // 校验是否已经为当前状态
97 97 if (job.getStatus().equals(status)) {
98 98 throw exception(JOB_CHANGE_STATUS_EQUALS);
... ... @@ -112,7 +112,7 @@ public class JobServiceImpl implements JobService {
112 112 @Override
113 113 public void triggerJob(Long id) throws SchedulerException {
114 114 // 校验存在
115   - JobDO job = this.validateJobExists(id);
  115 + JobDO job = validateJobExists(id);
116 116  
117 117 // 触发 Quartz 中的 Job
118 118 schedulerManager.triggerJob(job.getId(), job.getHandlerName(), job.getHandlerParam());
... ... @@ -122,7 +122,7 @@ public class JobServiceImpl implements JobService {
122 122 @Transactional(rollbackFor = Exception.class)
123 123 public void deleteJob(Long id) throws SchedulerException {
124 124 // 校验存在
125   - JobDO job = this.validateJobExists(id);
  125 + JobDO job = validateJobExists(id);
126 126 // 更新
127 127 jobMapper.deleteById(id);
128 128  
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/logger/ApiErrorLogServiceImpl.java
1 1 package cn.iocoder.yudao.module.infra.service.logger;
2 2  
3   -import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
4 3 import cn.iocoder.yudao.framework.common.pojo.PageResult;
5 4 import cn.iocoder.yudao.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO;
6 5 import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiErrorLogExportReqVO;
... ... @@ -8,7 +7,6 @@ import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiE
8 7 import cn.iocoder.yudao.module.infra.convert.logger.ApiErrorLogConvert;
9 8 import cn.iocoder.yudao.module.infra.dal.dataobject.logger.ApiErrorLogDO;
10 9 import cn.iocoder.yudao.module.infra.dal.mysql.logger.ApiErrorLogMapper;
11   -import cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants;
12 10 import cn.iocoder.yudao.module.infra.enums.logger.ApiErrorLogProcessStatusEnum;
13 11 import org.springframework.stereotype.Service;
14 12 import org.springframework.validation.annotation.Validated;
... ... @@ -17,6 +15,10 @@ import javax.annotation.Resource;
17 15 import java.time.LocalDateTime;
18 16 import java.util.List;
19 17  
  18 +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
  19 +import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.API_ERROR_LOG_NOT_FOUND;
  20 +import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.API_ERROR_LOG_PROCESSED;
  21 +
20 22 /**
21 23 * API 错误日志 Service 实现类
22 24 *
... ... @@ -31,8 +33,8 @@ public class ApiErrorLogServiceImpl implements ApiErrorLogService {
31 33  
32 34 @Override
33 35 public void createApiErrorLog(ApiErrorLogCreateReqDTO createDTO) {
34   - ApiErrorLogDO apiErrorLog = ApiErrorLogConvert.INSTANCE.convert(createDTO);
35   - apiErrorLog.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
  36 + ApiErrorLogDO apiErrorLog = ApiErrorLogConvert.INSTANCE.convert(createDTO)
  37 + .setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
36 38 apiErrorLogMapper.insert(apiErrorLog);
37 39 }
38 40  
... ... @@ -50,10 +52,10 @@ public class ApiErrorLogServiceImpl implements ApiErrorLogService {
50 52 public void updateApiErrorLogProcess(Long id, Integer processStatus, Long processUserId) {
51 53 ApiErrorLogDO errorLog = apiErrorLogMapper.selectById(id);
52 54 if (errorLog == null) {
53   - throw ServiceExceptionUtil.exception(ErrorCodeConstants.API_ERROR_LOG_NOT_FOUND);
  55 + throw exception(API_ERROR_LOG_NOT_FOUND);
54 56 }
55 57 if (!ApiErrorLogProcessStatusEnum.INIT.getStatus().equals(errorLog.getProcessStatus())) {
56   - throw ServiceExceptionUtil.exception(ErrorCodeConstants.API_ERROR_LOG_PROCESSED);
  58 + throw exception(API_ERROR_LOG_PROCESSED);
57 59 }
58 60 // 标记处理
59 61 apiErrorLogMapper.updateById(ApiErrorLogDO.builder().id(id).processStatus(processStatus)
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/test/TestDemoServiceImpl.java
... ... @@ -45,7 +45,7 @@ public class TestDemoServiceImpl implements TestDemoService {
45 45 @CacheEvict(value = "test", key = "#updateReqVO.id")
46 46 public void updateTestDemo(TestDemoUpdateReqVO updateReqVO) {
47 47 // 校验存在
48   - this.validateTestDemoExists(updateReqVO.getId());
  48 + validateTestDemoExists(updateReqVO.getId());
49 49 // 更新
50 50 TestDemoDO updateObj = TestDemoConvert.INSTANCE.convert(updateReqVO);
51 51 testDemoMapper.updateById(updateObj);
... ... @@ -55,7 +55,7 @@ public class TestDemoServiceImpl implements TestDemoService {
55 55 @CacheEvict(value = "test", key = "#id")
56 56 public void deleteTestDemo(Long id) {
57 57 // 校验存在
58   - this.validateTestDemoExists(id);
  58 + validateTestDemoExists(id);
59 59 // 删除
60 60 testDemoMapper.deleteById(id);
61 61 }
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/java/service/serviceImpl.vm
... ... @@ -39,7 +39,7 @@ public class ${table.className}ServiceImpl implements ${table.className}Service
39 39 @Override
40 40 public void update${simpleClassName}(${sceneEnum.prefixClass}${table.className}UpdateReqVO updateReqVO) {
41 41 // 校验存在
42   - this.validate${simpleClassName}Exists(updateReqVO.getId());
  42 + validate${simpleClassName}Exists(updateReqVO.getId());
43 43 // 更新
44 44 ${table.className}DO updateObj = ${table.className}Convert.INSTANCE.convert(updateReqVO);
45 45 ${classNameVar}Mapper.updateById(updateObj);
... ... @@ -48,7 +48,7 @@ public class ${table.className}ServiceImpl implements ${table.className}Service
48 48 @Override
49 49 public void delete${simpleClassName}(${primaryColumn.javaType} id) {
50 50 // 校验存在
51   - this.validate${simpleClassName}Exists(id);
  51 + validate${simpleClassName}Exists(id);
52 52 // 删除
53 53 ${classNameVar}Mapper.deleteById(id);
54 54 }
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/java/test/serviceTest.vm
... ... @@ -52,7 +52,7 @@ import static org.mockito.Mockito.*;
52 52 #if (${column.listOperation})
53 53 #set ($JavaField = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})##首字母大写
54 54 #if (${column.listOperationCondition} == "BETWEEN")## BETWEEN 的情况
55   - reqVO.set${JavaField}((new LocalDateTime[]{}));
  55 + reqVO.set${JavaField}(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
56 56 #else
57 57 reqVO.set$JavaField(null);
58 58 #end
... ...
yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/config/ConfigServiceTest.java renamed to yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/config/ConfigServiceImplTest.java
... ... @@ -2,7 +2,6 @@ package cn.iocoder.yudao.module.infra.service.config;
2 2  
3 3 import cn.iocoder.yudao.framework.common.pojo.PageResult;
4 4 import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils;
5   -import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
6 5 import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
7 6 import cn.iocoder.yudao.framework.test.core.util.RandomUtils;
8 7 import cn.iocoder.yudao.module.infra.controller.admin.config.vo.ConfigCreateReqVO;
... ... @@ -12,17 +11,17 @@ import cn.iocoder.yudao.module.infra.controller.admin.config.vo.ConfigUpdateReqV
12 11 import cn.iocoder.yudao.module.infra.dal.dataobject.config.ConfigDO;
13 12 import cn.iocoder.yudao.module.infra.dal.mysql.config.ConfigMapper;
14 13 import cn.iocoder.yudao.module.infra.enums.config.ConfigTypeEnum;
15   -import org.junit.jupiter.api.Assertions;
16 14 import org.junit.jupiter.api.Test;
17 15 import org.springframework.context.annotation.Import;
18 16  
19 17 import javax.annotation.Resource;
20   -import java.time.LocalDateTime;
21 18 import java.util.List;
22 19 import java.util.function.Consumer;
23 20  
24 21 import static cn.hutool.core.util.RandomUtil.randomEle;
  22 +import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
25 23 import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
  24 +import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
26 25 import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
27 26 import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
28 27 import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
... ... @@ -30,7 +29,7 @@ import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.*;
30 29 import static org.junit.jupiter.api.Assertions.*;
31 30  
32 31 @Import(ConfigServiceImpl.class)
33   -public class ConfigServiceTest extends BaseDbUnitTest {
  32 +public class ConfigServiceImplTest extends BaseDbUnitTest {
34 33  
35 34 @Resource
36 35 private ConfigServiceImpl configService;
... ... @@ -50,7 +49,7 @@ public class ConfigServiceTest extends BaseDbUnitTest {
50 49 // 校验记录的属性是否正确
51 50 ConfigDO config = configMapper.selectById(configId);
52 51 assertPojoEquals(reqVO, config);
53   - Assertions.assertEquals(ConfigTypeEnum.CUSTOM.getType(), config.getType());
  52 + assertEquals(ConfigTypeEnum.CUSTOM.getType(), config.getType());
54 53 }
55 54  
56 55 @Test
... ... @@ -101,40 +100,40 @@ public class ConfigServiceTest extends BaseDbUnitTest {
101 100 }
102 101  
103 102 @Test
104   - public void testCheckConfigExists_success() {
  103 + public void testValidateConfigExists_success() {
105 104 // mock 数据
106 105 ConfigDO dbConfigDO = randomConfigDO();
107 106 configMapper.insert(dbConfigDO);// @Sql: 先插入出一条存在的数据
108 107  
109 108 // 调用成功
110   - configService.checkConfigExists(dbConfigDO.getId());
  109 + configService.validateConfigExists(dbConfigDO.getId());
111 110 }
112 111  
113 112 @Test
114   - public void testCheckConfigExist_notExists() {
115   - assertServiceException(() -> configService.checkConfigExists(randomLongId()), CONFIG_NOT_EXISTS);
  113 + public void testValidateConfigExist_notExists() {
  114 + assertServiceException(() -> configService.validateConfigExists(randomLongId()), CONFIG_NOT_EXISTS);
116 115 }
117 116  
118 117 @Test
119   - public void testCheckConfigKeyUnique_success() {
  118 + public void testValidateConfigKeyUnique_success() {
120 119 // 调用,成功
121   - configService.checkConfigKeyUnique(randomLongId(), randomString());
  120 + configService.validateConfigKeyUnique(randomLongId(), randomString());
122 121 }
123 122  
124 123 @Test
125   - public void testCheckConfigKeyUnique_keyDuplicateForCreate() {
  124 + public void testValidateConfigKeyUnique_keyDuplicateForCreate() {
126 125 // 准备参数
127 126 String key = randomString();
128 127 // mock 数据
129 128 configMapper.insert(randomConfigDO(o -> o.setConfigKey(key)));
130 129  
131 130 // 调用,校验异常
132   - assertServiceException(() -> configService.checkConfigKeyUnique(null, key),
  131 + assertServiceException(() -> configService.validateConfigKeyUnique(null, key),
133 132 CONFIG_KEY_DUPLICATE);
134 133 }
135 134  
136 135 @Test
137   - public void testCheckConfigKeyUnique_keyDuplicateForUpdate() {
  136 + public void testValidateConfigKeyUnique_keyDuplicateForUpdate() {
138 137 // 准备参数
139 138 Long id = randomLongId();
140 139 String key = randomString();
... ... @@ -142,7 +141,7 @@ public class ConfigServiceTest extends BaseDbUnitTest {
142 141 configMapper.insert(randomConfigDO(o -> o.setConfigKey(key)));
143 142  
144 143 // 调用,校验异常
145   - assertServiceException(() -> configService.checkConfigKeyUnique(id, key),
  144 + assertServiceException(() -> configService.validateConfigKeyUnique(id, key),
146 145 CONFIG_KEY_DUPLICATE);
147 146 }
148 147  
... ... @@ -157,19 +156,19 @@ public class ConfigServiceTest extends BaseDbUnitTest {
157 156 });
158 157 configMapper.insert(dbConfig);
159 158 // 测试 name 不匹配
160   - configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setName("土豆")));
  159 + configMapper.insert(cloneIgnoreId(dbConfig, o -> o.setName("土豆")));
161 160 // 测试 key 不匹配
162   - configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setConfigKey("tudou")));
  161 + configMapper.insert(cloneIgnoreId(dbConfig, o -> o.setConfigKey("tudou")));
163 162 // 测试 type 不匹配
164   - configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setType(ConfigTypeEnum.CUSTOM.getType())));
  163 + configMapper.insert(cloneIgnoreId(dbConfig, o -> o.setType(ConfigTypeEnum.CUSTOM.getType())));
165 164 // 测试 createTime 不匹配
166   - configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setCreateTime(buildTime(2021, 1, 1))));
  165 + configMapper.insert(cloneIgnoreId(dbConfig, o -> o.setCreateTime(buildTime(2021, 1, 1))));
167 166 // 准备参数
168 167 ConfigPageReqVO reqVO = new ConfigPageReqVO();
169 168 reqVO.setName("艿");
170 169 reqVO.setKey("nai");
171 170 reqVO.setType(ConfigTypeEnum.SYSTEM.getType());
172   - reqVO.setCreateTime((new LocalDateTime[]{buildTime(2021, 1, 15),buildTime(2021, 2, 15)}));
  171 + reqVO.setCreateTime(buildBetweenTime(2021, 1, 15, 2021, 2, 15));
173 172  
174 173 // 调用
175 174 PageResult<ConfigDO> pageResult = configService.getConfigPage(reqVO);
... ... @@ -190,19 +189,19 @@ public class ConfigServiceTest extends BaseDbUnitTest {
190 189 });
191 190 configMapper.insert(dbConfig);
192 191 // 测试 name 不匹配
193   - configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setName("土豆")));
  192 + configMapper.insert(cloneIgnoreId(dbConfig, o -> o.setName("土豆")));
194 193 // 测试 key 不匹配
195   - configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setConfigKey("tudou")));
  194 + configMapper.insert(cloneIgnoreId(dbConfig, o -> o.setConfigKey("tudou")));
196 195 // 测试 type 不匹配
197   - configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setType(ConfigTypeEnum.CUSTOM.getType())));
  196 + configMapper.insert(cloneIgnoreId(dbConfig, o -> o.setType(ConfigTypeEnum.CUSTOM.getType())));
198 197 // 测试 createTime 不匹配
199   - configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setCreateTime(buildTime(2021, 1, 1))));
  198 + configMapper.insert(cloneIgnoreId(dbConfig, o -> o.setCreateTime(buildTime(2021, 1, 1))));
200 199 // 准备参数
201 200 ConfigExportReqVO reqVO = new ConfigExportReqVO();
202 201 reqVO.setName("艿");
203 202 reqVO.setKey("nai");
204 203 reqVO.setType(ConfigTypeEnum.SYSTEM.getType());
205   - reqVO.setCreateTime((new LocalDateTime[]{buildTime(2021, 1, 15),buildTime(2021, 2, 15)}));
  204 + reqVO.setCreateTime(buildBetweenTime(2021, 1, 15, 2021, 2, 15));
206 205  
207 206 // 调用
208 207 List<ConfigDO> list = configService.getConfigList(reqVO);
... ... @@ -212,6 +211,21 @@ public class ConfigServiceTest extends BaseDbUnitTest {
212 211 }
213 212  
214 213 @Test
  214 + public void testGetConfig() {
  215 + // mock 数据
  216 + ConfigDO dbConfig = randomConfigDO();
  217 + configMapper.insert(dbConfig);// @Sql: 先插入出一条存在的数据
  218 + // 准备参数
  219 + Long id = dbConfig.getId();
  220 +
  221 + // 调用
  222 + ConfigDO config = configService.getConfig(id);
  223 + // 断言
  224 + assertNotNull(config);
  225 + assertPojoEquals(dbConfig, config);
  226 + }
  227 +
  228 + @Test
215 229 public void testGetConfigByKey() {
216 230 // mock 数据
217 231 ConfigDO dbConfig = randomConfigDO();
... ...
yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/db/DataSourceConfigServiceImplTest.java
1 1 package cn.iocoder.yudao.module.infra.service.db;
2 2  
  3 +import cn.hutool.core.map.MapUtil;
3 4 import cn.hutool.core.util.ReflectUtil;
4 5 import cn.hutool.crypto.symmetric.AES;
5 6 import cn.iocoder.yudao.framework.mybatis.core.type.EncryptTypeHandler;
... ... @@ -9,6 +10,7 @@ import cn.iocoder.yudao.module.infra.controller.admin.db.vo.DataSourceConfigCrea
9 10 import cn.iocoder.yudao.module.infra.controller.admin.db.vo.DataSourceConfigUpdateReqVO;
10 11 import cn.iocoder.yudao.module.infra.dal.dataobject.db.DataSourceConfigDO;
11 12 import cn.iocoder.yudao.module.infra.dal.mysql.db.DataSourceConfigMapper;
  13 +import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
12 14 import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
13 15 import org.junit.jupiter.api.BeforeEach;
14 16 import org.junit.jupiter.api.Test;
... ... @@ -18,14 +20,14 @@ import org.springframework.boot.test.mock.mockito.MockBean;
18 20 import org.springframework.context.annotation.Import;
19 21  
20 22 import javax.annotation.Resource;
  23 +import java.util.List;
21 24  
22 25 import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
23 26 import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
24 27 import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
25 28 import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
26 29 import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.DATA_SOURCE_CONFIG_NOT_EXISTS;
27   -import static org.junit.jupiter.api.Assertions.assertNotNull;
28   -import static org.junit.jupiter.api.Assertions.assertNull;
  30 +import static org.junit.jupiter.api.Assertions.*;
29 31 import static org.mockito.ArgumentMatchers.anyString;
30 32 import static org.mockito.ArgumentMatchers.eq;
31 33 import static org.mockito.Mockito.mockStatic;
... ... @@ -57,6 +59,11 @@ public class DataSourceConfigServiceImplTest extends BaseDbUnitTest {
57 59 ReflectUtil.setFieldValue(EncryptTypeHandler.class, "aes", aes);
58 60 when(aes.encryptBase64(anyString())).then((Answer<String>) invocation -> invocation.getArgument(0));
59 61 when(aes.decryptStr(anyString())).then((Answer<String>) invocation -> invocation.getArgument(0));
  62 +
  63 + // mock DynamicDataSourceProperties
  64 + when(dynamicDataSourceProperties.getPrimary()).thenReturn("primary");
  65 + when(dynamicDataSourceProperties.getDatasource()).thenReturn(MapUtil.of("primary",
  66 + new DataSourceProperty().setUrl("http://localhost:3306").setUsername("yunai").setPassword("tudou")));
60 67 }
61 68  
62 69 @Test
... ... @@ -89,7 +96,6 @@ public class DataSourceConfigServiceImplTest extends BaseDbUnitTest {
89 96 o.setId(dbDataSourceConfig.getId()); // 设置更新的 ID
90 97 });
91 98 // mock 方法
92   -// when(stringEncryptor.encrypt(eq(reqVO.getPassword()))).thenReturn("123456");
93 99 databaseUtilsMock.when(() -> JdbcUtils.isConnectionOK(eq(reqVO.getUrl()),
94 100 eq(reqVO.getUsername()), eq(reqVO.getPassword()))).thenReturn(true);
95 101  
... ... @@ -142,7 +148,58 @@ public class DataSourceConfigServiceImplTest extends BaseDbUnitTest {
142 148 // 调用
143 149 DataSourceConfigDO result = dataSourceConfigMapper.selectOne(DataSourceConfigDO::getPassword,
144 150 EncryptTypeHandler.encrypt(dbDataSourceConfig.getPassword()));
145   - System.out.println(result);
  151 + assertPojoEquals(dbDataSourceConfig, result);
  152 + }
  153 +
  154 + @Test
  155 + public void testGetDataSourceConfig_master() {
  156 + // 准备参数
  157 + Long id = 0L;
  158 + // mock 方法
  159 +
  160 + // 调用
  161 + DataSourceConfigDO dataSourceConfig = dataSourceConfigService.getDataSourceConfig(id);
  162 + // 断言
  163 + assertEquals(id, dataSourceConfig.getId());
  164 + assertEquals("primary", dataSourceConfig.getName());
  165 + assertEquals("http://localhost:3306", dataSourceConfig.getUrl());
  166 + assertEquals("yunai", dataSourceConfig.getUsername());
  167 + assertEquals("tudou", dataSourceConfig.getPassword());
  168 + }
  169 +
  170 + @Test
  171 + public void testGetDataSourceConfig_normal() {
  172 + // mock 数据
  173 + DataSourceConfigDO dbDataSourceConfig = randomPojo(DataSourceConfigDO.class);
  174 + dataSourceConfigMapper.insert(dbDataSourceConfig);// @Sql: 先插入出一条存在的数据
  175 + // 准备参数
  176 + Long id = dbDataSourceConfig.getId();
  177 +
  178 + // 调用
  179 + DataSourceConfigDO dataSourceConfig = dataSourceConfigService.getDataSourceConfig(id);
  180 + // 断言
  181 + assertPojoEquals(dbDataSourceConfig, dataSourceConfig);
  182 + }
  183 +
  184 + @Test
  185 + public void testGetDataSourceConfigList() {
  186 + // mock 数据
  187 + DataSourceConfigDO dbDataSourceConfig = randomPojo(DataSourceConfigDO.class);
  188 + dataSourceConfigMapper.insert(dbDataSourceConfig);// @Sql: 先插入出一条存在的数据
  189 + // 准备参数
  190 +
  191 + // 调用
  192 + List<DataSourceConfigDO> dataSourceConfigList = dataSourceConfigService.getDataSourceConfigList();
  193 + // 断言
  194 + assertEquals(2, dataSourceConfigList.size());
  195 + // master
  196 + assertEquals(0L, dataSourceConfigList.get(0).getId());
  197 + assertEquals("primary", dataSourceConfigList.get(0).getName());
  198 + assertEquals("http://localhost:3306", dataSourceConfigList.get(0).getUrl());
  199 + assertEquals("yunai", dataSourceConfigList.get(0).getUsername());
  200 + assertEquals("tudou", dataSourceConfigList.get(0).getPassword());
  201 + // normal
  202 + assertPojoEquals(dbDataSourceConfig, dataSourceConfigList.get(1));
146 203 }
147 204  
148 205 }
... ...
yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/db/DatabaseTableServiceImplTest.java 0 → 100644
  1 +package cn.iocoder.yudao.module.infra.service.db;
  2 +
  3 +import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
  4 +import cn.iocoder.yudao.module.infra.dal.dataobject.db.DataSourceConfigDO;
  5 +import com.baomidou.mybatisplus.generator.config.po.TableField;
  6 +import com.baomidou.mybatisplus.generator.config.po.TableInfo;
  7 +import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
  8 +import org.apache.ibatis.type.JdbcType;
  9 +import org.junit.jupiter.api.Test;
  10 +import org.springframework.boot.test.mock.mockito.MockBean;
  11 +import org.springframework.context.annotation.Import;
  12 +
  13 +import javax.annotation.Resource;
  14 +import java.util.List;
  15 +
  16 +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
  17 +import static org.junit.jupiter.api.Assertions.*;
  18 +import static org.mockito.ArgumentMatchers.eq;
  19 +import static org.mockito.Mockito.when;
  20 +
  21 +@Import(DatabaseTableServiceImpl.class)
  22 +public class DatabaseTableServiceImplTest extends BaseDbUnitTest {
  23 +
  24 + @Resource
  25 + private DatabaseTableServiceImpl databaseTableService;
  26 +
  27 + @MockBean
  28 + private DataSourceConfigService dataSourceConfigService;
  29 +
  30 + @Test
  31 + public void testGetTableList() {
  32 + // 准备参数
  33 + Long dataSourceConfigId = randomLongId();
  34 + // mock 方法
  35 + DataSourceConfigDO dataSourceConfig = new DataSourceConfigDO().setUsername("sa").setPassword("")
  36 + .setUrl("jdbc:h2:mem:testdb");
  37 + when(dataSourceConfigService.getDataSourceConfig(eq(dataSourceConfigId)))
  38 + .thenReturn(dataSourceConfig);
  39 +
  40 + // 调用
  41 + List<TableInfo> tables = databaseTableService.getTableList(dataSourceConfigId,
  42 + "config", "参数");
  43 + // 断言
  44 + assertEquals(1, tables.size());
  45 + assertTableInfo(tables.get(0));
  46 + }
  47 +
  48 + @Test
  49 + public void testGetTable() {
  50 + // 准备参数
  51 + Long dataSourceConfigId = randomLongId();
  52 + // mock 方法
  53 + DataSourceConfigDO dataSourceConfig = new DataSourceConfigDO().setUsername("sa").setPassword("")
  54 + .setUrl("jdbc:h2:mem:testdb");
  55 + when(dataSourceConfigService.getDataSourceConfig(eq(dataSourceConfigId)))
  56 + .thenReturn(dataSourceConfig);
  57 +
  58 + // 调用
  59 + TableInfo tableInfo = databaseTableService.getTable(dataSourceConfigId, "infra_config");
  60 + // 断言
  61 + assertTableInfo(tableInfo);
  62 + }
  63 +
  64 + private void assertTableInfo(TableInfo tableInfo) {
  65 + assertEquals("infra_config", tableInfo.getName());
  66 + assertEquals("参数配置表", tableInfo.getComment());
  67 + assertEquals(13, tableInfo.getFields().size());
  68 + // id 字段
  69 + TableField idField = tableInfo.getFields().get(0);
  70 + assertEquals("id", idField.getName());
  71 + assertEquals(JdbcType.BIGINT, idField.getMetaInfo().getJdbcType());
  72 + assertEquals("编号", idField.getComment());
  73 + assertFalse(idField.getMetaInfo().isNullable());
  74 + assertTrue(idField.isKeyFlag());
  75 + assertTrue(idField.isKeyIdentityFlag());
  76 + assertEquals(DbColumnType.LONG, idField.getColumnType());
  77 + assertEquals("id", idField.getPropertyName());
  78 + // name 字段
  79 + TableField nameField = tableInfo.getFields().get(3);
  80 + assertEquals("name", nameField.getName());
  81 + assertEquals(JdbcType.VARCHAR, nameField.getMetaInfo().getJdbcType());
  82 + assertEquals("名字", nameField.getComment());
  83 + assertFalse(nameField.getMetaInfo().isNullable());
  84 + assertFalse(nameField.isKeyFlag());
  85 + assertFalse(nameField.isKeyIdentityFlag());
  86 + assertEquals(DbColumnType.STRING, nameField.getColumnType());
  87 + assertEquals("name", nameField.getPropertyName());
  88 + }
  89 +}
... ...
yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/file/FileConfigServiceImplTest.java
... ... @@ -7,6 +7,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
7 7 import cn.iocoder.yudao.framework.file.core.client.FileClient;
8 8 import cn.iocoder.yudao.framework.file.core.client.FileClientConfig;
9 9 import cn.iocoder.yudao.framework.file.core.client.FileClientFactory;
  10 +import cn.iocoder.yudao.framework.file.core.client.local.LocalFileClient;
10 11 import cn.iocoder.yudao.framework.file.core.client.local.LocalFileClientConfig;
11 12 import cn.iocoder.yudao.framework.file.core.enums.FileStorageEnum;
12 13 import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
... ... @@ -242,6 +243,30 @@ public class FileConfigServiceImplTest extends BaseDbUnitTest {
242 243 assertEquals("https://www.iocoder.cn", fileConfigService.testFileConfig(id));
243 244 }
244 245  
  246 + @Test
  247 + public void testGetFileConfig() {
  248 + // mock 数据
  249 + FileConfigDO dbFileConfig = randomFileConfigDO().setMaster(false);
  250 + fileConfigMapper.insert(dbFileConfig);// @Sql: 先插入出一条存在的数据
  251 + // 准备参数
  252 + Long id = dbFileConfig.getId();
  253 +
  254 + // 调用,并断言
  255 + assertPojoEquals(dbFileConfig, fileConfigService.getFileConfig(id));
  256 + }
  257 +
  258 + @Test
  259 + public void testGetFileClient() {
  260 + // 准备参数
  261 + Long id = randomLongId();
  262 + // mock 获得 Client
  263 + FileClient fileClient = new LocalFileClient(id, new LocalFileClientConfig());
  264 + when(fileClientFactory.getFileClient(eq(id))).thenReturn(fileClient);
  265 +
  266 + // 调用,并断言
  267 + assertSame(fileClient, fileConfigService.getFileClient(id));
  268 + }
  269 +
245 270 private FileConfigDO randomFileConfigDO() {
246 271 return randomPojo(FileConfigDO.class).setStorage(randomEle(FileStorageEnum.values()).getStorage())
247 272 .setConfig(new EmptyFileClientConfig());
... ...
yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/file/FileServiceTest.java renamed to yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImplTest.java
... ... @@ -26,7 +26,7 @@ import static org.mockito.ArgumentMatchers.same;
26 26 import static org.mockito.Mockito.*;
27 27  
28 28 @Import({FileServiceImpl.class})
29   -public class FileServiceTest extends BaseDbUnitTest {
  29 +public class FileServiceImplTest extends BaseDbUnitTest {
30 30  
31 31 @Resource
32 32 private FileService fileService;
... ...
yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/job/JobLogServiceTest.java renamed to yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/job/JobLogServiceImplTest.java
1 1 package cn.iocoder.yudao.module.infra.service.job;
2 2  
3 3 import cn.iocoder.yudao.framework.common.pojo.PageResult;
4   -import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
5 4 import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
6 5 import cn.iocoder.yudao.module.infra.controller.admin.job.vo.log.JobLogExportReqVO;
7 6 import cn.iocoder.yudao.module.infra.controller.admin.job.vo.log.JobLogPageReqVO;
... ... @@ -12,18 +11,20 @@ import org.junit.jupiter.api.Test;
12 11 import org.springframework.context.annotation.Import;
13 12  
14 13 import javax.annotation.Resource;
15   -import java.util.ArrayList;
  14 +import java.time.LocalDateTime;
  15 +import java.util.Collection;
16 16 import java.util.List;
17 17  
18   -import static cn.hutool.core.util.RandomUtil.randomEle;
19 18 import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
  19 +import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
20 20 import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
21 21 import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
  22 +import static java.util.Collections.singleton;
22 23 import static org.junit.jupiter.api.Assertions.assertEquals;
23 24 import static org.junit.jupiter.api.Assertions.assertNotNull;
24 25  
25 26 @Import(JobLogServiceImpl.class)
26   -public class JobLogServiceTest extends BaseDbUnitTest {
  27 +public class JobLogServiceImplTest extends BaseDbUnitTest {
27 28  
28 29 @Resource
29 30 private JobLogServiceImpl jobLogService;
... ... @@ -31,66 +32,103 @@ public class JobLogServiceTest extends BaseDbUnitTest {
31 32 private JobLogMapper jobLogMapper;
32 33  
33 34 @Test
34   - public void testCreateJobLog_success() {
  35 + public void testCreateJobLog() {
35 36 // 准备参数
36   - JobLogDO reqVO = randomPojo(JobLogDO.class, o -> {
37   - o.setExecuteIndex(1);
38   - });
  37 + JobLogDO reqVO = randomPojo(JobLogDO.class, o -> o.setExecuteIndex(1));
  38 +
39 39 // 调用
40   - Long jobLogId = jobLogService.createJobLog(reqVO.getJobId(), reqVO.getBeginTime(), reqVO.getHandlerName(), reqVO.getHandlerParam(), reqVO.getExecuteIndex());
  40 + Long id = jobLogService.createJobLog(reqVO.getJobId(), reqVO.getBeginTime(),
  41 + reqVO.getHandlerName(), reqVO.getHandlerParam(), reqVO.getExecuteIndex());
41 42 // 断言
42   - assertNotNull(jobLogId);
  43 + assertNotNull(id);
43 44 // 校验记录的属性是否正确
44   - JobLogDO job = jobLogMapper.selectById(jobLogId);
  45 + JobLogDO job = jobLogMapper.selectById(id);
45 46 assertEquals(JobLogStatusEnum.RUNNING.getStatus(), job.getStatus());
46 47 }
47 48  
48 49 @Test
49 50 public void testUpdateJobLogResultAsync_success() {
50   - // 准备参数
51   - JobLogDO reqVO = randomPojo(JobLogDO.class, o -> {
  51 + // mock 数据
  52 + JobLogDO log = randomPojo(JobLogDO.class, o -> {
52 53 o.setExecuteIndex(1);
  54 + o.setStatus(JobLogStatusEnum.RUNNING.getStatus());
53 55 });
54   - JobLogDO log = JobLogDO.builder().jobId(reqVO.getJobId()).handlerName(reqVO.getHandlerName()).handlerParam(reqVO.getHandlerParam()).executeIndex(reqVO.getExecuteIndex())
55   - .beginTime(reqVO.getBeginTime()).status(JobLogStatusEnum.RUNNING.getStatus()).build();
56 56 jobLogMapper.insert(log);
57   - // 调用
58   - jobLogService.updateJobLogResultAsync(log.getId(), reqVO.getBeginTime(), reqVO.getDuration(), true,reqVO.getResult());
59   - // 校验记录的属性是否正确
60   - JobLogDO job = jobLogMapper.selectById(log.getId());
61   - assertEquals(JobLogStatusEnum.SUCCESS.getStatus(), job.getStatus());
  57 + // 准备参数
  58 + Long logId = log.getId();
  59 + LocalDateTime endTime = randomLocalDateTime();
  60 + Integer duration = randomInteger();
  61 + boolean success = true;
  62 + String result = randomString();
62 63  
63 64 // 调用
64   - jobLogService.updateJobLogResultAsync(log.getId(), reqVO.getBeginTime(), reqVO.getDuration(), false,reqVO.getResult());
  65 + jobLogService.updateJobLogResultAsync(logId, endTime, duration, success, result);
65 66 // 校验记录的属性是否正确
66   - JobLogDO job2 = jobLogMapper.selectById(log.getId());
67   - assertEquals(JobLogStatusEnum.FAILURE.getStatus(), job2.getStatus());
  67 + JobLogDO dbLog = jobLogMapper.selectById(log.getId());
  68 + assertEquals(endTime, dbLog.getEndTime());
  69 + assertEquals(duration, dbLog.getDuration());
  70 + assertEquals(JobLogStatusEnum.SUCCESS.getStatus(), dbLog.getStatus());
  71 + assertEquals(result, dbLog.getResult());
68 72 }
69 73  
70 74 @Test
71   - public void testGetJobLogListByIds_success() {
  75 + public void testUpdateJobLogResultAsync_failure() {
72 76 // mock 数据
73   - JobLogDO dbJobLog = randomPojo(JobLogDO.class, o -> {
  77 + JobLogDO log = randomPojo(JobLogDO.class, o -> {
74 78 o.setExecuteIndex(1);
75   - o.setStatus(randomEle(JobLogStatusEnum.values()).getStatus()); // 保证 status 的范围
  79 + o.setStatus(JobLogStatusEnum.RUNNING.getStatus());
76 80 });
77   - JobLogDO cloneJobLog = ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setHandlerName(randomString()));
  81 + jobLogMapper.insert(log);
  82 + // 准备参数
  83 + Long logId = log.getId();
  84 + LocalDateTime endTime = randomLocalDateTime();
  85 + Integer duration = randomInteger();
  86 + boolean success = false;
  87 + String result = randomString();
  88 +
  89 + // 调用
  90 + jobLogService.updateJobLogResultAsync(logId, endTime, duration, success, result);
  91 + // 校验记录的属性是否正确
  92 + JobLogDO dbLog = jobLogMapper.selectById(log.getId());
  93 + assertEquals(endTime, dbLog.getEndTime());
  94 + assertEquals(duration, dbLog.getDuration());
  95 + assertEquals(JobLogStatusEnum.FAILURE.getStatus(), dbLog.getStatus());
  96 + assertEquals(result, dbLog.getResult());
  97 + }
  98 +
  99 + @Test
  100 + public void testGetJobLog() {
  101 + // mock 数据
  102 + JobLogDO dbJobLog = randomPojo(JobLogDO.class, o -> o.setExecuteIndex(1));
  103 + jobLogMapper.insert(dbJobLog);
  104 + // 准备参数
  105 + Long id = dbJobLog.getId();
  106 +
  107 + // 调用
  108 + JobLogDO jobLog = jobLogService.getJobLog(id);
  109 + // 断言
  110 + assertPojoEquals(dbJobLog, jobLog);
  111 + }
  112 +
  113 + @Test
  114 + public void testGetJobLogList() {
  115 + // mock 数据
  116 + JobLogDO dbJobLog = randomPojo(JobLogDO.class, o -> o.setExecuteIndex(1));
78 117 jobLogMapper.insert(dbJobLog);
79 118 // 测试 handlerName 不匹配
80   - jobLogMapper.insert(cloneJobLog);
  119 + jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> {}));
81 120 // 准备参数
82   - ArrayList ids = new ArrayList<>();
83   - ids.add(dbJobLog.getId());
84   - ids.add(cloneJobLog.getId());
  121 + Collection<Long> ids = singleton(dbJobLog.getId());
  122 +
85 123 // 调用
86 124 List<JobLogDO> list = jobLogService.getJobLogList(ids);
87 125 // 断言
88   - assertEquals(2, list.size());
  126 + assertEquals(1, list.size());
89 127 assertPojoEquals(dbJobLog, list.get(0));
90 128 }
91 129  
92 130 @Test
93   - public void testGetJobPage_success() {
  131 + public void testGetJobPage() {
94 132 // mock 数据
95 133 JobLogDO dbJobLog = randomPojo(JobLogDO.class, o -> {
96 134 o.setExecuteIndex(1);
... ... @@ -101,15 +139,15 @@ public class JobLogServiceTest extends BaseDbUnitTest {
101 139 });
102 140 jobLogMapper.insert(dbJobLog);
103 141 // 测试 jobId 不匹配
104   - jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setJobId(randomLongId())));
  142 + jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setJobId(randomLongId())));
105 143 // 测试 handlerName 不匹配
106   - jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setHandlerName(randomString())));
  144 + jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setHandlerName(randomString())));
107 145 // 测试 beginTime 不匹配
108   - jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setBeginTime(buildTime(2021, 1, 7))));
  146 + jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setBeginTime(buildTime(2021, 1, 7))));
109 147 // 测试 endTime 不匹配
110   - jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setEndTime(buildTime(2021, 1, 9))));
  148 + jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setEndTime(buildTime(2021, 1, 9))));
111 149 // 测试 status 不匹配
112   - jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setStatus(JobLogStatusEnum.FAILURE.getStatus())));
  150 + jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setStatus(JobLogStatusEnum.FAILURE.getStatus())));
113 151 // 准备参数
114 152 JobLogPageReqVO reqVo = new JobLogPageReqVO();
115 153 reqVo.setJobId(dbJobLog.getJobId());
... ... @@ -117,6 +155,7 @@ public class JobLogServiceTest extends BaseDbUnitTest {
117 155 reqVo.setBeginTime(dbJobLog.getBeginTime());
118 156 reqVo.setEndTime(dbJobLog.getEndTime());
119 157 reqVo.setStatus(JobLogStatusEnum.SUCCESS.getStatus());
  158 +
120 159 // 调用
121 160 PageResult<JobLogDO> pageResult = jobLogService.getJobLogPage(reqVo);
122 161 // 断言
... ... @@ -126,7 +165,7 @@ public class JobLogServiceTest extends BaseDbUnitTest {
126 165 }
127 166  
128 167 @Test
129   - public void testGetJobListForExport_success() {
  168 + public void testGetJobList_export() {
130 169 // mock 数据
131 170 JobLogDO dbJobLog = randomPojo(JobLogDO.class, o -> {
132 171 o.setExecuteIndex(1);
... ... @@ -137,15 +176,15 @@ public class JobLogServiceTest extends BaseDbUnitTest {
137 176 });
138 177 jobLogMapper.insert(dbJobLog);
139 178 // 测试 jobId 不匹配
140   - jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setJobId(randomLongId())));
  179 + jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setJobId(randomLongId())));
141 180 // 测试 handlerName 不匹配
142   - jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setHandlerName(randomString())));
  181 + jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setHandlerName(randomString())));
143 182 // 测试 beginTime 不匹配
144   - jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setBeginTime(buildTime(2021, 1, 7))));
  183 + jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setBeginTime(buildTime(2021, 1, 7))));
145 184 // 测试 endTime 不匹配
146   - jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setEndTime(buildTime(2021, 1, 9))));
  185 + jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setEndTime(buildTime(2021, 1, 9))));
147 186 // 测试 status 不匹配
148   - jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setStatus(JobLogStatusEnum.FAILURE.getStatus())));
  187 + jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setStatus(JobLogStatusEnum.FAILURE.getStatus())));
149 188 // 准备参数
150 189 JobLogExportReqVO reqVo = new JobLogExportReqVO();
151 190 reqVo.setJobId(dbJobLog.getJobId());
... ... @@ -153,6 +192,7 @@ public class JobLogServiceTest extends BaseDbUnitTest {
153 192 reqVo.setBeginTime(dbJobLog.getBeginTime());
154 193 reqVo.setEndTime(dbJobLog.getEndTime());
155 194 reqVo.setStatus(JobLogStatusEnum.SUCCESS.getStatus());
  195 +
156 196 // 调用
157 197 List<JobLogDO> list = jobLogService.getJobLogList(reqVo);
158 198 // 断言
... ...
yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/job/JobServiceTest.java renamed to yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/job/JobServiceImplTest.java
1 1 package cn.iocoder.yudao.module.infra.service.job;
2 2  
3 3 import cn.iocoder.yudao.framework.common.pojo.PageResult;
4   -import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
5 4 import cn.iocoder.yudao.framework.quartz.core.scheduler.SchedulerManager;
6 5 import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
7 6 import cn.iocoder.yudao.module.infra.controller.admin.job.vo.job.JobCreateReqVO;
8 7 import cn.iocoder.yudao.module.infra.controller.admin.job.vo.job.JobExportReqVO;
9 8 import cn.iocoder.yudao.module.infra.controller.admin.job.vo.job.JobPageReqVO;
10 9 import cn.iocoder.yudao.module.infra.controller.admin.job.vo.job.JobUpdateReqVO;
11   -import cn.iocoder.yudao.module.infra.convert.job.JobConvert;
12 10 import cn.iocoder.yudao.module.infra.dal.dataobject.job.JobDO;
13 11 import cn.iocoder.yudao.module.infra.dal.mysql.job.JobMapper;
14 12 import cn.iocoder.yudao.module.infra.enums.job.JobStatusEnum;
... ... @@ -18,22 +16,23 @@ import org.springframework.boot.test.mock.mockito.MockBean;
18 16 import org.springframework.context.annotation.Import;
19 17  
20 18 import javax.annotation.Resource;
21   -import java.util.ArrayList;
  19 +import java.util.Collection;
22 20 import java.util.List;
23 21  
24 22 import static cn.hutool.core.util.RandomUtil.randomEle;
  23 +import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
25 24 import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
26 25 import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
27 26 import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
28 27 import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString;
29 28 import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.*;
  29 +import static java.util.Collections.singletonList;
30 30 import static org.junit.jupiter.api.Assertions.*;
31 31 import static org.mockito.ArgumentMatchers.eq;
32   -import static org.mockito.Mockito.times;
33 32 import static org.mockito.Mockito.verify;
34 33  
35 34 @Import(JobServiceImpl.class)
36   -public class JobServiceTest extends BaseDbUnitTest {
  35 +public class JobServiceImplTest extends BaseDbUnitTest {
37 36  
38 37 @Resource
39 38 private JobServiceImpl jobService;
... ... @@ -46,6 +45,7 @@ public class JobServiceTest extends BaseDbUnitTest {
46 45 public void testCreateJob_cronExpressionValid() {
47 46 // 准备参数。Cron 表达式为 String 类型,默认随机字符串。
48 47 JobCreateReqVO reqVO = randomPojo(JobCreateReqVO.class);
  48 +
49 49 // 调用,并断言异常
50 50 assertServiceException(() -> jobService.createJob(reqVO), JOB_CRON_EXPRESSION_VALID);
51 51 }
... ... @@ -54,6 +54,7 @@ public class JobServiceTest extends BaseDbUnitTest {
54 54 public void testCreateJob_jobHandlerExists() throws SchedulerException {
55 55 // 准备参数 指定 Cron 表达式
56 56 JobCreateReqVO reqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
  57 +
57 58 // 调用
58 59 jobService.createJob(reqVO);
59 60 // 调用,并断言异常
... ... @@ -64,6 +65,7 @@ public class JobServiceTest extends BaseDbUnitTest {
64 65 public void testCreateJob_success() throws SchedulerException {
65 66 // 准备参数 指定 Cron 表达式
66 67 JobCreateReqVO reqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
  68 +
67 69 // 调用
68 70 Long jobId = jobService.createJob(reqVO);
69 71 // 断言
... ... @@ -73,14 +75,15 @@ public class JobServiceTest extends BaseDbUnitTest {
73 75 assertPojoEquals(reqVO, job);
74 76 assertEquals(JobStatusEnum.NORMAL.getStatus(), job.getStatus());
75 77 // 校验调用
76   - verify(schedulerManager, times(1)).addJob(eq(job.getId()), eq(job.getHandlerName()), eq(job.getHandlerParam()), eq(job.getCronExpression()),
77   - eq(reqVO.getRetryCount()), eq(reqVO.getRetryInterval()));
  78 + verify(schedulerManager).addJob(eq(job.getId()), eq(job.getHandlerName()), eq(job.getHandlerParam()),
  79 + eq(job.getCronExpression()), eq(reqVO.getRetryCount()), eq(reqVO.getRetryInterval()));
78 80 }
79 81  
80 82 @Test
81 83 public void testUpdateJob_jobNotExists(){
82 84 // 准备参数
83 85 JobUpdateReqVO reqVO = randomPojo(JobUpdateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
  86 +
84 87 // 调用,并断言异常
85 88 assertServiceException(() -> jobService.updateJob(reqVO), JOB_NOT_EXISTS);
86 89 }
... ... @@ -88,150 +91,136 @@ public class JobServiceTest extends BaseDbUnitTest {
88 91 @Test
89 92 public void testUpdateJob_onlyNormalStatus(){
90 93 // mock 数据
91   - JobCreateReqVO createReqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
92   - JobDO job = JobConvert.INSTANCE.convert(createReqVO);
93   - job.setStatus(JobStatusEnum.INIT.getStatus());
94   - fillJobMonitorTimeoutEmpty(job);
  94 + JobDO job = randomPojo(JobDO.class, o -> o.setStatus(JobStatusEnum.INIT.getStatus()));
95 95 jobMapper.insert(job);
96 96 // 准备参数
97 97 JobUpdateReqVO updateReqVO = randomPojo(JobUpdateReqVO.class, o -> {
98 98 o.setId(job.getId());
99   - o.setName(createReqVO.getName());
100   - o.setCronExpression(createReqVO.getCronExpression());
  99 + o.setCronExpression("0 0/1 * * * ? *");
101 100 });
  101 +
102 102 // 调用,并断言异常
103   - assertServiceException(() -> jobService.updateJob(updateReqVO), JOB_UPDATE_ONLY_NORMAL_STATUS);
  103 + assertServiceException(() -> jobService.updateJob(updateReqVO),
  104 + JOB_UPDATE_ONLY_NORMAL_STATUS);
104 105 }
105 106  
106 107 @Test
107 108 public void testUpdateJob_success() throws SchedulerException {
108 109 // mock 数据
109   - JobCreateReqVO createReqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
110   - JobDO job = JobConvert.INSTANCE.convert(createReqVO);
111   - job.setStatus(JobStatusEnum.NORMAL.getStatus());
112   - fillJobMonitorTimeoutEmpty(job);
  110 + JobDO job = randomPojo(JobDO.class, o -> o.setStatus(JobStatusEnum.NORMAL.getStatus()));
113 111 jobMapper.insert(job);
114 112 // 准备参数
115 113 JobUpdateReqVO updateReqVO = randomPojo(JobUpdateReqVO.class, o -> {
116 114 o.setId(job.getId());
117   - o.setName(createReqVO.getName());
118   - o.setCronExpression(createReqVO.getCronExpression());
  115 + o.setCronExpression("0 0/1 * * * ? *");
119 116 });
  117 +
120 118 // 调用
121 119 jobService.updateJob(updateReqVO);
122 120 // 校验记录的属性是否正确
123 121 JobDO updateJob = jobMapper.selectById(updateReqVO.getId());
124 122 assertPojoEquals(updateReqVO, updateJob);
125 123 // 校验调用
126   - verify(schedulerManager, times(1)).updateJob(eq(job.getHandlerName()), eq(updateReqVO.getHandlerParam()), eq(updateReqVO.getCronExpression()),
127   - eq(updateReqVO.getRetryCount()), eq(updateReqVO.getRetryInterval()));
  124 + verify(schedulerManager).updateJob(eq(job.getHandlerName()), eq(updateReqVO.getHandlerParam()),
  125 + eq(updateReqVO.getCronExpression()), eq(updateReqVO.getRetryCount()), eq(updateReqVO.getRetryInterval()));
128 126 }
129 127  
130 128 @Test
131 129 public void testUpdateJobStatus_changeStatusInvalid() {
132 130 // 调用,并断言异常
133   - assertServiceException(() -> jobService.updateJobStatus(1L, JobStatusEnum.INIT.getStatus()), JOB_CHANGE_STATUS_INVALID);
  131 + assertServiceException(() -> jobService.updateJobStatus(1L, JobStatusEnum.INIT.getStatus()),
  132 + JOB_CHANGE_STATUS_INVALID);
134 133 }
135 134  
136 135 @Test
137 136 public void testUpdateJobStatus_changeStatusEquals() {
138 137 // mock 数据
139   - JobCreateReqVO createReqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
140   - JobDO job = JobConvert.INSTANCE.convert(createReqVO);
141   - job.setStatus(JobStatusEnum.NORMAL.getStatus());
142   - fillJobMonitorTimeoutEmpty(job);
  138 + JobDO job = randomPojo(JobDO.class, o -> o.setStatus(JobStatusEnum.NORMAL.getStatus()));
143 139 jobMapper.insert(job);
  140 +
144 141 // 调用,并断言异常
145   - assertServiceException(() -> jobService.updateJobStatus(job.getId(), job.getStatus()), JOB_CHANGE_STATUS_EQUALS);
  142 + assertServiceException(() -> jobService.updateJobStatus(job.getId(), job.getStatus()),
  143 + JOB_CHANGE_STATUS_EQUALS);
146 144 }
147 145  
148 146 @Test
149   - public void testUpdateJobStatus_NormalToStop_success() throws SchedulerException {
  147 + public void testUpdateJobStatus_stopSuccess() throws SchedulerException {
150 148 // mock 数据
151   - JobCreateReqVO createReqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
152   - JobDO job = JobConvert.INSTANCE.convert(createReqVO);
153   - job.setStatus(JobStatusEnum.NORMAL.getStatus());
154   - fillJobMonitorTimeoutEmpty(job);
  149 + JobDO job = randomPojo(JobDO.class, o -> o.setStatus(JobStatusEnum.NORMAL.getStatus()));
155 150 jobMapper.insert(job);
  151 +
156 152 // 调用
157 153 jobService.updateJobStatus(job.getId(), JobStatusEnum.STOP.getStatus());
158 154 // 校验记录的属性是否正确
159   - JobDO updateJob = jobMapper.selectById(job.getId());
160   - assertEquals(JobStatusEnum.STOP.getStatus(), updateJob.getStatus());
  155 + JobDO dbJob = jobMapper.selectById(job.getId());
  156 + assertEquals(JobStatusEnum.STOP.getStatus(), dbJob.getStatus());
161 157 // 校验调用
162   - verify(schedulerManager, times(1)).pauseJob(eq(job.getHandlerName()));
  158 + verify(schedulerManager).pauseJob(eq(job.getHandlerName()));
163 159 }
164 160  
165 161 @Test
166   - public void testUpdateJobStatus_StopToNormal_success() throws SchedulerException {
  162 + public void testUpdateJobStatus_normalSuccess() throws SchedulerException {
167 163 // mock 数据
168   - JobCreateReqVO createReqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
169   - JobDO job = JobConvert.INSTANCE.convert(createReqVO);
170   - job.setStatus(JobStatusEnum.STOP.getStatus());
171   - fillJobMonitorTimeoutEmpty(job);
  164 + JobDO job = randomPojo(JobDO.class, o -> o.setStatus(JobStatusEnum.STOP.getStatus()));
172 165 jobMapper.insert(job);
  166 +
173 167 // 调用
174 168 jobService.updateJobStatus(job.getId(), JobStatusEnum.NORMAL.getStatus());
175 169 // 校验记录的属性是否正确
176   - JobDO updateJob = jobMapper.selectById(job.getId());
177   - assertEquals(JobStatusEnum.NORMAL.getStatus(), updateJob.getStatus());
  170 + JobDO dbJob = jobMapper.selectById(job.getId());
  171 + assertEquals(JobStatusEnum.NORMAL.getStatus(), dbJob.getStatus());
178 172 // 校验调用
179   - verify(schedulerManager, times(1)).resumeJob(eq(job.getHandlerName()));
  173 + verify(schedulerManager).resumeJob(eq(job.getHandlerName()));
180 174 }
181 175  
182 176 @Test
183 177 public void testTriggerJob_success() throws SchedulerException {
184 178 // mock 数据
185   - JobCreateReqVO createReqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
186   - JobDO job = JobConvert.INSTANCE.convert(createReqVO);
187   - job.setStatus(JobStatusEnum.NORMAL.getStatus());
188   - fillJobMonitorTimeoutEmpty(job);
  179 + JobDO job = randomPojo(JobDO.class);
189 180 jobMapper.insert(job);
  181 +
190 182 // 调用
191 183 jobService.triggerJob(job.getId());
192 184 // 校验调用
193   - verify(schedulerManager, times(1)).triggerJob(eq(job.getId()), eq(job.getHandlerName()), eq(job.getHandlerParam()));
  185 + verify(schedulerManager).triggerJob(eq(job.getId()),
  186 + eq(job.getHandlerName()), eq(job.getHandlerParam()));
194 187 }
195 188  
196 189 @Test
197 190 public void testDeleteJob_success() throws SchedulerException {
198 191 // mock 数据
199   - JobCreateReqVO createReqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
200   - JobDO job = JobConvert.INSTANCE.convert(createReqVO);
201   - job.setStatus(JobStatusEnum.NORMAL.getStatus());
202   - fillJobMonitorTimeoutEmpty(job);
  192 + JobDO job = randomPojo(JobDO.class);
203 193 jobMapper.insert(job);
204   - // 调用 UPDATE inf_job SET deleted=1 WHERE id=? AND deleted=0
  194 +
  195 + // 调用
205 196 jobService.deleteJob(job.getId());
206   - // 校验数据不存在了 WHERE id=? AND deleted=0 查询为空正常
  197 + // 校验不存在
207 198 assertNull(jobMapper.selectById(job.getId()));
208 199 // 校验调用
209   - verify(schedulerManager, times(1)).deleteJob(eq(job.getHandlerName()));
  200 + verify(schedulerManager).deleteJob(eq(job.getHandlerName()));
210 201 }
211 202  
212 203 @Test
213   - public void testGetJobListByIds_success() {
  204 + public void testGetJobList() {
214 205 // mock 数据
215 206 JobDO dbJob = randomPojo(JobDO.class, o -> {
216 207 o.setStatus(randomEle(JobStatusEnum.values()).getStatus()); // 保证 status 的范围
217 208 });
218   - JobDO cloneJob = ObjectUtils.cloneIgnoreId(dbJob, o -> o.setHandlerName(randomString()));
219 209 jobMapper.insert(dbJob);
220   - // 测试 handlerName 不匹配
221   - jobMapper.insert(cloneJob);
  210 + // 测试 id 不匹配
  211 + jobMapper.insert(cloneIgnoreId(dbJob, o -> {}));
  212 +
222 213 // 准备参数
223   - ArrayList<Long> ids = new ArrayList<>();
224   - ids.add(dbJob.getId());
225   - ids.add(cloneJob.getId());
  214 + Collection<Long> ids = singletonList(dbJob.getId());
226 215 // 调用
227 216 List<JobDO> list = jobService.getJobList(ids);
228 217 // 断言
229   - assertEquals(2, list.size());
  218 + assertEquals(1, list.size());
230 219 assertPojoEquals(dbJob, list.get(0));
231 220 }
232 221  
233 222 @Test
234   - public void testGetJobPage_success() {
  223 + public void testGetJobPage() {
235 224 // mock 数据
236 225 JobDO dbJob = randomPojo(JobDO.class, o -> {
237 226 o.setName("定时任务测试");
... ... @@ -240,16 +229,17 @@ public class JobServiceTest extends BaseDbUnitTest {
240 229 });
241 230 jobMapper.insert(dbJob);
242 231 // 测试 name 不匹配
243   - jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setName("土豆")));
  232 + jobMapper.insert(cloneIgnoreId(dbJob, o -> o.setName("土豆")));
244 233 // 测试 status 不匹配
245   - jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setStatus(JobStatusEnum.NORMAL.getStatus())));
  234 + jobMapper.insert(cloneIgnoreId(dbJob, o -> o.setStatus(JobStatusEnum.NORMAL.getStatus())));
246 235 // 测试 handlerName 不匹配
247   - jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setHandlerName(randomString())));
  236 + jobMapper.insert(cloneIgnoreId(dbJob, o -> o.setHandlerName(randomString())));
248 237 // 准备参数
249 238 JobPageReqVO reqVo = new JobPageReqVO();
250 239 reqVo.setName("定时");
251 240 reqVo.setStatus(JobStatusEnum.INIT.getStatus());
252 241 reqVo.setHandlerName("单元");
  242 +
253 243 // 调用
254 244 PageResult<JobDO> pageResult = jobService.getJobPage(reqVo);
255 245 // 断言
... ... @@ -259,7 +249,7 @@ public class JobServiceTest extends BaseDbUnitTest {
259 249 }
260 250  
261 251 @Test
262   - public void testGetJobListForExport_success() {
  252 + public void testGetJobList_export() {
263 253 // mock 数据
264 254 JobDO dbJob = randomPojo(JobDO.class, o -> {
265 255 o.setName("定时任务测试");
... ... @@ -268,16 +258,17 @@ public class JobServiceTest extends BaseDbUnitTest {
268 258 });
269 259 jobMapper.insert(dbJob);
270 260 // 测试 name 不匹配
271   - jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setName("土豆")));
  261 + jobMapper.insert(cloneIgnoreId(dbJob, o -> o.setName("土豆")));
272 262 // 测试 status 不匹配
273   - jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setStatus(JobStatusEnum.NORMAL.getStatus())));
  263 + jobMapper.insert(cloneIgnoreId(dbJob, o -> o.setStatus(JobStatusEnum.NORMAL.getStatus())));
274 264 // 测试 handlerName 不匹配
275   - jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setHandlerName(randomString())));
  265 + jobMapper.insert(cloneIgnoreId(dbJob, o -> o.setHandlerName(randomString())));
276 266 // 准备参数
277 267 JobExportReqVO reqVo = new JobExportReqVO();
278 268 reqVo.setName("定时");
279 269 reqVo.setStatus(JobStatusEnum.INIT.getStatus());
280 270 reqVo.setHandlerName("单元");
  271 +
281 272 // 调用
282 273 List<JobDO> list = jobService.getJobList(reqVo);
283 274 // 断言
... ... @@ -285,10 +276,15 @@ public class JobServiceTest extends BaseDbUnitTest {
285 276 assertPojoEquals(dbJob, list.get(0));
286 277 }
287 278  
288   - private static void fillJobMonitorTimeoutEmpty(JobDO job) {
289   - if (job.getMonitorTimeout() == null) {
290   - job.setMonitorTimeout(0);
291   - }
  279 + @Test
  280 + public void testGetJob() {
  281 + // mock 数据
  282 + JobDO dbJob = randomPojo(JobDO.class);
  283 + jobMapper.insert(dbJob);
  284 + // 调用
  285 + JobDO job = jobService.getJob(dbJob.getId());
  286 + // 断言
  287 + assertPojoEquals(dbJob, job);
292 288 }
293 289  
294 290 }
... ...
yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/logger/ApiAccessLogServiceImplTest.java
1 1 package cn.iocoder.yudao.module.infra.service.logger;
2 2  
3   -import cn.hutool.core.util.RandomUtil;
4 3 import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
5 4 import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
6 5 import cn.iocoder.yudao.framework.common.pojo.PageResult;
7   -import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
8 6 import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
9   -import cn.iocoder.yudao.framework.test.core.util.RandomUtils;
10 7 import cn.iocoder.yudao.module.infra.api.logger.dto.ApiAccessLogCreateReqDTO;
11 8 import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogExportReqVO;
12 9 import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogPageReqVO;
... ... @@ -16,149 +13,121 @@ import org.junit.jupiter.api.Test;
16 13 import org.springframework.context.annotation.Import;
17 14  
18 15 import javax.annotation.Resource;
19   -import java.time.LocalDateTime;
20 16 import java.util.List;
21 17  
  18 +import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
22 19 import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
  20 +import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
23 21 import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
  22 +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
24 23 import static org.junit.jupiter.api.Assertions.assertEquals;
25   -import static org.junit.jupiter.api.Assertions.assertNotNull;
26 24  
27 25 @Import(ApiAccessLogServiceImpl.class)
28 26 public class ApiAccessLogServiceImplTest extends BaseDbUnitTest {
29 27  
30 28 @Resource
31   - private ApiAccessLogService apiAccessLogService;
  29 + private ApiAccessLogServiceImpl apiAccessLogService;
32 30  
33 31 @Resource
34 32 private ApiAccessLogMapper apiAccessLogMapper;
35 33  
36 34 @Test
37 35 public void testGetApiAccessLogPage() {
38   - // 构造测试数据
39   - long userId = 2233L;
40   - int userType = UserTypeEnum.ADMIN.getValue();
41   - String applicationName = "yudao-test";
42   - String requestUrl = "foo";
43   - LocalDateTime beginTime = buildTime(2021, 3, 13);
44   - int duration = 1000;
45   - int resultCode = GlobalErrorCodeConstants.SUCCESS.getCode();
46   -
47   - ApiAccessLogDO infApiAccessLogDO = RandomUtils.randomPojo(ApiAccessLogDO.class, dto -> {
48   - dto.setUserId(userId);
49   - dto.setUserType(userType);
50   - dto.setApplicationName(applicationName);
51   - dto.setRequestUrl(requestUrl);
52   - dto.setBeginTime(beginTime);
53   - dto.setDuration(duration);
54   - dto.setResultCode(resultCode);
  36 + ApiAccessLogDO apiAccessLogDO = randomPojo(ApiAccessLogDO.class, o -> {
  37 + o.setUserId(2233L);
  38 + o.setUserType(UserTypeEnum.ADMIN.getValue());
  39 + o.setApplicationName("yudao-test");
  40 + o.setRequestUrl("foo");
  41 + o.setBeginTime(buildTime(2021, 3, 13));
  42 + o.setDuration(1000);
  43 + o.setResultCode(GlobalErrorCodeConstants.SUCCESS.getCode());
55 44 });
56   - apiAccessLogMapper.insert(infApiAccessLogDO);
57   -
58   - // 下面几个都是不匹配的数据
59   - // userId 不同的
60   - apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setUserId(3344L)));
61   - // userType
62   - apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue())));
63   - // applicationName 不同的
64   - apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setApplicationName("test")));
65   - // requestUrl 不同的
66   - apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setRequestUrl("bar")));
67   - // 构造一个早期时间 2021-02-06 00:00:00
68   - apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setBeginTime(buildTime(2021, 2, 6))));
69   - // duration 不同的
70   - apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setDuration(100)));
71   - // resultCode 不同的
72   - apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setResultCode(2)));
73   -
74   - // 构造调用参数
  45 + apiAccessLogMapper.insert(apiAccessLogDO);
  46 + // 测试 userId 不匹配
  47 + apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setUserId(3344L)));
  48 + // 测试 userType 不匹配
  49 + apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setUserType(UserTypeEnum.MEMBER.getValue())));
  50 + // 测试 applicationName 不匹配
  51 + apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setApplicationName("test")));
  52 + // 测试 requestUrl 不匹配
  53 + apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setRequestUrl("bar")));
  54 + // 测试 beginTime 不匹配:构造一个早期时间 2021-02-06 00:00:00
  55 + apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setBeginTime(buildTime(2021, 2, 6))));
  56 + // 测试 duration 不匹配
  57 + apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setDuration(100)));
  58 + // 测试 resultCode 不匹配
  59 + apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setResultCode(2)));
  60 + // 准备参数
75 61 ApiAccessLogPageReqVO reqVO = new ApiAccessLogPageReqVO();
76   - reqVO.setUserId(userId);
77   - reqVO.setUserType(userType);
78   - reqVO.setApplicationName(applicationName);
79   - reqVO.setRequestUrl(requestUrl);
80   - reqVO.setBeginTime((new LocalDateTime[]{buildTime(2021, 3, 12),buildTime(2021, 3, 14)}));
81   - reqVO.setDuration(duration);
82   - reqVO.setResultCode(resultCode);
  62 + reqVO.setUserId(2233L);
  63 + reqVO.setUserType(UserTypeEnum.ADMIN.getValue());
  64 + reqVO.setApplicationName("yudao-test");
  65 + reqVO.setRequestUrl("foo");
  66 + reqVO.setBeginTime(buildBetweenTime(2021, 3, 13, 2021, 3, 13));
  67 + reqVO.setDuration(1000);
  68 + reqVO.setResultCode(GlobalErrorCodeConstants.SUCCESS.getCode());
83 69  
84   - // 调用service方法
  70 + // 调用
85 71 PageResult<ApiAccessLogDO> pageResult = apiAccessLogService.getApiAccessLogPage(reqVO);
86   -
87 72 // 断言,只查到了一条符合条件的
88 73 assertEquals(1, pageResult.getTotal());
89 74 assertEquals(1, pageResult.getList().size());
90   - assertPojoEquals(infApiAccessLogDO, pageResult.getList().get(0));
  75 + assertPojoEquals(apiAccessLogDO, pageResult.getList().get(0));
91 76 }
92 77  
93 78 @Test
94 79 public void testGetApiAccessLogList() {
95   - // 构造测试数据
96   - long userId = 2233L;
97   - int userType = UserTypeEnum.ADMIN.getValue();
98   - String applicationName = "yudao-test";
99   - String requestUrl = "foo";
100   - LocalDateTime beginTime = buildTime(2021, 3, 13);
101   - int duration = 1000;
102   - int resultCode = GlobalErrorCodeConstants.SUCCESS.getCode();
103   -
104   - ApiAccessLogDO infApiAccessLogDO = RandomUtils.randomPojo(ApiAccessLogDO.class, dto -> {
105   - dto.setUserId(userId);
106   - dto.setUserType(userType);
107   - dto.setApplicationName(applicationName);
108   - dto.setRequestUrl(requestUrl);
109   - dto.setBeginTime(beginTime);
110   - dto.setDuration(duration);
111   - dto.setResultCode(resultCode);
  80 + ApiAccessLogDO apiAccessLogDO = randomPojo(ApiAccessLogDO.class, o -> {
  81 + o.setUserId(2233L);
  82 + o.setUserType(UserTypeEnum.ADMIN.getValue());
  83 + o.setApplicationName("yudao-test");
  84 + o.setRequestUrl("foo");
  85 + o.setBeginTime(buildTime(2021, 3, 13));
  86 + o.setDuration(1000);
  87 + o.setResultCode(GlobalErrorCodeConstants.SUCCESS.getCode());
112 88 });
113   - apiAccessLogMapper.insert(infApiAccessLogDO);
114   -
115   - // 下面几个都是不匹配的数据
116   - // userId 不同的
117   - apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setUserId(3344L)));
118   - // userType
119   - apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue())));
120   - // applicationName 不同的
121   - apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setApplicationName("test")));
122   - // requestUrl 不同的
123   - apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setRequestUrl("bar")));
124   - // 构造一个早期时间 2021-02-06 00:00:00
125   - apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setBeginTime(buildTime(2021, 2, 6))));
126   - // duration 不同的
127   - apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setDuration(100)));
128   - // resultCode 不同的
129   - apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setResultCode(2)));
130   -
131   - // 构造调用参数
  89 + apiAccessLogMapper.insert(apiAccessLogDO);
  90 + // 测试 userId 不匹配
  91 + apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setUserId(3344L)));
  92 + // 测试 userType 不匹配
  93 + apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setUserType(UserTypeEnum.MEMBER.getValue())));
  94 + // 测试 applicationName 不匹配
  95 + apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setApplicationName("test")));
  96 + // 测试 requestUrl 不匹配
  97 + apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setRequestUrl("bar")));
  98 + // 测试 beginTime 不匹配:构造一个早期时间 2021-02-06 00:00:00
  99 + apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setBeginTime(buildTime(2021, 2, 6))));
  100 + // 测试 duration 不匹配
  101 + apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setDuration(100)));
  102 + // 测试 resultCode 不匹配
  103 + apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setResultCode(2)));
  104 + // 准备参数
132 105 ApiAccessLogExportReqVO reqVO = new ApiAccessLogExportReqVO();
133   - reqVO.setUserId(userId);
134   - reqVO.setUserType(userType);
135   - reqVO.setApplicationName(applicationName);
136   - reqVO.setRequestUrl(requestUrl);
137   - reqVO.setBeginTime((new LocalDateTime[]{buildTime(2021, 3, 12),buildTime(2021, 3, 14)}));
138   - reqVO.setDuration(duration);
139   - reqVO.setResultCode(resultCode);
  106 + reqVO.setUserId(2233L);
  107 + reqVO.setUserType(UserTypeEnum.ADMIN.getValue());
  108 + reqVO.setApplicationName("yudao-test");
  109 + reqVO.setRequestUrl("foo");
  110 + reqVO.setBeginTime(buildBetweenTime(2021, 3, 13, 2021, 3, 13));
  111 + reqVO.setDuration(1000);
  112 + reqVO.setResultCode(GlobalErrorCodeConstants.SUCCESS.getCode());
140 113  
141   - // 调用service方法
  114 + // 调用
142 115 List<ApiAccessLogDO> list = apiAccessLogService.getApiAccessLogList(reqVO);
143   -
144 116 // 断言,只查到了一条符合条件的
145 117 assertEquals(1, list.size());
146   - assertPojoEquals(infApiAccessLogDO, list.get(0));
  118 + assertPojoEquals(apiAccessLogDO, list.get(0));
147 119 }
148 120  
149 121 @Test
150   - public void testCreateApiAccessLogAsync() {
  122 + public void testCreateApiAccessLog() {
151 123 // 准备参数
152   - ApiAccessLogCreateReqDTO createDTO = RandomUtils.randomPojo(ApiAccessLogCreateReqDTO.class,
153   - dto -> dto.setUserType(RandomUtil.randomEle(UserTypeEnum.values()).getValue()));
  124 + ApiAccessLogCreateReqDTO createDTO = randomPojo(ApiAccessLogCreateReqDTO.class);
154 125  
155 126 // 调用
156 127 apiAccessLogService.createApiAccessLog(createDTO);
157 128 // 断言
158   - ApiAccessLogDO infApiAccessLogDO = apiAccessLogMapper.selectOne(null);
159   - assertNotNull(infApiAccessLogDO);
160   - assertPojoEquals(createDTO, infApiAccessLogDO);
  129 + ApiAccessLogDO apiAccessLogDO = apiAccessLogMapper.selectOne(null);
  130 + assertPojoEquals(createDTO, apiAccessLogDO);
161 131 }
162 132  
163   -
164 133 }
... ...
yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/logger/ApiErrorLogServiceImplTest.java
1 1 package cn.iocoder.yudao.module.infra.service.logger;
2 2  
3   -import cn.hutool.core.util.RandomUtil;
4 3 import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
5 4 import cn.iocoder.yudao.framework.common.pojo.PageResult;
6   -import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
7 5 import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
8   -import cn.iocoder.yudao.framework.test.core.util.RandomUtils;
9 6 import cn.iocoder.yudao.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO;
10 7 import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiErrorLogExportReqVO;
11 8 import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiErrorLogPageReqVO;
12 9 import cn.iocoder.yudao.module.infra.dal.dataobject.logger.ApiErrorLogDO;
13 10 import cn.iocoder.yudao.module.infra.dal.mysql.logger.ApiErrorLogMapper;
14 11 import cn.iocoder.yudao.module.infra.enums.logger.ApiErrorLogProcessStatusEnum;
15   -import org.junit.jupiter.api.Assertions;
16 12 import org.junit.jupiter.api.Test;
17 13 import org.springframework.context.annotation.Import;
18 14  
19 15 import javax.annotation.Resource;
20   -import java.time.LocalDateTime;
21 16 import java.util.List;
22 17  
  18 +import static cn.hutool.core.util.RandomUtil.randomEle;
  19 +import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
23 20 import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
  21 +import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
24 22 import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
25 23 import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
  24 +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
  25 +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
26 26 import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.API_ERROR_LOG_NOT_FOUND;
27 27 import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.API_ERROR_LOG_PROCESSED;
28 28 import static org.junit.jupiter.api.Assertions.assertEquals;
... ... @@ -35,161 +35,150 @@ public class ApiErrorLogServiceImplTest extends BaseDbUnitTest {
35 35 private ApiErrorLogServiceImpl apiErrorLogService;
36 36  
37 37 @Resource
38   - private ApiErrorLogMapper infApiErrorLogMapper;
  38 + private ApiErrorLogMapper apiErrorLogMapper;
39 39  
40 40 @Test
41 41 public void testGetApiErrorLogPage() {
42   - // 构造测试数据
43   - long userId = 2233L;
44   - int userType = UserTypeEnum.ADMIN.getValue();
45   - String applicationName = "yudao-test";
46   - String requestUrl = "foo";
47   - LocalDateTime beginTime = buildTime(2021, 3, 13);
48   - int progressStatus = ApiErrorLogProcessStatusEnum.INIT.getStatus();
49   -
50   - ApiErrorLogDO infApiErrorLogDO = RandomUtils.randomPojo(ApiErrorLogDO.class, logDO -> {
51   - logDO.setUserId(userId);
52   - logDO.setUserType(userType);
53   - logDO.setApplicationName(applicationName);
54   - logDO.setRequestUrl(requestUrl);
55   - logDO.setExceptionTime(beginTime);
56   - logDO.setProcessStatus(progressStatus);
  42 + // mock 数据
  43 + ApiErrorLogDO apiErrorLogDO = randomPojo(ApiErrorLogDO.class, o -> {
  44 + o.setUserId(2233L);
  45 + o.setUserType(UserTypeEnum.ADMIN.getValue());
  46 + o.setApplicationName("yudao-test");
  47 + o.setRequestUrl("foo");
  48 + o.setExceptionTime(buildTime(2021, 3, 13));
  49 + o.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
57 50 });
58   - infApiErrorLogMapper.insert(infApiErrorLogDO);
59   -
60   - // 下面几个都是不匹配的数据
61   - // userId 不同的
62   - infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setUserId(3344L)));
63   - // userType
64   - infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue())));
65   - // applicationName 不同的
66   - infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setApplicationName("test")));
67   - // requestUrl 不同的
68   - infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setRequestUrl("bar")));
69   - // 构造一个早期时间 2021-02-06 00:00:00
70   - infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setExceptionTime(buildTime(2021, 2, 6))));
71   - // progressStatus 不同的
72   - infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus())));
73   -
74   - // 构造调用参数
  51 + apiErrorLogMapper.insert(apiErrorLogDO);
  52 + // 测试 userId 不匹配
  53 + apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, o -> o.setUserId(3344L)));
  54 + // 测试 userType 不匹配
  55 + apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue())));
  56 + // 测试 applicationName 不匹配
  57 + apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setApplicationName("test")));
  58 + // 测试 requestUrl 不匹配
  59 + apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setRequestUrl("bar")));
  60 + // 测试 exceptionTime 不匹配:构造一个早期时间 2021-02-06 00:00:00
  61 + apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setExceptionTime(buildTime(2021, 2, 6))));
  62 + // 测试 progressStatus 不匹配
  63 + apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus())));
  64 + // 准备参数
75 65 ApiErrorLogPageReqVO reqVO = new ApiErrorLogPageReqVO();
76   - reqVO.setUserId(userId);
77   - reqVO.setUserType(userType);
78   - reqVO.setApplicationName(applicationName);
79   - reqVO.setRequestUrl(requestUrl);
80   - reqVO.setExceptionTime((new LocalDateTime[]{buildTime(2021, 3, 12),buildTime(2021, 3, 14)}));
81   - reqVO.setProcessStatus(progressStatus);
82   -
83   - // 调用service方法
84   - PageResult<ApiErrorLogDO> pageResult = apiErrorLogService.getApiErrorLogPage(reqVO);
  66 + reqVO.setUserId(2233L);
  67 + reqVO.setUserType(UserTypeEnum.ADMIN.getValue());
  68 + reqVO.setApplicationName("yudao-test");
  69 + reqVO.setRequestUrl("foo");
  70 + reqVO.setExceptionTime(buildBetweenTime(2021, 3, 1, 2021, 3, 31));
  71 + reqVO.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
85 72  
  73 + // 调用
  74 + PageResult<ApiErrorLogDO> pageResult = apiErrorLogService.getApiErrorLogPage(reqVO);
86 75 // 断言,只查到了一条符合条件的
87 76 assertEquals(1, pageResult.getTotal());
88 77 assertEquals(1, pageResult.getList().size());
89   - assertPojoEquals(infApiErrorLogDO, pageResult.getList().get(0));
  78 + assertPojoEquals(apiErrorLogDO, pageResult.getList().get(0));
90 79 }
91 80  
92 81 @Test
93 82 public void testGetApiErrorLogList() {
94   - // 构造测试数据
95   - long userId = 2233L;
96   - int userType = UserTypeEnum.ADMIN.getValue();
97   - String applicationName = "yudao-test";
98   - String requestUrl = "foo";
99   - LocalDateTime beginTime = buildTime(2021, 3, 13);
100   - int progressStatus = ApiErrorLogProcessStatusEnum.INIT.getStatus();
101   -
102   - ApiErrorLogDO infApiErrorLogDO = RandomUtils.randomPojo(ApiErrorLogDO.class, logDO -> {
103   - logDO.setUserId(userId);
104   - logDO.setUserType(userType);
105   - logDO.setApplicationName(applicationName);
106   - logDO.setRequestUrl(requestUrl);
107   - logDO.setExceptionTime(beginTime);
108   - logDO.setProcessStatus(progressStatus);
  83 + // mock 数据
  84 + ApiErrorLogDO apiErrorLogDO = randomPojo(ApiErrorLogDO.class, o -> {
  85 + o.setUserId(2233L);
  86 + o.setUserType(UserTypeEnum.ADMIN.getValue());
  87 + o.setApplicationName("yudao-test");
  88 + o.setRequestUrl("foo");
  89 + o.setExceptionTime(buildTime(2021, 3, 13));
  90 + o.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
109 91 });
110   - infApiErrorLogMapper.insert(infApiErrorLogDO);
111   -
112   - // 下面几个都是不匹配的数据
113   - // userId 不同的
114   - infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setUserId(3344L)));
115   - // userType
116   - infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue())));
117   - // applicationName 不同的
118   - infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setApplicationName("test")));
119   - // requestUrl 不同的
120   - infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setRequestUrl("bar")));
121   - // 构造一个早期时间 2021-02-06 00:00:00
122   - infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setExceptionTime(buildTime(2021, 2, 6))));
123   - // progressStatus 不同的
124   - infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus())));
125   -
126   - // 构造调用参数
  92 + apiErrorLogMapper.insert(apiErrorLogDO);
  93 + // 测试 userId 不匹配
  94 + apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, o -> o.setUserId(3344L)));
  95 + // 测试 userType 不匹配
  96 + apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue())));
  97 + // 测试 applicationName 不匹配
  98 + apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setApplicationName("test")));
  99 + // 测试 requestUrl 不匹配
  100 + apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setRequestUrl("bar")));
  101 + // 测试 exceptionTime 不匹配:构造一个早期时间 2021-02-06 00:00:00
  102 + apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setExceptionTime(buildTime(2021, 2, 6))));
  103 + // 测试 progressStatus 不匹配
  104 + apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus())));
  105 + // 准备参数
127 106 ApiErrorLogExportReqVO reqVO = new ApiErrorLogExportReqVO();
128   - reqVO.setUserId(userId);
129   - reqVO.setUserType(userType);
130   - reqVO.setApplicationName(applicationName);
131   - reqVO.setRequestUrl(requestUrl);
132   - reqVO.setExceptionTime((new LocalDateTime[]{buildTime(2021, 3, 12),buildTime(2021, 3, 14)}));
133   - reqVO.setProcessStatus(progressStatus);
134   -
135   - // 调用service方法
136   - List<ApiErrorLogDO> list = apiErrorLogService.getApiErrorLogList(reqVO);
  107 + reqVO.setUserId(2233L);
  108 + reqVO.setUserType(UserTypeEnum.ADMIN.getValue());
  109 + reqVO.setApplicationName("yudao-test");
  110 + reqVO.setRequestUrl("foo");
  111 + reqVO.setExceptionTime(buildBetweenTime(2021, 3, 1, 2021, 3, 31));
  112 + reqVO.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
137 113  
  114 + // 调用
  115 + List<ApiErrorLogDO> list = apiErrorLogService.getApiErrorLogList(reqVO);
138 116 // 断言,只查到了一条符合条件的
139 117 assertEquals(1, list.size());
140   - assertPojoEquals(infApiErrorLogDO, list.get(0));
  118 + assertPojoEquals(apiErrorLogDO, list.get(0));
141 119 }
142 120  
143   -
144   - // TODO 芋艿:单元测试,可以拆小一点
145 121 @Test
146   - public void testUpdateApiErrorLogProcess() {
147   - // 先构造两条数据,第一条用于抛出异常,第二条用于正常的执行update操作
148   - Long processUserId = 2233L;
149   -
150   - ApiErrorLogDO first = RandomUtils.randomPojo(ApiErrorLogDO.class, logDO -> {
151   - logDO.setProcessUserId(processUserId);
152   - logDO.setUserType(UserTypeEnum.ADMIN.getValue());
153   - logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus());
154   - });
155   - infApiErrorLogMapper.insert(first);
  122 + public void testCreateApiErrorLog() {
  123 + // 准备参数
  124 + ApiErrorLogCreateReqDTO createDTO = randomPojo(ApiErrorLogCreateReqDTO.class);
156 125  
157   - ApiErrorLogDO second = RandomUtils.randomPojo(ApiErrorLogDO.class, logDO -> {
158   - logDO.setProcessUserId(1122L);
159   - logDO.setUserType(UserTypeEnum.ADMIN.getValue());
160   - logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
161   - });
162   - infApiErrorLogMapper.insert(second);
163   -
164   - Long firstId = first.getId();
165   - Long secondId = second.getId();
166   -
167   - // 执行正常的 update 操作
168   - apiErrorLogService.updateApiErrorLogProcess(secondId, ApiErrorLogProcessStatusEnum.DONE.getStatus(), processUserId);
169   - ApiErrorLogDO secondSelect = infApiErrorLogMapper.selectOne("id", secondId);
170   -
171   - // id 为 0 查询不到,应该抛出异常 API_ERROR_LOG_NOT_FOUND
172   - assertServiceException(() -> apiErrorLogService.updateApiErrorLogProcess(0L, ApiErrorLogProcessStatusEnum.DONE.getStatus(), processUserId), API_ERROR_LOG_NOT_FOUND);
173   - // id 为 first 的 progressStatus 为 DONE ,应该抛出 API_ERROR_LOG_PROCESSED
174   - assertServiceException(() -> apiErrorLogService.updateApiErrorLogProcess(firstId, ApiErrorLogProcessStatusEnum.DONE.getStatus(), processUserId), API_ERROR_LOG_PROCESSED);
175   - // 验证 progressStatus 是否修改成功
176   - Assertions.assertEquals(ApiErrorLogProcessStatusEnum.DONE.getStatus(), secondSelect.getProcessStatus());
177   - // 验证 progressUserId 是否修改成功
178   - Assertions.assertEquals(processUserId, secondSelect.getProcessUserId());
  126 + // 调用
  127 + apiErrorLogService.createApiErrorLog(createDTO);
  128 + // 断言
  129 + ApiErrorLogDO apiErrorLogDO = apiErrorLogMapper.selectOne(null);
  130 + assertPojoEquals(createDTO, apiErrorLogDO);
  131 + assertEquals(ApiErrorLogProcessStatusEnum.INIT.getStatus(), apiErrorLogDO.getProcessStatus());
179 132 }
180 133  
181 134 @Test
182   - public void testCreateApiErrorLogAsync() {
  135 + public void testUpdateApiErrorLogProcess_success() {
183 136 // 准备参数
184   - ApiErrorLogCreateReqDTO createDTO = RandomUtils.randomPojo(ApiErrorLogCreateReqDTO.class,
185   - dto -> dto.setUserType(RandomUtil.randomEle(UserTypeEnum.values()).getValue()));
  137 + ApiErrorLogDO apiErrorLogDO = randomPojo(ApiErrorLogDO.class,
  138 + o -> o.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus()));
  139 + apiErrorLogMapper.insert(apiErrorLogDO);
  140 + // 准备参数
  141 + Long id = apiErrorLogDO.getId();
  142 + Integer processStatus = randomEle(ApiErrorLogProcessStatusEnum.values()).getStatus();
  143 + Long processUserId = randomLongId();
186 144  
187 145 // 调用
188   - apiErrorLogService.createApiErrorLog(createDTO);
  146 + apiErrorLogService.updateApiErrorLogProcess(id, processStatus, processUserId);
189 147 // 断言
190   - ApiErrorLogDO infApiErrorLogDO = infApiErrorLogMapper.selectOne(null);
191   - assertNotNull(infApiErrorLogDO);
192   - assertPojoEquals(createDTO, infApiErrorLogDO);
  148 + ApiErrorLogDO dbApiErrorLogDO = apiErrorLogMapper.selectById(apiErrorLogDO.getId());
  149 + assertEquals(processStatus, dbApiErrorLogDO.getProcessStatus());
  150 + assertEquals(processUserId, dbApiErrorLogDO.getProcessUserId());
  151 + assertNotNull(dbApiErrorLogDO.getProcessTime());
  152 + }
  153 +
  154 + @Test
  155 + public void testUpdateApiErrorLogProcess_processed() {
  156 + // 准备参数
  157 + ApiErrorLogDO apiErrorLogDO = randomPojo(ApiErrorLogDO.class,
  158 + o -> o.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus()));
  159 + apiErrorLogMapper.insert(apiErrorLogDO);
  160 + // 准备参数
  161 + Long id = apiErrorLogDO.getId();
  162 + Integer processStatus = randomEle(ApiErrorLogProcessStatusEnum.values()).getStatus();
  163 + Long processUserId = randomLongId();
  164 +
  165 + // 调用,并断言异常
  166 + assertServiceException(() ->
  167 + apiErrorLogService.updateApiErrorLogProcess(id, processStatus, processUserId),
  168 + API_ERROR_LOG_PROCESSED);
  169 + }
  170 +
  171 + @Test
  172 + public void testUpdateApiErrorLogProcess_notFound() {
  173 + // 准备参数
  174 + Long id = randomLongId();
  175 + Integer processStatus = randomEle(ApiErrorLogProcessStatusEnum.values()).getStatus();
  176 + Long processUserId = randomLongId();
  177 +
  178 + // 调用,并断言异常
  179 + assertServiceException(() ->
  180 + apiErrorLogService.updateApiErrorLogProcess(id, processStatus, processUserId),
  181 + API_ERROR_LOG_NOT_FOUND);
193 182 }
194 183  
195 184 }
... ...
yudao-module-infra/yudao-module-infra-biz/src/test/resources/sql/create_tables.sql
1 1  
2 2 CREATE TABLE IF NOT EXISTS "infra_config" (
3   - "id" bigint(20) NOT NULL GENERATED BY DEFAULT AS IDENTITY,
  3 + "id" bigint(20) NOT NULL GENERATED BY DEFAULT AS IDENTITY COMMENT '编号',
4 4 "category" varchar(50) NOT NULL,
5 5 "type" tinyint NOT NULL,
6   - "name" varchar(100) NOT NULL DEFAULT '',
  6 + "name" varchar(100) NOT NULL DEFAULT '' COMMENT '名字',
7 7 "config_key" varchar(100) NOT NULL DEFAULT '',
8 8 "value" varchar(500) NOT NULL DEFAULT '',
9 9 "visible" bit NOT NULL,
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/user/UserProfileController.java
... ... @@ -2,7 +2,6 @@ package cn.iocoder.yudao.module.system.controller.admin.user;
2 2  
3 3 import cn.hutool.core.collection.CollUtil;
4 4 import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
5   -import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
6 5 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
7 6 import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
8 7 import cn.iocoder.yudao.module.system.controller.admin.user.vo.profile.UserProfileRespVO;
... ... @@ -31,6 +30,7 @@ import javax.annotation.Resource;
31 30 import javax.validation.Valid;
32 31 import java.util.List;
33 32  
  33 +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
34 34 import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
35 35 import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
36 36 import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_IS_EMPTY;
... ... @@ -99,7 +99,7 @@ public class UserProfileController {
99 99 @ApiOperation("上传用户个人头像")
100 100 public CommonResult<String> updateUserAvatar(@RequestParam("avatarFile") MultipartFile file) throws Exception {
101 101 if (file.isEmpty()) {
102   - throw ServiceExceptionUtil.exception(FILE_IS_EMPTY);
  102 + throw exception(FILE_IS_EMPTY);
103 103 }
104 104 String avatar = userService.updateUserAvatar(getLoginUserId(), file.getInputStream());
105 105 return success(avatar);
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/permission/MenuServiceImpl.java
... ... @@ -2,7 +2,6 @@ package cn.iocoder.yudao.module.system.service.permission;
2 2  
3 3 import cn.hutool.core.collection.CollUtil;
4 4 import cn.hutool.core.util.StrUtil;
5   -import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
6 5 import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
7 6 import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuCreateReqVO;
8 7 import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuListReqVO;
... ... @@ -31,6 +30,7 @@ import javax.annotation.Resource;
31 30 import java.util.*;
32 31 import java.util.stream.Collectors;
33 32  
  33 +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
34 34 import static cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO.ID_ROOT;
35 35 import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
36 36  
... ... @@ -118,7 +118,7 @@ public class MenuServiceImpl implements MenuService {
118 118 public void updateMenu(MenuUpdateReqVO reqVO) {
119 119 // 校验更新的菜单是否存在
120 120 if (menuMapper.selectById(reqVO.getId()) == null) {
121   - throw ServiceExceptionUtil.exception(MENU_NOT_EXISTS);
  121 + throw exception(MENU_NOT_EXISTS);
122 122 }
123 123 // 校验父菜单存在
124 124 validateParentMenu(reqVO.getParentId(), reqVO.getId());
... ... @@ -138,11 +138,11 @@ public class MenuServiceImpl implements MenuService {
138 138 public void deleteMenu(Long menuId) {
139 139 // 校验是否还有子菜单
140 140 if (menuMapper.selectCountByParentId(menuId) > 0) {
141   - throw ServiceExceptionUtil.exception(MENU_EXISTS_CHILDREN);
  141 + throw exception(MENU_EXISTS_CHILDREN);
142 142 }
143 143 // 校验删除的菜单是否存在
144 144 if (menuMapper.selectById(menuId) == null) {
145   - throw ServiceExceptionUtil.exception(MENU_NOT_EXISTS);
  145 + throw exception(MENU_NOT_EXISTS);
146 146 }
147 147 // 标记删除
148 148 menuMapper.deleteById(menuId);
... ... @@ -229,17 +229,17 @@ public class MenuServiceImpl implements MenuService {
229 229 }
230 230 // 不能设置自己为父菜单
231 231 if (parentId.equals(childId)) {
232   - throw ServiceExceptionUtil.exception(MENU_PARENT_ERROR);
  232 + throw exception(MENU_PARENT_ERROR);
233 233 }
234 234 MenuDO menu = menuMapper.selectById(parentId);
235 235 // 父菜单不存在
236 236 if (menu == null) {
237   - throw ServiceExceptionUtil.exception(MENU_PARENT_NOT_EXISTS);
  237 + throw exception(MENU_PARENT_NOT_EXISTS);
238 238 }
239 239 // 父菜单必须是目录或者菜单类型
240 240 if (!MenuTypeEnum.DIR.getType().equals(menu.getType())
241 241 && !MenuTypeEnum.MENU.getType().equals(menu.getType())) {
242   - throw ServiceExceptionUtil.exception(MENU_PARENT_NOT_DIR_OR_MENU);
  242 + throw exception(MENU_PARENT_NOT_DIR_OR_MENU);
243 243 }
244 244 }
245 245  
... ... @@ -260,10 +260,10 @@ public class MenuServiceImpl implements MenuService {
260 260 }
261 261 // 如果 id 为空,说明不用比较是否为相同 id 的菜单
262 262 if (id == null) {
263   - throw ServiceExceptionUtil.exception(MENU_NAME_DUPLICATE);
  263 + throw exception(MENU_NAME_DUPLICATE);
264 264 }
265 265 if (!menu.getId().equals(id)) {
266   - throw ServiceExceptionUtil.exception(MENU_NAME_DUPLICATE);
  266 + throw exception(MENU_NAME_DUPLICATE);
267 267 }
268 268 }
269 269  
... ...
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/tenant/TenantServiceImplTest.java
... ... @@ -34,6 +34,7 @@ import java.util.Collections;
34 34 import java.util.List;
35 35  
36 36 import static cn.iocoder.yudao.framework.common.util.collection.SetUtils.asSet;
  37 +import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
37 38 import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
38 39 import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
39 40 import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
... ... @@ -310,7 +311,7 @@ public class TenantServiceImplTest extends BaseDbUnitTest {
310 311 reqVO.setContactName("艿");
311 312 reqVO.setContactMobile("1560");
312 313 reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
313   - reqVO.setCreateTime(new LocalDateTime[]{buildTime(2020, 12, 1),buildTime(2020, 12, 24)});
  314 + reqVO.setCreateTime(buildBetweenTime(2020, 12, 1, 2020, 12, 24));
314 315  
315 316 // 调用
316 317 PageResult<TenantDO> pageResult = tenantService.getTenantPage(reqVO);
... ... @@ -347,7 +348,7 @@ public class TenantServiceImplTest extends BaseDbUnitTest {
347 348 reqVO.setContactName("艿");
348 349 reqVO.setContactMobile("1560");
349 350 reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
350   - reqVO.setCreateTime(new LocalDateTime[]{buildTime(2020, 12, 1),buildTime(2020, 12, 24)});
  351 + reqVO.setCreateTime(buildBetweenTime(2020, 12, 1, 2020, 12, 24));
351 352  
352 353 // 调用
353 354 List<TenantDO> list = tenantService.getTenantList(reqVO);
... ... @@ -356,7 +357,6 @@ public class TenantServiceImplTest extends BaseDbUnitTest {
356 357 assertPojoEquals(dbTenant, list.get(0));
357 358 }
358 359  
359   -
360 360 @Test
361 361 public void testGetTenantByName() {
362 362 // mock 数据
... ...