提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.infra.service.logger; |
H |
2 |
|
|
3 |
import com.iailab.framework.common.enums.UserTypeEnum; |
|
4 |
import com.iailab.framework.common.pojo.PageResult; |
|
5 |
import com.iailab.framework.test.core.ut.BaseDbUnitTest; |
|
6 |
import com.iailab.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO; |
|
7 |
import com.iailab.module.infra.controller.admin.logger.vo.apierrorlog.ApiErrorLogPageReqVO; |
|
8 |
import com.iailab.module.infra.dal.dataobject.logger.ApiErrorLogDO; |
|
9 |
import com.iailab.module.infra.dal.mysql.logger.ApiErrorLogMapper; |
|
10 |
import com.iailab.module.infra.enums.logger.ApiErrorLogProcessStatusEnum; |
|
11 |
import org.junit.jupiter.api.Test; |
|
12 |
import org.springframework.context.annotation.Import; |
|
13 |
|
|
14 |
import javax.annotation.Resource; |
|
15 |
import java.time.Duration; |
|
16 |
import java.util.List; |
|
17 |
|
|
18 |
import static cn.hutool.core.util.RandomUtil.randomEle; |
|
19 |
import static com.iailab.framework.common.util.date.LocalDateTimeUtils.*; |
|
20 |
import static com.iailab.framework.common.util.object.ObjectUtils.cloneIgnoreId; |
|
21 |
import static com.iailab.framework.test.core.util.AssertUtils.assertPojoEquals; |
|
22 |
import static com.iailab.framework.test.core.util.AssertUtils.assertServiceException; |
|
23 |
import static com.iailab.framework.test.core.util.RandomUtils.randomLongId; |
|
24 |
import static com.iailab.framework.test.core.util.RandomUtils.randomPojo; |
|
25 |
import static com.iailab.module.infra.enums.ErrorCodeConstants.API_ERROR_LOG_NOT_FOUND; |
|
26 |
import static com.iailab.module.infra.enums.ErrorCodeConstants.API_ERROR_LOG_PROCESSED; |
|
27 |
import static org.junit.jupiter.api.Assertions.assertEquals; |
|
28 |
import static org.junit.jupiter.api.Assertions.assertNotNull; |
|
29 |
|
|
30 |
@Import(ApiErrorLogServiceImpl.class) |
|
31 |
public class ApiErrorLogServiceImplTest extends BaseDbUnitTest { |
|
32 |
|
|
33 |
@Resource |
|
34 |
private ApiErrorLogServiceImpl apiErrorLogService; |
|
35 |
|
|
36 |
@Resource |
|
37 |
private ApiErrorLogMapper apiErrorLogMapper; |
|
38 |
|
|
39 |
@Test |
|
40 |
public void testGetApiErrorLogPage() { |
|
41 |
// mock 数据 |
|
42 |
ApiErrorLogDO apiErrorLogDO = randomPojo(ApiErrorLogDO.class, o -> { |
|
43 |
o.setUserId(2233L); |
|
44 |
o.setUserType(UserTypeEnum.ADMIN.getValue()); |
|
45 |
o.setApplicationName("iailab-test"); |
|
46 |
o.setRequestUrl("foo"); |
|
47 |
o.setExceptionTime(buildTime(2021, 3, 13)); |
|
48 |
o.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus()); |
|
49 |
}); |
|
50 |
apiErrorLogMapper.insert(apiErrorLogDO); |
|
51 |
// 测试 userId 不匹配 |
|
52 |
apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, o -> o.setUserId(3344L))); |
|
53 |
// 测试 userType 不匹配 |
|
54 |
apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, o -> o.setUserType(UserTypeEnum.MEMBER.getValue()))); |
|
55 |
// 测试 applicationName 不匹配 |
|
56 |
apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, o -> o.setApplicationName("test"))); |
|
57 |
// 测试 requestUrl 不匹配 |
|
58 |
apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, o -> o.setRequestUrl("bar"))); |
|
59 |
// 测试 exceptionTime 不匹配:构造一个早期时间 2021-02-06 00:00:00 |
|
60 |
apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, o -> o.setExceptionTime(buildTime(2021, 2, 6)))); |
|
61 |
// 测试 progressStatus 不匹配 |
|
62 |
apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus()))); |
|
63 |
// 准备参数 |
|
64 |
ApiErrorLogPageReqVO reqVO = new ApiErrorLogPageReqVO(); |
|
65 |
reqVO.setUserId(2233L); |
|
66 |
reqVO.setUserType(UserTypeEnum.ADMIN.getValue()); |
|
67 |
reqVO.setApplicationName("iailab-test"); |
|
68 |
reqVO.setRequestUrl("foo"); |
|
69 |
reqVO.setExceptionTime(buildBetweenTime(2021, 3, 1, 2021, 3, 31)); |
|
70 |
reqVO.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus()); |
|
71 |
|
|
72 |
// 调用 |
|
73 |
PageResult<ApiErrorLogDO> pageResult = apiErrorLogService.getApiErrorLogPage(reqVO); |
|
74 |
// 断言,只查到了一条符合条件的 |
|
75 |
assertEquals(1, pageResult.getTotal()); |
|
76 |
assertEquals(1, pageResult.getList().size()); |
|
77 |
assertPojoEquals(apiErrorLogDO, pageResult.getList().get(0)); |
|
78 |
} |
|
79 |
|
|
80 |
@Test |
|
81 |
public void testCreateApiErrorLog() { |
|
82 |
// 准备参数 |
|
83 |
ApiErrorLogCreateReqDTO createDTO = randomPojo(ApiErrorLogCreateReqDTO.class); |
|
84 |
|
|
85 |
// 调用 |
|
86 |
apiErrorLogService.createApiErrorLog(createDTO); |
|
87 |
// 断言 |
|
88 |
ApiErrorLogDO apiErrorLogDO = apiErrorLogMapper.selectOne(null); |
|
89 |
assertPojoEquals(createDTO, apiErrorLogDO); |
|
90 |
assertEquals(ApiErrorLogProcessStatusEnum.INIT.getStatus(), apiErrorLogDO.getProcessStatus()); |
|
91 |
} |
|
92 |
|
|
93 |
@Test |
|
94 |
public void testUpdateApiErrorLogProcess_success() { |
|
95 |
// 准备参数 |
|
96 |
ApiErrorLogDO apiErrorLogDO = randomPojo(ApiErrorLogDO.class, |
|
97 |
o -> o.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus())); |
|
98 |
apiErrorLogMapper.insert(apiErrorLogDO); |
|
99 |
// 准备参数 |
|
100 |
Long id = apiErrorLogDO.getId(); |
|
101 |
Integer processStatus = randomEle(ApiErrorLogProcessStatusEnum.values()).getStatus(); |
|
102 |
Long processUserId = randomLongId(); |
|
103 |
|
|
104 |
// 调用 |
|
105 |
apiErrorLogService.updateApiErrorLogProcess(id, processStatus, processUserId); |
|
106 |
// 断言 |
|
107 |
ApiErrorLogDO dbApiErrorLogDO = apiErrorLogMapper.selectById(apiErrorLogDO.getId()); |
|
108 |
assertEquals(processStatus, dbApiErrorLogDO.getProcessStatus()); |
|
109 |
assertEquals(processUserId, dbApiErrorLogDO.getProcessUserId()); |
|
110 |
assertNotNull(dbApiErrorLogDO.getProcessTime()); |
|
111 |
} |
|
112 |
|
|
113 |
@Test |
|
114 |
public void testUpdateApiErrorLogProcess_processed() { |
|
115 |
// 准备参数 |
|
116 |
ApiErrorLogDO apiErrorLogDO = randomPojo(ApiErrorLogDO.class, |
|
117 |
o -> o.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus())); |
|
118 |
apiErrorLogMapper.insert(apiErrorLogDO); |
|
119 |
// 准备参数 |
|
120 |
Long id = apiErrorLogDO.getId(); |
|
121 |
Integer processStatus = randomEle(ApiErrorLogProcessStatusEnum.values()).getStatus(); |
|
122 |
Long processUserId = randomLongId(); |
|
123 |
|
|
124 |
// 调用,并断言异常 |
|
125 |
assertServiceException(() -> |
|
126 |
apiErrorLogService.updateApiErrorLogProcess(id, processStatus, processUserId), |
|
127 |
API_ERROR_LOG_PROCESSED); |
|
128 |
} |
|
129 |
|
|
130 |
@Test |
|
131 |
public void testUpdateApiErrorLogProcess_notFound() { |
|
132 |
// 准备参数 |
|
133 |
Long id = randomLongId(); |
|
134 |
Integer processStatus = randomEle(ApiErrorLogProcessStatusEnum.values()).getStatus(); |
|
135 |
Long processUserId = randomLongId(); |
|
136 |
|
|
137 |
// 调用,并断言异常 |
|
138 |
assertServiceException(() -> |
|
139 |
apiErrorLogService.updateApiErrorLogProcess(id, processStatus, processUserId), |
|
140 |
API_ERROR_LOG_NOT_FOUND); |
|
141 |
} |
|
142 |
|
|
143 |
@Test |
|
144 |
public void testCleanJobLog() { |
|
145 |
// mock 数据 |
|
146 |
ApiErrorLogDO log01 = randomPojo(ApiErrorLogDO.class, o -> o.setCreateTime(addTime(Duration.ofDays(-3)))); |
|
147 |
apiErrorLogMapper.insert(log01); |
|
148 |
ApiErrorLogDO log02 = randomPojo(ApiErrorLogDO.class, o -> o.setCreateTime(addTime(Duration.ofDays(-1)))); |
|
149 |
apiErrorLogMapper.insert(log02); |
|
150 |
// 准备参数 |
|
151 |
Integer exceedDay = 2; |
|
152 |
Integer deleteLimit = 1; |
|
153 |
|
|
154 |
// 调用 |
|
155 |
Integer count = apiErrorLogService.cleanErrorLog(exceedDay, deleteLimit); |
|
156 |
// 断言 |
|
157 |
assertEquals(1, count); |
|
158 |
List<ApiErrorLogDO> logs = apiErrorLogMapper.selectList(); |
|
159 |
assertEquals(1, logs.size()); |
|
160 |
assertEquals(log02, logs.get(0)); |
|
161 |
} |
|
162 |
|
|
163 |
} |