mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-25 16:35:06 +08:00
Merge branch 'master' of https://gitee.com/zhijiantianya/ruoyi-vue-pro into master_unit-sql
# Conflicts: # yudao-module-system/yudao-module-system-biz/src/test/resources/sql/clean.sql
This commit is contained in:
@ -111,6 +111,12 @@
|
||||
<groupId>cn.iocoder.boot</groupId>
|
||||
<artifactId>yudao-spring-boot-starter-file</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- WebSocket -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
@ -1,6 +1,5 @@
|
||||
package cn.iocoder.yudao.module.infra.controller.admin.config;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
|
||||
@ -8,8 +7,8 @@ import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.config.vo.*;
|
||||
import cn.iocoder.yudao.module.infra.convert.config.ConfigConvert;
|
||||
import cn.iocoder.yudao.module.infra.dal.dataobject.config.ConfigDO;
|
||||
import cn.iocoder.yudao.module.infra.service.config.ConfigService;
|
||||
import cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants;
|
||||
import cn.iocoder.yudao.module.infra.service.config.ConfigService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiImplicitParam;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
@ -23,6 +22,7 @@ import javax.validation.Valid;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
|
||||
|
||||
@ -75,8 +75,8 @@ public class ConfigController {
|
||||
if (config == null) {
|
||||
return null;
|
||||
}
|
||||
if (config.getVisible()) {
|
||||
throw ServiceExceptionUtil.exception(ErrorCodeConstants.CONFIG_GET_VALUE_ERROR_IF_VISIBLE);
|
||||
if (!config.getVisible()) {
|
||||
throw exception(ErrorCodeConstants.CONFIG_GET_VALUE_ERROR_IF_VISIBLE);
|
||||
}
|
||||
return success(config.getValue());
|
||||
}
|
||||
@ -93,8 +93,8 @@ public class ConfigController {
|
||||
@ApiOperation("导出参数配置")
|
||||
@PreAuthorize("@ss.hasPermission('infra:config:export')")
|
||||
@OperateLog(type = EXPORT)
|
||||
public void exportSysConfig(@Valid ConfigExportReqVO reqVO,
|
||||
HttpServletResponse response) throws IOException {
|
||||
public void exportConfig(@Valid ConfigExportReqVO reqVO,
|
||||
HttpServletResponse response) throws IOException {
|
||||
List<ConfigDO> list = configService.getConfigList(reqVO);
|
||||
// 拼接数据
|
||||
List<ConfigExcelVO> datas = ConfigConvert.INSTANCE.convertList(list);
|
||||
|
@ -11,9 +11,11 @@ import cn.iocoder.yudao.module.infra.dal.dataobject.codegen.CodegenColumnDO;
|
||||
import cn.iocoder.yudao.module.infra.dal.dataobject.codegen.CodegenTableDO;
|
||||
import com.baomidou.mybatisplus.generator.config.po.TableField;
|
||||
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
|
||||
import org.apache.ibatis.type.JdbcType;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.Mappings;
|
||||
import org.mapstruct.Named;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
import java.util.List;
|
||||
@ -37,7 +39,7 @@ public interface CodegenConvert {
|
||||
|
||||
@Mappings({
|
||||
@Mapping(source = "name", target = "columnName"),
|
||||
@Mapping(source = "type", target = "dataType"),
|
||||
@Mapping(source = "metaInfo.jdbcType", target = "dataType", qualifiedByName = "getDataType"),
|
||||
@Mapping(source = "comment", target = "columnComment"),
|
||||
@Mapping(source = "metaInfo.nullable", target = "nullable"),
|
||||
@Mapping(source = "keyFlag", target = "primaryKey"),
|
||||
@ -47,6 +49,11 @@ public interface CodegenConvert {
|
||||
})
|
||||
CodegenColumnDO convert(TableField bean);
|
||||
|
||||
@Named("getDataType")
|
||||
default String getDataType(JdbcType jdbcType) {
|
||||
return jdbcType.name();
|
||||
}
|
||||
|
||||
// ========== CodegenTableDO 相关 ==========
|
||||
|
||||
// List<CodegenTableRespVO> convertList02(List<CodegenTableDO> list);
|
||||
|
@ -6,6 +6,7 @@ import cn.iocoder.yudao.module.infra.enums.codegen.CodegenColumnListConditionEnu
|
||||
import com.baomidou.mybatisplus.annotation.KeySequence;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.baomidou.mybatisplus.generator.config.po.TableField;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
@ -29,7 +30,7 @@ public class CodegenColumnDO extends BaseDO {
|
||||
private Long id;
|
||||
/**
|
||||
* 表编号
|
||||
*
|
||||
* <p>
|
||||
* 关联 {@link CodegenTableDO#getId()}
|
||||
*/
|
||||
private Long tableId;
|
||||
@ -38,26 +39,38 @@ public class CodegenColumnDO extends BaseDO {
|
||||
|
||||
/**
|
||||
* 字段名
|
||||
*
|
||||
* 关联 {@link TableField#getName()}
|
||||
*/
|
||||
private String columnName;
|
||||
/**
|
||||
* 字段类型
|
||||
* 数据库字段类型
|
||||
*
|
||||
* 关联 {@link TableField.MetaInfo#getJdbcType()}
|
||||
*/
|
||||
private String dataType;
|
||||
/**
|
||||
* 字段描述
|
||||
*
|
||||
* 关联 {@link TableField#getComment()}
|
||||
*/
|
||||
private String columnComment;
|
||||
/**
|
||||
* 是否允许为空
|
||||
*
|
||||
* 关联 {@link TableField.MetaInfo#isNullable()}
|
||||
*/
|
||||
private Boolean nullable;
|
||||
/**
|
||||
* 是否主键
|
||||
*
|
||||
* 关联 {@link TableField#isKeyFlag()}
|
||||
*/
|
||||
private Boolean primaryKey;
|
||||
/**
|
||||
* 是否自增
|
||||
*
|
||||
* 关联 {@link TableField#isKeyIdentityFlag()}
|
||||
*/
|
||||
private Boolean autoIncrement;
|
||||
/**
|
||||
@ -71,15 +84,19 @@ public class CodegenColumnDO extends BaseDO {
|
||||
* Java 属性类型
|
||||
*
|
||||
* 例如说 String、Boolean 等等
|
||||
*
|
||||
* 关联 {@link TableField#getColumnType()}
|
||||
*/
|
||||
private String javaType;
|
||||
/**
|
||||
* Java 属性名
|
||||
*
|
||||
* 关联 {@link TableField#getPropertyName()}
|
||||
*/
|
||||
private String javaField;
|
||||
/**
|
||||
* 字典类型
|
||||
*
|
||||
* <p>
|
||||
* 关联 DictTypeDO 的 type 属性
|
||||
*/
|
||||
private String dictType;
|
||||
@ -104,7 +121,7 @@ public class CodegenColumnDO extends BaseDO {
|
||||
private Boolean listOperation;
|
||||
/**
|
||||
* List 查询操作的条件类型
|
||||
*
|
||||
* <p>
|
||||
* 枚举 {@link CodegenColumnListConditionEnum}
|
||||
*/
|
||||
private String listOperationCondition;
|
||||
@ -117,7 +134,7 @@ public class CodegenColumnDO extends BaseDO {
|
||||
|
||||
/**
|
||||
* 显示类型
|
||||
*
|
||||
* <p>
|
||||
* 枚举 {@link CodegenColumnHtmlTypeEnum}
|
||||
*/
|
||||
private String htmlType;
|
||||
|
@ -6,6 +6,7 @@ import cn.iocoder.yudao.module.infra.enums.codegen.CodegenSceneEnum;
|
||||
import cn.iocoder.yudao.module.infra.enums.codegen.CodegenTemplateTypeEnum;
|
||||
import com.baomidou.mybatisplus.annotation.KeySequence;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
@ -44,10 +45,14 @@ public class CodegenTableDO extends BaseDO {
|
||||
|
||||
/**
|
||||
* 表名称
|
||||
*
|
||||
* 关联 {@link TableInfo#getName()}
|
||||
*/
|
||||
private String tableName;
|
||||
/**
|
||||
* 表描述
|
||||
*
|
||||
* 关联 {@link TableInfo#getComment()}
|
||||
*/
|
||||
private String tableComment;
|
||||
/**
|
||||
|
@ -6,15 +6,7 @@ import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.file.vo.config.FileConfigPageReqVO;
|
||||
import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileConfigDO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 文件配置 Mapper
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Mapper
|
||||
public interface FileConfigMapper extends BaseMapperX<FileConfigDO> {
|
||||
|
||||
@ -26,7 +18,4 @@ public interface FileConfigMapper extends BaseMapperX<FileConfigDO> {
|
||||
.orderByDesc(FileConfigDO::getId));
|
||||
}
|
||||
|
||||
@Select("SELECT COUNT(*) FROM infra_file_config WHERE update_time > #{maxUpdateTime}")
|
||||
Long selectCountByUpdateTimeGt(LocalDateTime maxUpdateTime);
|
||||
|
||||
}
|
||||
|
@ -1,11 +1,14 @@
|
||||
package cn.iocoder.yudao.module.infra.dal.mysql.file;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.iocoder.yudao.framework.file.core.client.db.DBFileContentFrameworkDAO;
|
||||
import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileContentDO;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public class FileContentDAOImpl implements DBFileContentFrameworkDAO {
|
||||
@ -27,9 +30,11 @@ public class FileContentDAOImpl implements DBFileContentFrameworkDAO {
|
||||
|
||||
@Override
|
||||
public byte[] selectContent(Long configId, String path) {
|
||||
FileContentDO fileContentDO = fileContentMapper.selectOne(
|
||||
buildQuery(configId, path).select(FileContentDO::getContent));
|
||||
return fileContentDO != null ? fileContentDO.getContent() : null;
|
||||
List<FileContentDO> list = fileContentMapper.selectList(
|
||||
buildQuery(configId, path).select(FileContentDO::getContent).orderByDesc(FileContentDO::getId));
|
||||
return Optional.ofNullable(CollUtil.getFirst(list))
|
||||
.map(FileContentDO::getContent)
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
private LambdaQueryWrapper<FileContentDO> buildQuery(Long configId, String path) {
|
||||
|
@ -2,7 +2,7 @@ package cn.iocoder.yudao.module.infra.dal.mysql.job;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.job.vo.log.JobLogExportReqVO;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.job.vo.log.JobLogPageReqVO;
|
||||
import cn.iocoder.yudao.module.infra.dal.dataobject.job.JobLogDO;
|
||||
@ -19,24 +19,24 @@ import java.util.List;
|
||||
public interface JobLogMapper extends BaseMapperX<JobLogDO> {
|
||||
|
||||
default PageResult<JobLogDO> selectPage(JobLogPageReqVO reqVO) {
|
||||
return selectPage(reqVO, new QueryWrapperX<JobLogDO>()
|
||||
.eqIfPresent("job_id", reqVO.getJobId())
|
||||
.likeIfPresent("handler_name", reqVO.getHandlerName())
|
||||
.geIfPresent("begin_time", reqVO.getBeginTime())
|
||||
.leIfPresent("end_time", reqVO.getEndTime())
|
||||
.eqIfPresent("status", reqVO.getStatus())
|
||||
.orderByDesc("id") // ID 倒序
|
||||
return selectPage(reqVO, new LambdaQueryWrapperX<JobLogDO>()
|
||||
.eqIfPresent(JobLogDO::getJobId, reqVO.getJobId())
|
||||
.likeIfPresent(JobLogDO::getHandlerName, reqVO.getHandlerName())
|
||||
.geIfPresent(JobLogDO::getBeginTime, reqVO.getBeginTime())
|
||||
.leIfPresent(JobLogDO::getEndTime, reqVO.getEndTime())
|
||||
.eqIfPresent(JobLogDO::getStatus, reqVO.getStatus())
|
||||
.orderByDesc(JobLogDO::getId) // ID 倒序
|
||||
);
|
||||
}
|
||||
|
||||
default List<JobLogDO> selectList(JobLogExportReqVO reqVO) {
|
||||
return selectList(new QueryWrapperX<JobLogDO>()
|
||||
.eqIfPresent("job_id", reqVO.getJobId())
|
||||
.likeIfPresent("handler_name", reqVO.getHandlerName())
|
||||
.geIfPresent("begin_time", reqVO.getBeginTime())
|
||||
.leIfPresent("end_time", reqVO.getEndTime())
|
||||
.eqIfPresent("status", reqVO.getStatus())
|
||||
.orderByDesc("id") // ID 倒序
|
||||
return selectList(new LambdaQueryWrapperX<JobLogDO>()
|
||||
.eqIfPresent(JobLogDO::getJobId, reqVO.getJobId())
|
||||
.likeIfPresent(JobLogDO::getHandlerName, reqVO.getHandlerName())
|
||||
.geIfPresent(JobLogDO::getBeginTime, reqVO.getBeginTime())
|
||||
.leIfPresent(JobLogDO::getEndTime, reqVO.getEndTime())
|
||||
.eqIfPresent(JobLogDO::getStatus, reqVO.getStatus())
|
||||
.orderByDesc(JobLogDO::getId) // ID 倒序
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -23,7 +23,7 @@ public class FileConfigRefreshConsumer extends AbstractChannelMessageListener<Fi
|
||||
@Override
|
||||
public void onMessage(FileConfigRefreshMessage message) {
|
||||
log.info("[onMessage][收到 FileConfig 刷新消息]");
|
||||
fileConfigService.initFileClients();
|
||||
fileConfigService.initLocalCache();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -76,7 +76,7 @@ public class CodegenServiceImpl implements CodegenService {
|
||||
|
||||
private Long createCodegen0(Long userId, Long dataSourceConfigId, TableInfo tableInfo) {
|
||||
// 校验导入的表和字段非空
|
||||
checkTableInfo(tableInfo);
|
||||
validateTableInfo(tableInfo);
|
||||
// 校验是否已经存在
|
||||
if (codegenTableMapper.selectByTableNameAndDataSourceConfigId(tableInfo.getName(),
|
||||
dataSourceConfigId) != null) {
|
||||
@ -100,7 +100,7 @@ public class CodegenServiceImpl implements CodegenService {
|
||||
return table.getId();
|
||||
}
|
||||
|
||||
private void checkTableInfo(TableInfo tableInfo) {
|
||||
private void validateTableInfo(TableInfo tableInfo) {
|
||||
if (tableInfo == null) {
|
||||
throw exception(CODEGEN_IMPORT_TABLE_NULL);
|
||||
}
|
||||
@ -149,7 +149,7 @@ public class CodegenServiceImpl implements CodegenService {
|
||||
|
||||
private void syncCodegen0(Long tableId, TableInfo tableInfo) {
|
||||
// 校验导入的表和字段非空
|
||||
checkTableInfo(tableInfo);
|
||||
validateTableInfo(tableInfo);
|
||||
List<TableField> tableFields = tableInfo.getFields();
|
||||
|
||||
// 构建 CodegenColumnDO 数组,只同步新增的字段
|
||||
@ -237,10 +237,6 @@ public class CodegenServiceImpl implements CodegenService {
|
||||
@Override
|
||||
public List<DatabaseTableRespVO> getDatabaseTableList(Long dataSourceConfigId, String name, String comment) {
|
||||
List<TableInfo> tables = databaseTableService.getTableList(dataSourceConfigId, name, comment);
|
||||
// 移除置顶前缀的表名 // TODO 未来做成可配置
|
||||
tables.removeIf(table -> table.getName().toUpperCase().startsWith("QRTZ_"));
|
||||
tables.removeIf(table -> table.getName().toUpperCase().startsWith("ACT_"));
|
||||
tables.removeIf(table -> table.getName().toUpperCase().startsWith("FLW_"));
|
||||
// 移除已经生成的表
|
||||
// 移除在 Codegen 中,已经存在的
|
||||
Set<String> existsTables = CollectionUtils.convertSet(
|
||||
|
@ -9,7 +9,6 @@ import cn.iocoder.yudao.module.infra.controller.admin.config.vo.ConfigUpdateReqV
|
||||
import cn.iocoder.yudao.module.infra.convert.config.ConfigConvert;
|
||||
import cn.iocoder.yudao.module.infra.dal.dataobject.config.ConfigDO;
|
||||
import cn.iocoder.yudao.module.infra.dal.mysql.config.ConfigMapper;
|
||||
import cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants;
|
||||
import cn.iocoder.yudao.module.infra.enums.config.ConfigTypeEnum;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -20,6 +19,7 @@ import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.*;
|
||||
|
||||
/**
|
||||
* 参数配置 Service 实现类
|
||||
@ -35,7 +35,7 @@ public class ConfigServiceImpl implements ConfigService {
|
||||
@Override
|
||||
public Long createConfig(ConfigCreateReqVO reqVO) {
|
||||
// 校验正确性
|
||||
checkCreateOrUpdate(null, reqVO.getKey());
|
||||
validateConfigForCreateOrUpdate(null, reqVO.getKey());
|
||||
// 插入参数配置
|
||||
ConfigDO config = ConfigConvert.INSTANCE.convert(reqVO);
|
||||
config.setType(ConfigTypeEnum.CUSTOM.getType());
|
||||
@ -46,19 +46,19 @@ public class ConfigServiceImpl implements ConfigService {
|
||||
@Override
|
||||
public void updateConfig(ConfigUpdateReqVO reqVO) {
|
||||
// 校验正确性
|
||||
checkCreateOrUpdate(reqVO.getId(), null); // 不允许更新 key
|
||||
validateConfigForCreateOrUpdate(reqVO.getId(), null); // 不允许更新 key
|
||||
// 更新参数配置
|
||||
ConfigDO updateObj = ConfigConvert.INSTANCE.convert(reqVO);
|
||||
configMapper.updateById(updateObj);;
|
||||
configMapper.updateById(updateObj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteConfig(Long id) {
|
||||
// 校验配置存在
|
||||
ConfigDO config = checkConfigExists(id);
|
||||
ConfigDO config = validateConfigExists(id);
|
||||
// 内置配置,不允许删除
|
||||
if (ConfigTypeEnum.SYSTEM.getType().equals(config.getType())) {
|
||||
throw exception(ErrorCodeConstants.CONFIG_CAN_NOT_DELETE_SYSTEM_TYPE);
|
||||
throw exception(CONFIG_CAN_NOT_DELETE_SYSTEM_TYPE);
|
||||
}
|
||||
// 删除
|
||||
configMapper.deleteById(id);
|
||||
@ -84,39 +84,39 @@ public class ConfigServiceImpl implements ConfigService {
|
||||
return configMapper.selectList(reqVO);
|
||||
}
|
||||
|
||||
private void checkCreateOrUpdate(Long id, String key) {
|
||||
private void validateConfigForCreateOrUpdate(Long id, String key) {
|
||||
// 校验自己存在
|
||||
checkConfigExists(id);
|
||||
validateConfigExists(id);
|
||||
// 校验参数配置 key 的唯一性
|
||||
if (StrUtil.isNotEmpty(key)) {
|
||||
checkConfigKeyUnique(id, key);
|
||||
validateConfigKeyUnique(id, key);
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public ConfigDO checkConfigExists(Long id) {
|
||||
public ConfigDO validateConfigExists(Long id) {
|
||||
if (id == null) {
|
||||
return null;
|
||||
}
|
||||
ConfigDO config = configMapper.selectById(id);
|
||||
if (config == null) {
|
||||
throw exception(ErrorCodeConstants.CONFIG_NOT_EXISTS);
|
||||
throw exception(CONFIG_NOT_EXISTS);
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void checkConfigKeyUnique(Long id, String key) {
|
||||
public void validateConfigKeyUnique(Long id, String key) {
|
||||
ConfigDO config = configMapper.selectByKey(key);
|
||||
if (config == null) {
|
||||
return;
|
||||
}
|
||||
// 如果 id 为空,说明不用比较是否为相同 id 的参数配置
|
||||
if (id == null) {
|
||||
throw exception(ErrorCodeConstants.CONFIG_KEY_DUPLICATE);
|
||||
throw exception(CONFIG_KEY_DUPLICATE);
|
||||
}
|
||||
if (!config.getId().equals(id)) {
|
||||
throw exception(ErrorCodeConstants.CONFIG_KEY_DUPLICATE);
|
||||
throw exception(CONFIG_KEY_DUPLICATE);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -37,7 +37,7 @@ public class DataSourceConfigServiceImpl implements DataSourceConfigService {
|
||||
@Override
|
||||
public Long createDataSourceConfig(DataSourceConfigCreateReqVO createReqVO) {
|
||||
DataSourceConfigDO dataSourceConfig = DataSourceConfigConvert.INSTANCE.convert(createReqVO);
|
||||
checkConnectionOK(dataSourceConfig);
|
||||
validateConnectionOK(dataSourceConfig);
|
||||
|
||||
// 插入
|
||||
dataSourceConfigMapper.insert(dataSourceConfig);
|
||||
@ -50,7 +50,7 @@ public class DataSourceConfigServiceImpl implements DataSourceConfigService {
|
||||
// 校验存在
|
||||
validateDataSourceConfigExists(updateReqVO.getId());
|
||||
DataSourceConfigDO updateObj = DataSourceConfigConvert.INSTANCE.convert(updateReqVO);
|
||||
checkConnectionOK(updateObj);
|
||||
validateConnectionOK(updateObj);
|
||||
|
||||
// 更新
|
||||
dataSourceConfigMapper.updateById(updateObj);
|
||||
@ -88,7 +88,7 @@ public class DataSourceConfigServiceImpl implements DataSourceConfigService {
|
||||
return result;
|
||||
}
|
||||
|
||||
private void checkConnectionOK(DataSourceConfigDO config) {
|
||||
private void validateConnectionOK(DataSourceConfigDO config) {
|
||||
boolean success = JdbcUtils.isConnectionOK(config.getUrl(), config.getUsername(), config.getPassword());
|
||||
if (!success) {
|
||||
throw exception(DATA_SOURCE_CONFIG_NOT_OK);
|
||||
|
@ -41,7 +41,7 @@ public class DatabaseTableServiceImpl implements DatabaseTableService {
|
||||
return CollUtil.getFirst(getTableList0(dataSourceConfigId, name));
|
||||
}
|
||||
|
||||
public List<TableInfo> getTableList0(Long dataSourceConfigId, String name) {
|
||||
private List<TableInfo> getTableList0(Long dataSourceConfigId, String name) {
|
||||
// 获得数据源配置
|
||||
DataSourceConfigDO config = dataSourceConfigService.getDataSourceConfig(dataSourceConfigId);
|
||||
Assert.notNull(config, "数据源({}) 不存在!", dataSourceConfigId);
|
||||
@ -52,7 +52,11 @@ public class DatabaseTableServiceImpl implements DatabaseTableService {
|
||||
StrategyConfig.Builder strategyConfig = new StrategyConfig.Builder();
|
||||
if (StrUtil.isNotEmpty(name)) {
|
||||
strategyConfig.addInclude(name);
|
||||
} else {
|
||||
// 移除工作流和定时任务前缀的表名 // TODO 未来做成可配置
|
||||
strategyConfig.addExclude("ACT_[\\S\\s]+|QRTZ_[\\S\\s]+|FLW_[\\S\\s]+");
|
||||
}
|
||||
|
||||
GlobalConfig globalConfig = new GlobalConfig.Builder().dateType(DateType.TIME_PACK).build(); // 只使用 Date 类型,不使用 LocalDate
|
||||
ConfigBuilder builder = new ConfigBuilder(null, dataSourceConfig, strategyConfig.build(),
|
||||
null, globalConfig, null);
|
||||
|
@ -8,8 +8,6 @@ import cn.iocoder.yudao.module.infra.controller.admin.file.vo.config.FileConfigU
|
||||
import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileConfigDO;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 文件配置 Service 接口
|
||||
@ -21,7 +19,7 @@ public interface FileConfigService {
|
||||
/**
|
||||
* 初始化文件客户端
|
||||
*/
|
||||
void initFileClients();
|
||||
void initLocalCache();
|
||||
|
||||
/**
|
||||
* 创建文件配置
|
||||
@ -60,14 +58,6 @@ public interface FileConfigService {
|
||||
*/
|
||||
FileConfigDO getFileConfig(Long id);
|
||||
|
||||
/**
|
||||
* 获得文件配置列表
|
||||
*
|
||||
* @param ids 编号
|
||||
* @return 文件配置列表
|
||||
*/
|
||||
List<FileConfigDO> getFileConfigList(Collection<Long> ids);
|
||||
|
||||
/**
|
||||
* 获得文件配置分页
|
||||
*
|
||||
|
@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.infra.service.file;
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
|
||||
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
||||
import cn.iocoder.yudao.framework.common.util.validation.ValidationUtils;
|
||||
import cn.iocoder.yudao.framework.file.core.client.FileClient;
|
||||
@ -19,7 +18,6 @@ import cn.iocoder.yudao.module.infra.dal.mysql.file.FileConfigMapper;
|
||||
import cn.iocoder.yudao.module.infra.mq.producer.file.FileConfigProducer;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.transaction.support.TransactionSynchronization;
|
||||
@ -29,8 +27,6 @@ import org.springframework.validation.annotation.Validated;
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import javax.validation.Validator;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@ -48,18 +44,6 @@ import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_CONFIG
|
||||
@Slf4j
|
||||
public class FileConfigServiceImpl implements FileConfigService {
|
||||
|
||||
/**
|
||||
* 定时执行 {@link #schedulePeriodicRefresh()} 的周期
|
||||
* 因为已经通过 Redis Pub/Sub 机制,所以频率不需要高
|
||||
*/
|
||||
private static final long SCHEDULER_PERIOD = 5 * 60 * 1000L;
|
||||
|
||||
/**
|
||||
* 缓存菜单的最大更新时间,用于后续的增量轮询,判断是否有更新
|
||||
*/
|
||||
@Getter
|
||||
private volatile LocalDateTime maxUpdateTime;
|
||||
|
||||
@Resource
|
||||
private FileClientFactory fileClientFactory;
|
||||
/**
|
||||
@ -79,34 +63,12 @@ public class FileConfigServiceImpl implements FileConfigService {
|
||||
|
||||
@Override
|
||||
@PostConstruct
|
||||
public void initFileClients() {
|
||||
initLocalCacheIfUpdate(null);
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelay = SCHEDULER_PERIOD, initialDelay = SCHEDULER_PERIOD)
|
||||
public void schedulePeriodicRefresh() {
|
||||
initLocalCacheIfUpdate(this.maxUpdateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新本地缓存
|
||||
*
|
||||
* @param maxUpdateTime 最大更新时间
|
||||
* 1. 如果 maxUpdateTime 为 null,则“强制”刷新缓存
|
||||
* 2. 如果 maxUpdateTime 不为 null,判断自 maxUpdateTime 是否有数据发生变化,有的情况下才刷新缓存
|
||||
*/
|
||||
private void initLocalCacheIfUpdate(LocalDateTime maxUpdateTime) {
|
||||
// 第一步:基于 maxUpdateTime 判断缓存是否刷新。
|
||||
// 如果没有增量的数据变化,则不进行本地缓存的刷新
|
||||
if (maxUpdateTime != null
|
||||
&& fileConfigMapper.selectCountByUpdateTimeGt(maxUpdateTime) == 0) {
|
||||
log.info("[initLocalCacheIfUpdate][数据未发生变化({}),本地缓存不刷新]", maxUpdateTime);
|
||||
return;
|
||||
}
|
||||
public void initLocalCache() {
|
||||
// 第一步:查询数据
|
||||
List<FileConfigDO> configs = fileConfigMapper.selectList();
|
||||
log.info("[initLocalCacheIfUpdate][缓存文件配置,数量为:{}]", configs.size());
|
||||
log.info("[initLocalCache][缓存文件配置,数量为:{}]", configs.size());
|
||||
|
||||
// 第二步:构建缓存。创建或更新文件 Client
|
||||
// 第二步:构建缓存:创建或更新文件 Client
|
||||
configs.forEach(config -> {
|
||||
fileClientFactory.createOrUpdateFileClient(config.getId(), config.getStorage(), config.getConfig());
|
||||
// 如果是 master,进行设置
|
||||
@ -114,9 +76,6 @@ public class FileConfigServiceImpl implements FileConfigService {
|
||||
masterFileClient = fileClientFactory.getFileClient(config.getId());
|
||||
}
|
||||
});
|
||||
|
||||
// 第三步:设置最新的 maxUpdateTime,用于下次的增量判断。
|
||||
this.maxUpdateTime = CollectionUtils.getMaxValue(configs, FileConfigDO::getUpdateTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -135,7 +94,7 @@ public class FileConfigServiceImpl implements FileConfigService {
|
||||
@Override
|
||||
public void updateFileConfig(FileConfigUpdateReqVO updateReqVO) {
|
||||
// 校验存在
|
||||
FileConfigDO config = this.validateFileConfigExists(updateReqVO.getId());
|
||||
FileConfigDO config = validateFileConfigExists(updateReqVO.getId());
|
||||
// 更新
|
||||
FileConfigDO updateObj = FileConfigConvert.INSTANCE.convert(updateReqVO)
|
||||
.setConfig(parseClientConfig(config.getStorage(), updateReqVO.getConfig()));
|
||||
@ -148,7 +107,7 @@ public class FileConfigServiceImpl implements FileConfigService {
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void updateFileConfigMaster(Long id) {
|
||||
// 校验存在
|
||||
this.validateFileConfigExists(id);
|
||||
validateFileConfigExists(id);
|
||||
// 更新其它为非 master
|
||||
fileConfigMapper.updateBatch(new FileConfigDO().setMaster(false));
|
||||
// 更新
|
||||
@ -178,7 +137,7 @@ public class FileConfigServiceImpl implements FileConfigService {
|
||||
@Override
|
||||
public void deleteFileConfig(Long id) {
|
||||
// 校验存在
|
||||
FileConfigDO config = this.validateFileConfigExists(id);
|
||||
FileConfigDO config = validateFileConfigExists(id);
|
||||
if (Boolean.TRUE.equals(config.getMaster())) {
|
||||
throw exception(FILE_CONFIG_DELETE_FAIL_MASTER);
|
||||
}
|
||||
@ -201,11 +160,6 @@ public class FileConfigServiceImpl implements FileConfigService {
|
||||
return fileConfigMapper.selectById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FileConfigDO> getFileConfigList(Collection<Long> ids) {
|
||||
return fileConfigMapper.selectBatchIds(ids);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult<FileConfigDO> getFileConfigPage(FileConfigPageReqVO pageReqVO) {
|
||||
return fileConfigMapper.selectPage(pageReqVO);
|
||||
@ -214,7 +168,7 @@ public class FileConfigServiceImpl implements FileConfigService {
|
||||
@Override
|
||||
public String testFileConfig(Long id) throws Exception {
|
||||
// 校验存在
|
||||
this.validateFileConfigExists(id);
|
||||
validateFileConfigExists(id);
|
||||
// 上传文件
|
||||
byte[] content = ResourceUtil.readBytes("file/erweima.jpg");
|
||||
return fileClientFactory.getFileClient(id).upload(content, IdUtil.fastSimpleUUID() + ".jpg", "image/jpeg");
|
||||
|
@ -69,7 +69,7 @@ public class FileServiceImpl implements FileService {
|
||||
@Override
|
||||
public void deleteFile(Long id) throws Exception {
|
||||
// 校验存在
|
||||
FileDO file = this.validateFileExists(id);
|
||||
FileDO file = validateFileExists(id);
|
||||
|
||||
// 从文件存储器中删除
|
||||
FileClient client = fileConfigService.getFileClient(file.getConfigId());
|
||||
|
@ -69,7 +69,7 @@ public class JobServiceImpl implements JobService {
|
||||
public void updateJob(JobUpdateReqVO updateReqVO) throws SchedulerException {
|
||||
validateCronExpression(updateReqVO.getCronExpression());
|
||||
// 校验存在
|
||||
JobDO job = this.validateJobExists(updateReqVO.getId());
|
||||
JobDO job = validateJobExists(updateReqVO.getId());
|
||||
// 只有开启状态,才可以修改.原因是,如果出暂停状态,修改 Quartz Job 时,会导致任务又开始执行
|
||||
if (!job.getStatus().equals(JobStatusEnum.NORMAL.getStatus())) {
|
||||
throw exception(JOB_UPDATE_ONLY_NORMAL_STATUS);
|
||||
@ -92,7 +92,7 @@ public class JobServiceImpl implements JobService {
|
||||
throw exception(JOB_CHANGE_STATUS_INVALID);
|
||||
}
|
||||
// 校验存在
|
||||
JobDO job = this.validateJobExists(id);
|
||||
JobDO job = validateJobExists(id);
|
||||
// 校验是否已经为当前状态
|
||||
if (job.getStatus().equals(status)) {
|
||||
throw exception(JOB_CHANGE_STATUS_EQUALS);
|
||||
@ -112,7 +112,7 @@ public class JobServiceImpl implements JobService {
|
||||
@Override
|
||||
public void triggerJob(Long id) throws SchedulerException {
|
||||
// 校验存在
|
||||
JobDO job = this.validateJobExists(id);
|
||||
JobDO job = validateJobExists(id);
|
||||
|
||||
// 触发 Quartz 中的 Job
|
||||
schedulerManager.triggerJob(job.getId(), job.getHandlerName(), job.getHandlerParam());
|
||||
@ -122,7 +122,7 @@ public class JobServiceImpl implements JobService {
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void deleteJob(Long id) throws SchedulerException {
|
||||
// 校验存在
|
||||
JobDO job = this.validateJobExists(id);
|
||||
JobDO job = validateJobExists(id);
|
||||
// 更新
|
||||
jobMapper.deleteById(id);
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
package cn.iocoder.yudao.module.infra.service.logger;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO;
|
||||
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
|
||||
import cn.iocoder.yudao.module.infra.convert.logger.ApiErrorLogConvert;
|
||||
import cn.iocoder.yudao.module.infra.dal.dataobject.logger.ApiErrorLogDO;
|
||||
import cn.iocoder.yudao.module.infra.dal.mysql.logger.ApiErrorLogMapper;
|
||||
import cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants;
|
||||
import cn.iocoder.yudao.module.infra.enums.logger.ApiErrorLogProcessStatusEnum;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
@ -17,6 +15,10 @@ import javax.annotation.Resource;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.API_ERROR_LOG_NOT_FOUND;
|
||||
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.API_ERROR_LOG_PROCESSED;
|
||||
|
||||
/**
|
||||
* API 错误日志 Service 实现类
|
||||
*
|
||||
@ -31,8 +33,8 @@ public class ApiErrorLogServiceImpl implements ApiErrorLogService {
|
||||
|
||||
@Override
|
||||
public void createApiErrorLog(ApiErrorLogCreateReqDTO createDTO) {
|
||||
ApiErrorLogDO apiErrorLog = ApiErrorLogConvert.INSTANCE.convert(createDTO);
|
||||
apiErrorLog.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
|
||||
ApiErrorLogDO apiErrorLog = ApiErrorLogConvert.INSTANCE.convert(createDTO)
|
||||
.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
|
||||
apiErrorLogMapper.insert(apiErrorLog);
|
||||
}
|
||||
|
||||
@ -50,10 +52,10 @@ public class ApiErrorLogServiceImpl implements ApiErrorLogService {
|
||||
public void updateApiErrorLogProcess(Long id, Integer processStatus, Long processUserId) {
|
||||
ApiErrorLogDO errorLog = apiErrorLogMapper.selectById(id);
|
||||
if (errorLog == null) {
|
||||
throw ServiceExceptionUtil.exception(ErrorCodeConstants.API_ERROR_LOG_NOT_FOUND);
|
||||
throw exception(API_ERROR_LOG_NOT_FOUND);
|
||||
}
|
||||
if (!ApiErrorLogProcessStatusEnum.INIT.getStatus().equals(errorLog.getProcessStatus())) {
|
||||
throw ServiceExceptionUtil.exception(ErrorCodeConstants.API_ERROR_LOG_PROCESSED);
|
||||
throw exception(API_ERROR_LOG_PROCESSED);
|
||||
}
|
||||
// 标记处理
|
||||
apiErrorLogMapper.updateById(ApiErrorLogDO.builder().id(id).processStatus(processStatus)
|
||||
|
@ -45,7 +45,7 @@ public class TestDemoServiceImpl implements TestDemoService {
|
||||
@CacheEvict(value = "test", key = "#updateReqVO.id")
|
||||
public void updateTestDemo(TestDemoUpdateReqVO updateReqVO) {
|
||||
// 校验存在
|
||||
this.validateTestDemoExists(updateReqVO.getId());
|
||||
validateTestDemoExists(updateReqVO.getId());
|
||||
// 更新
|
||||
TestDemoDO updateObj = TestDemoConvert.INSTANCE.convert(updateReqVO);
|
||||
testDemoMapper.updateById(updateObj);
|
||||
@ -55,7 +55,7 @@ public class TestDemoServiceImpl implements TestDemoService {
|
||||
@CacheEvict(value = "test", key = "#id")
|
||||
public void deleteTestDemo(Long id) {
|
||||
// 校验存在
|
||||
this.validateTestDemoExists(id);
|
||||
validateTestDemoExists(id);
|
||||
// 删除
|
||||
testDemoMapper.deleteById(id);
|
||||
}
|
||||
@ -79,7 +79,7 @@ public class TestDemoServiceImpl implements TestDemoService {
|
||||
|
||||
@Override
|
||||
public PageResult<TestDemoDO> getTestDemoPage(TestDemoPageReqVO pageReqVO) {
|
||||
// testDemoMapper.selectList2();
|
||||
testDemoMapper.selectList2();
|
||||
return testDemoMapper.selectPage(pageReqVO);
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,45 @@
|
||||
package cn.iocoder.yudao.module.infra.websocket;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.concurrent.Semaphore;
|
||||
|
||||
/**
|
||||
* 信号量相关处理
|
||||
*
|
||||
*/
|
||||
@Slf4j
|
||||
public class SemaphoreUtils {
|
||||
|
||||
/**
|
||||
* 获取信号量
|
||||
*
|
||||
* @param semaphore
|
||||
* @return
|
||||
*/
|
||||
public static boolean tryAcquire(Semaphore semaphore) {
|
||||
boolean flag = false;
|
||||
|
||||
try {
|
||||
flag = semaphore.tryAcquire();
|
||||
} catch (Exception e) {
|
||||
log.error("获取信号量异常", e);
|
||||
}
|
||||
|
||||
return flag;
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放信号量
|
||||
*
|
||||
* @param semaphore
|
||||
*/
|
||||
public static void release(Semaphore semaphore) {
|
||||
|
||||
try {
|
||||
semaphore.release();
|
||||
} catch (Exception e) {
|
||||
log.error("释放信号量异常", e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package cn.iocoder.yudao.module.infra.websocket;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
|
||||
|
||||
/**
|
||||
* websocket 配置
|
||||
*/
|
||||
@Configuration
|
||||
public class WebSocketConfig {
|
||||
@Bean
|
||||
public ServerEndpointExporter serverEndpointExporter() {
|
||||
return new ServerEndpointExporter();
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
package cn.iocoder.yudao.module.infra.websocket;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.websocket.*;
|
||||
import javax.websocket.server.ServerEndpoint;
|
||||
import java.util.concurrent.Semaphore;
|
||||
|
||||
/**
|
||||
* websocket 消息处理
|
||||
*/
|
||||
@Component
|
||||
@ServerEndpoint("/websocket/message")
|
||||
@Slf4j
|
||||
public class WebSocketServer {
|
||||
|
||||
/**
|
||||
* 默认最多允许同时在线用户数100
|
||||
*/
|
||||
public static int socketMaxOnlineCount = 100;
|
||||
|
||||
private static final Semaphore SOCKET_SEMAPHORE = new Semaphore(socketMaxOnlineCount);
|
||||
|
||||
/**
|
||||
* 连接建立成功调用的方法
|
||||
*/
|
||||
@OnOpen
|
||||
public void onOpen(Session session) throws Exception {
|
||||
// 尝试获取信号量
|
||||
boolean semaphoreFlag = SemaphoreUtils.tryAcquire(SOCKET_SEMAPHORE);
|
||||
if (!semaphoreFlag) {
|
||||
// 未获取到信号量
|
||||
log.error("当前在线人数超过限制数:{}", socketMaxOnlineCount);
|
||||
WebSocketUsers.sendMessage(session, "当前在线人数超过限制数:" + socketMaxOnlineCount);
|
||||
session.close();
|
||||
} else {
|
||||
String userId = WebSocketUsers.getParam("userId", session);
|
||||
if (userId != null) {
|
||||
// 添加用户
|
||||
WebSocketUsers.addSession(userId, session);
|
||||
log.info("用户【userId={}】建立连接,当前连接用户总数:{}", userId, WebSocketUsers.getUsers().size());
|
||||
WebSocketUsers.sendMessage(session, "接收内容:连接成功");
|
||||
} else {
|
||||
WebSocketUsers.sendMessage(session, "接收内容:连接失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接关闭时处理
|
||||
*/
|
||||
@OnClose
|
||||
public void onClose(Session session) {
|
||||
log.info("用户【sessionId={}】关闭连接!", session.getId());
|
||||
// 移除用户
|
||||
WebSocketUsers.removeSession(session);
|
||||
// 获取到信号量则需释放
|
||||
SemaphoreUtils.release(SOCKET_SEMAPHORE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 抛出异常时处理
|
||||
*/
|
||||
@OnError
|
||||
public void onError(Session session, Throwable exception) throws Exception {
|
||||
if (session.isOpen()) {
|
||||
// 关闭连接
|
||||
session.close();
|
||||
}
|
||||
String sessionId = session.getId();
|
||||
log.info("用户【sessionId={}】连接异常!异常信息:{}", sessionId, exception);
|
||||
// 移出用户
|
||||
WebSocketUsers.removeSession(session);
|
||||
// 获取到信号量则需释放
|
||||
SemaphoreUtils.release(SOCKET_SEMAPHORE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 收到客户端消息时调用的方法
|
||||
*/
|
||||
@OnMessage
|
||||
public void onMessage(Session session, String message) {
|
||||
WebSocketUsers.sendMessage(session, "接收内容:" + message);
|
||||
}
|
||||
}
|
@ -0,0 +1,178 @@
|
||||
package cn.iocoder.yudao.module.infra.websocket;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.bouncycastle.util.Strings;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.websocket.Session;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* websocket 客户端用户
|
||||
*/
|
||||
@Slf4j
|
||||
public class WebSocketUsers {
|
||||
|
||||
/**
|
||||
* 用户集
|
||||
* TODO 需要登录用户的session?
|
||||
*/
|
||||
private static final Map<String, Session> SESSION_MAP = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* 存储用户
|
||||
*
|
||||
* @param userId 唯一键
|
||||
* @param session 用户信息
|
||||
*/
|
||||
public static void addSession(String userId, Session session) {
|
||||
SESSION_MAP.put(userId, session);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除用户
|
||||
*
|
||||
* @param session 用户信息
|
||||
* @return 移除结果
|
||||
*/
|
||||
public static boolean removeSession(Session session) {
|
||||
String key = null;
|
||||
boolean flag = SESSION_MAP.containsValue(session);
|
||||
if (flag) {
|
||||
Set<Map.Entry<String, Session>> entries = SESSION_MAP.entrySet();
|
||||
for (Map.Entry<String, Session> entry : entries) {
|
||||
Session value = entry.getValue();
|
||||
if (value.equals(session)) {
|
||||
key = entry.getKey();
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
return removeSession(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移出用户
|
||||
*
|
||||
* @param userId 用户id
|
||||
*/
|
||||
public static boolean removeSession(String userId) {
|
||||
log.info("用户【userId={}】退出", userId);
|
||||
Session remove = SESSION_MAP.remove(userId);
|
||||
if (remove != null) {
|
||||
boolean containsValue = SESSION_MAP.containsValue(remove);
|
||||
log.info("用户【userId={}】退出{},当前连接用户总数:{}", userId, containsValue ? "失败" : "成功", SESSION_MAP.size());
|
||||
return containsValue;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取在线用户列表
|
||||
*
|
||||
* @return 返回用户集合
|
||||
*/
|
||||
public static Map<String, Session> getUsers() {
|
||||
return SESSION_MAP;
|
||||
}
|
||||
|
||||
/**
|
||||
* 向所有在线人发送消息
|
||||
*
|
||||
* @param message 消息内容
|
||||
*/
|
||||
public static void sendMessageToAll(String message) {
|
||||
SESSION_MAP.forEach((userId, session) -> {
|
||||
if (session.isOpen()) {
|
||||
sendMessage(session, message);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步发送文本消息
|
||||
*
|
||||
* @param session 用户session
|
||||
* @param message 消息内容
|
||||
*/
|
||||
public static void sendMessageAsync(Session session, String message) {
|
||||
if (session.isOpen()) {
|
||||
// TODO 需要加synchronized锁(synchronized(session))?单个session创建线程?
|
||||
session.getAsyncRemote().sendText(message);
|
||||
} else {
|
||||
log.warn("用户【session={}】不在线", session.getId());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步发送文本消息
|
||||
*
|
||||
* @param session 用户session
|
||||
* @param message 消息内容
|
||||
*/
|
||||
public static void sendMessage(Session session, String message) {
|
||||
try {
|
||||
if (session.isOpen()) {
|
||||
// TODO 需要加synchronized锁(synchronized(session))?单个session创建线程?
|
||||
session.getBasicRemote().sendText(message);
|
||||
} else {
|
||||
log.warn("用户【session={}】不在线", session.getId());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("发送消息异常", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户id发送消息
|
||||
*
|
||||
* @param userId 用户id
|
||||
* @param message 消息内容
|
||||
*/
|
||||
public static void sendMessage(String userId, String message) {
|
||||
Session session = SESSION_MAP.get(userId);
|
||||
//判断是否存在该用户的session,并且是否在线
|
||||
if (session == null || !session.isOpen()) {
|
||||
return;
|
||||
}
|
||||
sendMessage(session, message);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取session中的指定参数值
|
||||
*
|
||||
* @param key 参数key
|
||||
* @param session 用户session
|
||||
*/
|
||||
public static String getParam(@NotNull String key, Session session) {
|
||||
//TODO 目前只针对获取一个key的值,后期根据情况拓展多个 或者直接在onClose onOpen上获取参数?
|
||||
String value = null;
|
||||
Map<String, List<String>> parameters = session.getRequestParameterMap();
|
||||
if (MapUtil.isNotEmpty(parameters)) {
|
||||
value = parameters.get(key).get(0);
|
||||
} else {
|
||||
String queryString = session.getQueryString();
|
||||
if (!StrUtil.isEmpty(queryString)) {
|
||||
String[] params = Strings.split(queryString, '&');
|
||||
for (String paramPair : params) {
|
||||
String[] nameValues = Strings.split(paramPair, '=');
|
||||
if (key.equals(nameValues[0])) {
|
||||
value = nameValues[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
@ -39,7 +39,7 @@ public class ${table.className}ServiceImpl implements ${table.className}Service
|
||||
@Override
|
||||
public void update${simpleClassName}(${sceneEnum.prefixClass}${table.className}UpdateReqVO updateReqVO) {
|
||||
// 校验存在
|
||||
this.validate${simpleClassName}Exists(updateReqVO.getId());
|
||||
validate${simpleClassName}Exists(updateReqVO.getId());
|
||||
// 更新
|
||||
${table.className}DO updateObj = ${table.className}Convert.INSTANCE.convert(updateReqVO);
|
||||
${classNameVar}Mapper.updateById(updateObj);
|
||||
@ -48,7 +48,7 @@ public class ${table.className}ServiceImpl implements ${table.className}Service
|
||||
@Override
|
||||
public void delete${simpleClassName}(${primaryColumn.javaType} id) {
|
||||
// 校验存在
|
||||
this.validate${simpleClassName}Exists(id);
|
||||
validate${simpleClassName}Exists(id);
|
||||
// 删除
|
||||
${classNameVar}Mapper.deleteById(id);
|
||||
}
|
||||
|
@ -52,7 +52,7 @@ import static org.mockito.Mockito.*;
|
||||
#if (${column.listOperation})
|
||||
#set ($JavaField = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})##首字母大写
|
||||
#if (${column.listOperationCondition} == "BETWEEN")## BETWEEN 的情况
|
||||
reqVO.set${JavaField}((new LocalDateTime[]{}));
|
||||
reqVO.set${JavaField}(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
|
||||
#else
|
||||
reqVO.set$JavaField(null);
|
||||
#end
|
||||
|
@ -1,8 +1,4 @@
|
||||
import { reactive } from 'vue'
|
||||
import { useI18n } from '@/hooks/web/useI18n'
|
||||
import { DICT_TYPE } from '@/utils/dict'
|
||||
import { required } from '@/utils/formRules'
|
||||
import { VxeCrudSchema, useVxeCrudSchemas } from '@/hooks/web/useVxeCrudSchemas'
|
||||
import type { VxeCrudSchema } from '@/hooks/web/useVxeCrudSchemas'
|
||||
const { t } = useI18n() // 国际化
|
||||
// 表单校验
|
||||
export const rules = reactive({
|
||||
|
@ -1,7 +1,7 @@
|
||||
<template>
|
||||
<ContentWrap>
|
||||
<!-- 列表 -->
|
||||
<vxe-grid ref="xGrid" v-bind="gridOptions" class="xtable-scrollbar">
|
||||
<XTable @register="registerTable">
|
||||
<template #toolbar_buttons>
|
||||
<!-- 操作:新增 -->
|
||||
<XButton
|
||||
@ -17,7 +17,7 @@
|
||||
preIcon="ep:download"
|
||||
:title="t('action.export')"
|
||||
v-hasPermi="['${permissionPrefix}:export']"
|
||||
@click="handleExport()"
|
||||
@click="exportList('${table.classComment}.xls')"
|
||||
/>
|
||||
</template>
|
||||
<template #actionbtns_default="{ row }">
|
||||
@ -40,10 +40,10 @@
|
||||
preIcon="ep:delete"
|
||||
:title="t('action.del')"
|
||||
v-hasPermi="['${permissionPrefix}:delete']"
|
||||
@click="handleDelete(row.id)"
|
||||
@click="deleteData(row.id)"
|
||||
/>
|
||||
</template>
|
||||
</vxe-grid>
|
||||
</XTable>
|
||||
</ContentWrap>
|
||||
<!-- 弹窗 -->
|
||||
<XModal id="${classNameVar}Model" :loading="modelLoading" v-model="modelVisible" :title="modelTitle">
|
||||
@ -75,12 +75,6 @@
|
||||
</XModal>
|
||||
</template>
|
||||
<script setup lang="ts" name="${simpleClassName}">
|
||||
// 全局相关的 import
|
||||
import { ref, unref } from 'vue'
|
||||
import { useI18n } from '@/hooks/web/useI18n'
|
||||
import { useMessage } from '@/hooks/web/useMessage'
|
||||
import { useVxeGrid } from '@/hooks/web/useVxeGrid'
|
||||
import { VxeGridInstance } from 'vxe-table'
|
||||
import { FormExpose } from '@/components/Form'
|
||||
// 业务相关的 import
|
||||
import { rules, allSchemas } from './${classNameVar}.data'
|
||||
@ -90,8 +84,7 @@ const { t } = useI18n() // 国际化
|
||||
const message = useMessage() // 消息弹窗
|
||||
|
||||
// 列表相关的变量
|
||||
const xGrid = ref<VxeGridInstance>() // 列表 Grid Ref
|
||||
const { gridOptions, getList, deleteData, exportList } = useVxeGrid<${simpleClassName}Api.${simpleClassName}VO>({
|
||||
const [registerTable, { reload, deleteData, exportList }] = useXTable({
|
||||
allSchemas: allSchemas,
|
||||
getListApi: ${simpleClassName}Api.get${simpleClassName}PageApi,
|
||||
deleteApi: ${simpleClassName}Api.delete${simpleClassName}Api,
|
||||
@ -121,11 +114,6 @@ const handleCreate = () => {
|
||||
modelLoading.value = false
|
||||
}
|
||||
|
||||
// 导出操作
|
||||
const handleExport = async () => {
|
||||
await exportList(xGrid, '${table.classComment}.xls')
|
||||
}
|
||||
|
||||
// 修改操作
|
||||
const handleUpdate = async (rowId: number) => {
|
||||
setDialogTile('update')
|
||||
@ -143,11 +131,6 @@ const handleDetail = async (rowId: number) => {
|
||||
modelLoading.value = false
|
||||
}
|
||||
|
||||
// 删除操作
|
||||
const handleDelete = async (rowId: number) => {
|
||||
await deleteData(xGrid, rowId)
|
||||
}
|
||||
|
||||
// 提交按钮
|
||||
const submitForm = async () => {
|
||||
const elForm = unref(formRef)?.getElFormRef()
|
||||
@ -169,7 +152,7 @@ const submitForm = async () => {
|
||||
} finally {
|
||||
actionLoading.value = false
|
||||
// 刷新列表
|
||||
await getList(xGrid)
|
||||
await reload()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -1,7 +1,7 @@
|
||||
package cn.iocoder.yudao.module.infra.service;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.generator.IDatabaseQuery.DefaultDatabaseQuery;
|
||||
import com.baomidou.mybatisplus.generator.query.DefaultQuery;
|
||||
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
|
||||
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
|
||||
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
|
||||
@ -19,7 +19,7 @@ public class DefaultDatabaseQueryTest {
|
||||
|
||||
ConfigBuilder builder = new ConfigBuilder(null, dataSourceConfig, null, null, null, null);
|
||||
|
||||
DefaultDatabaseQuery query = new DefaultDatabaseQuery(builder);
|
||||
DefaultQuery query = new DefaultQuery(builder);
|
||||
|
||||
long time = System.currentTimeMillis();
|
||||
List<TableInfo> tableInfos = query.queryTables();
|
||||
|
@ -2,7 +2,6 @@ package cn.iocoder.yudao.module.infra.service.config;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils;
|
||||
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
import cn.iocoder.yudao.framework.test.core.util.RandomUtils;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.config.vo.ConfigCreateReqVO;
|
||||
@ -12,17 +11,17 @@ import cn.iocoder.yudao.module.infra.controller.admin.config.vo.ConfigUpdateReqV
|
||||
import cn.iocoder.yudao.module.infra.dal.dataobject.config.ConfigDO;
|
||||
import cn.iocoder.yudao.module.infra.dal.mysql.config.ConfigMapper;
|
||||
import cn.iocoder.yudao.module.infra.enums.config.ConfigTypeEnum;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static cn.hutool.core.util.RandomUtil.randomEle;
|
||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
|
||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
|
||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
|
||||
@ -30,7 +29,7 @@ import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@Import(ConfigServiceImpl.class)
|
||||
public class ConfigServiceTest extends BaseDbUnitTest {
|
||||
public class ConfigServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private ConfigServiceImpl configService;
|
||||
@ -50,7 +49,7 @@ public class ConfigServiceTest extends BaseDbUnitTest {
|
||||
// 校验记录的属性是否正确
|
||||
ConfigDO config = configMapper.selectById(configId);
|
||||
assertPojoEquals(reqVO, config);
|
||||
Assertions.assertEquals(ConfigTypeEnum.CUSTOM.getType(), config.getType());
|
||||
assertEquals(ConfigTypeEnum.CUSTOM.getType(), config.getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -101,40 +100,40 @@ public class ConfigServiceTest extends BaseDbUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckConfigExists_success() {
|
||||
public void testValidateConfigExists_success() {
|
||||
// mock 数据
|
||||
ConfigDO dbConfigDO = randomConfigDO();
|
||||
configMapper.insert(dbConfigDO);// @Sql: 先插入出一条存在的数据
|
||||
|
||||
// 调用成功
|
||||
configService.checkConfigExists(dbConfigDO.getId());
|
||||
configService.validateConfigExists(dbConfigDO.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckConfigExist_notExists() {
|
||||
assertServiceException(() -> configService.checkConfigExists(randomLongId()), CONFIG_NOT_EXISTS);
|
||||
public void testValidateConfigExist_notExists() {
|
||||
assertServiceException(() -> configService.validateConfigExists(randomLongId()), CONFIG_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckConfigKeyUnique_success() {
|
||||
public void testValidateConfigKeyUnique_success() {
|
||||
// 调用,成功
|
||||
configService.checkConfigKeyUnique(randomLongId(), randomString());
|
||||
configService.validateConfigKeyUnique(randomLongId(), randomString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckConfigKeyUnique_keyDuplicateForCreate() {
|
||||
public void testValidateConfigKeyUnique_keyDuplicateForCreate() {
|
||||
// 准备参数
|
||||
String key = randomString();
|
||||
// mock 数据
|
||||
configMapper.insert(randomConfigDO(o -> o.setConfigKey(key)));
|
||||
|
||||
// 调用,校验异常
|
||||
assertServiceException(() -> configService.checkConfigKeyUnique(null, key),
|
||||
assertServiceException(() -> configService.validateConfigKeyUnique(null, key),
|
||||
CONFIG_KEY_DUPLICATE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckConfigKeyUnique_keyDuplicateForUpdate() {
|
||||
public void testValidateConfigKeyUnique_keyDuplicateForUpdate() {
|
||||
// 准备参数
|
||||
Long id = randomLongId();
|
||||
String key = randomString();
|
||||
@ -142,7 +141,7 @@ public class ConfigServiceTest extends BaseDbUnitTest {
|
||||
configMapper.insert(randomConfigDO(o -> o.setConfigKey(key)));
|
||||
|
||||
// 调用,校验异常
|
||||
assertServiceException(() -> configService.checkConfigKeyUnique(id, key),
|
||||
assertServiceException(() -> configService.validateConfigKeyUnique(id, key),
|
||||
CONFIG_KEY_DUPLICATE);
|
||||
}
|
||||
|
||||
@ -157,19 +156,19 @@ public class ConfigServiceTest extends BaseDbUnitTest {
|
||||
});
|
||||
configMapper.insert(dbConfig);
|
||||
// 测试 name 不匹配
|
||||
configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setName("土豆")));
|
||||
configMapper.insert(cloneIgnoreId(dbConfig, o -> o.setName("土豆")));
|
||||
// 测试 key 不匹配
|
||||
configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setConfigKey("tudou")));
|
||||
configMapper.insert(cloneIgnoreId(dbConfig, o -> o.setConfigKey("tudou")));
|
||||
// 测试 type 不匹配
|
||||
configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setType(ConfigTypeEnum.CUSTOM.getType())));
|
||||
configMapper.insert(cloneIgnoreId(dbConfig, o -> o.setType(ConfigTypeEnum.CUSTOM.getType())));
|
||||
// 测试 createTime 不匹配
|
||||
configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setCreateTime(buildTime(2021, 1, 1))));
|
||||
configMapper.insert(cloneIgnoreId(dbConfig, o -> o.setCreateTime(buildTime(2021, 1, 1))));
|
||||
// 准备参数
|
||||
ConfigPageReqVO reqVO = new ConfigPageReqVO();
|
||||
reqVO.setName("艿");
|
||||
reqVO.setKey("nai");
|
||||
reqVO.setType(ConfigTypeEnum.SYSTEM.getType());
|
||||
reqVO.setCreateTime((new LocalDateTime[]{buildTime(2021, 1, 15),buildTime(2021, 2, 15)}));
|
||||
reqVO.setCreateTime(buildBetweenTime(2021, 1, 15, 2021, 2, 15));
|
||||
|
||||
// 调用
|
||||
PageResult<ConfigDO> pageResult = configService.getConfigPage(reqVO);
|
||||
@ -190,19 +189,19 @@ public class ConfigServiceTest extends BaseDbUnitTest {
|
||||
});
|
||||
configMapper.insert(dbConfig);
|
||||
// 测试 name 不匹配
|
||||
configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setName("土豆")));
|
||||
configMapper.insert(cloneIgnoreId(dbConfig, o -> o.setName("土豆")));
|
||||
// 测试 key 不匹配
|
||||
configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setConfigKey("tudou")));
|
||||
configMapper.insert(cloneIgnoreId(dbConfig, o -> o.setConfigKey("tudou")));
|
||||
// 测试 type 不匹配
|
||||
configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setType(ConfigTypeEnum.CUSTOM.getType())));
|
||||
configMapper.insert(cloneIgnoreId(dbConfig, o -> o.setType(ConfigTypeEnum.CUSTOM.getType())));
|
||||
// 测试 createTime 不匹配
|
||||
configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setCreateTime(buildTime(2021, 1, 1))));
|
||||
configMapper.insert(cloneIgnoreId(dbConfig, o -> o.setCreateTime(buildTime(2021, 1, 1))));
|
||||
// 准备参数
|
||||
ConfigExportReqVO reqVO = new ConfigExportReqVO();
|
||||
reqVO.setName("艿");
|
||||
reqVO.setKey("nai");
|
||||
reqVO.setType(ConfigTypeEnum.SYSTEM.getType());
|
||||
reqVO.setCreateTime((new LocalDateTime[]{buildTime(2021, 1, 15),buildTime(2021, 2, 15)}));
|
||||
reqVO.setCreateTime(buildBetweenTime(2021, 1, 15, 2021, 2, 15));
|
||||
|
||||
// 调用
|
||||
List<ConfigDO> list = configService.getConfigList(reqVO);
|
||||
@ -211,6 +210,21 @@ public class ConfigServiceTest extends BaseDbUnitTest {
|
||||
assertPojoEquals(dbConfig, list.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetConfig() {
|
||||
// mock 数据
|
||||
ConfigDO dbConfig = randomConfigDO();
|
||||
configMapper.insert(dbConfig);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
Long id = dbConfig.getId();
|
||||
|
||||
// 调用
|
||||
ConfigDO config = configService.getConfig(id);
|
||||
// 断言
|
||||
assertNotNull(config);
|
||||
assertPojoEquals(dbConfig, config);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetConfigByKey() {
|
||||
// mock 数据
|
@ -1,5 +1,6 @@
|
||||
package cn.iocoder.yudao.module.infra.service.db;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.crypto.symmetric.AES;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.type.EncryptTypeHandler;
|
||||
@ -9,6 +10,7 @@ import cn.iocoder.yudao.module.infra.controller.admin.db.vo.DataSourceConfigCrea
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.db.vo.DataSourceConfigUpdateReqVO;
|
||||
import cn.iocoder.yudao.module.infra.dal.dataobject.db.DataSourceConfigDO;
|
||||
import cn.iocoder.yudao.module.infra.dal.mysql.db.DataSourceConfigMapper;
|
||||
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
|
||||
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -18,14 +20,14 @@ import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
|
||||
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.DATA_SOURCE_CONFIG_NOT_EXISTS;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
@ -57,6 +59,11 @@ public class DataSourceConfigServiceImplTest extends BaseDbUnitTest {
|
||||
ReflectUtil.setFieldValue(EncryptTypeHandler.class, "aes", aes);
|
||||
when(aes.encryptBase64(anyString())).then((Answer<String>) invocation -> invocation.getArgument(0));
|
||||
when(aes.decryptStr(anyString())).then((Answer<String>) invocation -> invocation.getArgument(0));
|
||||
|
||||
// mock DynamicDataSourceProperties
|
||||
when(dynamicDataSourceProperties.getPrimary()).thenReturn("primary");
|
||||
when(dynamicDataSourceProperties.getDatasource()).thenReturn(MapUtil.of("primary",
|
||||
new DataSourceProperty().setUrl("http://localhost:3306").setUsername("yunai").setPassword("tudou")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -89,7 +96,6 @@ public class DataSourceConfigServiceImplTest extends BaseDbUnitTest {
|
||||
o.setId(dbDataSourceConfig.getId()); // 设置更新的 ID
|
||||
});
|
||||
// mock 方法
|
||||
// when(stringEncryptor.encrypt(eq(reqVO.getPassword()))).thenReturn("123456");
|
||||
databaseUtilsMock.when(() -> JdbcUtils.isConnectionOK(eq(reqVO.getUrl()),
|
||||
eq(reqVO.getUsername()), eq(reqVO.getPassword()))).thenReturn(true);
|
||||
|
||||
@ -142,7 +148,58 @@ public class DataSourceConfigServiceImplTest extends BaseDbUnitTest {
|
||||
// 调用
|
||||
DataSourceConfigDO result = dataSourceConfigMapper.selectOne(DataSourceConfigDO::getPassword,
|
||||
EncryptTypeHandler.encrypt(dbDataSourceConfig.getPassword()));
|
||||
System.out.println(result);
|
||||
assertPojoEquals(dbDataSourceConfig, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDataSourceConfig_master() {
|
||||
// 准备参数
|
||||
Long id = 0L;
|
||||
// mock 方法
|
||||
|
||||
// 调用
|
||||
DataSourceConfigDO dataSourceConfig = dataSourceConfigService.getDataSourceConfig(id);
|
||||
// 断言
|
||||
assertEquals(id, dataSourceConfig.getId());
|
||||
assertEquals("primary", dataSourceConfig.getName());
|
||||
assertEquals("http://localhost:3306", dataSourceConfig.getUrl());
|
||||
assertEquals("yunai", dataSourceConfig.getUsername());
|
||||
assertEquals("tudou", dataSourceConfig.getPassword());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDataSourceConfig_normal() {
|
||||
// mock 数据
|
||||
DataSourceConfigDO dbDataSourceConfig = randomPojo(DataSourceConfigDO.class);
|
||||
dataSourceConfigMapper.insert(dbDataSourceConfig);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
Long id = dbDataSourceConfig.getId();
|
||||
|
||||
// 调用
|
||||
DataSourceConfigDO dataSourceConfig = dataSourceConfigService.getDataSourceConfig(id);
|
||||
// 断言
|
||||
assertPojoEquals(dbDataSourceConfig, dataSourceConfig);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDataSourceConfigList() {
|
||||
// mock 数据
|
||||
DataSourceConfigDO dbDataSourceConfig = randomPojo(DataSourceConfigDO.class);
|
||||
dataSourceConfigMapper.insert(dbDataSourceConfig);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
|
||||
// 调用
|
||||
List<DataSourceConfigDO> dataSourceConfigList = dataSourceConfigService.getDataSourceConfigList();
|
||||
// 断言
|
||||
assertEquals(2, dataSourceConfigList.size());
|
||||
// master
|
||||
assertEquals(0L, dataSourceConfigList.get(0).getId());
|
||||
assertEquals("primary", dataSourceConfigList.get(0).getName());
|
||||
assertEquals("http://localhost:3306", dataSourceConfigList.get(0).getUrl());
|
||||
assertEquals("yunai", dataSourceConfigList.get(0).getUsername());
|
||||
assertEquals("tudou", dataSourceConfigList.get(0).getPassword());
|
||||
// normal
|
||||
assertPojoEquals(dbDataSourceConfig, dataSourceConfigList.get(1));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,89 @@
|
||||
package cn.iocoder.yudao.module.infra.service.db;
|
||||
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
import cn.iocoder.yudao.module.infra.dal.dataobject.db.DataSourceConfigDO;
|
||||
import com.baomidou.mybatisplus.generator.config.po.TableField;
|
||||
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
|
||||
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
|
||||
import org.apache.ibatis.type.JdbcType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@Import(DatabaseTableServiceImpl.class)
|
||||
public class DatabaseTableServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private DatabaseTableServiceImpl databaseTableService;
|
||||
|
||||
@MockBean
|
||||
private DataSourceConfigService dataSourceConfigService;
|
||||
|
||||
@Test
|
||||
public void testGetTableList() {
|
||||
// 准备参数
|
||||
Long dataSourceConfigId = randomLongId();
|
||||
// mock 方法
|
||||
DataSourceConfigDO dataSourceConfig = new DataSourceConfigDO().setUsername("sa").setPassword("")
|
||||
.setUrl("jdbc:h2:mem:testdb");
|
||||
when(dataSourceConfigService.getDataSourceConfig(eq(dataSourceConfigId)))
|
||||
.thenReturn(dataSourceConfig);
|
||||
|
||||
// 调用
|
||||
List<TableInfo> tables = databaseTableService.getTableList(dataSourceConfigId,
|
||||
"config", "参数");
|
||||
// 断言
|
||||
assertEquals(1, tables.size());
|
||||
assertTableInfo(tables.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetTable() {
|
||||
// 准备参数
|
||||
Long dataSourceConfigId = randomLongId();
|
||||
// mock 方法
|
||||
DataSourceConfigDO dataSourceConfig = new DataSourceConfigDO().setUsername("sa").setPassword("")
|
||||
.setUrl("jdbc:h2:mem:testdb");
|
||||
when(dataSourceConfigService.getDataSourceConfig(eq(dataSourceConfigId)))
|
||||
.thenReturn(dataSourceConfig);
|
||||
|
||||
// 调用
|
||||
TableInfo tableInfo = databaseTableService.getTable(dataSourceConfigId, "infra_config");
|
||||
// 断言
|
||||
assertTableInfo(tableInfo);
|
||||
}
|
||||
|
||||
private void assertTableInfo(TableInfo tableInfo) {
|
||||
assertEquals("infra_config", tableInfo.getName());
|
||||
assertEquals("参数配置表", tableInfo.getComment());
|
||||
assertEquals(13, tableInfo.getFields().size());
|
||||
// id 字段
|
||||
TableField idField = tableInfo.getFields().get(0);
|
||||
assertEquals("id", idField.getName());
|
||||
assertEquals(JdbcType.BIGINT, idField.getMetaInfo().getJdbcType());
|
||||
assertEquals("编号", idField.getComment());
|
||||
assertFalse(idField.getMetaInfo().isNullable());
|
||||
assertTrue(idField.isKeyFlag());
|
||||
assertTrue(idField.isKeyIdentityFlag());
|
||||
assertEquals(DbColumnType.LONG, idField.getColumnType());
|
||||
assertEquals("id", idField.getPropertyName());
|
||||
// name 字段
|
||||
TableField nameField = tableInfo.getFields().get(3);
|
||||
assertEquals("name", nameField.getName());
|
||||
assertEquals(JdbcType.VARCHAR, nameField.getMetaInfo().getJdbcType());
|
||||
assertEquals("名字", nameField.getComment());
|
||||
assertFalse(nameField.getMetaInfo().isNullable());
|
||||
assertFalse(nameField.isKeyFlag());
|
||||
assertFalse(nameField.isKeyIdentityFlag());
|
||||
assertEquals(DbColumnType.STRING, nameField.getColumnType());
|
||||
assertEquals("name", nameField.getPropertyName());
|
||||
}
|
||||
}
|
@ -7,6 +7,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.file.core.client.FileClient;
|
||||
import cn.iocoder.yudao.framework.file.core.client.FileClientConfig;
|
||||
import cn.iocoder.yudao.framework.file.core.client.FileClientFactory;
|
||||
import cn.iocoder.yudao.framework.file.core.client.local.LocalFileClient;
|
||||
import cn.iocoder.yudao.framework.file.core.client.local.LocalFileClientConfig;
|
||||
import cn.iocoder.yudao.framework.file.core.enums.FileStorageEnum;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
@ -30,7 +31,6 @@ import java.util.Map;
|
||||
import static cn.hutool.core.util.RandomUtil.randomEle;
|
||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
|
||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
|
||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.max;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
|
||||
@ -74,16 +74,13 @@ public class FileConfigServiceImplTest extends BaseDbUnitTest {
|
||||
when(fileClientFactory.getFileClient(eq(1L))).thenReturn(masterFileClient);
|
||||
|
||||
// 调用
|
||||
fileConfigService.initFileClients();
|
||||
fileConfigService.initLocalCache();
|
||||
// 断言 fileClientFactory 调用
|
||||
verify(fileClientFactory).createOrUpdateFileClient(eq(1L),
|
||||
eq(configDO1.getStorage()), eq(configDO1.getConfig()));
|
||||
verify(fileClientFactory).createOrUpdateFileClient(eq(2L),
|
||||
eq(configDO2.getStorage()), eq(configDO2.getConfig()));
|
||||
assertSame(masterFileClient, fileConfigService.getMasterFileClient());
|
||||
// 断言 maxUpdateTime 缓存
|
||||
assertEquals(max(configDO1.getUpdateTime(), configDO2.getUpdateTime()),
|
||||
fileConfigService.getMaxUpdateTime());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -246,6 +243,30 @@ public class FileConfigServiceImplTest extends BaseDbUnitTest {
|
||||
assertEquals("https://www.iocoder.cn", fileConfigService.testFileConfig(id));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFileConfig() {
|
||||
// mock 数据
|
||||
FileConfigDO dbFileConfig = randomFileConfigDO().setMaster(false);
|
||||
fileConfigMapper.insert(dbFileConfig);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
Long id = dbFileConfig.getId();
|
||||
|
||||
// 调用,并断言
|
||||
assertPojoEquals(dbFileConfig, fileConfigService.getFileConfig(id));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFileClient() {
|
||||
// 准备参数
|
||||
Long id = randomLongId();
|
||||
// mock 获得 Client
|
||||
FileClient fileClient = new LocalFileClient(id, new LocalFileClientConfig());
|
||||
when(fileClientFactory.getFileClient(eq(id))).thenReturn(fileClient);
|
||||
|
||||
// 调用,并断言
|
||||
assertSame(fileClient, fileConfigService.getFileClient(id));
|
||||
}
|
||||
|
||||
private FileConfigDO randomFileConfigDO() {
|
||||
return randomPojo(FileConfigDO.class).setStorage(randomEle(FileStorageEnum.values()).getStorage())
|
||||
.setConfig(new EmptyFileClientConfig());
|
||||
|
@ -26,7 +26,7 @@ import static org.mockito.ArgumentMatchers.same;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@Import({FileServiceImpl.class})
|
||||
public class FileServiceTest extends BaseDbUnitTest {
|
||||
public class FileServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private FileService fileService;
|
@ -1,7 +1,6 @@
|
||||
package cn.iocoder.yudao.module.infra.service.job;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.job.vo.log.JobLogExportReqVO;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.job.vo.log.JobLogPageReqVO;
|
||||
@ -12,18 +11,20 @@ import org.junit.jupiter.api.Test;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.hutool.core.util.RandomUtil.randomEle;
|
||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
|
||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
|
||||
import static java.util.Collections.singleton;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
@Import(JobLogServiceImpl.class)
|
||||
public class JobLogServiceTest extends BaseDbUnitTest {
|
||||
public class JobLogServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private JobLogServiceImpl jobLogService;
|
||||
@ -31,66 +32,103 @@ public class JobLogServiceTest extends BaseDbUnitTest {
|
||||
private JobLogMapper jobLogMapper;
|
||||
|
||||
@Test
|
||||
public void testCreateJobLog_success() {
|
||||
public void testCreateJobLog() {
|
||||
// 准备参数
|
||||
JobLogDO reqVO = randomPojo(JobLogDO.class, o -> {
|
||||
o.setExecuteIndex(1);
|
||||
});
|
||||
JobLogDO reqVO = randomPojo(JobLogDO.class, o -> o.setExecuteIndex(1));
|
||||
|
||||
// 调用
|
||||
Long jobLogId = jobLogService.createJobLog(reqVO.getJobId(), reqVO.getBeginTime(), reqVO.getHandlerName(), reqVO.getHandlerParam(), reqVO.getExecuteIndex());
|
||||
Long id = jobLogService.createJobLog(reqVO.getJobId(), reqVO.getBeginTime(),
|
||||
reqVO.getHandlerName(), reqVO.getHandlerParam(), reqVO.getExecuteIndex());
|
||||
// 断言
|
||||
assertNotNull(jobLogId);
|
||||
assertNotNull(id);
|
||||
// 校验记录的属性是否正确
|
||||
JobLogDO job = jobLogMapper.selectById(jobLogId);
|
||||
JobLogDO job = jobLogMapper.selectById(id);
|
||||
assertEquals(JobLogStatusEnum.RUNNING.getStatus(), job.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateJobLogResultAsync_success() {
|
||||
// 准备参数
|
||||
JobLogDO reqVO = randomPojo(JobLogDO.class, o -> {
|
||||
// mock 数据
|
||||
JobLogDO log = randomPojo(JobLogDO.class, o -> {
|
||||
o.setExecuteIndex(1);
|
||||
o.setStatus(JobLogStatusEnum.RUNNING.getStatus());
|
||||
});
|
||||
JobLogDO log = JobLogDO.builder().jobId(reqVO.getJobId()).handlerName(reqVO.getHandlerName()).handlerParam(reqVO.getHandlerParam()).executeIndex(reqVO.getExecuteIndex())
|
||||
.beginTime(reqVO.getBeginTime()).status(JobLogStatusEnum.RUNNING.getStatus()).build();
|
||||
jobLogMapper.insert(log);
|
||||
// 调用
|
||||
jobLogService.updateJobLogResultAsync(log.getId(), reqVO.getBeginTime(), reqVO.getDuration(), true,reqVO.getResult());
|
||||
// 校验记录的属性是否正确
|
||||
JobLogDO job = jobLogMapper.selectById(log.getId());
|
||||
assertEquals(JobLogStatusEnum.SUCCESS.getStatus(), job.getStatus());
|
||||
// 准备参数
|
||||
Long logId = log.getId();
|
||||
LocalDateTime endTime = randomLocalDateTime();
|
||||
Integer duration = randomInteger();
|
||||
boolean success = true;
|
||||
String result = randomString();
|
||||
|
||||
// 调用
|
||||
jobLogService.updateJobLogResultAsync(log.getId(), reqVO.getBeginTime(), reqVO.getDuration(), false,reqVO.getResult());
|
||||
jobLogService.updateJobLogResultAsync(logId, endTime, duration, success, result);
|
||||
// 校验记录的属性是否正确
|
||||
JobLogDO job2 = jobLogMapper.selectById(log.getId());
|
||||
assertEquals(JobLogStatusEnum.FAILURE.getStatus(), job2.getStatus());
|
||||
JobLogDO dbLog = jobLogMapper.selectById(log.getId());
|
||||
assertEquals(endTime, dbLog.getEndTime());
|
||||
assertEquals(duration, dbLog.getDuration());
|
||||
assertEquals(JobLogStatusEnum.SUCCESS.getStatus(), dbLog.getStatus());
|
||||
assertEquals(result, dbLog.getResult());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJobLogListByIds_success() {
|
||||
public void testUpdateJobLogResultAsync_failure() {
|
||||
// mock 数据
|
||||
JobLogDO dbJobLog = randomPojo(JobLogDO.class, o -> {
|
||||
JobLogDO log = randomPojo(JobLogDO.class, o -> {
|
||||
o.setExecuteIndex(1);
|
||||
o.setStatus(randomEle(JobLogStatusEnum.values()).getStatus()); // 保证 status 的范围
|
||||
o.setStatus(JobLogStatusEnum.RUNNING.getStatus());
|
||||
});
|
||||
JobLogDO cloneJobLog = ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setHandlerName(randomString()));
|
||||
jobLogMapper.insert(log);
|
||||
// 准备参数
|
||||
Long logId = log.getId();
|
||||
LocalDateTime endTime = randomLocalDateTime();
|
||||
Integer duration = randomInteger();
|
||||
boolean success = false;
|
||||
String result = randomString();
|
||||
|
||||
// 调用
|
||||
jobLogService.updateJobLogResultAsync(logId, endTime, duration, success, result);
|
||||
// 校验记录的属性是否正确
|
||||
JobLogDO dbLog = jobLogMapper.selectById(log.getId());
|
||||
assertEquals(endTime, dbLog.getEndTime());
|
||||
assertEquals(duration, dbLog.getDuration());
|
||||
assertEquals(JobLogStatusEnum.FAILURE.getStatus(), dbLog.getStatus());
|
||||
assertEquals(result, dbLog.getResult());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJobLog() {
|
||||
// mock 数据
|
||||
JobLogDO dbJobLog = randomPojo(JobLogDO.class, o -> o.setExecuteIndex(1));
|
||||
jobLogMapper.insert(dbJobLog);
|
||||
// 准备参数
|
||||
Long id = dbJobLog.getId();
|
||||
|
||||
// 调用
|
||||
JobLogDO jobLog = jobLogService.getJobLog(id);
|
||||
// 断言
|
||||
assertPojoEquals(dbJobLog, jobLog);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJobLogList() {
|
||||
// mock 数据
|
||||
JobLogDO dbJobLog = randomPojo(JobLogDO.class, o -> o.setExecuteIndex(1));
|
||||
jobLogMapper.insert(dbJobLog);
|
||||
// 测试 handlerName 不匹配
|
||||
jobLogMapper.insert(cloneJobLog);
|
||||
jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> {}));
|
||||
// 准备参数
|
||||
ArrayList ids = new ArrayList<>();
|
||||
ids.add(dbJobLog.getId());
|
||||
ids.add(cloneJobLog.getId());
|
||||
Collection<Long> ids = singleton(dbJobLog.getId());
|
||||
|
||||
// 调用
|
||||
List<JobLogDO> list = jobLogService.getJobLogList(ids);
|
||||
// 断言
|
||||
assertEquals(2, list.size());
|
||||
assertEquals(1, list.size());
|
||||
assertPojoEquals(dbJobLog, list.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJobPage_success() {
|
||||
public void testGetJobPage() {
|
||||
// mock 数据
|
||||
JobLogDO dbJobLog = randomPojo(JobLogDO.class, o -> {
|
||||
o.setExecuteIndex(1);
|
||||
@ -101,15 +139,15 @@ public class JobLogServiceTest extends BaseDbUnitTest {
|
||||
});
|
||||
jobLogMapper.insert(dbJobLog);
|
||||
// 测试 jobId 不匹配
|
||||
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setJobId(randomLongId())));
|
||||
jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setJobId(randomLongId())));
|
||||
// 测试 handlerName 不匹配
|
||||
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setHandlerName(randomString())));
|
||||
jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setHandlerName(randomString())));
|
||||
// 测试 beginTime 不匹配
|
||||
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setBeginTime(buildTime(2021, 1, 7))));
|
||||
jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setBeginTime(buildTime(2021, 1, 7))));
|
||||
// 测试 endTime 不匹配
|
||||
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setEndTime(buildTime(2021, 1, 9))));
|
||||
jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setEndTime(buildTime(2021, 1, 9))));
|
||||
// 测试 status 不匹配
|
||||
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setStatus(JobLogStatusEnum.FAILURE.getStatus())));
|
||||
jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setStatus(JobLogStatusEnum.FAILURE.getStatus())));
|
||||
// 准备参数
|
||||
JobLogPageReqVO reqVo = new JobLogPageReqVO();
|
||||
reqVo.setJobId(dbJobLog.getJobId());
|
||||
@ -117,6 +155,7 @@ public class JobLogServiceTest extends BaseDbUnitTest {
|
||||
reqVo.setBeginTime(dbJobLog.getBeginTime());
|
||||
reqVo.setEndTime(dbJobLog.getEndTime());
|
||||
reqVo.setStatus(JobLogStatusEnum.SUCCESS.getStatus());
|
||||
|
||||
// 调用
|
||||
PageResult<JobLogDO> pageResult = jobLogService.getJobLogPage(reqVo);
|
||||
// 断言
|
||||
@ -126,7 +165,7 @@ public class JobLogServiceTest extends BaseDbUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJobListForExport_success() {
|
||||
public void testGetJobList_export() {
|
||||
// mock 数据
|
||||
JobLogDO dbJobLog = randomPojo(JobLogDO.class, o -> {
|
||||
o.setExecuteIndex(1);
|
||||
@ -137,15 +176,15 @@ public class JobLogServiceTest extends BaseDbUnitTest {
|
||||
});
|
||||
jobLogMapper.insert(dbJobLog);
|
||||
// 测试 jobId 不匹配
|
||||
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setJobId(randomLongId())));
|
||||
jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setJobId(randomLongId())));
|
||||
// 测试 handlerName 不匹配
|
||||
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setHandlerName(randomString())));
|
||||
jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setHandlerName(randomString())));
|
||||
// 测试 beginTime 不匹配
|
||||
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setBeginTime(buildTime(2021, 1, 7))));
|
||||
jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setBeginTime(buildTime(2021, 1, 7))));
|
||||
// 测试 endTime 不匹配
|
||||
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setEndTime(buildTime(2021, 1, 9))));
|
||||
jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setEndTime(buildTime(2021, 1, 9))));
|
||||
// 测试 status 不匹配
|
||||
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setStatus(JobLogStatusEnum.FAILURE.getStatus())));
|
||||
jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setStatus(JobLogStatusEnum.FAILURE.getStatus())));
|
||||
// 准备参数
|
||||
JobLogExportReqVO reqVo = new JobLogExportReqVO();
|
||||
reqVo.setJobId(dbJobLog.getJobId());
|
||||
@ -153,6 +192,7 @@ public class JobLogServiceTest extends BaseDbUnitTest {
|
||||
reqVo.setBeginTime(dbJobLog.getBeginTime());
|
||||
reqVo.setEndTime(dbJobLog.getEndTime());
|
||||
reqVo.setStatus(JobLogStatusEnum.SUCCESS.getStatus());
|
||||
|
||||
// 调用
|
||||
List<JobLogDO> list = jobLogService.getJobLogList(reqVo);
|
||||
// 断言
|
@ -1,14 +1,12 @@
|
||||
package cn.iocoder.yudao.module.infra.service.job;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
|
||||
import cn.iocoder.yudao.framework.quartz.core.scheduler.SchedulerManager;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.job.vo.job.JobCreateReqVO;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.job.vo.job.JobExportReqVO;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.job.vo.job.JobPageReqVO;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.job.vo.job.JobUpdateReqVO;
|
||||
import cn.iocoder.yudao.module.infra.convert.job.JobConvert;
|
||||
import cn.iocoder.yudao.module.infra.dal.dataobject.job.JobDO;
|
||||
import cn.iocoder.yudao.module.infra.dal.mysql.job.JobMapper;
|
||||
import cn.iocoder.yudao.module.infra.enums.job.JobStatusEnum;
|
||||
@ -18,22 +16,23 @@ import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.hutool.core.util.RandomUtil.randomEle;
|
||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString;
|
||||
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.*;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@Import(JobServiceImpl.class)
|
||||
public class JobServiceTest extends BaseDbUnitTest {
|
||||
public class JobServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private JobServiceImpl jobService;
|
||||
@ -46,6 +45,7 @@ public class JobServiceTest extends BaseDbUnitTest {
|
||||
public void testCreateJob_cronExpressionValid() {
|
||||
// 准备参数。Cron 表达式为 String 类型,默认随机字符串。
|
||||
JobCreateReqVO reqVO = randomPojo(JobCreateReqVO.class);
|
||||
|
||||
// 调用,并断言异常
|
||||
assertServiceException(() -> jobService.createJob(reqVO), JOB_CRON_EXPRESSION_VALID);
|
||||
}
|
||||
@ -54,6 +54,7 @@ public class JobServiceTest extends BaseDbUnitTest {
|
||||
public void testCreateJob_jobHandlerExists() throws SchedulerException {
|
||||
// 准备参数 指定 Cron 表达式
|
||||
JobCreateReqVO reqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
|
||||
|
||||
// 调用
|
||||
jobService.createJob(reqVO);
|
||||
// 调用,并断言异常
|
||||
@ -64,6 +65,7 @@ public class JobServiceTest extends BaseDbUnitTest {
|
||||
public void testCreateJob_success() throws SchedulerException {
|
||||
// 准备参数 指定 Cron 表达式
|
||||
JobCreateReqVO reqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
|
||||
|
||||
// 调用
|
||||
Long jobId = jobService.createJob(reqVO);
|
||||
// 断言
|
||||
@ -73,14 +75,15 @@ public class JobServiceTest extends BaseDbUnitTest {
|
||||
assertPojoEquals(reqVO, job);
|
||||
assertEquals(JobStatusEnum.NORMAL.getStatus(), job.getStatus());
|
||||
// 校验调用
|
||||
verify(schedulerManager, times(1)).addJob(eq(job.getId()), eq(job.getHandlerName()), eq(job.getHandlerParam()), eq(job.getCronExpression()),
|
||||
eq(reqVO.getRetryCount()), eq(reqVO.getRetryInterval()));
|
||||
verify(schedulerManager).addJob(eq(job.getId()), eq(job.getHandlerName()), eq(job.getHandlerParam()),
|
||||
eq(job.getCronExpression()), eq(reqVO.getRetryCount()), eq(reqVO.getRetryInterval()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateJob_jobNotExists(){
|
||||
// 准备参数
|
||||
JobUpdateReqVO reqVO = randomPojo(JobUpdateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
|
||||
|
||||
// 调用,并断言异常
|
||||
assertServiceException(() -> jobService.updateJob(reqVO), JOB_NOT_EXISTS);
|
||||
}
|
||||
@ -88,150 +91,136 @@ public class JobServiceTest extends BaseDbUnitTest {
|
||||
@Test
|
||||
public void testUpdateJob_onlyNormalStatus(){
|
||||
// mock 数据
|
||||
JobCreateReqVO createReqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
|
||||
JobDO job = JobConvert.INSTANCE.convert(createReqVO);
|
||||
job.setStatus(JobStatusEnum.INIT.getStatus());
|
||||
fillJobMonitorTimeoutEmpty(job);
|
||||
JobDO job = randomPojo(JobDO.class, o -> o.setStatus(JobStatusEnum.INIT.getStatus()));
|
||||
jobMapper.insert(job);
|
||||
// 准备参数
|
||||
JobUpdateReqVO updateReqVO = randomPojo(JobUpdateReqVO.class, o -> {
|
||||
o.setId(job.getId());
|
||||
o.setName(createReqVO.getName());
|
||||
o.setCronExpression(createReqVO.getCronExpression());
|
||||
o.setCronExpression("0 0/1 * * * ? *");
|
||||
});
|
||||
|
||||
// 调用,并断言异常
|
||||
assertServiceException(() -> jobService.updateJob(updateReqVO), JOB_UPDATE_ONLY_NORMAL_STATUS);
|
||||
assertServiceException(() -> jobService.updateJob(updateReqVO),
|
||||
JOB_UPDATE_ONLY_NORMAL_STATUS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateJob_success() throws SchedulerException {
|
||||
// mock 数据
|
||||
JobCreateReqVO createReqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
|
||||
JobDO job = JobConvert.INSTANCE.convert(createReqVO);
|
||||
job.setStatus(JobStatusEnum.NORMAL.getStatus());
|
||||
fillJobMonitorTimeoutEmpty(job);
|
||||
JobDO job = randomPojo(JobDO.class, o -> o.setStatus(JobStatusEnum.NORMAL.getStatus()));
|
||||
jobMapper.insert(job);
|
||||
// 准备参数
|
||||
JobUpdateReqVO updateReqVO = randomPojo(JobUpdateReqVO.class, o -> {
|
||||
o.setId(job.getId());
|
||||
o.setName(createReqVO.getName());
|
||||
o.setCronExpression(createReqVO.getCronExpression());
|
||||
o.setCronExpression("0 0/1 * * * ? *");
|
||||
});
|
||||
|
||||
// 调用
|
||||
jobService.updateJob(updateReqVO);
|
||||
// 校验记录的属性是否正确
|
||||
JobDO updateJob = jobMapper.selectById(updateReqVO.getId());
|
||||
assertPojoEquals(updateReqVO, updateJob);
|
||||
// 校验调用
|
||||
verify(schedulerManager, times(1)).updateJob(eq(job.getHandlerName()), eq(updateReqVO.getHandlerParam()), eq(updateReqVO.getCronExpression()),
|
||||
eq(updateReqVO.getRetryCount()), eq(updateReqVO.getRetryInterval()));
|
||||
verify(schedulerManager).updateJob(eq(job.getHandlerName()), eq(updateReqVO.getHandlerParam()),
|
||||
eq(updateReqVO.getCronExpression()), eq(updateReqVO.getRetryCount()), eq(updateReqVO.getRetryInterval()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateJobStatus_changeStatusInvalid() {
|
||||
// 调用,并断言异常
|
||||
assertServiceException(() -> jobService.updateJobStatus(1L, JobStatusEnum.INIT.getStatus()), JOB_CHANGE_STATUS_INVALID);
|
||||
assertServiceException(() -> jobService.updateJobStatus(1L, JobStatusEnum.INIT.getStatus()),
|
||||
JOB_CHANGE_STATUS_INVALID);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateJobStatus_changeStatusEquals() {
|
||||
// mock 数据
|
||||
JobCreateReqVO createReqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
|
||||
JobDO job = JobConvert.INSTANCE.convert(createReqVO);
|
||||
job.setStatus(JobStatusEnum.NORMAL.getStatus());
|
||||
fillJobMonitorTimeoutEmpty(job);
|
||||
JobDO job = randomPojo(JobDO.class, o -> o.setStatus(JobStatusEnum.NORMAL.getStatus()));
|
||||
jobMapper.insert(job);
|
||||
|
||||
// 调用,并断言异常
|
||||
assertServiceException(() -> jobService.updateJobStatus(job.getId(), job.getStatus()), JOB_CHANGE_STATUS_EQUALS);
|
||||
assertServiceException(() -> jobService.updateJobStatus(job.getId(), job.getStatus()),
|
||||
JOB_CHANGE_STATUS_EQUALS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateJobStatus_NormalToStop_success() throws SchedulerException {
|
||||
public void testUpdateJobStatus_stopSuccess() throws SchedulerException {
|
||||
// mock 数据
|
||||
JobCreateReqVO createReqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
|
||||
JobDO job = JobConvert.INSTANCE.convert(createReqVO);
|
||||
job.setStatus(JobStatusEnum.NORMAL.getStatus());
|
||||
fillJobMonitorTimeoutEmpty(job);
|
||||
JobDO job = randomPojo(JobDO.class, o -> o.setStatus(JobStatusEnum.NORMAL.getStatus()));
|
||||
jobMapper.insert(job);
|
||||
|
||||
// 调用
|
||||
jobService.updateJobStatus(job.getId(), JobStatusEnum.STOP.getStatus());
|
||||
// 校验记录的属性是否正确
|
||||
JobDO updateJob = jobMapper.selectById(job.getId());
|
||||
assertEquals(JobStatusEnum.STOP.getStatus(), updateJob.getStatus());
|
||||
JobDO dbJob = jobMapper.selectById(job.getId());
|
||||
assertEquals(JobStatusEnum.STOP.getStatus(), dbJob.getStatus());
|
||||
// 校验调用
|
||||
verify(schedulerManager, times(1)).pauseJob(eq(job.getHandlerName()));
|
||||
verify(schedulerManager).pauseJob(eq(job.getHandlerName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateJobStatus_StopToNormal_success() throws SchedulerException {
|
||||
public void testUpdateJobStatus_normalSuccess() throws SchedulerException {
|
||||
// mock 数据
|
||||
JobCreateReqVO createReqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
|
||||
JobDO job = JobConvert.INSTANCE.convert(createReqVO);
|
||||
job.setStatus(JobStatusEnum.STOP.getStatus());
|
||||
fillJobMonitorTimeoutEmpty(job);
|
||||
JobDO job = randomPojo(JobDO.class, o -> o.setStatus(JobStatusEnum.STOP.getStatus()));
|
||||
jobMapper.insert(job);
|
||||
|
||||
// 调用
|
||||
jobService.updateJobStatus(job.getId(), JobStatusEnum.NORMAL.getStatus());
|
||||
// 校验记录的属性是否正确
|
||||
JobDO updateJob = jobMapper.selectById(job.getId());
|
||||
assertEquals(JobStatusEnum.NORMAL.getStatus(), updateJob.getStatus());
|
||||
JobDO dbJob = jobMapper.selectById(job.getId());
|
||||
assertEquals(JobStatusEnum.NORMAL.getStatus(), dbJob.getStatus());
|
||||
// 校验调用
|
||||
verify(schedulerManager, times(1)).resumeJob(eq(job.getHandlerName()));
|
||||
verify(schedulerManager).resumeJob(eq(job.getHandlerName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTriggerJob_success() throws SchedulerException {
|
||||
// mock 数据
|
||||
JobCreateReqVO createReqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
|
||||
JobDO job = JobConvert.INSTANCE.convert(createReqVO);
|
||||
job.setStatus(JobStatusEnum.NORMAL.getStatus());
|
||||
fillJobMonitorTimeoutEmpty(job);
|
||||
JobDO job = randomPojo(JobDO.class);
|
||||
jobMapper.insert(job);
|
||||
|
||||
// 调用
|
||||
jobService.triggerJob(job.getId());
|
||||
// 校验调用
|
||||
verify(schedulerManager, times(1)).triggerJob(eq(job.getId()), eq(job.getHandlerName()), eq(job.getHandlerParam()));
|
||||
verify(schedulerManager).triggerJob(eq(job.getId()),
|
||||
eq(job.getHandlerName()), eq(job.getHandlerParam()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteJob_success() throws SchedulerException {
|
||||
// mock 数据
|
||||
JobCreateReqVO createReqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
|
||||
JobDO job = JobConvert.INSTANCE.convert(createReqVO);
|
||||
job.setStatus(JobStatusEnum.NORMAL.getStatus());
|
||||
fillJobMonitorTimeoutEmpty(job);
|
||||
JobDO job = randomPojo(JobDO.class);
|
||||
jobMapper.insert(job);
|
||||
// 调用 UPDATE inf_job SET deleted=1 WHERE id=? AND deleted=0
|
||||
|
||||
// 调用
|
||||
jobService.deleteJob(job.getId());
|
||||
// 校验数据不存在了 WHERE id=? AND deleted=0 查询为空正常
|
||||
// 校验不存在
|
||||
assertNull(jobMapper.selectById(job.getId()));
|
||||
// 校验调用
|
||||
verify(schedulerManager, times(1)).deleteJob(eq(job.getHandlerName()));
|
||||
verify(schedulerManager).deleteJob(eq(job.getHandlerName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJobListByIds_success() {
|
||||
public void testGetJobList() {
|
||||
// mock 数据
|
||||
JobDO dbJob = randomPojo(JobDO.class, o -> {
|
||||
o.setStatus(randomEle(JobStatusEnum.values()).getStatus()); // 保证 status 的范围
|
||||
});
|
||||
JobDO cloneJob = ObjectUtils.cloneIgnoreId(dbJob, o -> o.setHandlerName(randomString()));
|
||||
jobMapper.insert(dbJob);
|
||||
// 测试 handlerName 不匹配
|
||||
jobMapper.insert(cloneJob);
|
||||
// 测试 id 不匹配
|
||||
jobMapper.insert(cloneIgnoreId(dbJob, o -> {}));
|
||||
|
||||
// 准备参数
|
||||
ArrayList<Long> ids = new ArrayList<>();
|
||||
ids.add(dbJob.getId());
|
||||
ids.add(cloneJob.getId());
|
||||
Collection<Long> ids = singletonList(dbJob.getId());
|
||||
// 调用
|
||||
List<JobDO> list = jobService.getJobList(ids);
|
||||
// 断言
|
||||
assertEquals(2, list.size());
|
||||
assertEquals(1, list.size());
|
||||
assertPojoEquals(dbJob, list.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJobPage_success() {
|
||||
public void testGetJobPage() {
|
||||
// mock 数据
|
||||
JobDO dbJob = randomPojo(JobDO.class, o -> {
|
||||
o.setName("定时任务测试");
|
||||
@ -240,16 +229,17 @@ public class JobServiceTest extends BaseDbUnitTest {
|
||||
});
|
||||
jobMapper.insert(dbJob);
|
||||
// 测试 name 不匹配
|
||||
jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setName("土豆")));
|
||||
jobMapper.insert(cloneIgnoreId(dbJob, o -> o.setName("土豆")));
|
||||
// 测试 status 不匹配
|
||||
jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setStatus(JobStatusEnum.NORMAL.getStatus())));
|
||||
jobMapper.insert(cloneIgnoreId(dbJob, o -> o.setStatus(JobStatusEnum.NORMAL.getStatus())));
|
||||
// 测试 handlerName 不匹配
|
||||
jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setHandlerName(randomString())));
|
||||
jobMapper.insert(cloneIgnoreId(dbJob, o -> o.setHandlerName(randomString())));
|
||||
// 准备参数
|
||||
JobPageReqVO reqVo = new JobPageReqVO();
|
||||
reqVo.setName("定时");
|
||||
reqVo.setStatus(JobStatusEnum.INIT.getStatus());
|
||||
reqVo.setHandlerName("单元");
|
||||
|
||||
// 调用
|
||||
PageResult<JobDO> pageResult = jobService.getJobPage(reqVo);
|
||||
// 断言
|
||||
@ -259,7 +249,7 @@ public class JobServiceTest extends BaseDbUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJobListForExport_success() {
|
||||
public void testGetJobList_export() {
|
||||
// mock 数据
|
||||
JobDO dbJob = randomPojo(JobDO.class, o -> {
|
||||
o.setName("定时任务测试");
|
||||
@ -268,16 +258,17 @@ public class JobServiceTest extends BaseDbUnitTest {
|
||||
});
|
||||
jobMapper.insert(dbJob);
|
||||
// 测试 name 不匹配
|
||||
jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setName("土豆")));
|
||||
jobMapper.insert(cloneIgnoreId(dbJob, o -> o.setName("土豆")));
|
||||
// 测试 status 不匹配
|
||||
jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setStatus(JobStatusEnum.NORMAL.getStatus())));
|
||||
jobMapper.insert(cloneIgnoreId(dbJob, o -> o.setStatus(JobStatusEnum.NORMAL.getStatus())));
|
||||
// 测试 handlerName 不匹配
|
||||
jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setHandlerName(randomString())));
|
||||
jobMapper.insert(cloneIgnoreId(dbJob, o -> o.setHandlerName(randomString())));
|
||||
// 准备参数
|
||||
JobExportReqVO reqVo = new JobExportReqVO();
|
||||
reqVo.setName("定时");
|
||||
reqVo.setStatus(JobStatusEnum.INIT.getStatus());
|
||||
reqVo.setHandlerName("单元");
|
||||
|
||||
// 调用
|
||||
List<JobDO> list = jobService.getJobList(reqVo);
|
||||
// 断言
|
||||
@ -285,10 +276,15 @@ public class JobServiceTest extends BaseDbUnitTest {
|
||||
assertPojoEquals(dbJob, list.get(0));
|
||||
}
|
||||
|
||||
private static void fillJobMonitorTimeoutEmpty(JobDO job) {
|
||||
if (job.getMonitorTimeout() == null) {
|
||||
job.setMonitorTimeout(0);
|
||||
}
|
||||
@Test
|
||||
public void testGetJob() {
|
||||
// mock 数据
|
||||
JobDO dbJob = randomPojo(JobDO.class);
|
||||
jobMapper.insert(dbJob);
|
||||
// 调用
|
||||
JobDO job = jobService.getJob(dbJob.getId());
|
||||
// 断言
|
||||
assertPojoEquals(dbJob, job);
|
||||
}
|
||||
|
||||
}
|
@ -1,12 +1,9 @@
|
||||
package cn.iocoder.yudao.module.infra.service.logger;
|
||||
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
||||
import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
import cn.iocoder.yudao.framework.test.core.util.RandomUtils;
|
||||
import cn.iocoder.yudao.module.infra.api.logger.dto.ApiAccessLogCreateReqDTO;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogExportReqVO;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogPageReqVO;
|
||||
@ -16,149 +13,121 @@ import org.junit.jupiter.api.Test;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
|
||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
|
||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
@Import(ApiAccessLogServiceImpl.class)
|
||||
public class ApiAccessLogServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private ApiAccessLogService apiAccessLogService;
|
||||
private ApiAccessLogServiceImpl apiAccessLogService;
|
||||
|
||||
@Resource
|
||||
private ApiAccessLogMapper apiAccessLogMapper;
|
||||
|
||||
@Test
|
||||
public void testGetApiAccessLogPage() {
|
||||
// 构造测试数据
|
||||
long userId = 2233L;
|
||||
int userType = UserTypeEnum.ADMIN.getValue();
|
||||
String applicationName = "yudao-test";
|
||||
String requestUrl = "foo";
|
||||
LocalDateTime beginTime = buildTime(2021, 3, 13);
|
||||
int duration = 1000;
|
||||
int resultCode = GlobalErrorCodeConstants.SUCCESS.getCode();
|
||||
|
||||
ApiAccessLogDO infApiAccessLogDO = RandomUtils.randomPojo(ApiAccessLogDO.class, dto -> {
|
||||
dto.setUserId(userId);
|
||||
dto.setUserType(userType);
|
||||
dto.setApplicationName(applicationName);
|
||||
dto.setRequestUrl(requestUrl);
|
||||
dto.setBeginTime(beginTime);
|
||||
dto.setDuration(duration);
|
||||
dto.setResultCode(resultCode);
|
||||
ApiAccessLogDO apiAccessLogDO = randomPojo(ApiAccessLogDO.class, o -> {
|
||||
o.setUserId(2233L);
|
||||
o.setUserType(UserTypeEnum.ADMIN.getValue());
|
||||
o.setApplicationName("yudao-test");
|
||||
o.setRequestUrl("foo");
|
||||
o.setBeginTime(buildTime(2021, 3, 13));
|
||||
o.setDuration(1000);
|
||||
o.setResultCode(GlobalErrorCodeConstants.SUCCESS.getCode());
|
||||
});
|
||||
apiAccessLogMapper.insert(infApiAccessLogDO);
|
||||
|
||||
// 下面几个都是不匹配的数据
|
||||
// userId 不同的
|
||||
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setUserId(3344L)));
|
||||
// userType
|
||||
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue())));
|
||||
// applicationName 不同的
|
||||
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setApplicationName("test")));
|
||||
// requestUrl 不同的
|
||||
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setRequestUrl("bar")));
|
||||
// 构造一个早期时间 2021-02-06 00:00:00
|
||||
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setBeginTime(buildTime(2021, 2, 6))));
|
||||
// duration 不同的
|
||||
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setDuration(100)));
|
||||
// resultCode 不同的
|
||||
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setResultCode(2)));
|
||||
|
||||
// 构造调用参数
|
||||
apiAccessLogMapper.insert(apiAccessLogDO);
|
||||
// 测试 userId 不匹配
|
||||
apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setUserId(3344L)));
|
||||
// 测试 userType 不匹配
|
||||
apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setUserType(UserTypeEnum.MEMBER.getValue())));
|
||||
// 测试 applicationName 不匹配
|
||||
apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setApplicationName("test")));
|
||||
// 测试 requestUrl 不匹配
|
||||
apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setRequestUrl("bar")));
|
||||
// 测试 beginTime 不匹配:构造一个早期时间 2021-02-06 00:00:00
|
||||
apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setBeginTime(buildTime(2021, 2, 6))));
|
||||
// 测试 duration 不匹配
|
||||
apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setDuration(100)));
|
||||
// 测试 resultCode 不匹配
|
||||
apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setResultCode(2)));
|
||||
// 准备参数
|
||||
ApiAccessLogPageReqVO reqVO = new ApiAccessLogPageReqVO();
|
||||
reqVO.setUserId(userId);
|
||||
reqVO.setUserType(userType);
|
||||
reqVO.setApplicationName(applicationName);
|
||||
reqVO.setRequestUrl(requestUrl);
|
||||
reqVO.setBeginTime((new LocalDateTime[]{buildTime(2021, 3, 12),buildTime(2021, 3, 14)}));
|
||||
reqVO.setDuration(duration);
|
||||
reqVO.setResultCode(resultCode);
|
||||
reqVO.setUserId(2233L);
|
||||
reqVO.setUserType(UserTypeEnum.ADMIN.getValue());
|
||||
reqVO.setApplicationName("yudao-test");
|
||||
reqVO.setRequestUrl("foo");
|
||||
reqVO.setBeginTime(buildBetweenTime(2021, 3, 13, 2021, 3, 13));
|
||||
reqVO.setDuration(1000);
|
||||
reqVO.setResultCode(GlobalErrorCodeConstants.SUCCESS.getCode());
|
||||
|
||||
// 调用service方法
|
||||
// 调用
|
||||
PageResult<ApiAccessLogDO> pageResult = apiAccessLogService.getApiAccessLogPage(reqVO);
|
||||
|
||||
// 断言,只查到了一条符合条件的
|
||||
assertEquals(1, pageResult.getTotal());
|
||||
assertEquals(1, pageResult.getList().size());
|
||||
assertPojoEquals(infApiAccessLogDO, pageResult.getList().get(0));
|
||||
assertPojoEquals(apiAccessLogDO, pageResult.getList().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetApiAccessLogList() {
|
||||
// 构造测试数据
|
||||
long userId = 2233L;
|
||||
int userType = UserTypeEnum.ADMIN.getValue();
|
||||
String applicationName = "yudao-test";
|
||||
String requestUrl = "foo";
|
||||
LocalDateTime beginTime = buildTime(2021, 3, 13);
|
||||
int duration = 1000;
|
||||
int resultCode = GlobalErrorCodeConstants.SUCCESS.getCode();
|
||||
|
||||
ApiAccessLogDO infApiAccessLogDO = RandomUtils.randomPojo(ApiAccessLogDO.class, dto -> {
|
||||
dto.setUserId(userId);
|
||||
dto.setUserType(userType);
|
||||
dto.setApplicationName(applicationName);
|
||||
dto.setRequestUrl(requestUrl);
|
||||
dto.setBeginTime(beginTime);
|
||||
dto.setDuration(duration);
|
||||
dto.setResultCode(resultCode);
|
||||
ApiAccessLogDO apiAccessLogDO = randomPojo(ApiAccessLogDO.class, o -> {
|
||||
o.setUserId(2233L);
|
||||
o.setUserType(UserTypeEnum.ADMIN.getValue());
|
||||
o.setApplicationName("yudao-test");
|
||||
o.setRequestUrl("foo");
|
||||
o.setBeginTime(buildTime(2021, 3, 13));
|
||||
o.setDuration(1000);
|
||||
o.setResultCode(GlobalErrorCodeConstants.SUCCESS.getCode());
|
||||
});
|
||||
apiAccessLogMapper.insert(infApiAccessLogDO);
|
||||
|
||||
// 下面几个都是不匹配的数据
|
||||
// userId 不同的
|
||||
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setUserId(3344L)));
|
||||
// userType
|
||||
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue())));
|
||||
// applicationName 不同的
|
||||
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setApplicationName("test")));
|
||||
// requestUrl 不同的
|
||||
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setRequestUrl("bar")));
|
||||
// 构造一个早期时间 2021-02-06 00:00:00
|
||||
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setBeginTime(buildTime(2021, 2, 6))));
|
||||
// duration 不同的
|
||||
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setDuration(100)));
|
||||
// resultCode 不同的
|
||||
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setResultCode(2)));
|
||||
|
||||
// 构造调用参数
|
||||
apiAccessLogMapper.insert(apiAccessLogDO);
|
||||
// 测试 userId 不匹配
|
||||
apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setUserId(3344L)));
|
||||
// 测试 userType 不匹配
|
||||
apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setUserType(UserTypeEnum.MEMBER.getValue())));
|
||||
// 测试 applicationName 不匹配
|
||||
apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setApplicationName("test")));
|
||||
// 测试 requestUrl 不匹配
|
||||
apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setRequestUrl("bar")));
|
||||
// 测试 beginTime 不匹配:构造一个早期时间 2021-02-06 00:00:00
|
||||
apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setBeginTime(buildTime(2021, 2, 6))));
|
||||
// 测试 duration 不匹配
|
||||
apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setDuration(100)));
|
||||
// 测试 resultCode 不匹配
|
||||
apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setResultCode(2)));
|
||||
// 准备参数
|
||||
ApiAccessLogExportReqVO reqVO = new ApiAccessLogExportReqVO();
|
||||
reqVO.setUserId(userId);
|
||||
reqVO.setUserType(userType);
|
||||
reqVO.setApplicationName(applicationName);
|
||||
reqVO.setRequestUrl(requestUrl);
|
||||
reqVO.setBeginTime((new LocalDateTime[]{buildTime(2021, 3, 12),buildTime(2021, 3, 14)}));
|
||||
reqVO.setDuration(duration);
|
||||
reqVO.setResultCode(resultCode);
|
||||
reqVO.setUserId(2233L);
|
||||
reqVO.setUserType(UserTypeEnum.ADMIN.getValue());
|
||||
reqVO.setApplicationName("yudao-test");
|
||||
reqVO.setRequestUrl("foo");
|
||||
reqVO.setBeginTime(buildBetweenTime(2021, 3, 13, 2021, 3, 13));
|
||||
reqVO.setDuration(1000);
|
||||
reqVO.setResultCode(GlobalErrorCodeConstants.SUCCESS.getCode());
|
||||
|
||||
// 调用service方法
|
||||
// 调用
|
||||
List<ApiAccessLogDO> list = apiAccessLogService.getApiAccessLogList(reqVO);
|
||||
|
||||
// 断言,只查到了一条符合条件的
|
||||
assertEquals(1, list.size());
|
||||
assertPojoEquals(infApiAccessLogDO, list.get(0));
|
||||
assertPojoEquals(apiAccessLogDO, list.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateApiAccessLogAsync() {
|
||||
public void testCreateApiAccessLog() {
|
||||
// 准备参数
|
||||
ApiAccessLogCreateReqDTO createDTO = RandomUtils.randomPojo(ApiAccessLogCreateReqDTO.class,
|
||||
dto -> dto.setUserType(RandomUtil.randomEle(UserTypeEnum.values()).getValue()));
|
||||
ApiAccessLogCreateReqDTO createDTO = randomPojo(ApiAccessLogCreateReqDTO.class);
|
||||
|
||||
// 调用
|
||||
apiAccessLogService.createApiAccessLog(createDTO);
|
||||
// 断言
|
||||
ApiAccessLogDO infApiAccessLogDO = apiAccessLogMapper.selectOne(null);
|
||||
assertNotNull(infApiAccessLogDO);
|
||||
assertPojoEquals(createDTO, infApiAccessLogDO);
|
||||
ApiAccessLogDO apiAccessLogDO = apiAccessLogMapper.selectOne(null);
|
||||
assertPojoEquals(createDTO, apiAccessLogDO);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,28 +1,28 @@
|
||||
package cn.iocoder.yudao.module.infra.service.logger;
|
||||
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
import cn.iocoder.yudao.framework.test.core.util.RandomUtils;
|
||||
import cn.iocoder.yudao.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiErrorLogExportReqVO;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiErrorLogPageReqVO;
|
||||
import cn.iocoder.yudao.module.infra.dal.dataobject.logger.ApiErrorLogDO;
|
||||
import cn.iocoder.yudao.module.infra.dal.mysql.logger.ApiErrorLogMapper;
|
||||
import cn.iocoder.yudao.module.infra.enums.logger.ApiErrorLogProcessStatusEnum;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.hutool.core.util.RandomUtil.randomEle;
|
||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
|
||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
|
||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
|
||||
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.API_ERROR_LOG_NOT_FOUND;
|
||||
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.API_ERROR_LOG_PROCESSED;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
@ -35,161 +35,150 @@ public class ApiErrorLogServiceImplTest extends BaseDbUnitTest {
|
||||
private ApiErrorLogServiceImpl apiErrorLogService;
|
||||
|
||||
@Resource
|
||||
private ApiErrorLogMapper infApiErrorLogMapper;
|
||||
private ApiErrorLogMapper apiErrorLogMapper;
|
||||
|
||||
@Test
|
||||
public void testGetApiErrorLogPage() {
|
||||
// 构造测试数据
|
||||
long userId = 2233L;
|
||||
int userType = UserTypeEnum.ADMIN.getValue();
|
||||
String applicationName = "yudao-test";
|
||||
String requestUrl = "foo";
|
||||
LocalDateTime beginTime = buildTime(2021, 3, 13);
|
||||
int progressStatus = ApiErrorLogProcessStatusEnum.INIT.getStatus();
|
||||
|
||||
ApiErrorLogDO infApiErrorLogDO = RandomUtils.randomPojo(ApiErrorLogDO.class, logDO -> {
|
||||
logDO.setUserId(userId);
|
||||
logDO.setUserType(userType);
|
||||
logDO.setApplicationName(applicationName);
|
||||
logDO.setRequestUrl(requestUrl);
|
||||
logDO.setExceptionTime(beginTime);
|
||||
logDO.setProcessStatus(progressStatus);
|
||||
// mock 数据
|
||||
ApiErrorLogDO apiErrorLogDO = randomPojo(ApiErrorLogDO.class, o -> {
|
||||
o.setUserId(2233L);
|
||||
o.setUserType(UserTypeEnum.ADMIN.getValue());
|
||||
o.setApplicationName("yudao-test");
|
||||
o.setRequestUrl("foo");
|
||||
o.setExceptionTime(buildTime(2021, 3, 13));
|
||||
o.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
|
||||
});
|
||||
infApiErrorLogMapper.insert(infApiErrorLogDO);
|
||||
|
||||
// 下面几个都是不匹配的数据
|
||||
// userId 不同的
|
||||
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setUserId(3344L)));
|
||||
// userType
|
||||
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue())));
|
||||
// applicationName 不同的
|
||||
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setApplicationName("test")));
|
||||
// requestUrl 不同的
|
||||
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setRequestUrl("bar")));
|
||||
// 构造一个早期时间 2021-02-06 00:00:00
|
||||
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setExceptionTime(buildTime(2021, 2, 6))));
|
||||
// progressStatus 不同的
|
||||
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus())));
|
||||
|
||||
// 构造调用参数
|
||||
apiErrorLogMapper.insert(apiErrorLogDO);
|
||||
// 测试 userId 不匹配
|
||||
apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, o -> o.setUserId(3344L)));
|
||||
// 测试 userType 不匹配
|
||||
apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue())));
|
||||
// 测试 applicationName 不匹配
|
||||
apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setApplicationName("test")));
|
||||
// 测试 requestUrl 不匹配
|
||||
apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setRequestUrl("bar")));
|
||||
// 测试 exceptionTime 不匹配:构造一个早期时间 2021-02-06 00:00:00
|
||||
apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setExceptionTime(buildTime(2021, 2, 6))));
|
||||
// 测试 progressStatus 不匹配
|
||||
apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus())));
|
||||
// 准备参数
|
||||
ApiErrorLogPageReqVO reqVO = new ApiErrorLogPageReqVO();
|
||||
reqVO.setUserId(userId);
|
||||
reqVO.setUserType(userType);
|
||||
reqVO.setApplicationName(applicationName);
|
||||
reqVO.setRequestUrl(requestUrl);
|
||||
reqVO.setExceptionTime((new LocalDateTime[]{buildTime(2021, 3, 12),buildTime(2021, 3, 14)}));
|
||||
reqVO.setProcessStatus(progressStatus);
|
||||
reqVO.setUserId(2233L);
|
||||
reqVO.setUserType(UserTypeEnum.ADMIN.getValue());
|
||||
reqVO.setApplicationName("yudao-test");
|
||||
reqVO.setRequestUrl("foo");
|
||||
reqVO.setExceptionTime(buildBetweenTime(2021, 3, 1, 2021, 3, 31));
|
||||
reqVO.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
|
||||
|
||||
// 调用service方法
|
||||
// 调用
|
||||
PageResult<ApiErrorLogDO> pageResult = apiErrorLogService.getApiErrorLogPage(reqVO);
|
||||
|
||||
// 断言,只查到了一条符合条件的
|
||||
assertEquals(1, pageResult.getTotal());
|
||||
assertEquals(1, pageResult.getList().size());
|
||||
assertPojoEquals(infApiErrorLogDO, pageResult.getList().get(0));
|
||||
assertPojoEquals(apiErrorLogDO, pageResult.getList().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetApiErrorLogList() {
|
||||
// 构造测试数据
|
||||
long userId = 2233L;
|
||||
int userType = UserTypeEnum.ADMIN.getValue();
|
||||
String applicationName = "yudao-test";
|
||||
String requestUrl = "foo";
|
||||
LocalDateTime beginTime = buildTime(2021, 3, 13);
|
||||
int progressStatus = ApiErrorLogProcessStatusEnum.INIT.getStatus();
|
||||
|
||||
ApiErrorLogDO infApiErrorLogDO = RandomUtils.randomPojo(ApiErrorLogDO.class, logDO -> {
|
||||
logDO.setUserId(userId);
|
||||
logDO.setUserType(userType);
|
||||
logDO.setApplicationName(applicationName);
|
||||
logDO.setRequestUrl(requestUrl);
|
||||
logDO.setExceptionTime(beginTime);
|
||||
logDO.setProcessStatus(progressStatus);
|
||||
// mock 数据
|
||||
ApiErrorLogDO apiErrorLogDO = randomPojo(ApiErrorLogDO.class, o -> {
|
||||
o.setUserId(2233L);
|
||||
o.setUserType(UserTypeEnum.ADMIN.getValue());
|
||||
o.setApplicationName("yudao-test");
|
||||
o.setRequestUrl("foo");
|
||||
o.setExceptionTime(buildTime(2021, 3, 13));
|
||||
o.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
|
||||
});
|
||||
infApiErrorLogMapper.insert(infApiErrorLogDO);
|
||||
|
||||
// 下面几个都是不匹配的数据
|
||||
// userId 不同的
|
||||
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setUserId(3344L)));
|
||||
// userType
|
||||
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue())));
|
||||
// applicationName 不同的
|
||||
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setApplicationName("test")));
|
||||
// requestUrl 不同的
|
||||
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setRequestUrl("bar")));
|
||||
// 构造一个早期时间 2021-02-06 00:00:00
|
||||
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setExceptionTime(buildTime(2021, 2, 6))));
|
||||
// progressStatus 不同的
|
||||
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus())));
|
||||
|
||||
// 构造调用参数
|
||||
apiErrorLogMapper.insert(apiErrorLogDO);
|
||||
// 测试 userId 不匹配
|
||||
apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, o -> o.setUserId(3344L)));
|
||||
// 测试 userType 不匹配
|
||||
apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue())));
|
||||
// 测试 applicationName 不匹配
|
||||
apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setApplicationName("test")));
|
||||
// 测试 requestUrl 不匹配
|
||||
apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setRequestUrl("bar")));
|
||||
// 测试 exceptionTime 不匹配:构造一个早期时间 2021-02-06 00:00:00
|
||||
apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setExceptionTime(buildTime(2021, 2, 6))));
|
||||
// 测试 progressStatus 不匹配
|
||||
apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus())));
|
||||
// 准备参数
|
||||
ApiErrorLogExportReqVO reqVO = new ApiErrorLogExportReqVO();
|
||||
reqVO.setUserId(userId);
|
||||
reqVO.setUserType(userType);
|
||||
reqVO.setApplicationName(applicationName);
|
||||
reqVO.setRequestUrl(requestUrl);
|
||||
reqVO.setExceptionTime((new LocalDateTime[]{buildTime(2021, 3, 12),buildTime(2021, 3, 14)}));
|
||||
reqVO.setProcessStatus(progressStatus);
|
||||
reqVO.setUserId(2233L);
|
||||
reqVO.setUserType(UserTypeEnum.ADMIN.getValue());
|
||||
reqVO.setApplicationName("yudao-test");
|
||||
reqVO.setRequestUrl("foo");
|
||||
reqVO.setExceptionTime(buildBetweenTime(2021, 3, 1, 2021, 3, 31));
|
||||
reqVO.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
|
||||
|
||||
// 调用service方法
|
||||
// 调用
|
||||
List<ApiErrorLogDO> list = apiErrorLogService.getApiErrorLogList(reqVO);
|
||||
|
||||
// 断言,只查到了一条符合条件的
|
||||
assertEquals(1, list.size());
|
||||
assertPojoEquals(infApiErrorLogDO, list.get(0));
|
||||
}
|
||||
|
||||
|
||||
// TODO 芋艿:单元测试,可以拆小一点
|
||||
@Test
|
||||
public void testUpdateApiErrorLogProcess() {
|
||||
// 先构造两条数据,第一条用于抛出异常,第二条用于正常的执行update操作
|
||||
Long processUserId = 2233L;
|
||||
|
||||
ApiErrorLogDO first = RandomUtils.randomPojo(ApiErrorLogDO.class, logDO -> {
|
||||
logDO.setProcessUserId(processUserId);
|
||||
logDO.setUserType(UserTypeEnum.ADMIN.getValue());
|
||||
logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus());
|
||||
});
|
||||
infApiErrorLogMapper.insert(first);
|
||||
|
||||
ApiErrorLogDO second = RandomUtils.randomPojo(ApiErrorLogDO.class, logDO -> {
|
||||
logDO.setProcessUserId(1122L);
|
||||
logDO.setUserType(UserTypeEnum.ADMIN.getValue());
|
||||
logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
|
||||
});
|
||||
infApiErrorLogMapper.insert(second);
|
||||
|
||||
Long firstId = first.getId();
|
||||
Long secondId = second.getId();
|
||||
|
||||
// 执行正常的 update 操作
|
||||
apiErrorLogService.updateApiErrorLogProcess(secondId, ApiErrorLogProcessStatusEnum.DONE.getStatus(), processUserId);
|
||||
ApiErrorLogDO secondSelect = infApiErrorLogMapper.selectOne("id", secondId);
|
||||
|
||||
// id 为 0 查询不到,应该抛出异常 API_ERROR_LOG_NOT_FOUND
|
||||
assertServiceException(() -> apiErrorLogService.updateApiErrorLogProcess(0L, ApiErrorLogProcessStatusEnum.DONE.getStatus(), processUserId), API_ERROR_LOG_NOT_FOUND);
|
||||
// id 为 first 的 progressStatus 为 DONE ,应该抛出 API_ERROR_LOG_PROCESSED
|
||||
assertServiceException(() -> apiErrorLogService.updateApiErrorLogProcess(firstId, ApiErrorLogProcessStatusEnum.DONE.getStatus(), processUserId), API_ERROR_LOG_PROCESSED);
|
||||
// 验证 progressStatus 是否修改成功
|
||||
Assertions.assertEquals(ApiErrorLogProcessStatusEnum.DONE.getStatus(), secondSelect.getProcessStatus());
|
||||
// 验证 progressUserId 是否修改成功
|
||||
Assertions.assertEquals(processUserId, secondSelect.getProcessUserId());
|
||||
assertPojoEquals(apiErrorLogDO, list.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateApiErrorLogAsync() {
|
||||
public void testCreateApiErrorLog() {
|
||||
// 准备参数
|
||||
ApiErrorLogCreateReqDTO createDTO = RandomUtils.randomPojo(ApiErrorLogCreateReqDTO.class,
|
||||
dto -> dto.setUserType(RandomUtil.randomEle(UserTypeEnum.values()).getValue()));
|
||||
ApiErrorLogCreateReqDTO createDTO = randomPojo(ApiErrorLogCreateReqDTO.class);
|
||||
|
||||
// 调用
|
||||
apiErrorLogService.createApiErrorLog(createDTO);
|
||||
// 断言
|
||||
ApiErrorLogDO infApiErrorLogDO = infApiErrorLogMapper.selectOne(null);
|
||||
assertNotNull(infApiErrorLogDO);
|
||||
assertPojoEquals(createDTO, infApiErrorLogDO);
|
||||
ApiErrorLogDO apiErrorLogDO = apiErrorLogMapper.selectOne(null);
|
||||
assertPojoEquals(createDTO, apiErrorLogDO);
|
||||
assertEquals(ApiErrorLogProcessStatusEnum.INIT.getStatus(), apiErrorLogDO.getProcessStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateApiErrorLogProcess_success() {
|
||||
// 准备参数
|
||||
ApiErrorLogDO apiErrorLogDO = randomPojo(ApiErrorLogDO.class,
|
||||
o -> o.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus()));
|
||||
apiErrorLogMapper.insert(apiErrorLogDO);
|
||||
// 准备参数
|
||||
Long id = apiErrorLogDO.getId();
|
||||
Integer processStatus = randomEle(ApiErrorLogProcessStatusEnum.values()).getStatus();
|
||||
Long processUserId = randomLongId();
|
||||
|
||||
// 调用
|
||||
apiErrorLogService.updateApiErrorLogProcess(id, processStatus, processUserId);
|
||||
// 断言
|
||||
ApiErrorLogDO dbApiErrorLogDO = apiErrorLogMapper.selectById(apiErrorLogDO.getId());
|
||||
assertEquals(processStatus, dbApiErrorLogDO.getProcessStatus());
|
||||
assertEquals(processUserId, dbApiErrorLogDO.getProcessUserId());
|
||||
assertNotNull(dbApiErrorLogDO.getProcessTime());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateApiErrorLogProcess_processed() {
|
||||
// 准备参数
|
||||
ApiErrorLogDO apiErrorLogDO = randomPojo(ApiErrorLogDO.class,
|
||||
o -> o.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus()));
|
||||
apiErrorLogMapper.insert(apiErrorLogDO);
|
||||
// 准备参数
|
||||
Long id = apiErrorLogDO.getId();
|
||||
Integer processStatus = randomEle(ApiErrorLogProcessStatusEnum.values()).getStatus();
|
||||
Long processUserId = randomLongId();
|
||||
|
||||
// 调用,并断言异常
|
||||
assertServiceException(() ->
|
||||
apiErrorLogService.updateApiErrorLogProcess(id, processStatus, processUserId),
|
||||
API_ERROR_LOG_PROCESSED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateApiErrorLogProcess_notFound() {
|
||||
// 准备参数
|
||||
Long id = randomLongId();
|
||||
Integer processStatus = randomEle(ApiErrorLogProcessStatusEnum.values()).getStatus();
|
||||
Long processUserId = randomLongId();
|
||||
|
||||
// 调用,并断言异常
|
||||
assertServiceException(() ->
|
||||
apiErrorLogService.updateApiErrorLogProcess(id, processStatus, processUserId),
|
||||
API_ERROR_LOG_NOT_FOUND);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "infra_config" (
|
||||
"id" bigint(20) NOT NULL GENERATED BY DEFAULT AS IDENTITY,
|
||||
"id" bigint(20) NOT NULL GENERATED BY DEFAULT AS IDENTITY COMMENT '编号',
|
||||
"category" varchar(50) NOT NULL,
|
||||
"type" tinyint NOT NULL,
|
||||
"name" varchar(100) NOT NULL DEFAULT '',
|
||||
"name" varchar(100) NOT NULL DEFAULT '' COMMENT '名字',
|
||||
"config_key" varchar(100) NOT NULL DEFAULT '',
|
||||
"value" varchar(500) NOT NULL DEFAULT '',
|
||||
"visible" bit NOT NULL,
|
||||
|
Reference in New Issue
Block a user