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.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();

View File

@ -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();

View File

@ -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<TestResourcePool> 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<TestResourcePool> 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<OptionDTO> options = new ArrayList<>();
plusins.forEach(serviceIntegration -> {
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.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<TestResourcePool> 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<TestResourcePool> 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");
}

View File

@ -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");

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.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<OptionDTO> getTestResourcePoolOptions(ProjectPoolRequest request) {
List<OptionDTO> optionDTOS = new ArrayList<>();
//获取制定组织的资源池 和全部组织的资源池
List<TestResourcePool> testResourcePools = new ArrayList<>();
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<TestResourcePool> testResourcePools = commonProjectPoolService.getOrgTestResourcePools(request.getOrganizationId(), true);
//这里需要获取项目开启的模块 判断资源池开启的使用范围的模块是否在项目开启的模块中
List<String> moduleIds = request.getModulesIds();
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.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;
@ -75,7 +74,7 @@ public class TestPlanCollectionMinderService {
private ProjectMapper projectMapper;
@Resource
private TestResourcePoolMapper testResourcePoolMapper;
private CommonProjectPoolService commonProjectPoolService;
@Resource
private TestPlanMapper testPlanMapper;
@ -92,9 +91,7 @@ public class TestPlanCollectionMinderService {
Project project = projectMapper.selectByPrimaryKey(testPlan.getProjectId());
List<TestResourcePool> 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");
}