Commit ec913adb207b88fde0d0bc95f5db5386d4314038

Authored by YunaiV
1 parent ebf441ef

infra:完善 logger 的单元测试

yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/logger/ApiErrorLogServiceImpl.java
1 1 package cn.iocoder.yudao.module.infra.service.logger;
2 2  
3   -import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
4 3 import cn.iocoder.yudao.framework.common.pojo.PageResult;
5 4 import cn.iocoder.yudao.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO;
6 5 import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiErrorLogExportReqVO;
... ... @@ -8,7 +7,6 @@ import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiE
8 7 import cn.iocoder.yudao.module.infra.convert.logger.ApiErrorLogConvert;
9 8 import cn.iocoder.yudao.module.infra.dal.dataobject.logger.ApiErrorLogDO;
10 9 import cn.iocoder.yudao.module.infra.dal.mysql.logger.ApiErrorLogMapper;
11   -import cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants;
12 10 import cn.iocoder.yudao.module.infra.enums.logger.ApiErrorLogProcessStatusEnum;
13 11 import org.springframework.stereotype.Service;
14 12 import org.springframework.validation.annotation.Validated;
... ... @@ -17,6 +15,10 @@ import javax.annotation.Resource;
17 15 import java.time.LocalDateTime;
18 16 import java.util.List;
19 17  
  18 +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
  19 +import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.API_ERROR_LOG_NOT_FOUND;
  20 +import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.API_ERROR_LOG_PROCESSED;
  21 +
20 22 /**
21 23 * API 错误日志 Service 实现类
22 24 *
... ... @@ -31,8 +33,8 @@ public class ApiErrorLogServiceImpl implements ApiErrorLogService {
31 33  
32 34 @Override
33 35 public void createApiErrorLog(ApiErrorLogCreateReqDTO createDTO) {
34   - ApiErrorLogDO apiErrorLog = ApiErrorLogConvert.INSTANCE.convert(createDTO);
35   - apiErrorLog.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
  36 + ApiErrorLogDO apiErrorLog = ApiErrorLogConvert.INSTANCE.convert(createDTO)
  37 + .setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
36 38 apiErrorLogMapper.insert(apiErrorLog);
37 39 }
38 40  
... ... @@ -50,10 +52,10 @@ public class ApiErrorLogServiceImpl implements ApiErrorLogService {
50 52 public void updateApiErrorLogProcess(Long id, Integer processStatus, Long processUserId) {
51 53 ApiErrorLogDO errorLog = apiErrorLogMapper.selectById(id);
52 54 if (errorLog == null) {
53   - throw ServiceExceptionUtil.exception(ErrorCodeConstants.API_ERROR_LOG_NOT_FOUND);
  55 + throw exception(API_ERROR_LOG_NOT_FOUND);
54 56 }
55 57 if (!ApiErrorLogProcessStatusEnum.INIT.getStatus().equals(errorLog.getProcessStatus())) {
56   - throw ServiceExceptionUtil.exception(ErrorCodeConstants.API_ERROR_LOG_PROCESSED);
  58 + throw exception(API_ERROR_LOG_PROCESSED);
57 59 }
58 60 // 标记处理
59 61 apiErrorLogMapper.updateById(ApiErrorLogDO.builder().id(id).processStatus(processStatus)
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/test/TestDemoServiceImpl.java
... ... @@ -45,7 +45,7 @@ public class TestDemoServiceImpl implements TestDemoService {
45 45 @CacheEvict(value = "test", key = "#updateReqVO.id")
46 46 public void updateTestDemo(TestDemoUpdateReqVO updateReqVO) {
47 47 // 校验存在
48   - this.validateTestDemoExists(updateReqVO.getId());
  48 + validateTestDemoExists(updateReqVO.getId());
49 49 // 更新
50 50 TestDemoDO updateObj = TestDemoConvert.INSTANCE.convert(updateReqVO);
51 51 testDemoMapper.updateById(updateObj);
... ... @@ -55,7 +55,7 @@ public class TestDemoServiceImpl implements TestDemoService {
55 55 @CacheEvict(value = "test", key = "#id")
56 56 public void deleteTestDemo(Long id) {
57 57 // 校验存在
58   - this.validateTestDemoExists(id);
  58 + validateTestDemoExists(id);
59 59 // 删除
60 60 testDemoMapper.deleteById(id);
61 61 }
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/java/service/serviceImpl.vm
... ... @@ -39,7 +39,7 @@ public class ${table.className}ServiceImpl implements ${table.className}Service
39 39 @Override
40 40 public void update${simpleClassName}(${sceneEnum.prefixClass}${table.className}UpdateReqVO updateReqVO) {
41 41 // 校验存在
42   - this.validate${simpleClassName}Exists(updateReqVO.getId());
  42 + validate${simpleClassName}Exists(updateReqVO.getId());
43 43 // 更新
44 44 ${table.className}DO updateObj = ${table.className}Convert.INSTANCE.convert(updateReqVO);
45 45 ${classNameVar}Mapper.updateById(updateObj);
... ... @@ -48,7 +48,7 @@ public class ${table.className}ServiceImpl implements ${table.className}Service
48 48 @Override
49 49 public void delete${simpleClassName}(${primaryColumn.javaType} id) {
50 50 // 校验存在
51   - this.validate${simpleClassName}Exists(id);
  51 + validate${simpleClassName}Exists(id);
52 52 // 删除
53 53 ${classNameVar}Mapper.deleteById(id);
54 54 }
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/java/test/serviceTest.vm
... ... @@ -52,7 +52,7 @@ import static org.mockito.Mockito.*;
52 52 #if (${column.listOperation})
53 53 #set ($JavaField = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})##首字母大写
54 54 #if (${column.listOperationCondition} == "BETWEEN")## BETWEEN 的情况
55   - reqVO.set${JavaField}((new LocalDateTime[]{}));
  55 + reqVO.set${JavaField}(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
56 56 #else
57 57 reqVO.set$JavaField(null);
58 58 #end
... ...
yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/logger/ApiAccessLogServiceImplTest.java
1 1 package cn.iocoder.yudao.module.infra.service.logger;
2 2  
3   -import cn.hutool.core.util.RandomUtil;
4 3 import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
5 4 import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
6 5 import cn.iocoder.yudao.framework.common.pojo.PageResult;
7   -import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
8 6 import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
9   -import cn.iocoder.yudao.framework.test.core.util.RandomUtils;
10 7 import cn.iocoder.yudao.module.infra.api.logger.dto.ApiAccessLogCreateReqDTO;
11 8 import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogExportReqVO;
12 9 import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogPageReqVO;
... ... @@ -16,149 +13,121 @@ import org.junit.jupiter.api.Test;
16 13 import org.springframework.context.annotation.Import;
17 14  
18 15 import javax.annotation.Resource;
19   -import java.time.LocalDateTime;
20 16 import java.util.List;
21 17  
  18 +import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
22 19 import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
  20 +import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
23 21 import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
  22 +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
24 23 import static org.junit.jupiter.api.Assertions.assertEquals;
25   -import static org.junit.jupiter.api.Assertions.assertNotNull;
26 24  
27 25 @Import(ApiAccessLogServiceImpl.class)
28 26 public class ApiAccessLogServiceImplTest extends BaseDbUnitTest {
29 27  
30 28 @Resource
31   - private ApiAccessLogService apiAccessLogService;
  29 + private ApiAccessLogServiceImpl apiAccessLogService;
32 30  
33 31 @Resource
34 32 private ApiAccessLogMapper apiAccessLogMapper;
35 33  
36 34 @Test
37 35 public void testGetApiAccessLogPage() {
38   - // 构造测试数据
39   - long userId = 2233L;
40   - int userType = UserTypeEnum.ADMIN.getValue();
41   - String applicationName = "yudao-test";
42   - String requestUrl = "foo";
43   - LocalDateTime beginTime = buildTime(2021, 3, 13);
44   - int duration = 1000;
45   - int resultCode = GlobalErrorCodeConstants.SUCCESS.getCode();
46   -
47   - ApiAccessLogDO infApiAccessLogDO = RandomUtils.randomPojo(ApiAccessLogDO.class, dto -> {
48   - dto.setUserId(userId);
49   - dto.setUserType(userType);
50   - dto.setApplicationName(applicationName);
51   - dto.setRequestUrl(requestUrl);
52   - dto.setBeginTime(beginTime);
53   - dto.setDuration(duration);
54   - dto.setResultCode(resultCode);
  36 + ApiAccessLogDO apiAccessLogDO = randomPojo(ApiAccessLogDO.class, o -> {
  37 + o.setUserId(2233L);
  38 + o.setUserType(UserTypeEnum.ADMIN.getValue());
  39 + o.setApplicationName("yudao-test");
  40 + o.setRequestUrl("foo");
  41 + o.setBeginTime(buildTime(2021, 3, 13));
  42 + o.setDuration(1000);
  43 + o.setResultCode(GlobalErrorCodeConstants.SUCCESS.getCode());
55 44 });
56   - apiAccessLogMapper.insert(infApiAccessLogDO);
57   -
58   - // 下面几个都是不匹配的数据
59   - // userId 不同的
60   - apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setUserId(3344L)));
61   - // userType
62   - apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue())));
63   - // applicationName 不同的
64   - apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setApplicationName("test")));
65   - // requestUrl 不同的
66   - apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setRequestUrl("bar")));
67   - // 构造一个早期时间 2021-02-06 00:00:00
68   - apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setBeginTime(buildTime(2021, 2, 6))));
69   - // duration 不同的
70   - apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setDuration(100)));
71   - // resultCode 不同的
72   - apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setResultCode(2)));
73   -
74   - // 构造调用参数
  45 + apiAccessLogMapper.insert(apiAccessLogDO);
  46 + // 测试 userId 不匹配
  47 + apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setUserId(3344L)));
  48 + // 测试 userType 不匹配
  49 + apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setUserType(UserTypeEnum.MEMBER.getValue())));
  50 + // 测试 applicationName 不匹配
  51 + apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setApplicationName("test")));
  52 + // 测试 requestUrl 不匹配
  53 + apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setRequestUrl("bar")));
  54 + // 测试 beginTime 不匹配:构造一个早期时间 2021-02-06 00:00:00
  55 + apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setBeginTime(buildTime(2021, 2, 6))));
  56 + // 测试 duration 不匹配
  57 + apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setDuration(100)));
  58 + // 测试 resultCode 不匹配
  59 + apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setResultCode(2)));
  60 + // 准备参数
75 61 ApiAccessLogPageReqVO reqVO = new ApiAccessLogPageReqVO();
76   - reqVO.setUserId(userId);
77   - reqVO.setUserType(userType);
78   - reqVO.setApplicationName(applicationName);
79   - reqVO.setRequestUrl(requestUrl);
80   - reqVO.setBeginTime((new LocalDateTime[]{buildTime(2021, 3, 12),buildTime(2021, 3, 14)}));
81   - reqVO.setDuration(duration);
82   - reqVO.setResultCode(resultCode);
  62 + reqVO.setUserId(2233L);
  63 + reqVO.setUserType(UserTypeEnum.ADMIN.getValue());
  64 + reqVO.setApplicationName("yudao-test");
  65 + reqVO.setRequestUrl("foo");
  66 + reqVO.setBeginTime(buildBetweenTime(2021, 3, 13, 2021, 3, 13));
  67 + reqVO.setDuration(1000);
  68 + reqVO.setResultCode(GlobalErrorCodeConstants.SUCCESS.getCode());
83 69  
84   - // 调用service方法
  70 + // 调用
85 71 PageResult<ApiAccessLogDO> pageResult = apiAccessLogService.getApiAccessLogPage(reqVO);
86   -
87 72 // 断言,只查到了一条符合条件的
88 73 assertEquals(1, pageResult.getTotal());
89 74 assertEquals(1, pageResult.getList().size());
90   - assertPojoEquals(infApiAccessLogDO, pageResult.getList().get(0));
  75 + assertPojoEquals(apiAccessLogDO, pageResult.getList().get(0));
91 76 }
92 77  
93 78 @Test
94 79 public void testGetApiAccessLogList() {
95   - // 构造测试数据
96   - long userId = 2233L;
97   - int userType = UserTypeEnum.ADMIN.getValue();
98   - String applicationName = "yudao-test";
99   - String requestUrl = "foo";
100   - LocalDateTime beginTime = buildTime(2021, 3, 13);
101   - int duration = 1000;
102   - int resultCode = GlobalErrorCodeConstants.SUCCESS.getCode();
103   -
104   - ApiAccessLogDO infApiAccessLogDO = RandomUtils.randomPojo(ApiAccessLogDO.class, dto -> {
105   - dto.setUserId(userId);
106   - dto.setUserType(userType);
107   - dto.setApplicationName(applicationName);
108   - dto.setRequestUrl(requestUrl);
109   - dto.setBeginTime(beginTime);
110   - dto.setDuration(duration);
111   - dto.setResultCode(resultCode);
  80 + ApiAccessLogDO apiAccessLogDO = randomPojo(ApiAccessLogDO.class, o -> {
  81 + o.setUserId(2233L);
  82 + o.setUserType(UserTypeEnum.ADMIN.getValue());
  83 + o.setApplicationName("yudao-test");
  84 + o.setRequestUrl("foo");
  85 + o.setBeginTime(buildTime(2021, 3, 13));
  86 + o.setDuration(1000);
  87 + o.setResultCode(GlobalErrorCodeConstants.SUCCESS.getCode());
112 88 });
113   - apiAccessLogMapper.insert(infApiAccessLogDO);
114   -
115   - // 下面几个都是不匹配的数据
116   - // userId 不同的
117   - apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setUserId(3344L)));
118   - // userType
119   - apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue())));
120   - // applicationName 不同的
121   - apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setApplicationName("test")));
122   - // requestUrl 不同的
123   - apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setRequestUrl("bar")));
124   - // 构造一个早期时间 2021-02-06 00:00:00
125   - apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setBeginTime(buildTime(2021, 2, 6))));
126   - // duration 不同的
127   - apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setDuration(100)));
128   - // resultCode 不同的
129   - apiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setResultCode(2)));
130   -
131   - // 构造调用参数
  89 + apiAccessLogMapper.insert(apiAccessLogDO);
  90 + // 测试 userId 不匹配
  91 + apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setUserId(3344L)));
  92 + // 测试 userType 不匹配
  93 + apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setUserType(UserTypeEnum.MEMBER.getValue())));
  94 + // 测试 applicationName 不匹配
  95 + apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setApplicationName("test")));
  96 + // 测试 requestUrl 不匹配
  97 + apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setRequestUrl("bar")));
  98 + // 测试 beginTime 不匹配:构造一个早期时间 2021-02-06 00:00:00
  99 + apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setBeginTime(buildTime(2021, 2, 6))));
  100 + // 测试 duration 不匹配
  101 + apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setDuration(100)));
  102 + // 测试 resultCode 不匹配
  103 + apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setResultCode(2)));
  104 + // 准备参数
132 105 ApiAccessLogExportReqVO reqVO = new ApiAccessLogExportReqVO();
133   - reqVO.setUserId(userId);
134   - reqVO.setUserType(userType);
135   - reqVO.setApplicationName(applicationName);
136   - reqVO.setRequestUrl(requestUrl);
137   - reqVO.setBeginTime((new LocalDateTime[]{buildTime(2021, 3, 12),buildTime(2021, 3, 14)}));
138   - reqVO.setDuration(duration);
139   - reqVO.setResultCode(resultCode);
  106 + reqVO.setUserId(2233L);
  107 + reqVO.setUserType(UserTypeEnum.ADMIN.getValue());
  108 + reqVO.setApplicationName("yudao-test");
  109 + reqVO.setRequestUrl("foo");
  110 + reqVO.setBeginTime(buildBetweenTime(2021, 3, 13, 2021, 3, 13));
  111 + reqVO.setDuration(1000);
  112 + reqVO.setResultCode(GlobalErrorCodeConstants.SUCCESS.getCode());
140 113  
141   - // 调用service方法
  114 + // 调用
142 115 List<ApiAccessLogDO> list = apiAccessLogService.getApiAccessLogList(reqVO);
143   -
144 116 // 断言,只查到了一条符合条件的
145 117 assertEquals(1, list.size());
146   - assertPojoEquals(infApiAccessLogDO, list.get(0));
  118 + assertPojoEquals(apiAccessLogDO, list.get(0));
147 119 }
148 120  
149 121 @Test
150   - public void testCreateApiAccessLogAsync() {
  122 + public void testCreateApiAccessLog() {
151 123 // 准备参数
152   - ApiAccessLogCreateReqDTO createDTO = RandomUtils.randomPojo(ApiAccessLogCreateReqDTO.class,
153   - dto -> dto.setUserType(RandomUtil.randomEle(UserTypeEnum.values()).getValue()));
  124 + ApiAccessLogCreateReqDTO createDTO = randomPojo(ApiAccessLogCreateReqDTO.class);
154 125  
155 126 // 调用
156 127 apiAccessLogService.createApiAccessLog(createDTO);
157 128 // 断言
158   - ApiAccessLogDO infApiAccessLogDO = apiAccessLogMapper.selectOne(null);
159   - assertNotNull(infApiAccessLogDO);
160   - assertPojoEquals(createDTO, infApiAccessLogDO);
  129 + ApiAccessLogDO apiAccessLogDO = apiAccessLogMapper.selectOne(null);
  130 + assertPojoEquals(createDTO, apiAccessLogDO);
161 131 }
162 132  
163   -
164 133 }
... ...
yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/logger/ApiErrorLogServiceImplTest.java
1 1 package cn.iocoder.yudao.module.infra.service.logger;
2 2  
3   -import cn.hutool.core.util.RandomUtil;
4 3 import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
5 4 import cn.iocoder.yudao.framework.common.pojo.PageResult;
6   -import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
7 5 import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
8   -import cn.iocoder.yudao.framework.test.core.util.RandomUtils;
9 6 import cn.iocoder.yudao.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO;
10 7 import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiErrorLogExportReqVO;
11 8 import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiErrorLogPageReqVO;
12 9 import cn.iocoder.yudao.module.infra.dal.dataobject.logger.ApiErrorLogDO;
13 10 import cn.iocoder.yudao.module.infra.dal.mysql.logger.ApiErrorLogMapper;
14 11 import cn.iocoder.yudao.module.infra.enums.logger.ApiErrorLogProcessStatusEnum;
15   -import org.junit.jupiter.api.Assertions;
16 12 import org.junit.jupiter.api.Test;
17 13 import org.springframework.context.annotation.Import;
18 14  
19 15 import javax.annotation.Resource;
20   -import java.time.LocalDateTime;
21 16 import java.util.List;
22 17  
  18 +import static cn.hutool.core.util.RandomUtil.randomEle;
  19 +import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
23 20 import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
  21 +import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
24 22 import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
25 23 import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
  24 +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
  25 +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
26 26 import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.API_ERROR_LOG_NOT_FOUND;
27 27 import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.API_ERROR_LOG_PROCESSED;
28 28 import static org.junit.jupiter.api.Assertions.assertEquals;
... ... @@ -35,161 +35,150 @@ public class ApiErrorLogServiceImplTest extends BaseDbUnitTest {
35 35 private ApiErrorLogServiceImpl apiErrorLogService;
36 36  
37 37 @Resource
38   - private ApiErrorLogMapper infApiErrorLogMapper;
  38 + private ApiErrorLogMapper apiErrorLogMapper;
39 39  
40 40 @Test
41 41 public void testGetApiErrorLogPage() {
42   - // 构造测试数据
43   - long userId = 2233L;
44   - int userType = UserTypeEnum.ADMIN.getValue();
45   - String applicationName = "yudao-test";
46   - String requestUrl = "foo";
47   - LocalDateTime beginTime = buildTime(2021, 3, 13);
48   - int progressStatus = ApiErrorLogProcessStatusEnum.INIT.getStatus();
49   -
50   - ApiErrorLogDO infApiErrorLogDO = RandomUtils.randomPojo(ApiErrorLogDO.class, logDO -> {
51   - logDO.setUserId(userId);
52   - logDO.setUserType(userType);
53   - logDO.setApplicationName(applicationName);
54   - logDO.setRequestUrl(requestUrl);
55   - logDO.setExceptionTime(beginTime);
56   - logDO.setProcessStatus(progressStatus);
  42 + // mock 数据
  43 + ApiErrorLogDO apiErrorLogDO = randomPojo(ApiErrorLogDO.class, o -> {
  44 + o.setUserId(2233L);
  45 + o.setUserType(UserTypeEnum.ADMIN.getValue());
  46 + o.setApplicationName("yudao-test");
  47 + o.setRequestUrl("foo");
  48 + o.setExceptionTime(buildTime(2021, 3, 13));
  49 + o.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
57 50 });
58   - infApiErrorLogMapper.insert(infApiErrorLogDO);
59   -
60   - // 下面几个都是不匹配的数据
61   - // userId 不同的
62   - infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setUserId(3344L)));
63   - // userType
64   - infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue())));
65   - // applicationName 不同的
66   - infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setApplicationName("test")));
67   - // requestUrl 不同的
68   - infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setRequestUrl("bar")));
69   - // 构造一个早期时间 2021-02-06 00:00:00
70   - infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setExceptionTime(buildTime(2021, 2, 6))));
71   - // progressStatus 不同的
72   - infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus())));
73   -
74   - // 构造调用参数
  51 + apiErrorLogMapper.insert(apiErrorLogDO);
  52 + // 测试 userId 不匹配
  53 + apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, o -> o.setUserId(3344L)));
  54 + // 测试 userType 不匹配
  55 + apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue())));
  56 + // 测试 applicationName 不匹配
  57 + apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setApplicationName("test")));
  58 + // 测试 requestUrl 不匹配
  59 + apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setRequestUrl("bar")));
  60 + // 测试 exceptionTime 不匹配:构造一个早期时间 2021-02-06 00:00:00
  61 + apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setExceptionTime(buildTime(2021, 2, 6))));
  62 + // 测试 progressStatus 不匹配
  63 + apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus())));
  64 + // 准备参数
75 65 ApiErrorLogPageReqVO reqVO = new ApiErrorLogPageReqVO();
76   - reqVO.setUserId(userId);
77   - reqVO.setUserType(userType);
78   - reqVO.setApplicationName(applicationName);
79   - reqVO.setRequestUrl(requestUrl);
80   - reqVO.setExceptionTime((new LocalDateTime[]{buildTime(2021, 3, 12),buildTime(2021, 3, 14)}));
81   - reqVO.setProcessStatus(progressStatus);
82   -
83   - // 调用service方法
84   - PageResult<ApiErrorLogDO> pageResult = apiErrorLogService.getApiErrorLogPage(reqVO);
  66 + reqVO.setUserId(2233L);
  67 + reqVO.setUserType(UserTypeEnum.ADMIN.getValue());
  68 + reqVO.setApplicationName("yudao-test");
  69 + reqVO.setRequestUrl("foo");
  70 + reqVO.setExceptionTime(buildBetweenTime(2021, 3, 1, 2021, 3, 31));
  71 + reqVO.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
85 72  
  73 + // 调用
  74 + PageResult<ApiErrorLogDO> pageResult = apiErrorLogService.getApiErrorLogPage(reqVO);
86 75 // 断言,只查到了一条符合条件的
87 76 assertEquals(1, pageResult.getTotal());
88 77 assertEquals(1, pageResult.getList().size());
89   - assertPojoEquals(infApiErrorLogDO, pageResult.getList().get(0));
  78 + assertPojoEquals(apiErrorLogDO, pageResult.getList().get(0));
90 79 }
91 80  
92 81 @Test
93 82 public void testGetApiErrorLogList() {
94   - // 构造测试数据
95   - long userId = 2233L;
96   - int userType = UserTypeEnum.ADMIN.getValue();
97   - String applicationName = "yudao-test";
98   - String requestUrl = "foo";
99   - LocalDateTime beginTime = buildTime(2021, 3, 13);
100   - int progressStatus = ApiErrorLogProcessStatusEnum.INIT.getStatus();
101   -
102   - ApiErrorLogDO infApiErrorLogDO = RandomUtils.randomPojo(ApiErrorLogDO.class, logDO -> {
103   - logDO.setUserId(userId);
104   - logDO.setUserType(userType);
105   - logDO.setApplicationName(applicationName);
106   - logDO.setRequestUrl(requestUrl);
107   - logDO.setExceptionTime(beginTime);
108   - logDO.setProcessStatus(progressStatus);
  83 + // mock 数据
  84 + ApiErrorLogDO apiErrorLogDO = randomPojo(ApiErrorLogDO.class, o -> {
  85 + o.setUserId(2233L);
  86 + o.setUserType(UserTypeEnum.ADMIN.getValue());
  87 + o.setApplicationName("yudao-test");
  88 + o.setRequestUrl("foo");
  89 + o.setExceptionTime(buildTime(2021, 3, 13));
  90 + o.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
109 91 });
110   - infApiErrorLogMapper.insert(infApiErrorLogDO);
111   -
112   - // 下面几个都是不匹配的数据
113   - // userId 不同的
114   - infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setUserId(3344L)));
115   - // userType
116   - infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue())));
117   - // applicationName 不同的
118   - infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setApplicationName("test")));
119   - // requestUrl 不同的
120   - infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setRequestUrl("bar")));
121   - // 构造一个早期时间 2021-02-06 00:00:00
122   - infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setExceptionTime(buildTime(2021, 2, 6))));
123   - // progressStatus 不同的
124   - infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus())));
125   -
126   - // 构造调用参数
  92 + apiErrorLogMapper.insert(apiErrorLogDO);
  93 + // 测试 userId 不匹配
  94 + apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, o -> o.setUserId(3344L)));
  95 + // 测试 userType 不匹配
  96 + apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue())));
  97 + // 测试 applicationName 不匹配
  98 + apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setApplicationName("test")));
  99 + // 测试 requestUrl 不匹配
  100 + apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setRequestUrl("bar")));
  101 + // 测试 exceptionTime 不匹配:构造一个早期时间 2021-02-06 00:00:00
  102 + apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setExceptionTime(buildTime(2021, 2, 6))));
  103 + // 测试 progressStatus 不匹配
  104 + apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus())));
  105 + // 准备参数
127 106 ApiErrorLogExportReqVO reqVO = new ApiErrorLogExportReqVO();
128   - reqVO.setUserId(userId);
129   - reqVO.setUserType(userType);
130   - reqVO.setApplicationName(applicationName);
131   - reqVO.setRequestUrl(requestUrl);
132   - reqVO.setExceptionTime((new LocalDateTime[]{buildTime(2021, 3, 12),buildTime(2021, 3, 14)}));
133   - reqVO.setProcessStatus(progressStatus);
134   -
135   - // 调用service方法
136   - List<ApiErrorLogDO> list = apiErrorLogService.getApiErrorLogList(reqVO);
  107 + reqVO.setUserId(2233L);
  108 + reqVO.setUserType(UserTypeEnum.ADMIN.getValue());
  109 + reqVO.setApplicationName("yudao-test");
  110 + reqVO.setRequestUrl("foo");
  111 + reqVO.setExceptionTime(buildBetweenTime(2021, 3, 1, 2021, 3, 31));
  112 + reqVO.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
137 113  
  114 + // 调用
  115 + List<ApiErrorLogDO> list = apiErrorLogService.getApiErrorLogList(reqVO);
138 116 // 断言,只查到了一条符合条件的
139 117 assertEquals(1, list.size());
140   - assertPojoEquals(infApiErrorLogDO, list.get(0));
  118 + assertPojoEquals(apiErrorLogDO, list.get(0));
141 119 }
142 120  
143   -
144   - // TODO 芋艿:单元测试,可以拆小一点
145 121 @Test
146   - public void testUpdateApiErrorLogProcess() {
147   - // 先构造两条数据,第一条用于抛出异常,第二条用于正常的执行update操作
148   - Long processUserId = 2233L;
149   -
150   - ApiErrorLogDO first = RandomUtils.randomPojo(ApiErrorLogDO.class, logDO -> {
151   - logDO.setProcessUserId(processUserId);
152   - logDO.setUserType(UserTypeEnum.ADMIN.getValue());
153   - logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus());
154   - });
155   - infApiErrorLogMapper.insert(first);
  122 + public void testCreateApiErrorLog() {
  123 + // 准备参数
  124 + ApiErrorLogCreateReqDTO createDTO = randomPojo(ApiErrorLogCreateReqDTO.class);
156 125  
157   - ApiErrorLogDO second = RandomUtils.randomPojo(ApiErrorLogDO.class, logDO -> {
158   - logDO.setProcessUserId(1122L);
159   - logDO.setUserType(UserTypeEnum.ADMIN.getValue());
160   - logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
161   - });
162   - infApiErrorLogMapper.insert(second);
163   -
164   - Long firstId = first.getId();
165   - Long secondId = second.getId();
166   -
167   - // 执行正常的 update 操作
168   - apiErrorLogService.updateApiErrorLogProcess(secondId, ApiErrorLogProcessStatusEnum.DONE.getStatus(), processUserId);
169   - ApiErrorLogDO secondSelect = infApiErrorLogMapper.selectOne("id", secondId);
170   -
171   - // id 为 0 查询不到,应该抛出异常 API_ERROR_LOG_NOT_FOUND
172   - assertServiceException(() -> apiErrorLogService.updateApiErrorLogProcess(0L, ApiErrorLogProcessStatusEnum.DONE.getStatus(), processUserId), API_ERROR_LOG_NOT_FOUND);
173   - // id 为 first 的 progressStatus 为 DONE ,应该抛出 API_ERROR_LOG_PROCESSED
174   - assertServiceException(() -> apiErrorLogService.updateApiErrorLogProcess(firstId, ApiErrorLogProcessStatusEnum.DONE.getStatus(), processUserId), API_ERROR_LOG_PROCESSED);
175   - // 验证 progressStatus 是否修改成功
176   - Assertions.assertEquals(ApiErrorLogProcessStatusEnum.DONE.getStatus(), secondSelect.getProcessStatus());
177   - // 验证 progressUserId 是否修改成功
178   - Assertions.assertEquals(processUserId, secondSelect.getProcessUserId());
  126 + // 调用
  127 + apiErrorLogService.createApiErrorLog(createDTO);
  128 + // 断言
  129 + ApiErrorLogDO apiErrorLogDO = apiErrorLogMapper.selectOne(null);
  130 + assertPojoEquals(createDTO, apiErrorLogDO);
  131 + assertEquals(ApiErrorLogProcessStatusEnum.INIT.getStatus(), apiErrorLogDO.getProcessStatus());
179 132 }
180 133  
181 134 @Test
182   - public void testCreateApiErrorLogAsync() {
  135 + public void testUpdateApiErrorLogProcess_success() {
183 136 // 准备参数
184   - ApiErrorLogCreateReqDTO createDTO = RandomUtils.randomPojo(ApiErrorLogCreateReqDTO.class,
185   - dto -> dto.setUserType(RandomUtil.randomEle(UserTypeEnum.values()).getValue()));
  137 + ApiErrorLogDO apiErrorLogDO = randomPojo(ApiErrorLogDO.class,
  138 + o -> o.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus()));
  139 + apiErrorLogMapper.insert(apiErrorLogDO);
  140 + // 准备参数
  141 + Long id = apiErrorLogDO.getId();
  142 + Integer processStatus = randomEle(ApiErrorLogProcessStatusEnum.values()).getStatus();
  143 + Long processUserId = randomLongId();
186 144  
187 145 // 调用
188   - apiErrorLogService.createApiErrorLog(createDTO);
  146 + apiErrorLogService.updateApiErrorLogProcess(id, processStatus, processUserId);
189 147 // 断言
190   - ApiErrorLogDO infApiErrorLogDO = infApiErrorLogMapper.selectOne(null);
191   - assertNotNull(infApiErrorLogDO);
192   - assertPojoEquals(createDTO, infApiErrorLogDO);
  148 + ApiErrorLogDO dbApiErrorLogDO = apiErrorLogMapper.selectById(apiErrorLogDO.getId());
  149 + assertEquals(processStatus, dbApiErrorLogDO.getProcessStatus());
  150 + assertEquals(processUserId, dbApiErrorLogDO.getProcessUserId());
  151 + assertNotNull(dbApiErrorLogDO.getProcessTime());
  152 + }
  153 +
  154 + @Test
  155 + public void testUpdateApiErrorLogProcess_processed() {
  156 + // 准备参数
  157 + ApiErrorLogDO apiErrorLogDO = randomPojo(ApiErrorLogDO.class,
  158 + o -> o.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus()));
  159 + apiErrorLogMapper.insert(apiErrorLogDO);
  160 + // 准备参数
  161 + Long id = apiErrorLogDO.getId();
  162 + Integer processStatus = randomEle(ApiErrorLogProcessStatusEnum.values()).getStatus();
  163 + Long processUserId = randomLongId();
  164 +
  165 + // 调用,并断言异常
  166 + assertServiceException(() ->
  167 + apiErrorLogService.updateApiErrorLogProcess(id, processStatus, processUserId),
  168 + API_ERROR_LOG_PROCESSED);
  169 + }
  170 +
  171 + @Test
  172 + public void testUpdateApiErrorLogProcess_notFound() {
  173 + // 准备参数
  174 + Long id = randomLongId();
  175 + Integer processStatus = randomEle(ApiErrorLogProcessStatusEnum.values()).getStatus();
  176 + Long processUserId = randomLongId();
  177 +
  178 + // 调用,并断言异常
  179 + assertServiceException(() ->
  180 + apiErrorLogService.updateApiErrorLogProcess(id, processStatus, processUserId),
  181 + API_ERROR_LOG_NOT_FOUND);
193 182 }
194 183  
195 184 }
... ...