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 1d43ff2c53..0fb0a4f71b 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 0000000000..9b1c6cf235 --- /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 0000000000..a4de61dbc6 --- /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; + } +}