mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-22 15:21:53 +08:00
infra:完善 logger 的单元测试
This commit is contained in:
parent
ebf441ef13
commit
ec913adb20
@ -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);
|
||||
}
|
||||
|
@ -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,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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user