refactor(资源池): 调整当前项目获取全部资源池方法

This commit is contained in:
guoyuqi 2024-10-25 12:00:42 +08:00 committed by 刘瑞斌
parent 14639792a6
commit a8a9b150d9
8 changed files with 115 additions and 50 deletions

View File

@ -294,7 +294,7 @@ public class ApiTaskCenterControllerTests extends BaseTest {
testResourcePool.setCreateUser("admin"); testResourcePool.setCreateUser("admin");
testResourcePool.setAllOrg(true); testResourcePool.setAllOrg(true);
testResourcePool.setEnable(true); testResourcePool.setEnable(true);
testResourcePool.setType(ResourcePoolTypeEnum.NODE.name()); testResourcePool.setType(ResourcePoolTypeEnum.NODE.getName());
TestResourcePoolBlob testResourcePoolBlob = new TestResourcePoolBlob(); TestResourcePoolBlob testResourcePoolBlob = new TestResourcePoolBlob();
testResourcePoolBlob.setId("api-pool-id"); testResourcePoolBlob.setId("api-pool-id");
TestResourceDTO testResourceDTO = new TestResourceDTO(); TestResourceDTO testResourceDTO = new TestResourceDTO();

View File

@ -68,7 +68,7 @@ public class BaseResourcePoolTestService {
testResourcePool.setCreateUser("admin"); testResourcePool.setCreateUser("admin");
testResourcePool.setAllOrg(false); testResourcePool.setAllOrg(false);
testResourcePool.setEnable(true); testResourcePool.setEnable(true);
testResourcePool.setType(ResourcePoolTypeEnum.NODE.name()); testResourcePool.setType(ResourcePoolTypeEnum.NODE.getName());
TestResourcePoolBlob testResourcePoolBlob = new TestResourcePoolBlob(); TestResourcePoolBlob testResourcePoolBlob = new TestResourcePoolBlob();
testResourcePoolBlob.setId(id); testResourcePoolBlob.setId(id);
TestResourceDTO testResourceDTO = new TestResourceDTO(); TestResourceDTO testResourceDTO = new TestResourceDTO();

View File

@ -67,6 +67,8 @@ public class ProjectApplicationService {
private ProjectMapper projectMapper; private ProjectMapper projectMapper;
@Resource @Resource
private TestResourcePoolMapper testResourcePoolMapper; private TestResourcePoolMapper testResourcePoolMapper;
@Resource
private CommonProjectPoolService commonProjectPoolService;
/** /**
* 更新配置信息 * 更新配置信息
@ -141,15 +143,18 @@ public class ProjectApplicationService {
moduleType = "api_test"; moduleType = "api_test";
} }
Project project = projectMapper.selectByPrimaryKey(projectId); Project project = projectMapper.selectByPrimaryKey(projectId);
List<TestResourcePool> testResourcePools = new ArrayList<>();
if (project.getAllResourcePool()) {
testResourcePools= commonProjectPoolService.getProjectAllPoolsByEffect(project);
}
if (StringUtils.isNotBlank(poolType) && StringUtils.isNotBlank(moduleType)) { if (StringUtils.isNotBlank(poolType) && StringUtils.isNotBlank(moduleType)) {
if (configMap.containsKey(poolType)) { if (configMap.containsKey(poolType)) {
//如果是适用于所有的组织 //如果是适用于所有的组织
int count = 0; int count = 0;
TestResourcePoolExample example = new TestResourcePoolExample();
if (project.getAllResourcePool()) { if (project.getAllResourcePool()) {
example.createCriteria().andIdEqualTo(configMap.get(poolType).toString()).andEnableEqualTo(true).andDeletedEqualTo(false); count = testResourcePools.size();
count = (int) testResourcePoolMapper.countByExample(example);
}else { }else {
TestResourcePoolExample example = new TestResourcePoolExample();
example.createCriteria().andIdEqualTo(configMap.get(poolType).toString()).andAllOrgEqualTo(true); example.createCriteria().andIdEqualTo(configMap.get(poolType).toString()).andAllOrgEqualTo(true);
if (testResourcePoolMapper.countByExample(example) > 0) { if (testResourcePoolMapper.countByExample(example) > 0) {
count = extProjectMapper.resourcePoolIsExist(configMap.get(poolType).toString(), projectId); count = extProjectMapper.resourcePoolIsExist(configMap.get(poolType).toString(), projectId);
@ -164,9 +169,6 @@ public class ProjectApplicationService {
} }
if (!configMap.containsKey(poolType)) { if (!configMap.containsKey(poolType)) {
if (project.getAllResourcePool()){ if (project.getAllResourcePool()){
TestResourcePoolExample example = new TestResourcePoolExample();
example.createCriteria().andEnableEqualTo(true).andDeletedEqualTo(false);
List<TestResourcePool> testResourcePools = testResourcePoolMapper.selectByExample(example);
if (CollectionUtils.isNotEmpty(testResourcePools)) { if (CollectionUtils.isNotEmpty(testResourcePools)) {
testResourcePools.sort(Comparator.comparing(TestResourcePool::getId)); testResourcePools.sort(Comparator.comparing(TestResourcePool::getId));
configMap.put(poolType, testResourcePools.getFirst().getId()); configMap.put(poolType, testResourcePools.getFirst().getId());
@ -212,7 +214,7 @@ public class ProjectApplicationService {
.filter(serviceIntegration -> { .filter(serviceIntegration -> {
return serviceIntegration.getEnable() // 服务集成开启 return serviceIntegration.getEnable() // 服务集成开启
&& orgPluginIds.contains(serviceIntegration.getPluginId()); // 该服务集成对应的插件有权限 && orgPluginIds.contains(serviceIntegration.getPluginId()); // 该服务集成对应的插件有权限
}).collect(Collectors.toList()); }).toList();
List<OptionDTO> options = new ArrayList<>(); List<OptionDTO> options = new ArrayList<>();
plusins.forEach(serviceIntegration -> { plusins.forEach(serviceIntegration -> {
PluginWrapper pluginWrapper = pluginLoadService.getPluginWrapper(serviceIntegration.getPluginId()); PluginWrapper pluginWrapper = pluginLoadService.getPluginWrapper(serviceIntegration.getPluginId());

View File

@ -13,15 +13,22 @@ import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.CommonBeanFactory; import io.metersphere.sdk.util.CommonBeanFactory;
import io.metersphere.sdk.util.Translator; 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.ProjectDTO;
import io.metersphere.system.dto.sdk.OptionDTO; import io.metersphere.system.dto.sdk.OptionDTO;
import io.metersphere.system.dto.sdk.SessionUser; import io.metersphere.system.dto.sdk.SessionUser;
import io.metersphere.system.dto.taskhub.ResourcePoolOptionsDTO; import io.metersphere.system.dto.taskhub.ResourcePoolOptionsDTO;
import io.metersphere.system.dto.user.UserDTO; import io.metersphere.system.dto.user.UserDTO;
import io.metersphere.system.dto.user.UserExtendDTO; 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.BaseTaskHubService;
import io.metersphere.system.service.CommonProjectPoolService;
import io.metersphere.system.service.CommonProjectService; import io.metersphere.system.service.CommonProjectService;
import io.metersphere.system.service.UserLoginService; import io.metersphere.system.service.UserLoginService;
import io.metersphere.system.utils.ServiceUtils; import io.metersphere.system.utils.ServiceUtils;
@ -56,7 +63,7 @@ public class ProjectService {
@Resource @Resource
private CommonProjectService commonProjectService; private CommonProjectService commonProjectService;
@Resource @Resource
private TestResourcePoolMapper testResourcePoolMapper; private CommonProjectPoolService commonProjectPoolService;
@Resource @Resource
private ProjectTestResourcePoolMapper projectTestResourcePoolMapper; private ProjectTestResourcePoolMapper projectTestResourcePoolMapper;
@Resource @Resource
@ -291,15 +298,14 @@ public class ProjectService {
/** /**
* 获取项目下可用的资源池 * 获取项目下可用的资源池
*
* @param projectId 项目ID * @param projectId 项目ID
* @return 资源池列表 * @return 资源池列表
*/ */
public List<TestResourcePool> getPoolOption(String projectId) { public List<TestResourcePool> getPoolOption(String projectId) {
Project project = projectMapper.selectByPrimaryKey(projectId); Project project = projectMapper.selectByPrimaryKey(projectId);
if (project.getAllResourcePool()) { if (project.getAllResourcePool()) {
TestResourcePoolExample example = new TestResourcePoolExample(); return commonProjectPoolService.getProjectAllPoolsByEffect(project);
example.createCriteria().andEnableEqualTo(true).andDeletedEqualTo(false);
return testResourcePoolMapper.selectByExample(example);
} else { } else {
return extProjectMapper.getResourcePoolOption(projectId, "api_test"); return extProjectMapper.getResourcePoolOption(projectId, "api_test");
} }
@ -323,9 +329,7 @@ public class ProjectService {
private List<TestResourcePool> getAllPoolOption(String projectId) { private List<TestResourcePool> getAllPoolOption(String projectId) {
Project project = projectMapper.selectByPrimaryKey(projectId); Project project = projectMapper.selectByPrimaryKey(projectId);
if (project.getAllResourcePool()) { if (project.getAllResourcePool()) {
TestResourcePoolExample example = new TestResourcePoolExample(); return commonProjectPoolService.getOrgTestResourcePools(project.getOrganizationId(), false);
example.createCriteria().andDeletedEqualTo(false);
return testResourcePoolMapper.selectByExample(example);
} else { } else {
return extProjectMapper.getResourcePoolOption(projectId, "api_test"); return extProjectMapper.getResourcePoolOption(projectId, "api_test");
} }

View File

@ -74,7 +74,9 @@ public class ProjectApplicationControllerTests extends BaseTest {
//测试计划 - 清理报告配置 //测试计划 - 清理报告配置
@Test @Test
@Order(1) @Order(1)
@Sql(scripts = {"/dml/init_project_application_test.sql"}, config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED))
public void testTestPlanClean() throws Exception { public void testTestPlanClean() throws Exception {
this.testGetTestPlan(); this.testGetTestPlan();
//新增 //新增
ProjectApplication request = creatRequest(ProjectApplicationType.TEST_PLAN.TEST_PLAN_CLEAN_REPORT.name(), TIME_TYPE_VALUE); ProjectApplication request = creatRequest(ProjectApplicationType.TEST_PLAN.TEST_PLAN_CLEAN_REPORT.name(), TIME_TYPE_VALUE);
@ -279,7 +281,6 @@ public class ProjectApplicationControllerTests extends BaseTest {
//用例管理 - 获取平台下拉列表 //用例管理 - 获取平台下拉列表
@Test @Test
@Order(40) @Order(40)
@Sql(scripts = {"/dml/init_project_application_test.sql"}, config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED))
public void testGetPlatform() throws Exception { public void testGetPlatform() throws Exception {
this.requestGetWithOkAndReturn(GET_PLATFORM_URL + "/100002"); this.requestGetWithOkAndReturn(GET_PLATFORM_URL + "/100002");
MvcResult mvcResult = this.requestGetWithOkAndReturn(GET_PLATFORM_URL + "/100001"); MvcResult mvcResult = this.requestGetWithOkAndReturn(GET_PLATFORM_URL + "/100001");

View File

@ -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<TestResourcePool> getOrgTestResourcePools(String organizationId, boolean onlyEnable) {
List<TestResourcePool> testResourcePools = new ArrayList<>();
if (StringUtils.isNotBlank(organizationId)) {
TestResourcePoolOrganizationExample example = new TestResourcePoolOrganizationExample();
example.createCriteria().andOrgIdEqualTo(organizationId);
List<TestResourcePoolOrganization> orgPools = testResourcePoolOrganizationMapper.selectByExample(example);
if (CollectionUtils.isNotEmpty(orgPools)) {
List<String> 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<TestResourcePool> getProjectAllPoolsByEffect(Project project) {
List<TestResourcePool> testResourcePools = getOrgTestResourcePools(project.getOrganizationId(), true);
List<String> modulesIds = JSON.parseArray(project.getModuleSetting(), String.class);
if (modulesIds.contains(API_TEST) || modulesIds.contains(TEST_PLAN)) {
return testResourcePools;
} else {
return new ArrayList<>();
}
}
}

View File

@ -42,7 +42,6 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Service @Service
@ -72,12 +71,14 @@ public class CommonProjectService {
private OrganizationMapper organizationMapper; private OrganizationMapper organizationMapper;
@Resource @Resource
private TestResourcePoolMapper testResourcePoolMapper; private TestResourcePoolMapper testResourcePoolMapper;
@Resource
private TestResourcePoolOrganizationMapper testResourcePoolOrganizationMapper;
@Resource @Resource
private ProjectTestResourcePoolMapper projectTestResourcePoolMapper; private ProjectTestResourcePoolMapper projectTestResourcePoolMapper;
@Resource @Resource
private TestResourcePoolService testResourcePoolService; private TestResourcePoolService testResourcePoolService;
@Resource
private CommonProjectPoolService commonProjectPoolService;
public static final Integer DEFAULT_REMAIN_DAY_COUNT = 30; public static final Integer DEFAULT_REMAIN_DAY_COUNT = 30;
public static final String API_TEST = "apiTest"; public static final String API_TEST = "apiTest";
public static final String TEST_PLAN = "testPlan"; public static final String TEST_PLAN = "testPlan";
@ -636,24 +637,7 @@ public class CommonProjectService {
public List<OptionDTO> getTestResourcePoolOptions(ProjectPoolRequest request) { public List<OptionDTO> getTestResourcePoolOptions(ProjectPoolRequest request) {
List<OptionDTO> optionDTOS = new ArrayList<>(); List<OptionDTO> optionDTOS = new ArrayList<>();
//获取制定组织的资源池 和全部组织的资源池 //获取制定组织的资源池 和全部组织的资源池
List<TestResourcePool> testResourcePools = new ArrayList<>(); List<TestResourcePool> testResourcePools = commonProjectPoolService.getOrgTestResourcePools(request.getOrganizationId(), true);
if (StringUtils.isNotBlank(request.getOrganizationId())) {
TestResourcePoolOrganizationExample example = new TestResourcePoolOrganizationExample();
example.createCriteria().andOrgIdEqualTo(request.getOrganizationId());
List<TestResourcePoolOrganization> orgPools = testResourcePoolOrganizationMapper.selectByExample(example);
if (CollectionUtils.isNotEmpty(orgPools)) {
List<String> 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<String> moduleIds = request.getModulesIds(); List<String> moduleIds = request.getModulesIds();
testResourcePools.forEach(pool -> { testResourcePools.forEach(pool -> {

View File

@ -17,9 +17,8 @@ import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.Translator; import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.TestResourcePool; import io.metersphere.system.domain.TestResourcePool;
import io.metersphere.system.domain.TestResourcePoolExample;
import io.metersphere.system.dto.sdk.SessionUser; 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 io.metersphere.system.uid.IDGenerator;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
@ -65,8 +64,8 @@ public class TestPlanCollectionMinderService {
@Autowired @Autowired
private ApplicationContext applicationContext; private ApplicationContext applicationContext;
@Resource @Resource
private TestPlanConfigMapper testPlanConfigMapper; private TestPlanConfigMapper testPlanConfigMapper;
@Resource @Resource
private ExtProjectMapper extProjectMapper; private ExtProjectMapper extProjectMapper;
@ -75,7 +74,7 @@ public class TestPlanCollectionMinderService {
private ProjectMapper projectMapper; private ProjectMapper projectMapper;
@Resource @Resource
private TestResourcePoolMapper testResourcePoolMapper; private CommonProjectPoolService commonProjectPoolService;
@Resource @Resource
private TestPlanMapper testPlanMapper; private TestPlanMapper testPlanMapper;
@ -92,14 +91,12 @@ public class TestPlanCollectionMinderService {
Project project = projectMapper.selectByPrimaryKey(testPlan.getProjectId()); Project project = projectMapper.selectByPrimaryKey(testPlan.getProjectId());
List<TestResourcePool> apiTest; List<TestResourcePool> apiTest;
if (project.getAllResourcePool()) { if (project.getAllResourcePool()) {
TestResourcePoolExample example = new TestResourcePoolExample(); apiTest = commonProjectPoolService.getProjectAllPoolsByEffect(project);
example.createCriteria().andEnableEqualTo(true).andDeletedEqualTo(false);
apiTest= testResourcePoolMapper.selectByExample(example);
} else { } else {
apiTest = extProjectMapper.getResourcePoolOption(testPlan.getProjectId(), "api_test"); apiTest = extProjectMapper.getResourcePoolOption(testPlan.getProjectId(), "api_test");
} }
Map<String, String> resourcePoolMap = apiTest.stream().collect(Collectors.toMap(TestResourcePool::getId, TestResourcePool::getName)); Map<String, String> resourcePoolMap = apiTest.stream().collect(Collectors.toMap(TestResourcePool::getId, TestResourcePool::getName));
testPlanCollections.forEach(t->{ testPlanCollections.forEach(t -> {
if (StringUtils.isBlank(resourcePoolMap.get(t.getTestResourcePoolId()))) { if (StringUtils.isBlank(resourcePoolMap.get(t.getTestResourcePoolId()))) {
t.setPoolName(Translator.get("resource_pool_not_exist")); t.setPoolName(Translator.get("resource_pool_not_exist"));
t.setNoResourcePool(true); t.setNoResourcePool(true);