潘志宝
8 天以前 81ce77b2e152ffbedd61d3a768b5b292701a7c64
提交 | 用户 | 时间
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.exception.enums.GlobalErrorCodeConstants;
5 import com.iailab.framework.common.pojo.PageResult;
6 import com.iailab.framework.test.core.ut.BaseDbUnitTest;
7 import com.iailab.module.infra.api.logger.dto.ApiAccessLogCreateReqDTO;
8 import com.iailab.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogPageReqVO;
9 import com.iailab.module.infra.dal.dataobject.logger.ApiAccessLogDO;
10 import com.iailab.module.infra.dal.mysql.logger.ApiAccessLogMapper;
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 com.iailab.framework.common.util.date.LocalDateTimeUtils.*;
19 import static com.iailab.framework.common.util.object.ObjectUtils.cloneIgnoreId;
20 import static com.iailab.framework.test.core.util.AssertUtils.assertPojoEquals;
21 import static com.iailab.framework.test.core.util.RandomUtils.randomPojo;
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23
24 @Import(ApiAccessLogServiceImpl.class)
25 public class ApiAccessLogServiceImplTest extends BaseDbUnitTest {
26
27     @Resource
28     private ApiAccessLogServiceImpl apiAccessLogService;
29
30     @Resource
31     private ApiAccessLogMapper apiAccessLogMapper;
32
33     @Test
34     public void testGetApiAccessLogPage() {
35         ApiAccessLogDO apiAccessLogDO = randomPojo(ApiAccessLogDO.class, o -> {
36             o.setUserId(2233L);
37             o.setUserType(UserTypeEnum.ADMIN.getValue());
38             o.setApplicationName("iailab-test");
39             o.setRequestUrl("foo");
40             o.setBeginTime(buildTime(2021, 3, 13));
41             o.setDuration(1000);
42             o.setResultCode(GlobalErrorCodeConstants.SUCCESS.getCode());
43         });
44         apiAccessLogMapper.insert(apiAccessLogDO);
45         // 测试 userId 不匹配
46         apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setUserId(3344L)));
47         // 测试 userType 不匹配
48         apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setUserType(UserTypeEnum.MEMBER.getValue())));
49         // 测试 applicationName 不匹配
50         apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setApplicationName("test")));
51         // 测试 requestUrl 不匹配
52         apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setRequestUrl("bar")));
53         // 测试 beginTime 不匹配:构造一个早期时间 2021-02-06 00:00:00
54         apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setBeginTime(buildTime(2021, 2, 6))));
55         // 测试 duration 不匹配
56         apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setDuration(100)));
57         // 测试 resultCode 不匹配
58         apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setResultCode(2)));
59         // 准备参数
60         ApiAccessLogPageReqVO reqVO = new ApiAccessLogPageReqVO();
61         reqVO.setUserId(2233L);
62         reqVO.setUserType(UserTypeEnum.ADMIN.getValue());
63         reqVO.setApplicationName("iailab-test");
64         reqVO.setRequestUrl("foo");
65         reqVO.setBeginTime(buildBetweenTime(2021, 3, 13, 2021, 3, 13));
66         reqVO.setDuration(1000);
67         reqVO.setResultCode(GlobalErrorCodeConstants.SUCCESS.getCode());
68
69         // 调用
70         PageResult<ApiAccessLogDO> pageResult = apiAccessLogService.getApiAccessLogPage(reqVO);
71         // 断言,只查到了一条符合条件的
72         assertEquals(1, pageResult.getTotal());
73         assertEquals(1, pageResult.getList().size());
74         assertPojoEquals(apiAccessLogDO, pageResult.getList().get(0));
75     }
76
77     @Test
78     public void testCleanJobLog() {
79         // mock 数据
80         ApiAccessLogDO log01 = randomPojo(ApiAccessLogDO.class, o -> o.setCreateTime(addTime(Duration.ofDays(-3))));
81         apiAccessLogMapper.insert(log01);
82         ApiAccessLogDO log02 = randomPojo(ApiAccessLogDO.class, o -> o.setCreateTime(addTime(Duration.ofDays(-1))));
83         apiAccessLogMapper.insert(log02);
84         // 准备参数
85         Integer exceedDay = 2;
86         Integer deleteLimit = 1;
87
88         // 调用
89         Integer count = apiAccessLogService.cleanAccessLog(exceedDay, deleteLimit);
90         // 断言
91         assertEquals(1, count);
92         List<ApiAccessLogDO> logs = apiAccessLogMapper.selectList();
93         assertEquals(1, logs.size());
94         assertEquals(log02, logs.get(0));
95     }
96
97     @Test
98     public void testCreateApiAccessLog() {
99         // 准备参数
100         ApiAccessLogCreateReqDTO createDTO = randomPojo(ApiAccessLogCreateReqDTO.class);
101
102         // 调用
103         apiAccessLogService.createApiAccessLog(createDTO);
104         // 断言
105         ApiAccessLogDO apiAccessLogDO = apiAccessLogMapper.selectOne(null);
106         assertPojoEquals(createDTO, apiAccessLogDO);
107     }
108
109 }