完善 OperateLogServiceImpl 单元测试

pull/2/head
YunaiV 2023-02-01 08:27:02 +08:00
parent 987fcd366c
commit aa37c2cd7c
4 changed files with 51 additions and 67 deletions

View File

@ -71,7 +71,7 @@ public class OperateLogController {
@PreAuthorize("@ss.hasPermission('system:operate-log:export')") @PreAuthorize("@ss.hasPermission('system:operate-log:export')")
@OperateLog(type = EXPORT) @OperateLog(type = EXPORT)
public void exportOperateLog(HttpServletResponse response, @Valid OperateLogExportReqVO reqVO) throws IOException { public void exportOperateLog(HttpServletResponse response, @Valid OperateLogExportReqVO reqVO) throws IOException {
List<OperateLogDO> list = operateLogService.getOperateLogs(reqVO); List<OperateLogDO> list = operateLogService.getOperateLogList(reqVO);
// 获得拼接需要的数据 // 获得拼接需要的数据
Collection<Long> userIds = CollectionUtils.convertList(list, OperateLogDO::getUserId); Collection<Long> userIds = CollectionUtils.convertList(list, OperateLogDO::getUserId);

View File

@ -1,8 +1,6 @@
package cn.iocoder.yudao.module.system.service.logger; package cn.iocoder.yudao.module.system.service.logger;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.operatelog.core.service.OperateLog;
import cn.iocoder.yudao.framework.operatelog.core.service.OperateLogFrameworkService;
import cn.iocoder.yudao.module.system.api.logger.dto.OperateLogCreateReqDTO; import cn.iocoder.yudao.module.system.api.logger.dto.OperateLogCreateReqDTO;
import cn.iocoder.yudao.module.system.controller.admin.logger.vo.operatelog.OperateLogExportReqVO; import cn.iocoder.yudao.module.system.controller.admin.logger.vo.operatelog.OperateLogExportReqVO;
import cn.iocoder.yudao.module.system.controller.admin.logger.vo.operatelog.OperateLogPageReqVO; import cn.iocoder.yudao.module.system.controller.admin.logger.vo.operatelog.OperateLogPageReqVO;
@ -38,6 +36,6 @@ public interface OperateLogService {
* @param reqVO * @param reqVO
* @return * @return
*/ */
List<OperateLogDO> getOperateLogs(OperateLogExportReqVO reqVO); List<OperateLogDO> getOperateLogList(OperateLogExportReqVO reqVO);
} }

View File

@ -59,7 +59,7 @@ public class OperateLogServiceImpl implements OperateLogService {
} }
@Override @Override
public List<OperateLogDO> getOperateLogs(OperateLogExportReqVO reqVO) { public List<OperateLogDO> getOperateLogList(OperateLogExportReqVO reqVO) {
// 处理基于用户昵称的查询 // 处理基于用户昵称的查询
Collection<Long> userIds = null; Collection<Long> userIds = null;
if (StrUtil.isNotEmpty(reqVO.getUserNickname())) { if (StrUtil.isNotEmpty(reqVO.getUserNickname())) {

View File

@ -5,8 +5,6 @@ import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants; import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
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.operatelog.core.enums.OperateTypeEnum; import cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum;
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.framework.test.core.util.RandomUtils;
@ -16,20 +14,20 @@ import cn.iocoder.yudao.module.system.controller.admin.logger.vo.operatelog.Oper
import cn.iocoder.yudao.module.system.dal.dataobject.logger.OperateLogDO; import cn.iocoder.yudao.module.system.dal.dataobject.logger.OperateLogDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.dal.mysql.logger.OperateLogMapper; import cn.iocoder.yudao.module.system.dal.mysql.logger.OperateLogMapper;
import cn.iocoder.yudao.module.system.enums.common.SexEnum;
import cn.iocoder.yudao.module.system.service.user.AdminUserService; import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
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.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import static cn.hutool.core.util.RandomUtil.randomEle; import static cn.hutool.core.util.RandomUtil.randomEle;
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.BAD_REQUEST; import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.BAD_REQUEST;
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime; 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.framework.test.core.util.RandomUtils.randomLongId; import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ -49,34 +47,28 @@ public class OperateLogServiceImplTest extends BaseDbUnitTest {
@Test @Test
public void testCreateOperateLogAsync() { public void testCreateOperateLogAsync() {
String traceId = TracerUtils.getTraceId(); OperateLogCreateReqDTO reqVO = RandomUtils.randomPojo(OperateLogCreateReqDTO.class,
OperateLogCreateReqDTO reqVO = RandomUtils.randomPojo(OperateLogCreateReqDTO.class, o -> { o -> o.setExts(MapUtil.<String, Object>builder("orderId", randomLongId()).build()));
o.setTraceId(traceId);
o.setUserId(randomLongId());
o.setUserType(randomEle(UserTypeEnum.values()).getValue());
o.setExts(MapUtil.<String, Object>builder("orderId", randomLongId()).build());
});
// 执行service方法 // 调研
operateLogServiceImpl.createOperateLog(reqVO); operateLogServiceImpl.createOperateLog(reqVO);
// 断言插入是否正确 // 断言
OperateLogDO sysOperateLogDO = operateLogMapper.selectOne("trace_id", traceId); OperateLogDO operateLogDO = operateLogMapper.selectOne(null);
assertPojoEquals(reqVO, sysOperateLogDO); assertPojoEquals(reqVO, operateLogDO);
} }
@Test @Test
public void testGetOperateLogPage() { public void testGetOperateLogPage() {
// 构造测试数据 // mock用户信息
// 先构造用户
AdminUserDO user = RandomUtils.randomPojo(AdminUserDO.class, o -> { AdminUserDO user = RandomUtils.randomPojo(AdminUserDO.class, o -> {
o.setNickname("wangkai"); o.setNickname("wang");
o.setSex(SexEnum.MALE.getSex());
o.setStatus(CommonStatusEnum.ENABLE.getStatus()); o.setStatus(CommonStatusEnum.ENABLE.getStatus());
}); });
when(userService.getUsersByNickname("wangkai")).thenReturn(Collections.singletonList(user)); when(userService.getUsersByNickname("wang")).thenReturn(Collections.singletonList(user));
Long userId = user.getId(); Long userId = user.getId();
// 构造操作日志 // 构造操作日志
OperateLogDO sysOperateLogDO = RandomUtils.randomPojo(OperateLogDO.class, o -> { OperateLogDO operateLogDO = RandomUtils.randomPojo(OperateLogDO.class, o -> {
o.setUserId(userId); o.setUserId(userId);
o.setUserType(randomEle(UserTypeEnum.values()).getValue()); o.setUserType(randomEle(UserTypeEnum.values()).getValue());
o.setModule("order"); o.setModule("order");
@ -85,50 +77,46 @@ public class OperateLogServiceImplTest extends BaseDbUnitTest {
o.setResultCode(GlobalErrorCodeConstants.SUCCESS.getCode()); o.setResultCode(GlobalErrorCodeConstants.SUCCESS.getCode());
o.setExts(MapUtil.<String, Object>builder("orderId", randomLongId()).build()); o.setExts(MapUtil.<String, Object>builder("orderId", randomLongId()).build());
}); });
operateLogMapper.insert(sysOperateLogDO); operateLogMapper.insert(operateLogDO);
// 测试 userId 不匹配
// 下面几个是不匹配的数据 operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setUserId(userId + 1)));
// 随机 userId // 测试 module 不匹配
operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setUserId(userId + 1))); operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setModule("user")));
// module 不同 // 测试 type 不匹配
operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setModule("user"))); operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setType(OperateTypeEnum.IMPORT.getType())));
// type 不同 // 测试 createTime 不匹配
operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setType(OperateTypeEnum.IMPORT.getType()))); operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setStartTime(buildTime(2021, 2, 6))));
// createTime 不同 // 测试 resultCode 不匹配
operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setStartTime(buildTime(2021, 2, 6)))); operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setResultCode(BAD_REQUEST.getCode())));
// resultCode 不同
operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setResultCode(BAD_REQUEST.getCode())));
// 构造调用参数 // 构造调用参数
OperateLogPageReqVO reqVO = new OperateLogPageReqVO(); OperateLogPageReqVO reqVO = new OperateLogPageReqVO();
reqVO.setUserNickname("wangkai"); reqVO.setUserNickname("wang");
reqVO.setModule("order"); reqVO.setModule("order");
reqVO.setType(OperateTypeEnum.CREATE.getType()); reqVO.setType(OperateTypeEnum.CREATE.getType());
reqVO.setStartTime((new LocalDateTime[]{buildTime(2021, 3, 5), reqVO.setStartTime(buildBetweenTime(2021, 3, 5, 2021, 3, 7));
buildTime(2021, 3, 7)}));
reqVO.setSuccess(true); reqVO.setSuccess(true);
// 调用service方法 // 调用
PageResult<OperateLogDO> pageResult = operateLogServiceImpl.getOperateLogPage(reqVO); PageResult<OperateLogDO> pageResult = operateLogServiceImpl.getOperateLogPage(reqVO);
// 断言,只查到了一条符合条件的 // 断言,只查到了一条符合条件的
assertEquals(1, pageResult.getTotal()); assertEquals(1, pageResult.getTotal());
assertEquals(1, pageResult.getList().size()); assertEquals(1, pageResult.getList().size());
assertPojoEquals(sysOperateLogDO, pageResult.getList().get(0)); assertPojoEquals(operateLogDO, pageResult.getList().get(0));
} }
@Test @Test
public void testGetOperateLogs() { public void testGetOperateLogs() {
// 构造测试数据 // mock用户信息
// 先构造用户
AdminUserDO user = RandomUtils.randomPojo(AdminUserDO.class, o -> { AdminUserDO user = RandomUtils.randomPojo(AdminUserDO.class, o -> {
o.setNickname("wangkai"); o.setNickname("wang");
o.setSex(SexEnum.MALE.getSex());
o.setStatus(CommonStatusEnum.ENABLE.getStatus()); o.setStatus(CommonStatusEnum.ENABLE.getStatus());
}); });
when(userService.getUsersByNickname("wangkai")).thenReturn(Collections.singletonList(user)); when(userService.getUsersByNickname("wang")).thenReturn(Collections.singletonList(user));
Long userId = user.getId(); Long userId = user.getId();
// 构造操作日志 // 构造操作日志
OperateLogDO sysOperateLogDO = RandomUtils.randomPojo(OperateLogDO.class, o -> { OperateLogDO operateLogDO = RandomUtils.randomPojo(OperateLogDO.class, o -> {
o.setUserId(userId); o.setUserId(userId);
o.setUserType(randomEle(UserTypeEnum.values()).getValue()); o.setUserType(randomEle(UserTypeEnum.values()).getValue());
o.setModule("order"); o.setModule("order");
@ -137,33 +125,31 @@ public class OperateLogServiceImplTest extends BaseDbUnitTest {
o.setResultCode(GlobalErrorCodeConstants.SUCCESS.getCode()); o.setResultCode(GlobalErrorCodeConstants.SUCCESS.getCode());
o.setExts(MapUtil.<String, Object>builder("orderId", randomLongId()).build()); o.setExts(MapUtil.<String, Object>builder("orderId", randomLongId()).build());
}); });
operateLogMapper.insert(sysOperateLogDO); operateLogMapper.insert(operateLogDO);
// 测试 userId 不匹配
// 下面几个是不匹配的数据 operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setUserId(userId + 1)));
// 随机 userId // 测试 module 不匹配
operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setUserId(userId + 1))); operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setModule("user")));
// module 不同 // 测试 type 不匹配
operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setModule("user"))); operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setType(OperateTypeEnum.IMPORT.getType())));
// type 不同 // 测试 createTime 不匹配
operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setType(OperateTypeEnum.IMPORT.getType()))); operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setStartTime(buildTime(2021, 2, 6))));
// createTime 不同 // 测试 resultCode 不匹配
operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setStartTime(buildTime(2021, 2, 6)))); operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setResultCode(BAD_REQUEST.getCode())));
// resultCode 不同
operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setResultCode(BAD_REQUEST.getCode())));
// 构造调用参数 // 构造调用参数
OperateLogExportReqVO reqVO = new OperateLogExportReqVO(); OperateLogExportReqVO reqVO = new OperateLogExportReqVO();
reqVO.setUserNickname("wangkai"); reqVO.setUserNickname("wang");
reqVO.setModule("order"); reqVO.setModule("order");
reqVO.setType(OperateTypeEnum.CREATE.getType()); reqVO.setType(OperateTypeEnum.CREATE.getType());
reqVO.setStartTime((new LocalDateTime[]{buildTime(2021, 3, 5),buildTime(2021, 3, 7)})); reqVO.setStartTime(buildBetweenTime(2021, 3, 5, 2021, 3, 7));
reqVO.setSuccess(true); reqVO.setSuccess(true);
// 调用 service 方法 // 调用 service 方法
List<OperateLogDO> list = operateLogServiceImpl.getOperateLogs(reqVO); List<OperateLogDO> list = operateLogServiceImpl.getOperateLogList(reqVO);
// 断言,只查到了一条符合条件的 // 断言,只查到了一条符合条件的
assertEquals(1, list.size()); assertEquals(1, list.size());
assertPojoEquals(sysOperateLogDO, list.get(0)); assertPojoEquals(operateLogDO, list.get(0));
} }
} }