From da5c545e1440406ec5206d0a89f8de22d3efb28f Mon Sep 17 00:00:00 2001 From: WangXu10 Date: Tue, 11 Jun 2024 10:22:30 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=B5=8B=E8=AF=95=E8=AE=A1=E5=88=92):?= =?UTF-8?q?=20=E4=BC=98=E5=8C=96=E7=94=A8=E4=BE=8B=E8=BF=90=E8=A1=8C?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E5=B1=95=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plan/dto/TestPlanCollectionEnvDTO.java | 26 ++++++ .../dto/request/BasePlanCaseBatchRequest.java | 2 +- .../dto/request/TestPlanApiCaseRequest.java | 2 +- .../plan/dto/request/TestPlanCaseRequest.java | 2 +- .../response/TestPlanApiCasePageResponse.java | 3 - .../plan/mapper/ExtTestPlanApiCaseMapper.xml | 18 ++-- .../mapper/ExtTestPlanCollectionMapper.java | 3 + .../mapper/ExtTestPlanCollectionMapper.xml | 12 +++ .../ExtTestPlanFunctionalCaseMapper.xml | 14 +-- .../plan/service/TestPlanApiCaseService.java | 90 ++++++++++--------- .../resources/dml/init_test_plan_api_case.sql | 10 ++- 11 files changed, 106 insertions(+), 76 deletions(-) create mode 100644 backend/services/test-plan/src/main/java/io/metersphere/plan/dto/TestPlanCollectionEnvDTO.java diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/dto/TestPlanCollectionEnvDTO.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/dto/TestPlanCollectionEnvDTO.java new file mode 100644 index 0000000000..66275bb2b7 --- /dev/null +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/dto/TestPlanCollectionEnvDTO.java @@ -0,0 +1,26 @@ +package io.metersphere.plan.dto; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; + +/** + * @author wx + */ +@Data +public class TestPlanCollectionEnvDTO implements Serializable { + + @Serial + private static final long serialVersionUID = 1L; + + @Schema(description = "ID") + private String Id; + + @Schema(description = "环境ID") + private String environmentId; + + @Schema(description = "环境名称") + private String environmentName; +} diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/dto/request/BasePlanCaseBatchRequest.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/dto/request/BasePlanCaseBatchRequest.java index 5fb13fd53e..f76de94006 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/dto/request/BasePlanCaseBatchRequest.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/dto/request/BasePlanCaseBatchRequest.java @@ -26,5 +26,5 @@ public class BasePlanCaseBatchRequest extends TableBatchProcessDTO implements Se private List moduleIds; @Schema(description = "计划集id") - private List collectionIds; + private String collectionId; } diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/dto/request/TestPlanApiCaseRequest.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/dto/request/TestPlanApiCaseRequest.java index 57b4949b7c..7bbe82f943 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/dto/request/TestPlanApiCaseRequest.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/dto/request/TestPlanApiCaseRequest.java @@ -18,6 +18,6 @@ public class TestPlanApiCaseRequest extends ApiTestCasePageRequest { private String testPlanId; @Schema(description = "计划集id") - private List collectionIds; + private String collectionId; } \ No newline at end of file diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/dto/request/TestPlanCaseRequest.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/dto/request/TestPlanCaseRequest.java index 73ee9a3e38..2d59735976 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/dto/request/TestPlanCaseRequest.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/dto/request/TestPlanCaseRequest.java @@ -35,5 +35,5 @@ public class TestPlanCaseRequest extends BasePageRequest implements Serializable private List moduleIds; @Schema(description = "计划集id") - private List collectionIds; + private String collectionId; } diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/dto/response/TestPlanApiCasePageResponse.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/dto/response/TestPlanApiCasePageResponse.java index 77f0ecc229..ab0710a41f 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/dto/response/TestPlanApiCasePageResponse.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/dto/response/TestPlanApiCasePageResponse.java @@ -66,9 +66,6 @@ public class TestPlanApiCasePageResponse implements Serializable { @Schema(description = "测试集id") private String testPlanCollectionId; - @Schema(description = "计划集环境") - private String collectEnvironmentId; - @Schema(description = "报告id") private String lastExecReportId; } diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/mapper/ExtTestPlanApiCaseMapper.xml b/backend/services/test-plan/src/main/java/io/metersphere/plan/mapper/ExtTestPlanApiCaseMapper.xml index 37306d1ed6..e297719d8c 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/mapper/ExtTestPlanApiCaseMapper.xml +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/mapper/ExtTestPlanApiCaseMapper.xml @@ -419,13 +419,11 @@ t.last_exec_result, t.execute_user, t.last_exec_time, - t.last_exec_report_id, - tpc.environment_id as collectEnvironmentId + t.last_exec_report_id FROM api_test_case atc INNER JOIN api_definition a ON atc.api_definition_id = a.id inner join test_plan_api_case t on atc.id = t.api_case_id - left join test_plan_collection tpc on tpc.id = t.test_plan_collection_id WHERE atc.deleted =#{deleted} and t.test_plan_id = #{request.testPlanId} @@ -463,11 +461,8 @@ #{nodeId} - - and t.test_plan_collection_id in - - #{collectionId} - + + and t.test_plan_collection_id = #{request.collectionId} @@ -625,11 +620,8 @@ #{nodeId} - - and t.test_plan_collection_id in - - #{collectionId} - + + and t.test_plan_collection_id = #{request.collectionId} diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/mapper/ExtTestPlanCollectionMapper.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/mapper/ExtTestPlanCollectionMapper.java index 172170d60b..876ec9244c 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/mapper/ExtTestPlanCollectionMapper.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/mapper/ExtTestPlanCollectionMapper.java @@ -1,6 +1,7 @@ package io.metersphere.plan.mapper; import io.metersphere.plan.dto.TestPlanCollectionConfigDTO; +import io.metersphere.plan.dto.TestPlanCollectionEnvDTO; import org.apache.ibatis.annotations.Param; import java.util.List; @@ -9,4 +10,6 @@ public interface ExtTestPlanCollectionMapper { List getList(@Param("planId") String planId); + List selectSecondCollectionEnv(@Param("type") String type, @Param("parentId") String parentId); + } diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/mapper/ExtTestPlanCollectionMapper.xml b/backend/services/test-plan/src/main/java/io/metersphere/plan/mapper/ExtTestPlanCollectionMapper.xml index 9b592589bf..91aeb8d40d 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/mapper/ExtTestPlanCollectionMapper.xml +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/mapper/ExtTestPlanCollectionMapper.xml @@ -20,4 +20,16 @@ WHERE tpc.test_plan_id = #{planId} + \ No newline at end of file diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/mapper/ExtTestPlanFunctionalCaseMapper.xml b/backend/services/test-plan/src/main/java/io/metersphere/plan/mapper/ExtTestPlanFunctionalCaseMapper.xml index 3b65fa4c36..38024a3280 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/mapper/ExtTestPlanFunctionalCaseMapper.xml +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/mapper/ExtTestPlanFunctionalCaseMapper.xml @@ -142,11 +142,8 @@ or functional_case.num like concat('%', #{request.keyword},'%') ) - - and test_plan_functional_case.test_plan_collection_id in - - #{collectionId} - + + and test_plan_functional_case.test_plan_collection_id = #{request.collectionId} @@ -180,11 +177,8 @@ or functional_case.tags like concat('%', #{request.condition.keyword},'%') ) - - and test_plan_functional_case.test_plan_collection_id in - - #{collectionId} - + + and test_plan_functional_case.test_plan_collection_id = #{request.collectionId} diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiCaseService.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiCaseService.java index cd4a284874..7ff765d3f7 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiCaseService.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiCaseService.java @@ -21,10 +21,7 @@ import io.metersphere.plan.dto.request.*; import io.metersphere.plan.dto.response.TestPlanApiCasePageResponse; import io.metersphere.plan.dto.response.TestPlanAssociationResponse; import io.metersphere.plan.dto.response.TestPlanOperationResponse; -import io.metersphere.plan.mapper.ExtTestPlanApiCaseMapper; -import io.metersphere.plan.mapper.TestPlanApiCaseMapper; -import io.metersphere.plan.mapper.TestPlanCollectionMapper; -import io.metersphere.plan.mapper.TestPlanMapper; +import io.metersphere.plan.mapper.*; import io.metersphere.project.domain.Project; import io.metersphere.project.domain.ProjectExample; import io.metersphere.project.dto.ModuleCountDTO; @@ -96,6 +93,8 @@ public class TestPlanApiCaseService extends TestPlanResourceService implements G private TestPlanResourceLogService testPlanResourceLogService; @Resource private TestPlanCollectionMapper testPlanCollectionMapper; + @Resource + private ExtTestPlanCollectionMapper extTestPlanCollectionMapper; public TestPlanApiCaseService() { GetRunScriptServiceRegister.register(ApiExecuteResourceType.TEST_PLAN_API_CASE, this); @@ -216,13 +215,51 @@ public class TestPlanApiCaseService extends TestPlanResourceService implements G private void buildApiCaseResponse(List apiCaseList) { if (CollectionUtils.isNotEmpty(apiCaseList)) { Map projectMap = getProject(apiCaseList); - Map envMap = getEnvironmentMap(apiCaseList); Map userMap = getUserMap(apiCaseList); - apiCaseList.forEach(apiCase -> { - apiCase.setProjectName(projectMap.get(apiCase.getProjectId())); - apiCase.setEnvironmentName(envMap.get(apiCase.getId())); - apiCase.setCreateUserName(userMap.get(apiCase.getCreateUser())); - }); + handleCaseAndEnv(apiCaseList, projectMap, userMap); + } + } + + private void handleCaseAndEnv(List apiCaseList, Map projectMap, Map userMap) { + //获取二级节点环境 + List secondEnv = extTestPlanCollectionMapper.selectSecondCollectionEnv(CaseType.API_CASE.getKey(), ModuleConstants.ROOT_NODE_PARENT_ID); + Map secondEnvMap = secondEnv.stream().collect(Collectors.toMap(TestPlanCollectionEnvDTO::getId, item -> item)); + //当前用例环境 + List caseEnvIds = apiCaseList.stream().map(TestPlanApiCasePageResponse::getEnvironmentId).toList(); + EnvironmentExample environmentExample = new EnvironmentExample(); + environmentExample.createCriteria().andIdIn(caseEnvIds); + List caseEnv = environmentMapper.selectByExample(environmentExample); + Map caseEnvMap = caseEnv.stream().collect(Collectors.toMap(Environment::getId, Environment::getName)); + apiCaseList.forEach(item -> { + item.setProjectName(projectMap.get(item.getProjectId())); + item.setCreateUserName(userMap.get(item.getCreateUser())); + TestPlanCollectionEnvDTO collectEnv = secondEnvMap.get(item.getTestPlanCollectionId()); + if (StringUtils.equalsIgnoreCase(collectEnv.getEnvironmentId(), ModuleConstants.ROOT_NODE_PARENT_ID)) { + //计划集 == 默认环境 处理默认环境 + doHandleDefaultEnv(item, caseEnvMap); + } else { + //计划集 != 默认环境 + doHandleEnv(item, collectEnv); + } + }); + } + + private void doHandleEnv(TestPlanApiCasePageResponse item, TestPlanCollectionEnvDTO collectEnv) { + if (StringUtils.isNotBlank(collectEnv.getEnvironmentName())) { + item.setEnvironmentId(collectEnv.getEnvironmentId()); + item.setEnvironmentName(collectEnv.getEnvironmentName()); + } else { + item.setEnvironmentId(null); + item.setEnvironmentName(null); + } + } + + private void doHandleDefaultEnv(TestPlanApiCasePageResponse item, Map caseEnvMap) { + if (caseEnvMap.containsKey(item.getEnvironmentId())) { + item.setEnvironmentName(caseEnvMap.get(item.getEnvironmentId())); + } else { + item.setEnvironmentId(null); + item.setEnvironmentName(null); } } @@ -233,39 +270,6 @@ public class TestPlanApiCaseService extends TestPlanResourceService implements G return userLoginService.getUserNameMap(userIds.stream().filter(StringUtils::isNotBlank).distinct().toList()); } - private Map getEnvironmentMap(List apiCaseList) { - Map envMap = new HashMap<>(); - //默认环境 - List defaultEnv = apiCaseList.stream().filter(item -> StringUtils.equalsIgnoreCase(ModuleConstants.ROOT_NODE_PARENT_ID, item.getCollectEnvironmentId())).toList(); - if (CollectionUtils.isNotEmpty(defaultEnv)) { - List defaultEnvIds = defaultEnv.stream().map(TestPlanApiCasePageResponse::getEnvironmentId).distinct().toList(); - Map defaultEnvMap = defaultEnv.stream().collect(Collectors.toMap(TestPlanApiCasePageResponse::getEnvironmentId, testPlanApiCasePageResponse -> testPlanApiCasePageResponse)); - EnvironmentExample environmentExample = new EnvironmentExample(); - environmentExample.createCriteria().andIdIn(defaultEnvIds); - List environments = environmentMapper.selectByExample(environmentExample); - environments.forEach(item -> { - TestPlanApiCasePageResponse testPlanApiCasePageResponse = defaultEnvMap.get(item.getId()); - envMap.put(testPlanApiCasePageResponse.getId(), item.getName()); - }); - } - //非默认环境 - List collectEnv = apiCaseList.stream().filter(item -> !StringUtils.equalsIgnoreCase(ModuleConstants.ROOT_NODE_PARENT_ID, item.getCollectEnvironmentId())).toList(); - if (CollectionUtils.isNotEmpty(collectEnv)) { - List collectEnvIds = collectEnv.stream().map(TestPlanApiCasePageResponse::getCollectEnvironmentId).distinct().toList(); - Map> collectEnvMap = collectEnv.stream().collect(Collectors.groupingBy(TestPlanApiCasePageResponse::getCollectEnvironmentId)); - EnvironmentExample environmentExample = new EnvironmentExample(); - environmentExample.createCriteria().andIdIn(collectEnvIds); - List environments = environmentMapper.selectByExample(environmentExample); - environments.forEach(item -> { - List list = collectEnvMap.get(item.getId()); - list.forEach(response -> { - envMap.put(response.getId(), item.getName()); - }); - }); - } - return envMap; - } - private Map getProject(List apiCaseList) { List projectIds = apiCaseList.stream().map(TestPlanApiCasePageResponse::getProjectId).toList(); ProjectExample projectExample = new ProjectExample(); diff --git a/backend/services/test-plan/src/test/resources/dml/init_test_plan_api_case.sql b/backend/services/test-plan/src/test/resources/dml/init_test_plan_api_case.sql index 29f5ef2056..8788effce5 100644 --- a/backend/services/test-plan/src/test/resources/dml/init_test_plan_api_case.sql +++ b/backend/services/test-plan/src/test/resources/dml/init_test_plan_api_case.sql @@ -30,20 +30,22 @@ VALUES INSERT INTO `test_plan_api_case`(`id`, `test_plan_id`, `api_case_id`, `environment_id`, `last_exec_result`, `last_exec_report_id`, `execute_user`, `create_time`, `create_user`, `pos`, `test_plan_collection_id`, `last_exec_time`) VALUES - ('wxxx_1', 'wxxx_1', 'wxxx_api_case_1', '1', NULL, NULL, 'admin', 1716370415311, 'admin', 2, 'wxxx_1', 1716370415311), - ('wxxx_2', 'wxxx_1', 'wxxx_api_case_2', '1', NULL, NULL, 'admin', 1716370415311, 'admin', 2, 'wxxx_2', 1716370415311), + ('wxxx_1', 'wxxx_1', 'wxxx_api_case_1', '1', NULL, NULL, 'admin', 1716370415311, 'admin', 2, 'wxxx_2', 1716370415311), + ('wxxx_2', 'wxxx_1', 'wxxx_api_case_2', '123', NULL, NULL, 'admin', 1716370415311, 'admin', 2, 'wxxx_3', 1716370415311), ('wxxx_3', 'wxxx_2', 'wxxx_api_case_3', '1', NULL, NULL, 'admin', 1716370415311, 'admin', 2, 'wxxx_2', 1716370415311); INSERT INTO `test_plan_collection`(`id`, `test_plan_id`, `name`, `type`, `environment_id`, `test_resource_pool_id`, `pos`, `create_user`, `create_time`, `parent_id`) VALUES ('wxxx_1', 'wxxx_1', 'wxxx_1', 'API', 'NONE', 'NONE', 1, 'admin', 1716370415311, 'NONE'), - ('wxxx_2', 'wxxx_1', 'wxxx_2', 'API', '123', 'NONE', 2, 'admin', 1716370415311, 'NONE'), + ('wxxx_2', 'wxxx_1', 'wxxx_2', 'API', '123', 'NONE', 2, 'admin', 1716370415311, 'wxxx_1'), ('wxxx_3', 'wxxx_2', 'wxxx_3', 'API', 'NONE', 'NONE', 3, 'admin', 1716370415311, 'wxxx_1'); INSERT INTO `environment`(`id`, `name`, `project_id`, `create_user`, `update_user`, `create_time`, `update_time`, `mock`, `description`, `pos`) -VALUES ('123', 'Mock环境', 'wxx_1234', 'admin', 'admin', 1716175907000, 1716175907000, b'1', NULL, 64); +VALUES + ('123', 'Mock环境', 'wxx_1234', 'admin', 'admin', 1716175907000, 1716175907000, b'1', NULL, 64), + ('223', '测试环境', 'wxx_1234', 'admin', 'admin', 1716175907000, 1716175907000, b'1', NULL, 128); INSERT INTO `api_definition_module`(`id`, `name`, `parent_id`, `project_id`, `pos`, `create_time`, `update_time`, `update_user`, `create_user`) VALUES ('123', 'Halo', 'NONE', 'wxx_1234', 384, 1716280762025, 1716280762025, '805048669970432', '805048669970432');