From a8a9b150d960a8b4f266f353951168b674d87c85 Mon Sep 17 00:00:00 2001 From: guoyuqi Date: Fri, 25 Oct 2024 12:00:42 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E8=B5=84=E6=BA=90=E6=B1=A0):=20?= =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=BD=93=E5=89=8D=E9=A1=B9=E7=9B=AE=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E5=85=A8=E9=83=A8=E8=B5=84=E6=BA=90=E6=B1=A0=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ApiTaskCenterControllerTests.java | 2 +- .../service/BaseResourcePoolTestService.java | 2 +- .../service/ProjectApplicationService.java | 16 ++-- .../project/service/ProjectService.java | 22 +++--- .../ProjectApplicationControllerTests.java | 3 +- .../service/CommonProjectPoolService.java | 77 +++++++++++++++++++ .../system/service/CommonProjectService.java | 26 ++----- .../TestPlanCollectionMinderService.java | 17 ++-- 8 files changed, 115 insertions(+), 50 deletions(-) create mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/service/CommonProjectPoolService.java diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiTaskCenterControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiTaskCenterControllerTests.java index c11d7eb040..9e787eb654 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiTaskCenterControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiTaskCenterControllerTests.java @@ -294,7 +294,7 @@ public class ApiTaskCenterControllerTests extends BaseTest { testResourcePool.setCreateUser("admin"); testResourcePool.setAllOrg(true); testResourcePool.setEnable(true); - testResourcePool.setType(ResourcePoolTypeEnum.NODE.name()); + testResourcePool.setType(ResourcePoolTypeEnum.NODE.getName()); TestResourcePoolBlob testResourcePoolBlob = new TestResourcePoolBlob(); testResourcePoolBlob.setId("api-pool-id"); TestResourceDTO testResourceDTO = new TestResourceDTO(); diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/service/BaseResourcePoolTestService.java b/backend/services/api-test/src/test/java/io/metersphere/api/service/BaseResourcePoolTestService.java index 6559a69904..29061c9388 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/service/BaseResourcePoolTestService.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/service/BaseResourcePoolTestService.java @@ -68,7 +68,7 @@ public class BaseResourcePoolTestService { testResourcePool.setCreateUser("admin"); testResourcePool.setAllOrg(false); testResourcePool.setEnable(true); - testResourcePool.setType(ResourcePoolTypeEnum.NODE.name()); + testResourcePool.setType(ResourcePoolTypeEnum.NODE.getName()); TestResourcePoolBlob testResourcePoolBlob = new TestResourcePoolBlob(); testResourcePoolBlob.setId(id); TestResourceDTO testResourceDTO = new TestResourceDTO(); diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectApplicationService.java b/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectApplicationService.java index a8cd20c37a..9c442d3664 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectApplicationService.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectApplicationService.java @@ -67,6 +67,8 @@ public class ProjectApplicationService { private ProjectMapper projectMapper; @Resource private TestResourcePoolMapper testResourcePoolMapper; + @Resource + private CommonProjectPoolService commonProjectPoolService; /** * 更新配置信息 @@ -141,15 +143,18 @@ public class ProjectApplicationService { moduleType = "api_test"; } Project project = projectMapper.selectByPrimaryKey(projectId); + List testResourcePools = new ArrayList<>(); + if (project.getAllResourcePool()) { + testResourcePools= commonProjectPoolService.getProjectAllPoolsByEffect(project); + } if (StringUtils.isNotBlank(poolType) && StringUtils.isNotBlank(moduleType)) { if (configMap.containsKey(poolType)) { //如果是适用于所有的组织 int count = 0; - TestResourcePoolExample example = new TestResourcePoolExample(); if (project.getAllResourcePool()) { - example.createCriteria().andIdEqualTo(configMap.get(poolType).toString()).andEnableEqualTo(true).andDeletedEqualTo(false); - count = (int) testResourcePoolMapper.countByExample(example); + count = testResourcePools.size(); }else { + TestResourcePoolExample example = new TestResourcePoolExample(); example.createCriteria().andIdEqualTo(configMap.get(poolType).toString()).andAllOrgEqualTo(true); if (testResourcePoolMapper.countByExample(example) > 0) { count = extProjectMapper.resourcePoolIsExist(configMap.get(poolType).toString(), projectId); @@ -164,9 +169,6 @@ public class ProjectApplicationService { } if (!configMap.containsKey(poolType)) { if (project.getAllResourcePool()){ - TestResourcePoolExample example = new TestResourcePoolExample(); - example.createCriteria().andEnableEqualTo(true).andDeletedEqualTo(false); - List testResourcePools = testResourcePoolMapper.selectByExample(example); if (CollectionUtils.isNotEmpty(testResourcePools)) { testResourcePools.sort(Comparator.comparing(TestResourcePool::getId)); configMap.put(poolType, testResourcePools.getFirst().getId()); @@ -212,7 +214,7 @@ public class ProjectApplicationService { .filter(serviceIntegration -> { return serviceIntegration.getEnable() // 服务集成开启 && orgPluginIds.contains(serviceIntegration.getPluginId()); // 该服务集成对应的插件有权限 - }).collect(Collectors.toList()); + }).toList(); List options = new ArrayList<>(); plusins.forEach(serviceIntegration -> { PluginWrapper pluginWrapper = pluginLoadService.getPluginWrapper(serviceIntegration.getPluginId()); diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectService.java b/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectService.java index c4dcbf766f..b56f79109a 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectService.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectService.java @@ -13,15 +13,22 @@ import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.CommonBeanFactory; import io.metersphere.sdk.util.Translator; -import io.metersphere.system.domain.*; +import io.metersphere.system.domain.TestResourcePool; +import io.metersphere.system.domain.TestResourcePoolBlob; +import io.metersphere.system.domain.User; +import io.metersphere.system.domain.UserRoleRelationExample; import io.metersphere.system.dto.ProjectDTO; import io.metersphere.system.dto.sdk.OptionDTO; import io.metersphere.system.dto.sdk.SessionUser; import io.metersphere.system.dto.taskhub.ResourcePoolOptionsDTO; import io.metersphere.system.dto.user.UserDTO; import io.metersphere.system.dto.user.UserExtendDTO; -import io.metersphere.system.mapper.*; +import io.metersphere.system.mapper.BaseUserMapper; +import io.metersphere.system.mapper.ExtSystemProjectMapper; +import io.metersphere.system.mapper.OrganizationMapper; +import io.metersphere.system.mapper.UserRoleRelationMapper; import io.metersphere.system.service.BaseTaskHubService; +import io.metersphere.system.service.CommonProjectPoolService; import io.metersphere.system.service.CommonProjectService; import io.metersphere.system.service.UserLoginService; import io.metersphere.system.utils.ServiceUtils; @@ -56,7 +63,7 @@ public class ProjectService { @Resource private CommonProjectService commonProjectService; @Resource - private TestResourcePoolMapper testResourcePoolMapper; + private CommonProjectPoolService commonProjectPoolService; @Resource private ProjectTestResourcePoolMapper projectTestResourcePoolMapper; @Resource @@ -291,15 +298,14 @@ public class ProjectService { /** * 获取项目下可用的资源池 + * * @param projectId 项目ID * @return 资源池列表 */ public List getPoolOption(String projectId) { Project project = projectMapper.selectByPrimaryKey(projectId); if (project.getAllResourcePool()) { - TestResourcePoolExample example = new TestResourcePoolExample(); - example.createCriteria().andEnableEqualTo(true).andDeletedEqualTo(false); - return testResourcePoolMapper.selectByExample(example); + return commonProjectPoolService.getProjectAllPoolsByEffect(project); } else { return extProjectMapper.getResourcePoolOption(projectId, "api_test"); } @@ -323,9 +329,7 @@ public class ProjectService { private List getAllPoolOption(String projectId) { Project project = projectMapper.selectByPrimaryKey(projectId); if (project.getAllResourcePool()) { - TestResourcePoolExample example = new TestResourcePoolExample(); - example.createCriteria().andDeletedEqualTo(false); - return testResourcePoolMapper.selectByExample(example); + return commonProjectPoolService.getOrgTestResourcePools(project.getOrganizationId(), false); } else { return extProjectMapper.getResourcePoolOption(projectId, "api_test"); } diff --git a/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectApplicationControllerTests.java b/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectApplicationControllerTests.java index ca45155681..09a215c0d6 100644 --- a/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectApplicationControllerTests.java +++ b/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectApplicationControllerTests.java @@ -74,7 +74,9 @@ public class ProjectApplicationControllerTests extends BaseTest { //测试计划 - 清理报告配置 @Test @Order(1) + @Sql(scripts = {"/dml/init_project_application_test.sql"}, config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED)) public void testTestPlanClean() throws Exception { + this.testGetTestPlan(); //新增 ProjectApplication request = creatRequest(ProjectApplicationType.TEST_PLAN.TEST_PLAN_CLEAN_REPORT.name(), TIME_TYPE_VALUE); @@ -279,7 +281,6 @@ public class ProjectApplicationControllerTests extends BaseTest { //用例管理 - 获取平台下拉列表 @Test @Order(40) - @Sql(scripts = {"/dml/init_project_application_test.sql"}, config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED)) public void testGetPlatform() throws Exception { this.requestGetWithOkAndReturn(GET_PLATFORM_URL + "/100002"); MvcResult mvcResult = this.requestGetWithOkAndReturn(GET_PLATFORM_URL + "/100001"); diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/CommonProjectPoolService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/CommonProjectPoolService.java new file mode 100644 index 0000000000..bbc9d09af6 --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/CommonProjectPoolService.java @@ -0,0 +1,77 @@ +package io.metersphere.system.service; + + +import io.metersphere.project.domain.Project; +import io.metersphere.sdk.util.JSON; +import io.metersphere.system.domain.TestResourcePool; +import io.metersphere.system.domain.TestResourcePoolExample; +import io.metersphere.system.domain.TestResourcePoolOrganization; +import io.metersphere.system.domain.TestResourcePoolOrganizationExample; +import io.metersphere.system.mapper.TestResourcePoolMapper; +import io.metersphere.system.mapper.TestResourcePoolOrganizationMapper; +import jakarta.annotation.Resource; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; + +@Service +@Transactional(rollbackFor = Exception.class) +public class CommonProjectPoolService { + + public static final String API_TEST = "apiTest"; + public static final String TEST_PLAN = "testPlan"; + + @Resource + private TestResourcePoolOrganizationMapper testResourcePoolOrganizationMapper; + @Resource + private TestResourcePoolMapper testResourcePoolMapper; + + public List getOrgTestResourcePools(String organizationId, boolean onlyEnable) { + List testResourcePools = new ArrayList<>(); + if (StringUtils.isNotBlank(organizationId)) { + TestResourcePoolOrganizationExample example = new TestResourcePoolOrganizationExample(); + example.createCriteria().andOrgIdEqualTo(organizationId); + List orgPools = testResourcePoolOrganizationMapper.selectByExample(example); + if (CollectionUtils.isNotEmpty(orgPools)) { + List poolIds = orgPools.stream().map(TestResourcePoolOrganization::getTestResourcePoolId).toList(); + TestResourcePoolExample poolExample = new TestResourcePoolExample(); + if (onlyEnable) { + poolExample.createCriteria().andIdIn(poolIds).andEnableEqualTo(true).andDeletedEqualTo(false); + } else { + poolExample.createCriteria().andIdIn(poolIds).andDeletedEqualTo(false); + } + testResourcePools.addAll(testResourcePoolMapper.selectByExample(poolExample)); + } + } + //获取应用全部组织的资源池 + TestResourcePoolExample poolExample = new TestResourcePoolExample(); + if (onlyEnable) { + poolExample.createCriteria().andAllOrgEqualTo(true).andEnableEqualTo(true).andDeletedEqualTo(false); + } else { + poolExample.createCriteria().andAllOrgEqualTo(true).andDeletedEqualTo(false); + } + testResourcePools.addAll(testResourcePoolMapper.selectByExample(poolExample)); + + testResourcePools = testResourcePools.stream().filter(Objects::nonNull).distinct().collect(Collectors.toList()); + return testResourcePools; + } + + /** + * 获取当前项目所有可用资源池 + */ + public List getProjectAllPoolsByEffect(Project project) { + List testResourcePools = getOrgTestResourcePools(project.getOrganizationId(), true); + List modulesIds = JSON.parseArray(project.getModuleSetting(), String.class); + if (modulesIds.contains(API_TEST) || modulesIds.contains(TEST_PLAN)) { + return testResourcePools; + } else { + return new ArrayList<>(); + } + } +} diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/CommonProjectService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/CommonProjectService.java index b58cb1ddcf..8a67caa9b9 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/CommonProjectService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/CommonProjectService.java @@ -42,7 +42,6 @@ import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; import java.util.Map; -import java.util.Objects; import java.util.stream.Collectors; @Service @@ -72,12 +71,14 @@ public class CommonProjectService { private OrganizationMapper organizationMapper; @Resource private TestResourcePoolMapper testResourcePoolMapper; - @Resource - private TestResourcePoolOrganizationMapper testResourcePoolOrganizationMapper; + @Resource private ProjectTestResourcePoolMapper projectTestResourcePoolMapper; @Resource private TestResourcePoolService testResourcePoolService; + @Resource + private CommonProjectPoolService commonProjectPoolService; + public static final Integer DEFAULT_REMAIN_DAY_COUNT = 30; public static final String API_TEST = "apiTest"; public static final String TEST_PLAN = "testPlan"; @@ -636,24 +637,7 @@ public class CommonProjectService { public List getTestResourcePoolOptions(ProjectPoolRequest request) { List optionDTOS = new ArrayList<>(); //获取制定组织的资源池 和全部组织的资源池 - List testResourcePools = new ArrayList<>(); - if (StringUtils.isNotBlank(request.getOrganizationId())) { - TestResourcePoolOrganizationExample example = new TestResourcePoolOrganizationExample(); - example.createCriteria().andOrgIdEqualTo(request.getOrganizationId()); - List orgPools = testResourcePoolOrganizationMapper.selectByExample(example); - if (CollectionUtils.isNotEmpty(orgPools)) { - List poolIds = orgPools.stream().map(TestResourcePoolOrganization::getTestResourcePoolId).toList(); - TestResourcePoolExample poolExample = new TestResourcePoolExample(); - poolExample.createCriteria().andIdIn(poolIds).andEnableEqualTo(true).andDeletedEqualTo(false); - testResourcePools.addAll(testResourcePoolMapper.selectByExample(poolExample)); - } - } - //获取应用全部组织的资源池 - TestResourcePoolExample poolExample = new TestResourcePoolExample(); - poolExample.createCriteria().andAllOrgEqualTo(true).andEnableEqualTo(true).andDeletedEqualTo(false); - testResourcePools.addAll(testResourcePoolMapper.selectByExample(poolExample)); - - testResourcePools = testResourcePools.stream().filter(Objects::nonNull).distinct().collect(Collectors.toList()); + List testResourcePools = commonProjectPoolService.getOrgTestResourcePools(request.getOrganizationId(), true); //这里需要获取项目开启的模块 判断资源池开启的使用范围的模块是否在项目开启的模块中 List moduleIds = request.getModulesIds(); testResourcePools.forEach(pool -> { diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanCollectionMinderService.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanCollectionMinderService.java index 9aa7eef04a..a5f22dd889 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanCollectionMinderService.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanCollectionMinderService.java @@ -17,9 +17,8 @@ import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.Translator; import io.metersphere.system.domain.TestResourcePool; -import io.metersphere.system.domain.TestResourcePoolExample; import io.metersphere.system.dto.sdk.SessionUser; -import io.metersphere.system.mapper.TestResourcePoolMapper; +import io.metersphere.system.service.CommonProjectPoolService; import io.metersphere.system.uid.IDGenerator; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; @@ -65,8 +64,8 @@ public class TestPlanCollectionMinderService { @Autowired private ApplicationContext applicationContext; - @Resource - private TestPlanConfigMapper testPlanConfigMapper; + @Resource + private TestPlanConfigMapper testPlanConfigMapper; @Resource private ExtProjectMapper extProjectMapper; @@ -75,7 +74,7 @@ public class TestPlanCollectionMinderService { private ProjectMapper projectMapper; @Resource - private TestResourcePoolMapper testResourcePoolMapper; + private CommonProjectPoolService commonProjectPoolService; @Resource private TestPlanMapper testPlanMapper; @@ -92,14 +91,12 @@ public class TestPlanCollectionMinderService { Project project = projectMapper.selectByPrimaryKey(testPlan.getProjectId()); List apiTest; if (project.getAllResourcePool()) { - TestResourcePoolExample example = new TestResourcePoolExample(); - example.createCriteria().andEnableEqualTo(true).andDeletedEqualTo(false); - apiTest= testResourcePoolMapper.selectByExample(example); + apiTest = commonProjectPoolService.getProjectAllPoolsByEffect(project); } else { - apiTest = extProjectMapper.getResourcePoolOption(testPlan.getProjectId(), "api_test"); + apiTest = extProjectMapper.getResourcePoolOption(testPlan.getProjectId(), "api_test"); } Map resourcePoolMap = apiTest.stream().collect(Collectors.toMap(TestResourcePool::getId, TestResourcePool::getName)); - testPlanCollections.forEach(t->{ + testPlanCollections.forEach(t -> { if (StringUtils.isBlank(resourcePoolMap.get(t.getTestResourcePoolId()))) { t.setPoolName(Translator.get("resource_pool_not_exist")); t.setNoResourcePool(true);