From 5aaaa0cfc1afb3c589e41144a12bee93f55eb38c Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Tue, 21 Mar 2023 17:07:58 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=B5=8B=E8=AF=95=E8=B7=9F=E8=B8=AA):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=89=A7=E8=A1=8C=E6=B5=8B=E8=AF=95=E8=AE=A1?= =?UTF-8?q?=E5=88=92=E6=97=B6=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=E5=B1=95?= =?UTF-8?q?=E7=A4=BA=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1023758 --user=宋天阳 【测试跟踪】测试计划-操作执行-选择新环境-接口场景运行环境显示错误 https://www.tapd.cn/55049933/s/1353604 --- .../scenario/ApiScenarioExecuteService.java | 24 ++++++++++++------- .../plan/service/TestPlanReportService.java | 20 ++++------------ .../plan/utils/TestPlanReportUtil.java | 19 ++++++++++++++- .../business/plan/env/EnvSelectPopover.vue | 16 ++++++++----- 4 files changed, 49 insertions(+), 30 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioExecuteService.java b/api-test/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioExecuteService.java index e01b23b9b7..3619ed32ed 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioExecuteService.java +++ b/api-test/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioExecuteService.java @@ -281,6 +281,11 @@ public class ApiScenarioExecuteService { for (String testPlanScenarioId : request.getProcessVO().getTestPlanScenarioMap().keySet()) { TestPlanApiScenarioInfoDTO planApiScenario = request.getProcessVO().getTestPlanScenarioMap().get(testPlanScenarioId); ApiScenarioWithBLOBs scenario = scenarioMap.get(planApiScenario.getApiScenarioId()); + Set scenarioUsedProjectIdSet = null; + try { + scenarioUsedProjectIdSet = apiScenarioEnvService.getApiScenarioEnv(scenario.getScenarioDefinition()).getProjectIds(); + } catch (Exception ignore) { + } if (scenario.getStepTotal() == null || scenario.getStepTotal() == 0) { continue; } @@ -318,14 +323,17 @@ public class ApiScenarioExecuteService { if (MapUtils.isEmpty(runModeConfig.getEnvMap())) { apiCaseExecuteService.setRunModeConfigEnvironment(runModeConfig, planEnvMap); } - //对报告的envMap做过滤,过滤多余的key - Map diffEnvMap = new HashMap<>(); - planEnvMap.forEach((k, v) -> { - if (StringUtils.equals(planApiScenario.getProjectId(), k)) { - diffEnvMap.put(k, v); - } - }); - runModeConfig.setEnvMap(diffEnvMap); + //对报告的envMap做过滤,通过场景用到的项目来进行匹配,过滤掉使用不到的项目环境 + if (CollectionUtils.isNotEmpty(scenarioUsedProjectIdSet)) { + List scenarioUsedProjectIdList = new ArrayList<>(scenarioUsedProjectIdSet); + Map diffEnvMap = new HashMap<>(); + planEnvMap.forEach((k, v) -> { + if (scenarioUsedProjectIdList.contains(k)) { + diffEnvMap.put(k, v); + } + }); + runModeConfig.setEnvMap(diffEnvMap); + } report.setEnvConfig(JSON.toJSONString(runModeConfig)); } // 生成文档结构 diff --git a/test-track/backend/src/main/java/io/metersphere/plan/service/TestPlanReportService.java b/test-track/backend/src/main/java/io/metersphere/plan/service/TestPlanReportService.java index 75a2c11955..5ad87b486a 100644 --- a/test-track/backend/src/main/java/io/metersphere/plan/service/TestPlanReportService.java +++ b/test-track/backend/src/main/java/io/metersphere/plan/service/TestPlanReportService.java @@ -208,22 +208,12 @@ public class TestPlanReportService { List scenarios) { TestPlanReportRunInfoDTO runInfoDTO = new TestPlanReportRunInfoDTO(); - if (MapUtils.isNotEmpty(config.getEnvMap())) { - //判断记录选择的环境还是默认环境 - Map> requestEnvMap = new HashMap<>(); - for (Map.Entry entry : config.getEnvMap().entrySet()) { - requestEnvMap.put(entry.getKey(), new ArrayList<>() {{ - this.add(entry.getValue()); - }}); - } - runInfoDTO.setRequestEnvMap(requestEnvMap); - } else { - runInfoDTO.setRequestEnvMap(config.getTestPlanDefaultEnvMap()); - } - - final Map runEnvMap = MapUtils.isNotEmpty(config.getEnvMap()) ? config.getEnvMap() : new HashMap<>(); runInfoDTO.setRunMode(config.getMode()); + Map> projectInvMap = TestPlanReportUtil.getTestPlanExecutedEnvironments(config.getTestPlanDefaultEnvMap(), config.getEnvMap()); + runInfoDTO.setRequestEnvMap(projectInvMap); + + final Map runEnvMap = new HashMap<>(); if (StringUtils.equals(GROUP, config.getEnvironmentType()) && StringUtils.isNotEmpty(config.getEnvironmentGroupId())) { Map groupMap = baseEnvGroupProjectService.getEnvMap(config.getEnvironmentGroupId()); if (MapUtils.isNotEmpty(groupMap)) { @@ -1173,7 +1163,7 @@ public class TestPlanReportService { if (MapUtils.isEmpty(runInfoDTO.getRequestEnvMap())) { if (MapUtils.isNotEmpty(runInfoDTO.getApiCaseRunInfo())) { for (Map map : runInfoDTO.getApiCaseRunInfo().values()) { - requestEnvMap = TestPlanReportUtil.mergeApiCaseEnvMap(requestEnvMap, map); + requestEnvMap = TestPlanReportUtil.mergeEnvironmentMap(requestEnvMap, map); } } if (MapUtils.isNotEmpty(runInfoDTO.getScenarioRunInfo())) { diff --git a/test-track/backend/src/main/java/io/metersphere/plan/utils/TestPlanReportUtil.java b/test-track/backend/src/main/java/io/metersphere/plan/utils/TestPlanReportUtil.java index 3501c80fdd..55fe66449d 100644 --- a/test-track/backend/src/main/java/io/metersphere/plan/utils/TestPlanReportUtil.java +++ b/test-track/backend/src/main/java/io/metersphere/plan/utils/TestPlanReportUtil.java @@ -40,7 +40,24 @@ public class TestPlanReportUtil { return r; } - public static Map> mergeApiCaseEnvMap(Map> projectEnvMap, Map originProjectEnvMap) { + //获取测试计划执行时的环境 + public static Map> getTestPlanExecutedEnvironments(Map> defaultEnvMap, Map selectedEnvMap) { + Map> returnMap = new HashMap>(); + if (MapUtils.isNotEmpty(defaultEnvMap)) { + returnMap.putAll(defaultEnvMap); + } + if (MapUtils.isNotEmpty(selectedEnvMap)) { + for (Map.Entry entry : selectedEnvMap.entrySet()) { + String projectId = entry.getKey(); + returnMap.put(projectId, new ArrayList<>() {{ + this.add(entry.getValue()); + }}); + } + } + return returnMap; + } + + public static Map> mergeEnvironmentMap(Map> projectEnvMap, Map originProjectEnvMap) { if (projectEnvMap == null) { projectEnvMap = new HashMap>(); } diff --git a/test-track/frontend/src/business/plan/env/EnvSelectPopover.vue b/test-track/frontend/src/business/plan/env/EnvSelectPopover.vue index 104746cb77..9060c84c20 100644 --- a/test-track/frontend/src/business/plan/env/EnvSelectPopover.vue +++ b/test-track/frontend/src/business/plan/env/EnvSelectPopover.vue @@ -300,16 +300,20 @@ export default { let projectEnvMapElement = this.projectEnvMap[d]; if (projectEnvMapElement.length > 0) { projectEnvMapElement.forEach((envId) => { - let filter = envs.filter((e) => e.id === envId); + let filteredEnv = envs.filter((e) => e.id === envId); if (!this.selectedEnvName.has(d)) { let name = []; - name.push(filter[0].name); - this.selectedEnvName.set(d, name); + if (filteredEnv.length > 0) { + name.push(filteredEnv[0].name); + this.selectedEnvName.set(d, name); + this.$emit("setDefaultEnv", d, filteredEnv[0].id); + } } else { - this.selectedEnvName.get(d).push(filter[0].name); + if (filteredEnv.length > 0) { + this.selectedEnvName.get(d).push(filteredEnv[0].name); + this.$emit("setDefaultEnv", d, filteredEnv[0].id); + } } - - this.$emit("setDefaultEnv", d, filter[0].id); }); } }