refactor(通用功能): 调整项目下的资源池的获取

This commit is contained in:
guoyuqi 2024-10-18 16:50:50 +08:00 committed by Craftsman
parent 7aa2decb27
commit 025082fdfc
5 changed files with 129 additions and 21 deletions

View File

@ -16,10 +16,7 @@ import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.CommonBeanFactory;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.Plugin;
import io.metersphere.system.domain.ServiceIntegration;
import io.metersphere.system.domain.TestResourcePoolExample;
import io.metersphere.system.domain.User;
import io.metersphere.system.domain.*;
import io.metersphere.system.dto.sdk.OptionDTO;
import io.metersphere.system.log.constants.OperationLogModule;
import io.metersphere.system.log.constants.OperationLogType;
@ -143,11 +140,16 @@ public class ProjectApplicationService {
poolType = ProjectApplicationType.API.API_RESOURCE_POOL_ID.name();
moduleType = "api_test";
}
Project project = projectMapper.selectByPrimaryKey(projectId);
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);
}else {
example.createCriteria().andIdEqualTo(configMap.get(poolType).toString()).andAllOrgEqualTo(true);
if (testResourcePoolMapper.countByExample(example) > 0) {
count = extProjectMapper.resourcePoolIsExist(configMap.get(poolType).toString(), projectId);
@ -155,11 +157,22 @@ public class ProjectApplicationService {
//指定组织 则需要关联组织-资源池的关系表 看看是否再全部存在
count = extProjectMapper.resourcePoolIsExistByOrg(configMap.get(poolType).toString(), projectId);
}
}
if (count == 0) {
configMap.remove(poolType);
}
}
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());
}
} else {
List<ProjectTestResourcePool> projectTestResourcePools = extProjectMapper.getResourcePool(projectId);
if (CollectionUtils.isNotEmpty(projectTestResourcePools)) {
projectTestResourcePools.sort(Comparator.comparing(ProjectTestResourcePool::getTestResourcePoolId));
@ -167,6 +180,7 @@ public class ProjectApplicationService {
}
}
}
}
}

View File

@ -2,10 +2,11 @@ package io.metersphere.project.controller;
import io.metersphere.project.controller.param.ProjectApplicationDefinition;
import io.metersphere.project.controller.param.ProjectApplicationRequestDefinition;
import io.metersphere.project.domain.Project;
import io.metersphere.project.domain.ProjectApplication;
import io.metersphere.project.domain.ProjectApplicationExample;
import io.metersphere.project.mapper.ProjectApplicationMapper;
import io.metersphere.project.mapper.ProjectTestResourcePoolMapper;
import io.metersphere.project.mapper.ProjectMapper;
import io.metersphere.project.request.ProjectApplicationRequest;
import io.metersphere.project.service.ProjectApplicationService;
import io.metersphere.sdk.constants.ProjectApplicationType;
@ -18,7 +19,6 @@ import io.metersphere.system.domain.*;
import io.metersphere.system.dto.request.ServiceIntegrationUpdateRequest;
import io.metersphere.system.mapper.ServiceIntegrationMapper;
import io.metersphere.system.mapper.TestResourcePoolMapper;
import io.metersphere.system.mapper.TestResourcePoolOrganizationMapper;
import jakarta.annotation.Resource;
import lombok.Getter;
import lombok.Setter;
@ -59,9 +59,7 @@ public class ProjectApplicationControllerTests extends BaseTest {
@Resource
private TestResourcePoolMapper testResourcePoolMapper;
@Resource
private ProjectTestResourcePoolMapper projectTestResourcePoolMapper;
@Resource
private TestResourcePoolOrganizationMapper testResourcePoolOrganizationMapper;
private ProjectMapper projectMapper;
public static final String PROJECT_ID = "project_application_test_id";
public static final String TIME_TYPE_VALUE = "3M";
@ -208,6 +206,22 @@ public class ProjectApplicationControllerTests extends BaseTest {
public void testGetApi() throws Exception {
ProjectApplicationRequest request = this.getRequest("API");
this.requestPostWithOkAndReturn(GET_API_URL, request);
Project initProject = new Project();
initProject.setId("GYQALL");
initProject.setNum(null);
initProject.setOrganizationId(DEFAULT_ORGANIZATION_ID);
initProject.setName("测试项目版本");
initProject.setDescription("测试项目版本");
initProject.setCreateUser("admin");
initProject.setUpdateUser("admin");
initProject.setCreateTime(System.currentTimeMillis());
initProject.setUpdateTime(System.currentTimeMillis());
initProject.setEnable(true);
initProject.setModuleSetting("[\"apiTest\",\"uiTest\"]");
initProject.setAllResourcePool(true);
projectMapper.insertSelective(initProject);
request.setProjectId("GYQALL");
this.requestPostWithOkAndReturn(GET_API_URL, request);
}
//接口测试 - 获取项目成员

View File

@ -6,7 +6,9 @@ import io.metersphere.plan.dto.*;
import io.metersphere.plan.dto.request.BaseCollectionAssociateRequest;
import io.metersphere.plan.dto.request.TestPlanCollectionMinderEditRequest;
import io.metersphere.plan.mapper.*;
import io.metersphere.project.domain.Project;
import io.metersphere.project.mapper.ExtProjectMapper;
import io.metersphere.project.mapper.ProjectMapper;
import io.metersphere.sdk.constants.ApiBatchRunMode;
import io.metersphere.sdk.constants.CaseType;
import io.metersphere.sdk.constants.CommonConstants;
@ -15,7 +17,9 @@ 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.uid.IDGenerator;
import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
@ -67,6 +71,12 @@ public class TestPlanCollectionMinderService {
@Resource
private ExtProjectMapper extProjectMapper;
@Resource
private ProjectMapper projectMapper;
@Resource
private TestResourcePoolMapper testResourcePoolMapper;
@Resource
private TestPlanMapper testPlanMapper;
@ -79,7 +89,15 @@ public class TestPlanCollectionMinderService {
List<TestPlanCollectionMinderTreeDTO> list = new ArrayList<>();
List<TestPlanCollectionConfigDTO> testPlanCollections = extTestPlanCollectionMapper.getList(planId);
TestPlan testPlan = testPlanMapper.selectByPrimaryKey(planId);
List<TestResourcePool> apiTest = extProjectMapper.getResourcePoolOption(testPlan.getProjectId(), "api_test");
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);
} else {
apiTest = extProjectMapper.getResourcePoolOption(testPlan.getProjectId(), "api_test");
}
Map<String, String> resourcePoolMap = apiTest.stream().collect(Collectors.toMap(TestResourcePool::getId, TestResourcePool::getName));
testPlanCollections.forEach(t->{
if (StringUtils.isBlank(resourcePoolMap.get(t.getTestResourcePoolId()))) {

View File

@ -1,16 +1,20 @@
package io.metersphere.plan.controller;
import io.metersphere.plan.domain.TestPlan;
import io.metersphere.plan.domain.TestPlanCollection;
import io.metersphere.plan.domain.TestPlanCollectionExample;
import io.metersphere.system.dto.ModuleSelectDTO;
import io.metersphere.plan.dto.TestPlanCollectionAssociateDTO;
import io.metersphere.plan.dto.TestPlanCollectionMinderEditDTO;
import io.metersphere.plan.dto.TestPlanCollectionMinderTreeDTO;
import io.metersphere.plan.dto.request.TestPlanCollectionMinderEditRequest;
import io.metersphere.plan.mapper.TestPlanCollectionMapper;
import io.metersphere.plan.mapper.TestPlanMapper;
import io.metersphere.project.domain.Project;
import io.metersphere.project.mapper.ProjectMapper;
import io.metersphere.sdk.util.JSON;
import io.metersphere.system.base.BaseTest;
import io.metersphere.system.controller.handler.ResultHolder;
import io.metersphere.system.dto.ModuleSelectDTO;
import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.junit.jupiter.api.*;
@ -40,11 +44,34 @@ public class TestPlanCollectionMinderControllerTests extends BaseTest {
@Resource
private TestPlanCollectionMapper testPlanCollectionMapper;
@Resource
private ProjectMapper projectMapper;
@Resource
private TestPlanMapper testPlanMapper;
@Test
@Order(1)
@Sql(scripts = {"/dml/init_test_plan_mind.sql"}, config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED))
void tesPagePlanReportSuccess() throws Exception {
Project initProject = new Project();
initProject.setId("GYQALLPOOL");
initProject.setNum(null);
initProject.setOrganizationId(DEFAULT_ORGANIZATION_ID);
initProject.setName("测试项目版本");
initProject.setDescription("测试项目版本");
initProject.setCreateUser("admin");
initProject.setUpdateUser("admin");
initProject.setCreateTime(System.currentTimeMillis());
initProject.setUpdateTime(System.currentTimeMillis());
initProject.setEnable(true);
initProject.setModuleSetting("[\"apiTest\",\"uiTest\"]");
initProject.setAllResourcePool(true);
projectMapper.insertSelective(initProject);
TestPlan testPlan = new TestPlan();
testPlan.setId("gyq_plan_1");
testPlan.setProjectId("GYQALLPOOL");
testPlanMapper.updateByPrimaryKeySelective(testPlan);
MvcResult mvcResult = this.requestGetWithOkAndReturn(PLAN_MIND + "gyq_plan_1");
// 获取返回值
@ -55,6 +82,20 @@ public class TestPlanCollectionMinderControllerTests extends BaseTest {
List<TestPlanCollectionMinderTreeDTO> testPlanCollectionMinderTreeDTOS = JSON.parseArray(JSON.toJSONString(resultHolder.getData()), TestPlanCollectionMinderTreeDTO.class);
// 返回值不为空
Assertions.assertNotNull(testPlanCollectionMinderTreeDTOS);
testPlan.setId("gyq_plan_1");
testPlan.setProjectId("gyq_plan_project");
testPlanMapper.updateByPrimaryKeySelective(testPlan);
mvcResult = this.requestGetWithOkAndReturn(PLAN_MIND + "gyq_plan_1");
// 获取返回值
returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
resultHolder = JSON.parseObject(returnData, ResultHolder.class);
// 返回请求正常
Assertions.assertNotNull(resultHolder);
testPlanCollectionMinderTreeDTOS = JSON.parseArray(JSON.toJSONString(resultHolder.getData()), TestPlanCollectionMinderTreeDTO.class);
// 返回值不为空
Assertions.assertNotNull(testPlanCollectionMinderTreeDTOS);
}
@Test

View File

@ -19,6 +19,7 @@ import io.metersphere.plan.utils.TestPlanTestUtils;
import io.metersphere.project.domain.Project;
import io.metersphere.project.dto.filemanagement.request.FileModuleCreateRequest;
import io.metersphere.project.dto.filemanagement.request.FileModuleUpdateRequest;
import io.metersphere.project.mapper.ProjectMapper;
import io.metersphere.sdk.constants.*;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.CommonBeanFactory;
@ -93,6 +94,8 @@ public class TestPlanTests extends BaseTest {
private CommonProjectService commonProjectService;
@Resource
private TestPlanTestService testPlanTestService;
@Resource
private ProjectMapper projectMapper;
private static final List<CheckLogModel> LOG_CHECK_LIST = new ArrayList<>();
@ -2452,6 +2455,24 @@ public class TestPlanTests extends BaseTest {
@Test
@Order(308)
void testInitDefaultCollection() {
TestPlan testPlan = testPlanMapper.selectByPrimaryKey("init_plan_id");
Project project1 = projectMapper.selectByPrimaryKey(testPlan.getProjectId());
if (project1 == null) {
Project initProject = new Project();
initProject.setId(testPlan.getProjectId());
initProject.setNum(null);
initProject.setOrganizationId(DEFAULT_ORGANIZATION_ID);
initProject.setName("测试项目版本");
initProject.setDescription("测试项目版本");
initProject.setCreateUser("admin");
initProject.setUpdateUser("admin");
initProject.setCreateTime(System.currentTimeMillis());
initProject.setUpdateTime(System.currentTimeMillis());
initProject.setEnable(true);
initProject.setModuleSetting("[\"apiTest\",\"uiTest\"]");
initProject.setAllResourcePool(true);
projectMapper.insertSelective(initProject);
}
testPlanService.initDefaultPlanCollection("init_plan_id", "admin");
}