From e4c62c34b31132421df31778b62cf627f584e770 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Mon, 4 Dec 2023 20:43:32 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20=E5=A2=9E=E5=8A=A0=20infra=20logger?= =?UTF-8?q?=20=E6=A8=A1=E5=9D=97=E7=9A=84=E5=8D=95=E6=B5=8B=E8=A6=86?= =?UTF-8?q?=E7=9B=96=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../logger/ApiAccessLogServiceImplTest.java | 25 ++++++++++++++-- .../logger/ApiErrorLogServiceImplTest.java | 29 ++++++++++++++++--- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/logger/ApiAccessLogServiceImplTest.java b/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/logger/ApiAccessLogServiceImplTest.java index dc9b3d907..02362c7a7 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/logger/ApiAccessLogServiceImplTest.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/logger/ApiAccessLogServiceImplTest.java @@ -12,9 +12,10 @@ import org.junit.jupiter.api.Test; import org.springframework.context.annotation.Import; import javax.annotation.Resource; +import java.time.Duration; +import java.util.List; -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.*; 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.RandomUtils.randomPojo; @@ -73,6 +74,26 @@ public class ApiAccessLogServiceImplTest extends BaseDbUnitTest { assertPojoEquals(apiAccessLogDO, pageResult.getList().get(0)); } + @Test + public void testCleanJobLog() { + // mock 数据 + ApiAccessLogDO log01 = randomPojo(ApiAccessLogDO.class, o -> o.setCreateTime(addTime(Duration.ofDays(-3)))); + apiAccessLogMapper.insert(log01); + ApiAccessLogDO log02 = randomPojo(ApiAccessLogDO.class, o -> o.setCreateTime(addTime(Duration.ofDays(-1)))); + apiAccessLogMapper.insert(log02); + // 准备参数 + Integer exceedDay = 2; + Integer deleteLimit = 1; + + // 调用 + Integer count = apiAccessLogService.cleanAccessLog(exceedDay, deleteLimit); + // 断言 + assertEquals(1, count); + List logs = apiAccessLogMapper.selectList(); + assertEquals(1, logs.size()); + assertEquals(log02, logs.get(0)); + } + @Test public void testCreateApiAccessLog() { // 准备参数 diff --git a/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/logger/ApiErrorLogServiceImplTest.java b/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/logger/ApiErrorLogServiceImplTest.java index dab21bfff..acba4b47b 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/logger/ApiErrorLogServiceImplTest.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/logger/ApiErrorLogServiceImplTest.java @@ -12,10 +12,11 @@ import org.junit.jupiter.api.Test; import org.springframework.context.annotation.Import; import javax.annotation.Resource; +import java.time.Duration; +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.LocalDateTimeUtils.buildTime; +import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.*; 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.assertServiceException; @@ -122,7 +123,7 @@ public class ApiErrorLogServiceImplTest extends BaseDbUnitTest { // 调用,并断言异常 assertServiceException(() -> - apiErrorLogService.updateApiErrorLogProcess(id, processStatus, processUserId), + apiErrorLogService.updateApiErrorLogProcess(id, processStatus, processUserId), API_ERROR_LOG_PROCESSED); } @@ -135,8 +136,28 @@ public class ApiErrorLogServiceImplTest extends BaseDbUnitTest { // 调用,并断言异常 assertServiceException(() -> - apiErrorLogService.updateApiErrorLogProcess(id, processStatus, processUserId), + apiErrorLogService.updateApiErrorLogProcess(id, processStatus, processUserId), API_ERROR_LOG_NOT_FOUND); } + @Test + public void testCleanJobLog() { + // mock 数据 + ApiErrorLogDO log01 = randomPojo(ApiErrorLogDO.class, o -> o.setCreateTime(addTime(Duration.ofDays(-3)))); + apiErrorLogMapper.insert(log01); + ApiErrorLogDO log02 = randomPojo(ApiErrorLogDO.class, o -> o.setCreateTime(addTime(Duration.ofDays(-1)))); + apiErrorLogMapper.insert(log02); + // 准备参数 + Integer exceedDay = 2; + Integer deleteLimit = 1; + + // 调用 + Integer count = apiErrorLogService.cleanErrorLog(exceedDay, deleteLimit); + // 断言 + assertEquals(1, count); + List logs = apiErrorLogMapper.selectList(); + assertEquals(1, logs.size()); + assertEquals(log02, logs.get(0)); + } + }