diff --git a/src/main/java/cn/iocoder/dashboard/framework/logger/apilog/core/service/ApiAccessLogFrameworkService.java b/src/main/java/cn/iocoder/dashboard/framework/logger/apilog/core/service/ApiAccessLogFrameworkService.java index eda202ac3..ecafe5559 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/logger/apilog/core/service/ApiAccessLogFrameworkService.java +++ b/src/main/java/cn/iocoder/dashboard/framework/logger/apilog/core/service/ApiAccessLogFrameworkService.java @@ -3,6 +3,7 @@ package cn.iocoder.dashboard.framework.logger.apilog.core.service; import cn.iocoder.dashboard.framework.logger.apilog.core.service.dto.ApiAccessLogCreateDTO; import javax.validation.Valid; +import java.util.concurrent.Future; /** * API 访问日志 Framework Service 接口 @@ -15,7 +16,8 @@ public interface ApiAccessLogFrameworkService { * 创建 API 访问日志 * * @param createDTO 创建信息 + * @return 是否创建成功 */ - void createApiAccessLogAsync(@Valid ApiAccessLogCreateDTO createDTO); + Future createApiAccessLogAsync(@Valid ApiAccessLogCreateDTO createDTO); } diff --git a/src/main/java/cn/iocoder/dashboard/framework/logger/apilog/core/service/ApiErrorLogFrameworkService.java b/src/main/java/cn/iocoder/dashboard/framework/logger/apilog/core/service/ApiErrorLogFrameworkService.java index 032ef40e1..763db3a12 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/logger/apilog/core/service/ApiErrorLogFrameworkService.java +++ b/src/main/java/cn/iocoder/dashboard/framework/logger/apilog/core/service/ApiErrorLogFrameworkService.java @@ -3,6 +3,7 @@ package cn.iocoder.dashboard.framework.logger.apilog.core.service; import cn.iocoder.dashboard.framework.logger.apilog.core.service.dto.ApiErrorLogCreateDTO; import javax.validation.Valid; +import java.util.concurrent.Future; /** * API 错误日志 Framework Service 接口 @@ -15,7 +16,8 @@ public interface ApiErrorLogFrameworkService { * 创建 API 错误日志 * * @param createDTO 创建信息 + * @return 是否创建成功 */ - void createApiErrorLogAsync(@Valid ApiErrorLogCreateDTO createDTO); + Future createApiErrorLogAsync(@Valid ApiErrorLogCreateDTO createDTO); } diff --git a/src/main/java/cn/iocoder/dashboard/modules/infra/controller/logger/vo/apierrorlog/InfApiErrorLogExportReqVO.java b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/logger/vo/apierrorlog/InfApiErrorLogExportReqVO.java index a5bc820c2..991987d0e 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/infra/controller/logger/vo/apierrorlog/InfApiErrorLogExportReqVO.java +++ b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/logger/vo/apierrorlog/InfApiErrorLogExportReqVO.java @@ -14,7 +14,7 @@ import static cn.iocoder.dashboard.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOU public class InfApiErrorLogExportReqVO { @ApiModelProperty(value = "用户编号", example = "666") - private Integer userId; + private Long userId; @ApiModelProperty(value = "用户类型", example = "1") private Integer userType; diff --git a/src/main/java/cn/iocoder/dashboard/modules/infra/controller/logger/vo/apierrorlog/InfApiErrorLogPageReqVO.java b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/logger/vo/apierrorlog/InfApiErrorLogPageReqVO.java index 26f32411b..c966ab068 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/infra/controller/logger/vo/apierrorlog/InfApiErrorLogPageReqVO.java +++ b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/logger/vo/apierrorlog/InfApiErrorLogPageReqVO.java @@ -19,7 +19,7 @@ import static cn.iocoder.dashboard.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOU public class InfApiErrorLogPageReqVO extends PageParam { @ApiModelProperty(value = "用户编号", example = "666") - private Integer userId; + private Long userId; @ApiModelProperty(value = "用户类型", example = "1") private Integer userType; diff --git a/src/main/java/cn/iocoder/dashboard/modules/infra/dal/dataobject/logger/InfApiAccessLogDO.java b/src/main/java/cn/iocoder/dashboard/modules/infra/dal/dataobject/logger/InfApiAccessLogDO.java index 2a4d2cf19..ff32cea16 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/infra/dal/dataobject/logger/InfApiAccessLogDO.java +++ b/src/main/java/cn/iocoder/dashboard/modules/infra/dal/dataobject/logger/InfApiAccessLogDO.java @@ -37,7 +37,7 @@ public class InfApiAccessLogDO extends BaseDO { /** * 用户编号 */ - private Integer userId; + private Long userId; /** * 用户类型 * diff --git a/src/main/java/cn/iocoder/dashboard/modules/infra/dal/dataobject/logger/InfApiErrorLogDO.java b/src/main/java/cn/iocoder/dashboard/modules/infra/dal/dataobject/logger/InfApiErrorLogDO.java index 855c70315..dbe326cb7 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/infra/dal/dataobject/logger/InfApiErrorLogDO.java +++ b/src/main/java/cn/iocoder/dashboard/modules/infra/dal/dataobject/logger/InfApiErrorLogDO.java @@ -30,7 +30,7 @@ public class InfApiErrorLogDO extends BaseDO { /** * 用户编号 */ - private Integer userId; + private Long userId; /** * 链路追踪编号 * @@ -148,6 +148,6 @@ public class InfApiErrorLogDO extends BaseDO { * * 关联 {@link SysUserDO#getId()} */ - private Integer processUserId; + private Long processUserId; } diff --git a/src/main/java/cn/iocoder/dashboard/modules/infra/service/logger/impl/InfApiAccessLogServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/infra/service/logger/impl/InfApiAccessLogServiceImpl.java index f0d20aec4..7ed83d9e1 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/infra/service/logger/impl/InfApiAccessLogServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/infra/service/logger/impl/InfApiAccessLogServiceImpl.java @@ -9,12 +9,13 @@ import cn.iocoder.dashboard.modules.infra.dal.dataobject.logger.InfApiAccessLogD import cn.iocoder.dashboard.modules.infra.dal.mysql.logger.InfApiAccessLogMapper; import cn.iocoder.dashboard.modules.infra.service.logger.InfApiAccessLogService; import org.springframework.scheduling.annotation.Async; +import org.springframework.scheduling.annotation.AsyncResult; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; import javax.annotation.Resource; -import javax.validation.Valid; import java.util.List; +import java.util.concurrent.Future; /** * API 访问日志 Service 实现类 @@ -30,10 +31,11 @@ public class InfApiAccessLogServiceImpl implements InfApiAccessLogService { @Override @Async - public void createApiAccessLogAsync(ApiAccessLogCreateDTO createDTO) { + public Future createApiAccessLogAsync(ApiAccessLogCreateDTO createDTO) { // 插入 InfApiAccessLogDO apiAccessLog = InfApiAccessLogConvert.INSTANCE.convert(createDTO); - apiAccessLogMapper.insert(apiAccessLog); + int insert = apiAccessLogMapper.insert(apiAccessLog); + return new AsyncResult<>(insert == 1); } @Override diff --git a/src/main/java/cn/iocoder/dashboard/modules/infra/service/logger/impl/InfApiErrorLogServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/infra/service/logger/impl/InfApiErrorLogServiceImpl.java index c6a8418e4..647c0621b 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/infra/service/logger/impl/InfApiErrorLogServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/infra/service/logger/impl/InfApiErrorLogServiceImpl.java @@ -10,12 +10,14 @@ import cn.iocoder.dashboard.modules.infra.dal.mysql.logger.InfApiErrorLogMapper; import cn.iocoder.dashboard.modules.infra.enums.logger.InfApiErrorLogProcessStatusEnum; import cn.iocoder.dashboard.modules.infra.service.logger.InfApiErrorLogService; import org.springframework.scheduling.annotation.Async; +import org.springframework.scheduling.annotation.AsyncResult; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; import javax.annotation.Resource; import java.util.Date; import java.util.List; +import java.util.concurrent.Future; import static cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.dashboard.modules.infra.enums.InfErrorCodeConstants.API_ERROR_LOG_NOT_FOUND; @@ -35,10 +37,11 @@ public class InfApiErrorLogServiceImpl implements InfApiErrorLogService { @Override @Async - public void createApiErrorLogAsync(ApiErrorLogCreateDTO createDTO) { + public Future createApiErrorLogAsync(ApiErrorLogCreateDTO createDTO) { InfApiErrorLogDO apiErrorLog = InfApiErrorLogConvert.INSTANCE.convert(createDTO); apiErrorLog.setProcessStatus(InfApiErrorLogProcessStatusEnum.INIT.getStatus()); - apiErrorLogMapper.insert(apiErrorLog); + int insert = apiErrorLogMapper.insert(apiErrorLog); + return new AsyncResult<>(insert == 1); } @Override @@ -62,7 +65,7 @@ public class InfApiErrorLogServiceImpl implements InfApiErrorLogService { } // 标记处理 apiErrorLogMapper.updateById(InfApiErrorLogDO.builder().id(id).processStatus(processStatus) - .processUserId(processStatus).processTime(new Date()).build()); + .processUserId(processUserId).processTime(new Date()).build()); } } diff --git a/src/test/java/cn/iocoder/dashboard/modules/infra/service/logger/InfApiAccessLogServiceImplTest.java b/src/test/java/cn/iocoder/dashboard/modules/infra/service/logger/InfApiAccessLogServiceImplTest.java new file mode 100644 index 000000000..51b57fe4e --- /dev/null +++ b/src/test/java/cn/iocoder/dashboard/modules/infra/service/logger/InfApiAccessLogServiceImplTest.java @@ -0,0 +1,177 @@ +package cn.iocoder.dashboard.modules.infra.service.logger; + +import cn.hutool.core.util.RandomUtil; +import cn.iocoder.dashboard.BaseDbUnitTest; +import cn.iocoder.dashboard.common.enums.UserTypeEnum; +import cn.iocoder.dashboard.common.exception.enums.GlobalErrorCodeConstants; +import cn.iocoder.dashboard.common.pojo.PageResult; +import cn.iocoder.dashboard.framework.logger.apilog.core.service.dto.ApiAccessLogCreateDTO; +import cn.iocoder.dashboard.modules.infra.controller.logger.vo.apiaccesslog.InfApiAccessLogExportReqVO; +import cn.iocoder.dashboard.modules.infra.controller.logger.vo.apiaccesslog.InfApiAccessLogPageReqVO; +import cn.iocoder.dashboard.modules.infra.dal.dataobject.logger.InfApiAccessLogDO; +import cn.iocoder.dashboard.modules.infra.dal.mysql.logger.InfApiAccessLogMapper; +import cn.iocoder.dashboard.modules.infra.service.logger.impl.InfApiAccessLogServiceImpl; +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.util.Date; +import java.util.List; +import java.util.concurrent.Future; + +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; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +/** + * {@link InfApiAccessLogServiceImpl} 单元测试 + */ +@Import(InfApiAccessLogServiceImpl.class) +public class InfApiAccessLogServiceImplTest extends BaseDbUnitTest { + + @Resource + private InfApiAccessLogService infApiAccessLogServiceImpl; + + @Resource + private InfApiAccessLogMapper infApiAccessLogMapper; + + + @Test + public void testCreateApiAccessLogAsync() throws Exception { + ApiAccessLogCreateDTO createDTO = RandomUtils.randomPojo( + ApiAccessLogCreateDTO.class, + dto -> dto.setUserType(RandomUtil.randomEle(UserTypeEnum.values()).getValue()) + ); + + // 执行service方法 + Future future = infApiAccessLogServiceImpl.createApiAccessLogAsync(createDTO); + + // 等异步执行完 + future.get(); + + InfApiAccessLogDO infApiAccessLogDO = infApiAccessLogMapper.selectOne(null); + // 断言 + assertNotNull(infApiAccessLogDO); + // 断言,忽略基本字段 + assertPojoEquals(createDTO, infApiAccessLogDO); + } + + + @Test + public void testGetApiAccessLogPage() { + // 构造测试数据 + long userId = 2233L; + int userType = UserTypeEnum.ADMIN.getValue(); + String applicationName = "ruoyi-test"; + String requestUrl = "foo"; + Date beginTime = buildTime(2021, 3, 13); + int duration = 1000; + int resultCode = GlobalErrorCodeConstants.SUCCESS.getCode(); + + InfApiAccessLogDO infApiAccessLogDO = RandomUtils.randomPojo(InfApiAccessLogDO.class, dto -> { + dto.setUserId(userId); + dto.setUserType(userType); + dto.setApplicationName(applicationName); + dto.setRequestUrl(requestUrl); + dto.setBeginTime(beginTime); + dto.setDuration(duration); + dto.setResultCode(resultCode); + }); + infApiAccessLogMapper.insert(infApiAccessLogDO); + + // 下面几个都是不匹配的数据 + // userId 不同的 + infApiAccessLogMapper.insert(ObjectUtils.clone(infApiAccessLogDO, logDO -> logDO.setUserId(3344L))); + // userType + infApiAccessLogMapper.insert(ObjectUtils.clone(infApiAccessLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue()))); + // applicationName 不同的 + infApiAccessLogMapper.insert(ObjectUtils.clone(infApiAccessLogDO, logDO -> logDO.setApplicationName("test"))); + // requestUrl 不同的 + infApiAccessLogMapper.insert(ObjectUtils.clone(infApiAccessLogDO, logDO -> logDO.setRequestUrl("bar"))); + // 构造一个早期时间 2021-02-06 00:00:00 + infApiAccessLogMapper.insert(ObjectUtils.clone(infApiAccessLogDO, logDO -> logDO.setBeginTime(buildTime(2021, 2, 6)))); + // duration 不同的 + infApiAccessLogMapper.insert(ObjectUtils.clone(infApiAccessLogDO, logDO -> logDO.setDuration(100))); + // resultCode 不同的 + infApiAccessLogMapper.insert(ObjectUtils.clone(infApiAccessLogDO, logDO -> logDO.setResultCode(2))); + + // 构造调用参数 + InfApiAccessLogPageReqVO reqVO = new InfApiAccessLogPageReqVO(); + reqVO.setUserId(userId); + reqVO.setUserType(userType); + reqVO.setApplicationName(applicationName); + reqVO.setRequestUrl(requestUrl); + reqVO.setBeginBeginTime(buildTime(2021, 3, 12)); + reqVO.setEndBeginTime(buildTime(2021, 3, 14)); + reqVO.setDuration(duration); + reqVO.setResultCode(resultCode); + + // 调用service方法 + PageResult pageResult = infApiAccessLogServiceImpl.getApiAccessLogPage(reqVO); + + // 断言,只查到了一条符合条件的 + assertEquals(1, pageResult.getTotal()); + assertEquals(1, pageResult.getList().size()); + assertPojoEquals(infApiAccessLogDO, pageResult.getList().get(0)); + } + + @Test + public void testGetApiAccessLogList() { + // 构造测试数据 + long userId = 2233L; + int userType = UserTypeEnum.ADMIN.getValue(); + String applicationName = "ruoyi-test"; + String requestUrl = "foo"; + Date beginTime = buildTime(2021, 3, 13); + int duration = 1000; + int resultCode = GlobalErrorCodeConstants.SUCCESS.getCode(); + + InfApiAccessLogDO infApiAccessLogDO = RandomUtils.randomPojo(InfApiAccessLogDO.class, dto -> { + dto.setUserId(userId); + dto.setUserType(userType); + dto.setApplicationName(applicationName); + dto.setRequestUrl(requestUrl); + dto.setBeginTime(beginTime); + dto.setDuration(duration); + dto.setResultCode(resultCode); + }); + infApiAccessLogMapper.insert(infApiAccessLogDO); + + // 下面几个都是不匹配的数据 + // userId 不同的 + infApiAccessLogMapper.insert(ObjectUtils.clone(infApiAccessLogDO, logDO -> logDO.setUserId(3344L))); + // userType + infApiAccessLogMapper.insert(ObjectUtils.clone(infApiAccessLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue()))); + // applicationName 不同的 + infApiAccessLogMapper.insert(ObjectUtils.clone(infApiAccessLogDO, logDO -> logDO.setApplicationName("test"))); + // requestUrl 不同的 + infApiAccessLogMapper.insert(ObjectUtils.clone(infApiAccessLogDO, logDO -> logDO.setRequestUrl("bar"))); + // 构造一个早期时间 2021-02-06 00:00:00 + infApiAccessLogMapper.insert(ObjectUtils.clone(infApiAccessLogDO, logDO -> logDO.setBeginTime(buildTime(2021, 2, 6)))); + // duration 不同的 + infApiAccessLogMapper.insert(ObjectUtils.clone(infApiAccessLogDO, logDO -> logDO.setDuration(100))); + // resultCode 不同的 + infApiAccessLogMapper.insert(ObjectUtils.clone(infApiAccessLogDO, logDO -> logDO.setResultCode(2))); + + // 构造调用参数 + InfApiAccessLogExportReqVO reqVO = new InfApiAccessLogExportReqVO(); + reqVO.setUserId(userId); + reqVO.setUserType(userType); + reqVO.setApplicationName(applicationName); + reqVO.setRequestUrl(requestUrl); + reqVO.setBeginBeginTime(buildTime(2021, 3, 12)); + reqVO.setEndBeginTime(buildTime(2021, 3, 14)); + reqVO.setDuration(duration); + reqVO.setResultCode(resultCode); + + // 调用service方法 + List list = infApiAccessLogServiceImpl.getApiAccessLogList(reqVO); + + // 断言,只查到了一条符合条件的 + assertEquals(1, list.size()); + assertPojoEquals(infApiAccessLogDO, list.get(0)); + } +} diff --git a/src/test/java/cn/iocoder/dashboard/modules/infra/service/logger/InfApiErrorLogServiceImplTest.java b/src/test/java/cn/iocoder/dashboard/modules/infra/service/logger/InfApiErrorLogServiceImplTest.java new file mode 100644 index 000000000..79a0cfde3 --- /dev/null +++ b/src/test/java/cn/iocoder/dashboard/modules/infra/service/logger/InfApiErrorLogServiceImplTest.java @@ -0,0 +1,207 @@ +package cn.iocoder.dashboard.modules.infra.service.logger; + +import cn.hutool.core.util.RandomUtil; +import cn.iocoder.dashboard.BaseDbUnitTest; +import cn.iocoder.dashboard.common.enums.UserTypeEnum; +import cn.iocoder.dashboard.common.pojo.PageResult; +import cn.iocoder.dashboard.framework.logger.apilog.core.service.dto.ApiErrorLogCreateDTO; +import cn.iocoder.dashboard.modules.infra.controller.logger.vo.apierrorlog.InfApiErrorLogExportReqVO; +import cn.iocoder.dashboard.modules.infra.controller.logger.vo.apierrorlog.InfApiErrorLogPageReqVO; +import cn.iocoder.dashboard.modules.infra.dal.dataobject.logger.InfApiErrorLogDO; +import cn.iocoder.dashboard.modules.infra.dal.mysql.logger.InfApiErrorLogMapper; +import cn.iocoder.dashboard.modules.infra.enums.logger.InfApiErrorLogProcessStatusEnum; +import cn.iocoder.dashboard.modules.infra.service.logger.impl.InfApiErrorLogServiceImpl; +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.util.Date; +import java.util.List; +import java.util.concurrent.Future; + +import static cn.iocoder.dashboard.modules.infra.enums.InfErrorCodeConstants.API_ERROR_LOG_NOT_FOUND; +import static cn.iocoder.dashboard.modules.infra.enums.InfErrorCodeConstants.API_ERROR_LOG_PROCESSED; +import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals; +import static cn.iocoder.dashboard.util.AssertUtils.assertServiceException; +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; + +/** + * {@link InfApiErrorLogServiceImpl} 单元测试 + */ +@Import(InfApiErrorLogServiceImpl.class) +public class InfApiErrorLogServiceImplTest extends BaseDbUnitTest { + + @Resource + private InfApiErrorLogService infApiErrorLogServiceImpl; + + @Resource + private InfApiErrorLogMapper infApiErrorLogMapper; + + + @Test + public void testCreateApiErrorLogAsync() throws Exception { + ApiErrorLogCreateDTO createDTO = RandomUtils.randomPojo( + ApiErrorLogCreateDTO.class, + dto -> dto.setUserType(RandomUtil.randomEle(UserTypeEnum.values()).getValue()) + ); + + // 执行service方法 + Future future = infApiErrorLogServiceImpl.createApiErrorLogAsync(createDTO); + + // 等异步执行完 + future.get(); + + InfApiErrorLogDO infApiErrorLogDO = infApiErrorLogMapper.selectOne(null); + // 断言 + assertNotNull(infApiErrorLogDO); + // 断言,忽略基本字段 + assertPojoEquals(createDTO, infApiErrorLogDO); + } + + + @Test + public void testGetApiErrorLogPage() { + // 构造测试数据 + long userId = 2233L; + int userType = UserTypeEnum.ADMIN.getValue(); + String applicationName = "ruoyi-test"; + String requestUrl = "foo"; + Date beginTime = buildTime(2021, 3, 13); + int progressStatus = InfApiErrorLogProcessStatusEnum.INIT.getStatus(); + + InfApiErrorLogDO infApiErrorLogDO = RandomUtils.randomPojo(InfApiErrorLogDO.class, logDO -> { + logDO.setUserId(userId); + logDO.setUserType(userType); + logDO.setApplicationName(applicationName); + logDO.setRequestUrl(requestUrl); + logDO.setExceptionTime(beginTime); + logDO.setProcessStatus(progressStatus); + }); + infApiErrorLogMapper.insert(infApiErrorLogDO); + + // 下面几个都是不匹配的数据 + // userId 不同的 + infApiErrorLogMapper.insert(ObjectUtils.clone(infApiErrorLogDO, logDO -> logDO.setUserId(3344L))); + // userType + infApiErrorLogMapper.insert(ObjectUtils.clone(infApiErrorLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue()))); + // applicationName 不同的 + infApiErrorLogMapper.insert(ObjectUtils.clone(infApiErrorLogDO, logDO -> logDO.setApplicationName("test"))); + // requestUrl 不同的 + infApiErrorLogMapper.insert(ObjectUtils.clone(infApiErrorLogDO, logDO -> logDO.setRequestUrl("bar"))); + // 构造一个早期时间 2021-02-06 00:00:00 + infApiErrorLogMapper.insert(ObjectUtils.clone(infApiErrorLogDO, logDO -> logDO.setExceptionTime(buildTime(2021, 2, 6)))); + // progressStatus 不同的 + infApiErrorLogMapper.insert(ObjectUtils.clone(infApiErrorLogDO, logDO -> logDO.setProcessStatus(InfApiErrorLogProcessStatusEnum.DONE.getStatus()))); + + // 构造调用参数 + InfApiErrorLogPageReqVO reqVO = new InfApiErrorLogPageReqVO(); + reqVO.setUserId(userId); + reqVO.setUserType(userType); + reqVO.setApplicationName(applicationName); + reqVO.setRequestUrl(requestUrl); + reqVO.setBeginExceptionTime(buildTime(2021, 3, 12)); + reqVO.setEndExceptionTime(buildTime(2021, 3, 14)); + reqVO.setProcessStatus(progressStatus); + + // 调用service方法 + PageResult pageResult = infApiErrorLogServiceImpl.getApiErrorLogPage(reqVO); + + // 断言,只查到了一条符合条件的 + assertEquals(1, pageResult.getTotal()); + assertEquals(1, pageResult.getList().size()); + assertPojoEquals(infApiErrorLogDO, pageResult.getList().get(0)); + } + + @Test + public void testGetApiErrorLogList() { + // 构造测试数据 + long userId = 2233L; + int userType = UserTypeEnum.ADMIN.getValue(); + String applicationName = "ruoyi-test"; + String requestUrl = "foo"; + Date beginTime = buildTime(2021, 3, 13); + int progressStatus = InfApiErrorLogProcessStatusEnum.INIT.getStatus(); + + InfApiErrorLogDO infApiErrorLogDO = RandomUtils.randomPojo(InfApiErrorLogDO.class, logDO -> { + logDO.setUserId(userId); + logDO.setUserType(userType); + logDO.setApplicationName(applicationName); + logDO.setRequestUrl(requestUrl); + logDO.setExceptionTime(beginTime); + logDO.setProcessStatus(progressStatus); + }); + infApiErrorLogMapper.insert(infApiErrorLogDO); + + // 下面几个都是不匹配的数据 + // userId 不同的 + infApiErrorLogMapper.insert(ObjectUtils.clone(infApiErrorLogDO, logDO -> logDO.setUserId(3344L))); + // userType + infApiErrorLogMapper.insert(ObjectUtils.clone(infApiErrorLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue()))); + // applicationName 不同的 + infApiErrorLogMapper.insert(ObjectUtils.clone(infApiErrorLogDO, logDO -> logDO.setApplicationName("test"))); + // requestUrl 不同的 + infApiErrorLogMapper.insert(ObjectUtils.clone(infApiErrorLogDO, logDO -> logDO.setRequestUrl("bar"))); + // 构造一个早期时间 2021-02-06 00:00:00 + infApiErrorLogMapper.insert(ObjectUtils.clone(infApiErrorLogDO, logDO -> logDO.setExceptionTime(buildTime(2021, 2, 6)))); + // progressStatus 不同的 + infApiErrorLogMapper.insert(ObjectUtils.clone(infApiErrorLogDO, logDO -> logDO.setProcessStatus(InfApiErrorLogProcessStatusEnum.DONE.getStatus()))); + + // 构造调用参数 + InfApiErrorLogExportReqVO reqVO = new InfApiErrorLogExportReqVO(); + reqVO.setUserId(userId); + reqVO.setUserType(userType); + reqVO.setApplicationName(applicationName); + reqVO.setRequestUrl(requestUrl); + reqVO.setBeginExceptionTime(buildTime(2021, 3, 12)); + reqVO.setEndExceptionTime(buildTime(2021, 3, 14)); + reqVO.setProcessStatus(progressStatus); + + // 调用service方法 + List list = infApiErrorLogServiceImpl.getApiErrorLogList(reqVO); + + // 断言,只查到了一条符合条件的 + assertEquals(1, list.size()); + assertPojoEquals(infApiErrorLogDO, list.get(0)); + } + + + @Test + public void testUpdateApiErrorLogProcess() { + // 先构造两条数据,第一条用于抛出异常,第二条用于正常的执行update操作 + Long processUserId = 2233L; + + InfApiErrorLogDO first = RandomUtils.randomPojo(InfApiErrorLogDO.class, logDO -> { + logDO.setProcessUserId(processUserId); + logDO.setUserType(UserTypeEnum.ADMIN.getValue()); + logDO.setProcessStatus(InfApiErrorLogProcessStatusEnum.DONE.getStatus()); + }); + infApiErrorLogMapper.insert(first); + + InfApiErrorLogDO second = RandomUtils.randomPojo(InfApiErrorLogDO.class, logDO -> { + logDO.setProcessUserId(1122L); + logDO.setUserType(UserTypeEnum.ADMIN.getValue()); + logDO.setProcessStatus(InfApiErrorLogProcessStatusEnum.INIT.getStatus()); + }); + infApiErrorLogMapper.insert(second); + + Long firstId = first.getId(); + Long secondId = second.getId(); + + // 执行正常的 update 操作 + infApiErrorLogServiceImpl.updateApiErrorLogProcess(secondId, InfApiErrorLogProcessStatusEnum.DONE.getStatus(), processUserId); + InfApiErrorLogDO secondSelect = infApiErrorLogMapper.selectOne("id", secondId); + + // id 为 0 查询不到,应该抛出异常 API_ERROR_LOG_NOT_FOUND + assertServiceException(() -> infApiErrorLogServiceImpl.updateApiErrorLogProcess(0L, InfApiErrorLogProcessStatusEnum.DONE.getStatus(), processUserId), API_ERROR_LOG_NOT_FOUND); + // id 为 first 的 progressStatus 为 DONE ,应该抛出 API_ERROR_LOG_PROCESSED + assertServiceException(() -> infApiErrorLogServiceImpl.updateApiErrorLogProcess(firstId, InfApiErrorLogProcessStatusEnum.DONE.getStatus(), processUserId), API_ERROR_LOG_PROCESSED); + // 验证 progressStatus 是否修改成功 + assertEquals(InfApiErrorLogProcessStatusEnum.DONE.getStatus(), secondSelect.getProcessStatus()); + // 验证 progressUserId 是否修改成功 + assertEquals(processUserId, secondSelect.getProcessUserId()); + } +} diff --git a/src/test/resources/sql/create_tables.sql b/src/test/resources/sql/create_tables.sql index 4a9d218b9..26c588717 100644 --- a/src/test/resources/sql/create_tables.sql +++ b/src/test/resources/sql/create_tables.sql @@ -241,3 +241,60 @@ create table IF NOT EXISTS "sys_user" ( "deleted" bit not null default false, primary key ("id") ) comment '用户信息表'; + + +create table "inf_api_access_log" ( + "id" bigint not null GENERATED BY DEFAULT AS IDENTITY, + "trace_id" varchar(64) not null default '', + "user_id" bigint not null default '0', + "user_type" tinyint not null default '0', + "application_name" varchar(50) not null, + "request_method" varchar(16) not null default '', + "request_url" varchar(255) not null default '', + "request_params" varchar(8000) not null default '', + "user_ip" varchar(50) not null, + "user_agent" varchar(512) not null, + "begin_time" timestamp not null, + "end_time" timestamp not null, + "duration" integer not null, + "result_code" integer not null default '0', + "result_msg" varchar(512) default '', + "creator" varchar(64) default '', + "create_time" timestamp not null default current_timestamp, + "updater" varchar(64) default '', + "update_time" timestamp not null default current_timestamp, + "deleted" bit not null default false, + primary key ("id") +) comment 'API 访问日志表'; + + +create table "inf_api_error_log" ( + "id" integer not null GENERATED BY DEFAULT AS IDENTITY, + "trace_id" varchar(64) not null, + "user_id" bigint not null default '0', + "user_type" tinyint not null default '0', + "application_name" varchar(50) not null, + "request_method" varchar(16) not null, + "request_url" varchar(255) not null, + "request_params" varchar(8000) not null, + "user_ip" varchar(50) not null, + "user_agent" varchar(512) not null, + "exception_time" timestamp not null, + "exception_name" varchar(128) not null default '', + "exception_message" clob not null, + "exception_root_cause_message" clob not null, + "exception_stack_trace" clob not null, + "exception_class_name" varchar(512) not null, + "exception_file_name" varchar(512) not null, + "exception_method_name" varchar(512) not null, + "exception_line_number" integer not null, + "process_status" tinyint not null, + "process_time" timestamp default null, + "process_user_id" bigint default '0', + "creator" varchar(64) default '', + "create_time" timestamp not null default current_timestamp, + "updater" varchar(64) default '', + "update_time" timestamp not null default current_timestamp, + "deleted" bit not null default false, + primary key ("id") +) comment '系统异常日志'; \ No newline at end of file