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