提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.system.service.logger; |
H |
2 |
|
|
3 |
import com.iailab.framework.common.pojo.PageResult; |
|
4 |
import com.iailab.framework.test.core.ut.BaseDbUnitTest; |
|
5 |
import com.iailab.module.system.api.logger.dto.LoginLogCreateReqDTO; |
|
6 |
import com.iailab.module.system.controller.admin.logger.vo.loginlog.LoginLogPageReqVO; |
|
7 |
import com.iailab.module.system.dal.dataobject.logger.LoginLogDO; |
|
8 |
import com.iailab.module.system.dal.mysql.logger.LoginLogMapper; |
|
9 |
import org.junit.jupiter.api.Test; |
|
10 |
import org.springframework.context.annotation.Import; |
|
11 |
|
|
12 |
import javax.annotation.Resource; |
|
13 |
|
|
14 |
import static com.iailab.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime; |
|
15 |
import static com.iailab.framework.common.util.date.LocalDateTimeUtils.buildTime; |
|
16 |
import static com.iailab.framework.common.util.object.ObjectUtils.cloneIgnoreId; |
|
17 |
import static com.iailab.framework.test.core.util.AssertUtils.assertPojoEquals; |
|
18 |
import static com.iailab.framework.test.core.util.RandomUtils.randomPojo; |
|
19 |
import static com.iailab.module.system.enums.logger.LoginResultEnum.CAPTCHA_CODE_ERROR; |
|
20 |
import static com.iailab.module.system.enums.logger.LoginResultEnum.SUCCESS; |
|
21 |
import static org.junit.jupiter.api.Assertions.assertEquals; |
|
22 |
|
|
23 |
@Import(LoginLogServiceImpl.class) |
|
24 |
public class LoginLogServiceImplTest extends BaseDbUnitTest { |
|
25 |
|
|
26 |
@Resource |
|
27 |
private LoginLogServiceImpl loginLogService; |
|
28 |
|
|
29 |
@Resource |
|
30 |
private LoginLogMapper loginLogMapper; |
|
31 |
|
|
32 |
@Test |
|
33 |
public void testGetLoginLogPage() { |
|
34 |
// mock 数据 |
|
35 |
LoginLogDO loginLogDO = randomPojo(LoginLogDO.class, o -> { |
|
36 |
o.setUserIp("192.168.199.16"); |
|
37 |
o.setUsername("wang"); |
|
38 |
o.setResult(SUCCESS.getResult()); |
|
39 |
o.setCreateTime(buildTime(2021, 3, 6)); |
|
40 |
}); |
|
41 |
loginLogMapper.insert(loginLogDO); |
|
42 |
// 测试 status 不匹配 |
|
43 |
loginLogMapper.insert(cloneIgnoreId(loginLogDO, o -> o.setResult(CAPTCHA_CODE_ERROR.getResult()))); |
|
44 |
// 测试 ip 不匹配 |
|
45 |
loginLogMapper.insert(cloneIgnoreId(loginLogDO, o -> o.setUserIp("192.168.128.18"))); |
|
46 |
// 测试 username 不匹配 |
|
47 |
loginLogMapper.insert(cloneIgnoreId(loginLogDO, o -> o.setUsername("yunai"))); |
|
48 |
// 测试 createTime 不匹配 |
|
49 |
loginLogMapper.insert(cloneIgnoreId(loginLogDO, o -> o.setCreateTime(buildTime(2021, 2, 6)))); |
|
50 |
// 构造调用参数 |
|
51 |
LoginLogPageReqVO reqVO = new LoginLogPageReqVO(); |
|
52 |
reqVO.setUsername("wang"); |
|
53 |
reqVO.setUserIp("192.168.199"); |
|
54 |
reqVO.setStatus(true); |
|
55 |
reqVO.setCreateTime(buildBetweenTime(2021, 3, 5, 2021, 3, 7)); |
|
56 |
|
|
57 |
// 调用 |
|
58 |
PageResult<LoginLogDO> pageResult = loginLogService.getLoginLogPage(reqVO); |
|
59 |
// 断言,只查到了一条符合条件的 |
|
60 |
assertEquals(1, pageResult.getTotal()); |
|
61 |
assertEquals(1, pageResult.getList().size()); |
|
62 |
assertPojoEquals(loginLogDO, pageResult.getList().get(0)); |
|
63 |
} |
|
64 |
|
|
65 |
@Test |
|
66 |
public void testCreateLoginLog() { |
|
67 |
LoginLogCreateReqDTO reqDTO = randomPojo(LoginLogCreateReqDTO.class); |
|
68 |
|
|
69 |
// 调用 |
|
70 |
loginLogService.createLoginLog(reqDTO); |
|
71 |
// 断言 |
|
72 |
LoginLogDO loginLogDO = loginLogMapper.selectOne(null); |
|
73 |
assertPojoEquals(reqDTO, loginLogDO); |
|
74 |
} |
|
75 |
|
|
76 |
} |