完善 LoginLogServiceImplTest 单元测试

This commit is contained in:
YunaiV 2023-02-01 08:16:52 +08:00
parent 589aaab298
commit d60ff1688e
2 changed files with 48 additions and 80 deletions

View File

@ -50,7 +50,7 @@ public class RandomUtils {
} }
// 如果是 typestatus 结尾的字段返回 tinyint 范围 // 如果是 typestatus 结尾的字段返回 tinyint 范围
if (StrUtil.endWithAnyIgnoreCase(attributeMetadata.getAttributeName(), if (StrUtil.endWithAnyIgnoreCase(attributeMetadata.getAttributeName(),
"type", "status", "category", "scope")) { "type", "status", "category", "scope", "result")) {
return RandomUtil.randomInt(0, TINYINT_MAX + 1); return RandomUtil.randomInt(0, TINYINT_MAX + 1);
} }
return RandomUtil.randomInt(); return RandomUtil.randomInt();

View File

@ -1,29 +1,25 @@
package cn.iocoder.yudao.module.system.service.logger; package cn.iocoder.yudao.module.system.service.logger;
import cn.hutool.core.util.RandomUtil;
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.monitor.TracerUtils;
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.system.api.logger.dto.LoginLogCreateReqDTO; import cn.iocoder.yudao.module.system.api.logger.dto.LoginLogCreateReqDTO;
import cn.iocoder.yudao.module.system.controller.admin.logger.vo.loginlog.LoginLogExportReqVO; import cn.iocoder.yudao.module.system.controller.admin.logger.vo.loginlog.LoginLogExportReqVO;
import cn.iocoder.yudao.module.system.controller.admin.logger.vo.loginlog.LoginLogPageReqVO; import cn.iocoder.yudao.module.system.controller.admin.logger.vo.loginlog.LoginLogPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.logger.LoginLogDO; import cn.iocoder.yudao.module.system.dal.dataobject.logger.LoginLogDO;
import cn.iocoder.yudao.module.system.dal.mysql.logger.LoginLogMapper; import cn.iocoder.yudao.module.system.dal.mysql.logger.LoginLogMapper;
import cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum;
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.DateUtils.buildLocalDateTime; 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.module.system.enums.logger.LoginResultEnum.*; import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
import static cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum.CAPTCHA_CODE_ERROR;
import static cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum.SUCCESS;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@Import(LoginLogServiceImpl.class) @Import(LoginLogServiceImpl.class)
@ -37,42 +33,31 @@ public class LoginLogServiceImplTest extends BaseDbUnitTest {
@Test @Test
public void testGetLoginLogPage() { public void testGetLoginLogPage() {
// 构造测试数据 // mock 数据
// 登录成功的 LoginLogDO loginLogDO = randomPojo(LoginLogDO.class, o -> {
LoginLogDO loginLogDO = RandomUtils.randomPojo(LoginLogDO.class, logDO -> { o.setUserIp("192.168.199.16");
logDO.setLogType(RandomUtil.randomEle(LoginLogTypeEnum.values()).getType()); o.setUsername("wang");
logDO.setTraceId(TracerUtils.getTraceId()); o.setResult(SUCCESS.getResult());
logDO.setUserType(RandomUtil.randomEle(UserTypeEnum.values()).getValue()); o.setCreateTime(buildTime(2021, 3, 6));
logDO.setUserIp("192.168.199.16");
logDO.setUsername("wangkai");
logDO.setCreateTime(buildLocalDateTime(2021, 3, 6));
logDO.setResult(SUCCESS.getResult());
}); });
loginLogMapper.insert(loginLogDO); loginLogMapper.insert(loginLogDO);
// 测试 status 不匹配
// 下面几个都是不匹配的数据 loginLogMapper.insert(cloneIgnoreId(loginLogDO, o -> o.setResult(CAPTCHA_CODE_ERROR.getResult())));
// 登录失败的 // 测试 ip 不匹配
loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setResult(CAPTCHA_CODE_ERROR.getResult()))); loginLogMapper.insert(cloneIgnoreId(loginLogDO, o -> o.setUserIp("192.168.128.18")));
// 不同ip段的 // 测试 username 不匹配
loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setUserIp("192.168.128.18"))); loginLogMapper.insert(cloneIgnoreId(loginLogDO, o -> o.setUsername("yunai")));
// 不同username // 测试 createTime 不匹配
loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setUsername("yunai"))); loginLogMapper.insert(cloneIgnoreId(loginLogDO, o -> o.setCreateTime(buildTime(2021, 2, 6))));
// 构造一个早期时间 2021-02-06 00:00:00
loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setCreateTime(buildLocalDateTime(2021, 2, 6))));
// 构造调用参数 // 构造调用参数
LoginLogPageReqVO reqVO = new LoginLogPageReqVO(); LoginLogPageReqVO reqVO = new LoginLogPageReqVO();
reqVO.setUsername("wangkai"); reqVO.setUsername("wang");
reqVO.setUserIp("192.168.199"); reqVO.setUserIp("192.168.199");
reqVO.setStatus(true); reqVO.setStatus(true);
reqVO.setCreateTime((new LocalDateTime[]{buildLocalDateTime(2021, 3, 5), reqVO.setCreateTime(buildBetweenTime(2021, 3, 5, 2021, 3, 7));
buildLocalDateTime(2021, 3, 7)}));
// 调用service方法 // 调用
PageResult<LoginLogDO> pageResult = loginLogService.getLoginLogPage(reqVO); PageResult<LoginLogDO> pageResult = loginLogService.getLoginLogPage(reqVO);
// 断言只查到了一条符合条件的 // 断言只查到了一条符合条件的
assertEquals(1, pageResult.getTotal()); assertEquals(1, pageResult.getTotal());
assertEquals(1, pageResult.getList().size()); assertEquals(1, pageResult.getList().size());
@ -81,62 +66,45 @@ public class LoginLogServiceImplTest extends BaseDbUnitTest {
@Test @Test
public void testGetLoginLogList() { public void testGetLoginLogList() {
// 构造测试数据 // mock 数据
LoginLogDO loginLogDO = randomPojo(LoginLogDO.class, o -> {
// 登录成功的 o.setUserIp("192.168.199.16");
LoginLogDO loginLogDO = RandomUtils.randomPojo(LoginLogDO.class, logDO -> { o.setUsername("wang");
logDO.setLogType(RandomUtil.randomEle(LoginLogTypeEnum.values()).getType()); o.setResult(SUCCESS.getResult());
logDO.setTraceId(TracerUtils.getTraceId()); o.setCreateTime(buildTime(2021, 3, 6));
logDO.setUserType(RandomUtil.randomEle(UserTypeEnum.values()).getValue());
logDO.setUserIp("192.168.111.16");
logDO.setUsername("wangxiaokai");
logDO.setCreateTime(buildLocalDateTime(2021, 3, 6));
logDO.setResult(SUCCESS.getResult());
}); });
loginLogMapper.insert(loginLogDO); loginLogMapper.insert(loginLogDO);
// 测试 status 不匹配
// 下面几个都是不匹配的数据 loginLogMapper.insert(cloneIgnoreId(loginLogDO, o -> o.setResult(CAPTCHA_CODE_ERROR.getResult())));
// 登录失败的 // 测试 ip 不匹配
loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setResult(CAPTCHA_CODE_ERROR.getResult()))); loginLogMapper.insert(cloneIgnoreId(loginLogDO, o -> o.setUserIp("192.168.128.18")));
// 不同ip段的 // 测试 username 不匹配
loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setUserIp("192.168.128.18"))); loginLogMapper.insert(cloneIgnoreId(loginLogDO, o -> o.setUsername("yunai")));
// 不同username // 测试 createTime 不匹配
loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setUsername("yunai"))); loginLogMapper.insert(cloneIgnoreId(loginLogDO, o -> o.setCreateTime(buildTime(2021, 2, 6))));
// 构造一个早期时间 2021-02-06 00:00:00
loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setCreateTime(buildLocalDateTime(2021, 2, 6))));
// 构造调用参数 // 构造调用参数
LoginLogExportReqVO reqVO = new LoginLogExportReqVO(); LoginLogExportReqVO reqVO = new LoginLogExportReqVO();
reqVO.setUsername("wangxiaokai"); reqVO.setUsername("wang");
reqVO.setUserIp("192.168.111"); reqVO.setUserIp("192.168.199");
reqVO.setStatus(true); reqVO.setStatus(true);
reqVO.setCreateTime((new LocalDateTime[]{buildLocalDateTime(2021, 3, 5), reqVO.setCreateTime(buildBetweenTime(2021, 3, 5, 2021, 3, 7));
buildLocalDateTime(2021, 3, 7)}));
// 调用service方法 // 调用service方法
List<LoginLogDO> loginLogList = loginLogService.getLoginLogList(reqVO); List<LoginLogDO> list = loginLogService.getLoginLogList(reqVO);
// 断言 // 断言
assertEquals(1, loginLogList.size()); assertEquals(1, list.size());
assertPojoEquals(loginLogDO, loginLogList.get(0)); assertPojoEquals(loginLogDO, list.get(0));
} }
@Test @Test
public void testCreateLoginLog() { public void testCreateLoginLog() {
LoginLogCreateReqDTO reqDTO = RandomUtils.randomPojo(LoginLogCreateReqDTO.class, vo -> { LoginLogCreateReqDTO reqDTO = randomPojo(LoginLogCreateReqDTO.class);
// 指定随机的范围,避免超出范围入库失败
vo.setUserType(randomEle(UserTypeEnum.values()).getValue());
vo.setLogType(randomEle(LoginLogTypeEnum.values()).getType());
vo.setResult(randomEle(values()).getResult());
vo.setTraceId(TracerUtils.getTraceId());
});
// 调用 // 调用
loginLogService.createLoginLog(reqDTO); loginLogService.createLoginLog(reqDTO);
// 断言忽略基本字段 // 断言
LoginLogDO sysLoginLogDO = loginLogMapper.selectOne(null); LoginLogDO loginLogDO = loginLogMapper.selectOne(null);
assertPojoEquals(reqDTO, sysLoginLogDO); assertPojoEquals(reqDTO, loginLogDO);
} }
} }