infra:完善 logger 的单元测试

This commit is contained in:
YunaiV 2023-02-04 09:10:09 +08:00
parent ebf441ef13
commit ec913adb20
6 changed files with 208 additions and 248 deletions

View File

@ -1,6 +1,5 @@
package cn.iocoder.yudao.module.infra.service.logger; 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.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO; 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.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.convert.logger.ApiErrorLogConvert;
import cn.iocoder.yudao.module.infra.dal.dataobject.logger.ApiErrorLogDO; 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.dal.mysql.logger.ApiErrorLogMapper;
import cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants;
import cn.iocoder.yudao.module.infra.enums.logger.ApiErrorLogProcessStatusEnum; import cn.iocoder.yudao.module.infra.enums.logger.ApiErrorLogProcessStatusEnum;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
@ -17,6 +15,10 @@ import javax.annotation.Resource;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.List; 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 实现类 * API 错误日志 Service 实现类
* *
@ -31,8 +33,8 @@ public class ApiErrorLogServiceImpl implements ApiErrorLogService {
@Override @Override
public void createApiErrorLog(ApiErrorLogCreateReqDTO createDTO) { public void createApiErrorLog(ApiErrorLogCreateReqDTO createDTO) {
ApiErrorLogDO apiErrorLog = ApiErrorLogConvert.INSTANCE.convert(createDTO); ApiErrorLogDO apiErrorLog = ApiErrorLogConvert.INSTANCE.convert(createDTO)
apiErrorLog.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus()); .setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
apiErrorLogMapper.insert(apiErrorLog); apiErrorLogMapper.insert(apiErrorLog);
} }
@ -50,10 +52,10 @@ public class ApiErrorLogServiceImpl implements ApiErrorLogService {
public void updateApiErrorLogProcess(Long id, Integer processStatus, Long processUserId) { public void updateApiErrorLogProcess(Long id, Integer processStatus, Long processUserId) {
ApiErrorLogDO errorLog = apiErrorLogMapper.selectById(id); ApiErrorLogDO errorLog = apiErrorLogMapper.selectById(id);
if (errorLog == null) { 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())) { 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) apiErrorLogMapper.updateById(ApiErrorLogDO.builder().id(id).processStatus(processStatus)

View File

@ -45,7 +45,7 @@ public class TestDemoServiceImpl implements TestDemoService {
@CacheEvict(value = "test", key = "#updateReqVO.id") @CacheEvict(value = "test", key = "#updateReqVO.id")
public void updateTestDemo(TestDemoUpdateReqVO updateReqVO) { public void updateTestDemo(TestDemoUpdateReqVO updateReqVO) {
// 校验存在 // 校验存在
this.validateTestDemoExists(updateReqVO.getId()); validateTestDemoExists(updateReqVO.getId());
// 更新 // 更新
TestDemoDO updateObj = TestDemoConvert.INSTANCE.convert(updateReqVO); TestDemoDO updateObj = TestDemoConvert.INSTANCE.convert(updateReqVO);
testDemoMapper.updateById(updateObj); testDemoMapper.updateById(updateObj);
@ -55,7 +55,7 @@ public class TestDemoServiceImpl implements TestDemoService {
@CacheEvict(value = "test", key = "#id") @CacheEvict(value = "test", key = "#id")
public void deleteTestDemo(Long id) { public void deleteTestDemo(Long id) {
// 校验存在 // 校验存在
this.validateTestDemoExists(id); validateTestDemoExists(id);
// 删除 // 删除
testDemoMapper.deleteById(id); testDemoMapper.deleteById(id);
} }

View File

@ -39,7 +39,7 @@ public class ${table.className}ServiceImpl implements ${table.className}Service
@Override @Override
public void update${simpleClassName}(${sceneEnum.prefixClass}${table.className}UpdateReqVO updateReqVO) { 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); ${table.className}DO updateObj = ${table.className}Convert.INSTANCE.convert(updateReqVO);
${classNameVar}Mapper.updateById(updateObj); ${classNameVar}Mapper.updateById(updateObj);
@ -48,7 +48,7 @@ public class ${table.className}ServiceImpl implements ${table.className}Service
@Override @Override
public void delete${simpleClassName}(${primaryColumn.javaType} id) { public void delete${simpleClassName}(${primaryColumn.javaType} id) {
// 校验存在 // 校验存在
this.validate${simpleClassName}Exists(id); validate${simpleClassName}Exists(id);
// 删除 // 删除
${classNameVar}Mapper.deleteById(id); ${classNameVar}Mapper.deleteById(id);
} }

View File

@ -52,7 +52,7 @@ import static org.mockito.Mockito.*;
#if (${column.listOperation}) #if (${column.listOperation})
#set ($JavaField = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})##首字母大写 #set ($JavaField = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})##首字母大写
#if (${column.listOperationCondition} == "BETWEEN")## BETWEEN 的情况 #if (${column.listOperationCondition} == "BETWEEN")## BETWEEN 的情况
reqVO.set${JavaField}((new LocalDateTime[]{})); reqVO.set${JavaField}(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
#else #else
reqVO.set$JavaField(null); reqVO.set$JavaField(null);
#end #end

View File

@ -1,12 +1,9 @@
package cn.iocoder.yudao.module.infra.service.logger; 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.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants; import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
import cn.iocoder.yudao.framework.common.pojo.PageResult; 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.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.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.ApiAccessLogExportReqVO;
import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogPageReqVO; 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 org.springframework.context.annotation.Import;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List; 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.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.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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@Import(ApiAccessLogServiceImpl.class) @Import(ApiAccessLogServiceImpl.class)
public class ApiAccessLogServiceImplTest extends BaseDbUnitTest { public class ApiAccessLogServiceImplTest extends BaseDbUnitTest {
@Resource @Resource
private ApiAccessLogService apiAccessLogService; private ApiAccessLogServiceImpl apiAccessLogService;
@Resource @Resource
private ApiAccessLogMapper apiAccessLogMapper; private ApiAccessLogMapper apiAccessLogMapper;
@Test @Test
public void testGetApiAccessLogPage() { public void testGetApiAccessLogPage() {
// 构造测试数据 ApiAccessLogDO apiAccessLogDO = randomPojo(ApiAccessLogDO.class, o -> {
long userId = 2233L; o.setUserId(2233L);
int userType = UserTypeEnum.ADMIN.getValue(); o.setUserType(UserTypeEnum.ADMIN.getValue());
String applicationName = "yudao-test"; o.setApplicationName("yudao-test");
String requestUrl = "foo"; o.setRequestUrl("foo");
LocalDateTime beginTime = buildTime(2021, 3, 13); o.setBeginTime(buildTime(2021, 3, 13));
int duration = 1000; o.setDuration(1000);
int resultCode = GlobalErrorCodeConstants.SUCCESS.getCode(); o.setResultCode(GlobalErrorCodeConstants.SUCCESS.getCode());
ApiAccessLogDO infApiAccessLogDO = RandomUtils.randomPojo(ApiAccessLogDO.class, dto -> {
dto.setUserId(userId);
dto.setUserType(userType);
dto.setApplicationName(applicationName);
dto.setRequestUrl(requestUrl);
dto.setBeginTime(beginTime);
dto.setDuration(duration);
dto.setResultCode(resultCode);
}); });
apiAccessLogMapper.insert(infApiAccessLogDO); apiAccessLogMapper.insert(apiAccessLogDO);
// 测试 userId 不匹配
// 下面几个都是不匹配的数据 apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setUserId(3344L)));
// userId 不同的 // 测试 userType 不匹配
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setUserId(3344L))); apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setUserType(UserTypeEnum.MEMBER.getValue())));
// userType // 测试 applicationName 不匹配
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue()))); apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setApplicationName("test")));
// applicationName 不同的 // 测试 requestUrl 不匹配
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setApplicationName("test"))); apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setRequestUrl("bar")));
// requestUrl 不同的 // 测试 beginTime 不匹配构造一个早期时间 2021-02-06 00:00:00
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setRequestUrl("bar"))); apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setBeginTime(buildTime(2021, 2, 6))));
// 构造一个早期时间 2021-02-06 00:00:00 // 测试 duration 不匹配
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setBeginTime(buildTime(2021, 2, 6)))); apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setDuration(100)));
// duration 不同的 // 测试 resultCode 不匹配
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setDuration(100))); apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setResultCode(2)));
// resultCode 不同的 // 准备参数
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setResultCode(2)));
// 构造调用参数
ApiAccessLogPageReqVO reqVO = new ApiAccessLogPageReqVO(); ApiAccessLogPageReqVO reqVO = new ApiAccessLogPageReqVO();
reqVO.setUserId(userId); reqVO.setUserId(2233L);
reqVO.setUserType(userType); reqVO.setUserType(UserTypeEnum.ADMIN.getValue());
reqVO.setApplicationName(applicationName); reqVO.setApplicationName("yudao-test");
reqVO.setRequestUrl(requestUrl); reqVO.setRequestUrl("foo");
reqVO.setBeginTime((new LocalDateTime[]{buildTime(2021, 3, 12),buildTime(2021, 3, 14)})); reqVO.setBeginTime(buildBetweenTime(2021, 3, 13, 2021, 3, 13));
reqVO.setDuration(duration); reqVO.setDuration(1000);
reqVO.setResultCode(resultCode); reqVO.setResultCode(GlobalErrorCodeConstants.SUCCESS.getCode());
// 调用service方法 // 调用
PageResult<ApiAccessLogDO> pageResult = apiAccessLogService.getApiAccessLogPage(reqVO); PageResult<ApiAccessLogDO> pageResult = apiAccessLogService.getApiAccessLogPage(reqVO);
// 断言只查到了一条符合条件的 // 断言只查到了一条符合条件的
assertEquals(1, pageResult.getTotal()); assertEquals(1, pageResult.getTotal());
assertEquals(1, pageResult.getList().size()); assertEquals(1, pageResult.getList().size());
assertPojoEquals(infApiAccessLogDO, pageResult.getList().get(0)); assertPojoEquals(apiAccessLogDO, pageResult.getList().get(0));
} }
@Test @Test
public void testGetApiAccessLogList() { public void testGetApiAccessLogList() {
// 构造测试数据 ApiAccessLogDO apiAccessLogDO = randomPojo(ApiAccessLogDO.class, o -> {
long userId = 2233L; o.setUserId(2233L);
int userType = UserTypeEnum.ADMIN.getValue(); o.setUserType(UserTypeEnum.ADMIN.getValue());
String applicationName = "yudao-test"; o.setApplicationName("yudao-test");
String requestUrl = "foo"; o.setRequestUrl("foo");
LocalDateTime beginTime = buildTime(2021, 3, 13); o.setBeginTime(buildTime(2021, 3, 13));
int duration = 1000; o.setDuration(1000);
int resultCode = GlobalErrorCodeConstants.SUCCESS.getCode(); o.setResultCode(GlobalErrorCodeConstants.SUCCESS.getCode());
ApiAccessLogDO infApiAccessLogDO = RandomUtils.randomPojo(ApiAccessLogDO.class, dto -> {
dto.setUserId(userId);
dto.setUserType(userType);
dto.setApplicationName(applicationName);
dto.setRequestUrl(requestUrl);
dto.setBeginTime(beginTime);
dto.setDuration(duration);
dto.setResultCode(resultCode);
}); });
apiAccessLogMapper.insert(infApiAccessLogDO); apiAccessLogMapper.insert(apiAccessLogDO);
// 测试 userId 不匹配
// 下面几个都是不匹配的数据 apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setUserId(3344L)));
// userId 不同的 // 测试 userType 不匹配
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setUserId(3344L))); apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setUserType(UserTypeEnum.MEMBER.getValue())));
// userType // 测试 applicationName 不匹配
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue()))); apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setApplicationName("test")));
// applicationName 不同的 // 测试 requestUrl 不匹配
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setApplicationName("test"))); apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setRequestUrl("bar")));
// requestUrl 不同的 // 测试 beginTime 不匹配构造一个早期时间 2021-02-06 00:00:00
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setRequestUrl("bar"))); apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setBeginTime(buildTime(2021, 2, 6))));
// 构造一个早期时间 2021-02-06 00:00:00 // 测试 duration 不匹配
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setBeginTime(buildTime(2021, 2, 6)))); apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setDuration(100)));
// duration 不同的 // 测试 resultCode 不匹配
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setDuration(100))); apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setResultCode(2)));
// resultCode 不同的 // 准备参数
apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setResultCode(2)));
// 构造调用参数
ApiAccessLogExportReqVO reqVO = new ApiAccessLogExportReqVO(); ApiAccessLogExportReqVO reqVO = new ApiAccessLogExportReqVO();
reqVO.setUserId(userId); reqVO.setUserId(2233L);
reqVO.setUserType(userType); reqVO.setUserType(UserTypeEnum.ADMIN.getValue());
reqVO.setApplicationName(applicationName); reqVO.setApplicationName("yudao-test");
reqVO.setRequestUrl(requestUrl); reqVO.setRequestUrl("foo");
reqVO.setBeginTime((new LocalDateTime[]{buildTime(2021, 3, 12),buildTime(2021, 3, 14)})); reqVO.setBeginTime(buildBetweenTime(2021, 3, 13, 2021, 3, 13));
reqVO.setDuration(duration); reqVO.setDuration(1000);
reqVO.setResultCode(resultCode); reqVO.setResultCode(GlobalErrorCodeConstants.SUCCESS.getCode());
// 调用service方法 // 调用
List<ApiAccessLogDO> list = apiAccessLogService.getApiAccessLogList(reqVO); List<ApiAccessLogDO> list = apiAccessLogService.getApiAccessLogList(reqVO);
// 断言只查到了一条符合条件的 // 断言只查到了一条符合条件的
assertEquals(1, list.size()); assertEquals(1, list.size());
assertPojoEquals(infApiAccessLogDO, list.get(0)); assertPojoEquals(apiAccessLogDO, list.get(0));
} }
@Test @Test
public void testCreateApiAccessLogAsync() { public void testCreateApiAccessLog() {
// 准备参数 // 准备参数
ApiAccessLogCreateReqDTO createDTO = RandomUtils.randomPojo(ApiAccessLogCreateReqDTO.class, ApiAccessLogCreateReqDTO createDTO = randomPojo(ApiAccessLogCreateReqDTO.class);
dto -> dto.setUserType(RandomUtil.randomEle(UserTypeEnum.values()).getValue()));
// 调用 // 调用
apiAccessLogService.createApiAccessLog(createDTO); apiAccessLogService.createApiAccessLog(createDTO);
// 断言 // 断言
ApiAccessLogDO infApiAccessLogDO = apiAccessLogMapper.selectOne(null); ApiAccessLogDO apiAccessLogDO = apiAccessLogMapper.selectOne(null);
assertNotNull(infApiAccessLogDO); assertPojoEquals(createDTO, apiAccessLogDO);
assertPojoEquals(createDTO, infApiAccessLogDO);
} }
} }

View File

@ -1,28 +1,28 @@
package cn.iocoder.yudao.module.infra.service.logger; 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.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult; 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.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.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.ApiErrorLogExportReqVO;
import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiErrorLogPageReqVO; 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.dataobject.logger.ApiErrorLogDO;
import cn.iocoder.yudao.module.infra.dal.mysql.logger.ApiErrorLogMapper; import cn.iocoder.yudao.module.infra.dal.mysql.logger.ApiErrorLogMapper;
import cn.iocoder.yudao.module.infra.enums.logger.ApiErrorLogProcessStatusEnum; import cn.iocoder.yudao.module.infra.enums.logger.ApiErrorLogProcessStatusEnum;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List; 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.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.assertPojoEquals;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; 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_NOT_FOUND;
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.API_ERROR_LOG_PROCESSED; import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.API_ERROR_LOG_PROCESSED;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ -35,161 +35,150 @@ public class ApiErrorLogServiceImplTest extends BaseDbUnitTest {
private ApiErrorLogServiceImpl apiErrorLogService; private ApiErrorLogServiceImpl apiErrorLogService;
@Resource @Resource
private ApiErrorLogMapper infApiErrorLogMapper; private ApiErrorLogMapper apiErrorLogMapper;
@Test @Test
public void testGetApiErrorLogPage() { public void testGetApiErrorLogPage() {
// 构造测试数据 // mock 数据
long userId = 2233L; ApiErrorLogDO apiErrorLogDO = randomPojo(ApiErrorLogDO.class, o -> {
int userType = UserTypeEnum.ADMIN.getValue(); o.setUserId(2233L);
String applicationName = "yudao-test"; o.setUserType(UserTypeEnum.ADMIN.getValue());
String requestUrl = "foo"; o.setApplicationName("yudao-test");
LocalDateTime beginTime = buildTime(2021, 3, 13); o.setRequestUrl("foo");
int progressStatus = ApiErrorLogProcessStatusEnum.INIT.getStatus(); o.setExceptionTime(buildTime(2021, 3, 13));
o.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
ApiErrorLogDO infApiErrorLogDO = RandomUtils.randomPojo(ApiErrorLogDO.class, logDO -> {
logDO.setUserId(userId);
logDO.setUserType(userType);
logDO.setApplicationName(applicationName);
logDO.setRequestUrl(requestUrl);
logDO.setExceptionTime(beginTime);
logDO.setProcessStatus(progressStatus);
}); });
infApiErrorLogMapper.insert(infApiErrorLogDO); apiErrorLogMapper.insert(apiErrorLogDO);
// 测试 userId 不匹配
// 下面几个都是不匹配的数据 apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, o -> o.setUserId(3344L)));
// userId 不同的 // 测试 userType 不匹配
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setUserId(3344L))); apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue())));
// userType // 测试 applicationName 不匹配
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue()))); apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setApplicationName("test")));
// applicationName 不同的 // 测试 requestUrl 不匹配
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setApplicationName("test"))); apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setRequestUrl("bar")));
// requestUrl 不同的 // 测试 exceptionTime 不匹配构造一个早期时间 2021-02-06 00:00:00
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setRequestUrl("bar"))); apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setExceptionTime(buildTime(2021, 2, 6))));
// 构造一个早期时间 2021-02-06 00:00:00 // 测试 progressStatus 不匹配
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setExceptionTime(buildTime(2021, 2, 6)))); apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus())));
// progressStatus 不同的 // 准备参数
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus())));
// 构造调用参数
ApiErrorLogPageReqVO reqVO = new ApiErrorLogPageReqVO(); ApiErrorLogPageReqVO reqVO = new ApiErrorLogPageReqVO();
reqVO.setUserId(userId); reqVO.setUserId(2233L);
reqVO.setUserType(userType); reqVO.setUserType(UserTypeEnum.ADMIN.getValue());
reqVO.setApplicationName(applicationName); reqVO.setApplicationName("yudao-test");
reqVO.setRequestUrl(requestUrl); reqVO.setRequestUrl("foo");
reqVO.setExceptionTime((new LocalDateTime[]{buildTime(2021, 3, 12),buildTime(2021, 3, 14)})); reqVO.setExceptionTime(buildBetweenTime(2021, 3, 1, 2021, 3, 31));
reqVO.setProcessStatus(progressStatus); reqVO.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
// 调用service方法 // 调用
PageResult<ApiErrorLogDO> pageResult = apiErrorLogService.getApiErrorLogPage(reqVO); PageResult<ApiErrorLogDO> pageResult = apiErrorLogService.getApiErrorLogPage(reqVO);
// 断言只查到了一条符合条件的 // 断言只查到了一条符合条件的
assertEquals(1, pageResult.getTotal()); assertEquals(1, pageResult.getTotal());
assertEquals(1, pageResult.getList().size()); assertEquals(1, pageResult.getList().size());
assertPojoEquals(infApiErrorLogDO, pageResult.getList().get(0)); assertPojoEquals(apiErrorLogDO, pageResult.getList().get(0));
} }
@Test @Test
public void testGetApiErrorLogList() { public void testGetApiErrorLogList() {
// 构造测试数据 // mock 数据
long userId = 2233L; ApiErrorLogDO apiErrorLogDO = randomPojo(ApiErrorLogDO.class, o -> {
int userType = UserTypeEnum.ADMIN.getValue(); o.setUserId(2233L);
String applicationName = "yudao-test"; o.setUserType(UserTypeEnum.ADMIN.getValue());
String requestUrl = "foo"; o.setApplicationName("yudao-test");
LocalDateTime beginTime = buildTime(2021, 3, 13); o.setRequestUrl("foo");
int progressStatus = ApiErrorLogProcessStatusEnum.INIT.getStatus(); o.setExceptionTime(buildTime(2021, 3, 13));
o.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
ApiErrorLogDO infApiErrorLogDO = RandomUtils.randomPojo(ApiErrorLogDO.class, logDO -> {
logDO.setUserId(userId);
logDO.setUserType(userType);
logDO.setApplicationName(applicationName);
logDO.setRequestUrl(requestUrl);
logDO.setExceptionTime(beginTime);
logDO.setProcessStatus(progressStatus);
}); });
infApiErrorLogMapper.insert(infApiErrorLogDO); apiErrorLogMapper.insert(apiErrorLogDO);
// 测试 userId 不匹配
// 下面几个都是不匹配的数据 apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, o -> o.setUserId(3344L)));
// userId 不同的 // 测试 userType 不匹配
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setUserId(3344L))); apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue())));
// userType // 测试 applicationName 不匹配
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue()))); apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setApplicationName("test")));
// applicationName 不同的 // 测试 requestUrl 不匹配
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setApplicationName("test"))); apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setRequestUrl("bar")));
// requestUrl 不同的 // 测试 exceptionTime 不匹配构造一个早期时间 2021-02-06 00:00:00
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setRequestUrl("bar"))); apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setExceptionTime(buildTime(2021, 2, 6))));
// 构造一个早期时间 2021-02-06 00:00:00 // 测试 progressStatus 不匹配
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setExceptionTime(buildTime(2021, 2, 6)))); apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus())));
// progressStatus 不同的 // 准备参数
infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus())));
// 构造调用参数
ApiErrorLogExportReqVO reqVO = new ApiErrorLogExportReqVO(); ApiErrorLogExportReqVO reqVO = new ApiErrorLogExportReqVO();
reqVO.setUserId(userId); reqVO.setUserId(2233L);
reqVO.setUserType(userType); reqVO.setUserType(UserTypeEnum.ADMIN.getValue());
reqVO.setApplicationName(applicationName); reqVO.setApplicationName("yudao-test");
reqVO.setRequestUrl(requestUrl); reqVO.setRequestUrl("foo");
reqVO.setExceptionTime((new LocalDateTime[]{buildTime(2021, 3, 12),buildTime(2021, 3, 14)})); reqVO.setExceptionTime(buildBetweenTime(2021, 3, 1, 2021, 3, 31));
reqVO.setProcessStatus(progressStatus); reqVO.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
// 调用service方法 // 调用
List<ApiErrorLogDO> list = apiErrorLogService.getApiErrorLogList(reqVO); List<ApiErrorLogDO> list = apiErrorLogService.getApiErrorLogList(reqVO);
// 断言只查到了一条符合条件的 // 断言只查到了一条符合条件的
assertEquals(1, list.size()); assertEquals(1, list.size());
assertPojoEquals(infApiErrorLogDO, list.get(0)); assertPojoEquals(apiErrorLogDO, list.get(0));
}
// TODO 芋艿单元测试可以拆小一点
@Test
public void testUpdateApiErrorLogProcess() {
// 先构造两条数据第一条用于抛出异常第二条用于正常的执行update操作
Long processUserId = 2233L;
ApiErrorLogDO first = RandomUtils.randomPojo(ApiErrorLogDO.class, logDO -> {
logDO.setProcessUserId(processUserId);
logDO.setUserType(UserTypeEnum.ADMIN.getValue());
logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus());
});
infApiErrorLogMapper.insert(first);
ApiErrorLogDO second = RandomUtils.randomPojo(ApiErrorLogDO.class, logDO -> {
logDO.setProcessUserId(1122L);
logDO.setUserType(UserTypeEnum.ADMIN.getValue());
logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
});
infApiErrorLogMapper.insert(second);
Long firstId = first.getId();
Long secondId = second.getId();
// 执行正常的 update 操作
apiErrorLogService.updateApiErrorLogProcess(secondId, ApiErrorLogProcessStatusEnum.DONE.getStatus(), processUserId);
ApiErrorLogDO secondSelect = infApiErrorLogMapper.selectOne("id", secondId);
// id 0 查询不到应该抛出异常 API_ERROR_LOG_NOT_FOUND
assertServiceException(() -> apiErrorLogService.updateApiErrorLogProcess(0L, ApiErrorLogProcessStatusEnum.DONE.getStatus(), processUserId), API_ERROR_LOG_NOT_FOUND);
// id first progressStatus DONE 应该抛出 API_ERROR_LOG_PROCESSED
assertServiceException(() -> apiErrorLogService.updateApiErrorLogProcess(firstId, ApiErrorLogProcessStatusEnum.DONE.getStatus(), processUserId), API_ERROR_LOG_PROCESSED);
// 验证 progressStatus 是否修改成功
Assertions.assertEquals(ApiErrorLogProcessStatusEnum.DONE.getStatus(), secondSelect.getProcessStatus());
// 验证 progressUserId 是否修改成功
Assertions.assertEquals(processUserId, secondSelect.getProcessUserId());
} }
@Test @Test
public void testCreateApiErrorLogAsync() { public void testCreateApiErrorLog() {
// 准备参数 // 准备参数
ApiErrorLogCreateReqDTO createDTO = RandomUtils.randomPojo(ApiErrorLogCreateReqDTO.class, ApiErrorLogCreateReqDTO createDTO = randomPojo(ApiErrorLogCreateReqDTO.class);
dto -> dto.setUserType(RandomUtil.randomEle(UserTypeEnum.values()).getValue()));
// 调用 // 调用
apiErrorLogService.createApiErrorLog(createDTO); apiErrorLogService.createApiErrorLog(createDTO);
// 断言 // 断言
ApiErrorLogDO infApiErrorLogDO = infApiErrorLogMapper.selectOne(null); ApiErrorLogDO apiErrorLogDO = apiErrorLogMapper.selectOne(null);
assertNotNull(infApiErrorLogDO); assertPojoEquals(createDTO, apiErrorLogDO);
assertPojoEquals(createDTO, infApiErrorLogDO); 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);
} }
} }