修改 system logger junit review 后提出的问题
parent
75d21928a4
commit
c197417d92
|
@ -2,13 +2,16 @@ package cn.iocoder.dashboard.framework.logger.operatelog.core.service;
|
|||
|
||||
import cn.iocoder.dashboard.modules.system.controller.logger.vo.operatelog.SysOperateLogCreateReqVO;
|
||||
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
public interface OperateLogFrameworkService {
|
||||
|
||||
/**
|
||||
* 要不记录操作日志
|
||||
* 异步记录操作日志
|
||||
*
|
||||
* @param reqVO 操作日志请求
|
||||
* @return true: 记录成功,false: 记录失败
|
||||
*/
|
||||
void createOperateLogAsync(SysOperateLogCreateReqVO reqVO);
|
||||
Future<Boolean> createOperateLogAsync(SysOperateLogCreateReqVO reqVO);
|
||||
|
||||
}
|
||||
|
|
|
@ -7,20 +7,22 @@ import cn.iocoder.dashboard.modules.system.controller.logger.vo.operatelog.SysOp
|
|||
import cn.iocoder.dashboard.modules.system.controller.logger.vo.operatelog.SysOperateLogExportReqVO;
|
||||
import cn.iocoder.dashboard.modules.system.controller.logger.vo.operatelog.SysOperateLogPageReqVO;
|
||||
import cn.iocoder.dashboard.modules.system.convert.logger.SysOperateLogConvert;
|
||||
import cn.iocoder.dashboard.modules.system.dal.mysql.logger.SysOperateLogMapper;
|
||||
import cn.iocoder.dashboard.modules.system.dal.dataobject.logger.SysOperateLogDO;
|
||||
import cn.iocoder.dashboard.modules.system.dal.dataobject.user.SysUserDO;
|
||||
import cn.iocoder.dashboard.modules.system.dal.mysql.logger.SysOperateLogMapper;
|
||||
import cn.iocoder.dashboard.modules.system.service.logger.SysOperateLogService;
|
||||
import cn.iocoder.dashboard.modules.system.service.user.SysUserService;
|
||||
import cn.iocoder.dashboard.util.string.StrUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.scheduling.annotation.AsyncResult;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import static cn.iocoder.dashboard.modules.system.dal.dataobject.logger.SysOperateLogDO.JAVA_METHOD_ARGS_MAX_LENGTH;
|
||||
import static cn.iocoder.dashboard.modules.system.dal.dataobject.logger.SysOperateLogDO.RESULT_MAX_LENGTH;
|
||||
|
@ -38,16 +40,18 @@ public class SysOperateLogServiceImpl implements SysOperateLogService {
|
|||
|
||||
@Override
|
||||
@Async
|
||||
public void createOperateLogAsync(SysOperateLogCreateReqVO reqVO) {
|
||||
public Future<Boolean> createOperateLogAsync(SysOperateLogCreateReqVO reqVO) {
|
||||
boolean success = false;
|
||||
try {
|
||||
SysOperateLogDO logDO = SysOperateLogConvert.INSTANCE.convert(reqVO);
|
||||
logDO.setJavaMethodArgs(StrUtils.maxLength(logDO.getJavaMethodArgs(), JAVA_METHOD_ARGS_MAX_LENGTH));
|
||||
logDO.setResultData(StrUtils.maxLength(logDO.getResultData(), RESULT_MAX_LENGTH));
|
||||
operateLogMapper.insert(logDO);
|
||||
success = operateLogMapper.insert(logDO) == 1;
|
||||
} catch (Throwable throwable) {
|
||||
// 仅仅打印日志,不对外抛出。原因是,还是要保留现场数据。
|
||||
log.error("[createOperateLogAsync][记录操作日志异常,日志为 ({})]", reqVO, throwable);
|
||||
}
|
||||
return new AsyncResult<>(success);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -3,14 +3,13 @@ package cn.iocoder.dashboard.modules.system.service.logger;
|
|||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.iocoder.dashboard.BaseSpringBootUnitTest;
|
||||
import cn.iocoder.dashboard.BaseDbUnitTest;
|
||||
import cn.iocoder.dashboard.common.pojo.PageResult;
|
||||
import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO;
|
||||
import cn.iocoder.dashboard.framework.tracer.core.util.TracerUtils;
|
||||
import cn.iocoder.dashboard.modules.system.controller.logger.vo.loginlog.SysLoginLogCreateReqVO;
|
||||
import cn.iocoder.dashboard.modules.system.controller.logger.vo.loginlog.SysLoginLogExportReqVO;
|
||||
import cn.iocoder.dashboard.modules.system.controller.logger.vo.loginlog.SysLoginLogPageReqVO;
|
||||
import cn.iocoder.dashboard.modules.system.convert.logger.SysLoginLogConvert;
|
||||
import cn.iocoder.dashboard.modules.system.dal.dataobject.logger.SysLoginLogDO;
|
||||
import cn.iocoder.dashboard.modules.system.dal.mysql.logger.SysLoginLogMapper;
|
||||
import cn.iocoder.dashboard.modules.system.enums.logger.SysLoginLogTypeEnum;
|
||||
|
@ -19,6 +18,7 @@ import cn.iocoder.dashboard.modules.system.service.logger.impl.SysLoginLogServic
|
|||
import cn.iocoder.dashboard.util.RandomUtils;
|
||||
import cn.iocoder.dashboard.util.object.ObjectUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.lang.reflect.Field;
|
||||
|
@ -30,7 +30,8 @@ import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals;
|
|||
import static cn.iocoder.dashboard.util.date.DateUtils.buildTime;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class SysLoginLogServiceImplTest extends BaseSpringBootUnitTest {
|
||||
@Import(SysLoginLogServiceImpl.class)
|
||||
public class SysLoginLogServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private SysLoginLogServiceImpl sysLoginLogService;
|
||||
|
@ -40,7 +41,6 @@ public class SysLoginLogServiceImplTest extends BaseSpringBootUnitTest {
|
|||
|
||||
@Test
|
||||
public void testCreateLoginLog() {
|
||||
|
||||
String traceId = TracerUtils.getTraceId();
|
||||
SysLoginLogCreateReqVO reqVO = RandomUtils.randomPojo(SysLoginLogCreateReqVO.class, vo -> {
|
||||
// 指定随机的范围,避免超出范围入库失败
|
||||
|
@ -50,19 +50,12 @@ public class SysLoginLogServiceImplTest extends BaseSpringBootUnitTest {
|
|||
vo.setTraceId(traceId);
|
||||
});
|
||||
|
||||
|
||||
// 执行service方法
|
||||
sysLoginLogService.createLoginLog(reqVO);
|
||||
|
||||
// 查询插入的数据
|
||||
SysLoginLogDO sysLoginLogDO = loginLogMapper.selectOne("trace_id", traceId);
|
||||
|
||||
// 断言,忽略基本字段
|
||||
assertPojoEquals(
|
||||
SysLoginLogConvert.INSTANCE.convert(reqVO),
|
||||
sysLoginLogDO,
|
||||
getBaseDOFields()
|
||||
);
|
||||
SysLoginLogDO sysLoginLogDO = loginLogMapper.selectOne(null);
|
||||
assertPojoEquals(reqVO, sysLoginLogDO);
|
||||
}
|
||||
|
||||
|
||||
|
@ -155,17 +148,4 @@ public class SysLoginLogServiceImplTest extends BaseSpringBootUnitTest {
|
|||
assertEquals(1, loginLogList.size());
|
||||
assertPojoEquals(loginLogDO, loginLogList.get(0));
|
||||
}
|
||||
|
||||
|
||||
private static String[] getBaseDOFields() {
|
||||
Field[] fields = ReflectUtil.getFields(BaseDO.class);
|
||||
|
||||
List<String> collect = Arrays.stream(fields)
|
||||
.map(Field::getName)
|
||||
.collect(Collectors.toList());
|
||||
collect.add("id");
|
||||
|
||||
return ArrayUtil.toArray(collect, String.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
package cn.iocoder.dashboard.modules.system.service.logger;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.iocoder.dashboard.BaseSpringBootUnitTest;
|
||||
import cn.iocoder.dashboard.BaseDbUnitTest;
|
||||
import cn.iocoder.dashboard.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.dashboard.common.exception.enums.GlobalErrorCodeConstants;
|
||||
import cn.iocoder.dashboard.common.pojo.PageResult;
|
||||
|
@ -13,15 +14,18 @@ import cn.iocoder.dashboard.framework.tracer.core.util.TracerUtils;
|
|||
import cn.iocoder.dashboard.modules.system.controller.logger.vo.operatelog.SysOperateLogCreateReqVO;
|
||||
import cn.iocoder.dashboard.modules.system.controller.logger.vo.operatelog.SysOperateLogExportReqVO;
|
||||
import cn.iocoder.dashboard.modules.system.controller.logger.vo.operatelog.SysOperateLogPageReqVO;
|
||||
import cn.iocoder.dashboard.modules.system.convert.logger.SysOperateLogConvert;
|
||||
import cn.iocoder.dashboard.modules.system.dal.dataobject.logger.SysOperateLogDO;
|
||||
import cn.iocoder.dashboard.modules.system.dal.dataobject.user.SysUserDO;
|
||||
import cn.iocoder.dashboard.modules.system.dal.mysql.logger.SysOperateLogMapper;
|
||||
import cn.iocoder.dashboard.modules.system.dal.mysql.user.SysUserMapper;
|
||||
import cn.iocoder.dashboard.modules.system.enums.common.SysSexEnum;
|
||||
import cn.iocoder.dashboard.modules.system.service.logger.impl.SysOperateLogServiceImpl;
|
||||
import cn.iocoder.dashboard.modules.system.service.user.SysUserService;
|
||||
import cn.iocoder.dashboard.util.RandomUtils;
|
||||
import cn.iocoder.dashboard.util.object.ObjectUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.lang.reflect.Field;
|
||||
|
@ -29,6 +33,8 @@ import java.util.Arrays;
|
|||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals;
|
||||
|
@ -36,7 +42,8 @@ import static cn.iocoder.dashboard.util.date.DateUtils.buildTime;
|
|||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class SysOperateLogServiceImplTest extends BaseSpringBootUnitTest {
|
||||
@Import({SysOperateLogServiceImpl.class})
|
||||
public class SysOperateLogServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private SysOperateLogService sysOperateLogServiceImpl;
|
||||
|
@ -47,24 +54,26 @@ public class SysOperateLogServiceImplTest extends BaseSpringBootUnitTest {
|
|||
@Resource
|
||||
private SysUserMapper sysUserMapper;
|
||||
|
||||
@MockBean
|
||||
private SysUserService sysUserService;
|
||||
|
||||
@Test
|
||||
public void testCreateOperateLogAsync() throws InterruptedException {
|
||||
public void testCreateOperateLogAsync() throws InterruptedException, ExecutionException {
|
||||
|
||||
String traceId = TracerUtils.getTraceId();
|
||||
SysOperateLogCreateReqVO reqVO = RandomUtils.randomPojo(SysOperateLogCreateReqVO.class, vo -> {
|
||||
vo.setTraceId(traceId);
|
||||
vo.setUserId(RandomUtil.randomLong(1, Long.MAX_VALUE));
|
||||
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("orderId", 1);
|
||||
Map<String, Object> map = MapUtil.builder("orderId", (Object) 1).build();
|
||||
vo.setExts(map);
|
||||
});
|
||||
|
||||
// 执行service方法
|
||||
sysOperateLogServiceImpl.createOperateLogAsync(reqVO);
|
||||
Future<Boolean> future = sysOperateLogServiceImpl.createOperateLogAsync(reqVO);
|
||||
|
||||
// 等异步执行完
|
||||
Thread.sleep(2000);
|
||||
future.get();
|
||||
|
||||
// 查询插入的数据
|
||||
SysOperateLogDO sysOperateLogDO = sysOperateLogMapper.selectOne("trace_id", traceId);
|
||||
|
@ -72,11 +81,7 @@ public class SysOperateLogServiceImplTest extends BaseSpringBootUnitTest {
|
|||
// 断言
|
||||
assertNotNull(sysOperateLogDO);
|
||||
// 断言,忽略基本字段
|
||||
assertPojoEquals(
|
||||
SysOperateLogConvert.INSTANCE.convert(reqVO),
|
||||
sysOperateLogDO,
|
||||
getBaseDOFields()
|
||||
);
|
||||
assertPojoEquals(reqVO, sysOperateLogDO);
|
||||
}
|
||||
|
||||
|
||||
|
@ -165,8 +170,8 @@ public class SysOperateLogServiceImplTest extends BaseSpringBootUnitTest {
|
|||
entity.setStartTime(buildTime(2021, 3, 6));
|
||||
entity.setResultCode(GlobalErrorCodeConstants.SUCCESS.getCode());
|
||||
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("orderId", 1);
|
||||
|
||||
Map<String, Object> map = MapUtil.builder("orderId", (Object) 1).build();
|
||||
entity.setExts(map);
|
||||
});
|
||||
|
||||
|
@ -200,17 +205,4 @@ public class SysOperateLogServiceImplTest extends BaseSpringBootUnitTest {
|
|||
assertEquals(1, list.size());
|
||||
assertPojoEquals(sysOperateLogDO, list.get(0));
|
||||
}
|
||||
|
||||
|
||||
private static String[] getBaseDOFields() {
|
||||
Field[] fields = ReflectUtil.getFields(BaseDO.class);
|
||||
|
||||
List<String> collect = Arrays.stream(fields)
|
||||
.map(Field::getName)
|
||||
.collect(Collectors.toList());
|
||||
collect.add("id");
|
||||
|
||||
return ArrayUtil.toArray(collect, String.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -170,9 +170,9 @@ CREATE TABLE IF NOT EXISTS `sys_login_log` (
|
|||
`result` tinyint(4) NOT NULL,
|
||||
`user_ip` varchar(50) NOT NULL,
|
||||
`user_agent` varchar(512) NOT NULL,
|
||||
`create_by` varchar(64) DEFAULT '',
|
||||
`creator` varchar(64) DEFAULT '',
|
||||
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
`update_by` varchar(64) DEFAULT '',
|
||||
`updater` varchar(64) DEFAULT '',
|
||||
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
`deleted` bit(1) NOT NULL DEFAULT '0',
|
||||
PRIMARY KEY (`id`)
|
||||
|
@ -199,9 +199,9 @@ CREATE TABLE `sys_operate_log` (
|
|||
`result_code` int(11) NOT NULL DEFAULT '0',
|
||||
`result_msg` varchar(512) DEFAULT '',
|
||||
`result_data` varchar(4000) DEFAULT '',
|
||||
`create_by` varchar(64) DEFAULT '',
|
||||
`creator` varchar(64) DEFAULT '',
|
||||
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
`update_by` varchar(64) DEFAULT '',
|
||||
`updater` varchar(64) DEFAULT '',
|
||||
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
`deleted` bit(1) NOT NULL DEFAULT '0',
|
||||
PRIMARY KEY (`id`)
|
||||
|
@ -222,9 +222,9 @@ create table "sys_user" (
|
|||
"status" tinyint not null default '0',
|
||||
"login_ip" varchar(50) default '',
|
||||
"login_date" timestamp default null,
|
||||
"create_by" varchar(64) default '',
|
||||
"creator" varchar(64) default '',
|
||||
"create_time" timestamp not null default current_timestamp,
|
||||
"update_by" varchar(64) default '',
|
||||
"updater" varchar(64) default '',
|
||||
"update_time" timestamp not null default current_timestamp,
|
||||
"deleted" bit not null default false,
|
||||
primary key ("id")
|
||||
|
|
Loading…
Reference in New Issue