From b54c5eaa45e96ceebcaa1b3466c635781fd488e0 Mon Sep 17 00:00:00 2001 From: gaibu <1016771049@qq.com> Date: Sat, 18 Mar 2023 20:53:33 +0800 Subject: [PATCH 1/2] =?UTF-8?q?test:=20job=20=E7=A7=9F=E6=88=B7=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pom.xml | 4 +- .../tenant/core/job/TenantJobTest.java | 39 +++++++++++++++++++ .../framework/tenant/core/job/TestJob.java | 26 +++++++++++++ 3 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-tenant/src/test/java/cn/iocoder/yudao/framework/tenant/core/job/TenantJobTest.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-tenant/src/test/java/cn/iocoder/yudao/framework/tenant/core/job/TestJob.java diff --git a/yudao-framework/yudao-spring-boot-starter-biz-tenant/pom.xml b/yudao-framework/yudao-spring-boot-starter-biz-tenant/pom.xml index 1d43ff2c5..0fb0a4f71 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-tenant/pom.xml +++ b/yudao-framework/yudao-spring-boot-starter-biz-tenant/pom.xml @@ -52,8 +52,8 @@ - org.springframework.boot - spring-boot-starter-test + cn.iocoder.boot + yudao-spring-boot-starter-test test diff --git a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/test/java/cn/iocoder/yudao/framework/tenant/core/job/TenantJobTest.java b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/test/java/cn/iocoder/yudao/framework/tenant/core/job/TenantJobTest.java new file mode 100644 index 000000000..9b1c6cf23 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/test/java/cn/iocoder/yudao/framework/tenant/core/job/TenantJobTest.java @@ -0,0 +1,39 @@ +package cn.iocoder.yudao.framework.tenant.core.job; + +import cn.iocoder.yudao.framework.tenant.core.service.TenantFrameworkService; +import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; +import com.google.common.collect.Lists; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.mockito.Spy; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * 验证 job 租户逻辑 + * {@link TenantJobHandlerDecorator} + * + * @author gaibu + */ +public class TenantJobTest extends BaseMockitoUnitTest { + @Spy + TenantFrameworkService tenantFrameworkService; + + @Test + public void test() throws Exception { + // 准备测试租户 id + List tenantIds = Lists.newArrayList(1L, 2L, 3L); + // mock 数据 + Mockito.doReturn(tenantIds).when(tenantFrameworkService).getTenantIds(); + // 准备测试任务 + TestJob testJob = new TestJob(); + // 创建任务装饰器 + TenantJobHandlerDecorator tenantJobHandlerDecorator = new TenantJobHandlerDecorator(tenantFrameworkService, testJob); + // 执行任务 + tenantJobHandlerDecorator.execute(null); + // 断言返回值 + assertEquals(testJob.getTenantIds(), tenantIds); + } +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/test/java/cn/iocoder/yudao/framework/tenant/core/job/TestJob.java b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/test/java/cn/iocoder/yudao/framework/tenant/core/job/TestJob.java new file mode 100644 index 000000000..a4de61dbc --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/test/java/cn/iocoder/yudao/framework/tenant/core/job/TestJob.java @@ -0,0 +1,26 @@ +package cn.iocoder.yudao.framework.tenant.core.job; + +import cn.hutool.core.collection.CollUtil; +import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler; +import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + +@Component +@TenantJob // 标记多租户 +public class TestJob implements JobHandler { + private final List tenantIds = new CopyOnWriteArrayList<>(); + + @Override + public String execute(String param) throws Exception { + tenantIds.add(TenantContextHolder.getTenantId()); + return "success"; + } + + public List getTenantIds() { + CollUtil.sort(tenantIds, Long::compareTo); + return tenantIds; + } +} From 348a0ff02a9efadc6427ab86c3b68f576e53a236 Mon Sep 17 00:00:00 2001 From: gaibu <1016771049@qq.com> Date: Mon, 20 Mar 2023 21:50:53 +0800 Subject: [PATCH 2/2] =?UTF-8?q?test:=20=E4=BF=AE=E5=A4=8D=20code=20view=20?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yudao/framework/tenant/core/job/TenantJobTest.java | 7 +++++-- .../iocoder/yudao/framework/tenant/core/job/TestJob.java | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/test/java/cn/iocoder/yudao/framework/tenant/core/job/TenantJobTest.java b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/test/java/cn/iocoder/yudao/framework/tenant/core/job/TenantJobTest.java index 9b1c6cf23..8e23338bb 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/test/java/cn/iocoder/yudao/framework/tenant/core/job/TenantJobTest.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/test/java/cn/iocoder/yudao/framework/tenant/core/job/TenantJobTest.java @@ -4,8 +4,8 @@ import cn.iocoder.yudao.framework.tenant.core.service.TenantFrameworkService; import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; import com.google.common.collect.Lists; import org.junit.jupiter.api.Test; +import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.Spy; import java.util.List; @@ -18,7 +18,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; * @author gaibu */ public class TenantJobTest extends BaseMockitoUnitTest { - @Spy + + @Mock TenantFrameworkService tenantFrameworkService; @Test @@ -31,8 +32,10 @@ public class TenantJobTest extends BaseMockitoUnitTest { TestJob testJob = new TestJob(); // 创建任务装饰器 TenantJobHandlerDecorator tenantJobHandlerDecorator = new TenantJobHandlerDecorator(tenantFrameworkService, testJob); + // 执行任务 tenantJobHandlerDecorator.execute(null); + // 断言返回值 assertEquals(testJob.getTenantIds(), tenantIds); } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/test/java/cn/iocoder/yudao/framework/tenant/core/job/TestJob.java b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/test/java/cn/iocoder/yudao/framework/tenant/core/job/TestJob.java index a4de61dbc..bad1cb304 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/test/java/cn/iocoder/yudao/framework/tenant/core/job/TestJob.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/test/java/cn/iocoder/yudao/framework/tenant/core/job/TestJob.java @@ -11,6 +11,7 @@ import java.util.concurrent.CopyOnWriteArrayList; @Component @TenantJob // 标记多租户 public class TestJob implements JobHandler { + private final List tenantIds = new CopyOnWriteArrayList<>(); @Override