From 557b98e8f8dc59608c03f5ae9bad8f3d77067bee Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Wed, 16 Aug 2023 16:17:19 +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=B5=8B=E8=AF=95=E8=AE=A1=E5=88=92=E4=B8=8D?= =?UTF-8?q?=E9=80=89=E6=8B=A9=E7=8E=AF=E5=A2=83=E5=85=B3=E8=81=94=E5=9C=BA?= =?UTF-8?q?=E6=99=AF=E5=92=8C=E6=8E=A5=E5=8F=A3=E6=89=A7=E8=A1=8C=E6=97=B6?= =?UTF-8?q?=E6=8A=A5=E5=91=8A=E6=B2=A1=E6=9C=89=E7=8E=AF=E5=A2=83=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1028771 --user=宋天阳 【测试跟踪】测试计划-场景用例列表-操作-执行-任务中心-报告未显示运行环境字段 https://www.tapd.cn/55049933/s/1404646;--bug=1028789 --user=宋天阳 【测试跟踪】接口用例-操作-执行/批量执行-执行结果未显示运行环境字段 https://www.tapd.cn/55049933/s/1404648 --- .../api/exec/api/ApiCaseExecuteService.java | 17 +++++--- .../scenario/ApiScenarioExecuteService.java | 10 +++++ .../service/plan/TestPlanApiCaseService.java | 43 ++++++++++++++++++- .../plan/TestPlanScenarioCaseService.java | 19 +++----- 4 files changed, 68 insertions(+), 21 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseExecuteService.java b/api-test/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseExecuteService.java index e8529c7da0..0dc0df6cee 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseExecuteService.java +++ b/api-test/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseExecuteService.java @@ -108,8 +108,8 @@ public class ApiCaseExecuteService { List apiCaseIds = planApiCases.stream().map(TestPlanApiCaseInfoDTO::getApiCaseId).collect(Collectors.toList()); ApiTestCaseExample caseExample = new ApiTestCaseExample(); caseExample.createCriteria().andIdIn(apiCaseIds); - List apiTestCases = apiTestCaseMapper.selectByExample(caseExample); - Map caseMap = apiTestCases.stream().collect(Collectors.toMap(ApiTestCase::getId, a -> a, (k1, k2) -> k1)); + List apiTestCases = apiTestCaseMapper.selectByExampleWithBLOBs(caseExample); + Map caseMap = apiTestCases.stream().collect(Collectors.toMap(ApiTestCase::getId, a -> a, (k1, k2) -> k1)); // 资源池 String resourcePoolId = null; if (request.getConfig() != null && GenerateHashTreeUtil.isResourcePool(request.getConfig().getResourcePoolId()).isPool()) { @@ -120,14 +120,19 @@ public class ApiCaseExecuteService { //处理环境配置为空时的情况 RunModeConfigDTO runModeConfigDTO = new RunModeConfigDTO(); BeanUtils.copyBean(runModeConfigDTO, request.getConfig()); - ApiTestCase testCase = caseMap.get(testPlanApiCase.getApiCaseId()); + ApiTestCaseWithBLOBs testCase = caseMap.get(testPlanApiCase.getApiCaseId()); if (testCase == null) { continue; } if (MapUtils.isEmpty(runModeConfigDTO.getEnvMap())) { - runModeConfigDTO.setEnvMap(new HashMap<>() {{ - this.put(testCase.getProjectId(), testPlanApiCase.getEnvironmentId()); - }}); + if (StringUtils.isEmpty(testPlanApiCase.getEnvironmentId())) { + JSONObject jsonObject = new JSONObject(testCase.getRequest()); + runModeConfigDTO.setEnvMap(this.getEnvMap(jsonObject, testCase.getProjectId())); + } else { + runModeConfigDTO.setEnvMap(new HashMap<>() {{ + this.put(testCase.getProjectId(), testPlanApiCase.getEnvironmentId()); + }}); + } } ApiDefinitionExecResultWithBLOBs report = ApiDefinitionExecResultUtil.addResult(request, runModeConfigDTO, testPlanApiCase, status, testCase, resourcePoolId); executeQueue.put(testPlanApiCase.getId(), report); 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 27766cfb90..590efc844e 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 @@ -298,6 +298,16 @@ public class ApiScenarioExecuteService { } // 获取场景用例单独的执行环境 Map planEnvMap = apiScenarioEnvService.getPlanScenarioEnv(planApiScenario, configEnvMap); + if (MapUtils.isEmpty(planEnvMap)) { + Map> projectEnvMap = apiScenarioEnvService.selectApiScenarioEnv(new ArrayList<>() {{ + this.add(scenario); + }}); + projectEnvMap.forEach((projectId, envList) -> { + if (CollectionUtils.isNotEmpty(envList)) { + planEnvMap.put(projectId, envList.get(0)); + } + }); + } if (StringUtils.isEmpty(request.getProjectId())) { request.setProjectId(extTestPlanScenarioCaseMapper.getProjectIdById(testPlanScenarioId)); } diff --git a/api-test/backend/src/main/java/io/metersphere/service/plan/TestPlanApiCaseService.java b/api-test/backend/src/main/java/io/metersphere/service/plan/TestPlanApiCaseService.java index 033953a85e..9f6cb767c6 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/plan/TestPlanApiCaseService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/plan/TestPlanApiCaseService.java @@ -13,6 +13,8 @@ import io.metersphere.api.dto.definition.*; import io.metersphere.api.dto.plan.AutomationsRunInfoDTO; import io.metersphere.api.dto.plan.TestPlanApiCaseBatchRequest; import io.metersphere.api.dto.plan.TestPlanApiCaseInfoDTO; +import io.metersphere.api.dto.scenario.DatabaseConfig; +import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; import io.metersphere.api.exec.api.ApiCaseExecuteService; import io.metersphere.api.exec.api.ApiExecuteService; import io.metersphere.api.jmeter.JMeterService; @@ -30,6 +32,7 @@ import io.metersphere.commons.utils.*; import io.metersphere.dto.MsExecResponseDTO; import io.metersphere.dto.RunModeConfigDTO; import io.metersphere.environment.service.BaseEnvGroupProjectService; +import io.metersphere.environment.service.BaseEnvironmentService; import io.metersphere.log.vo.OperatingLogDetails; import io.metersphere.request.OrderRequest; import io.metersphere.request.ResetOrderRequest; @@ -44,6 +47,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; +import org.json.JSONObject; import org.mybatis.spring.SqlSessionUtils; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; @@ -98,6 +102,8 @@ public class TestPlanApiCaseService { private ApiCaseResultService apiCaseResultService; @Resource private RedisTemplateService redisTemplateService; + @Resource + private BaseEnvironmentService baseEnvironmentService; public List list(ApiTestCaseRequest request) { request.setProjectId(null); @@ -776,6 +782,36 @@ public class TestPlanApiCaseService { return nodeTrees; } + public Map getEnvMap(JSONObject request, String projectId) { + Map projectEnvMap = new HashMap<>(); + if (!request.has(PropertyConstant.TYPE)) { + return projectEnvMap; + } + if (StringUtils.equals(ElementConstants.HTTP_SAMPLER, request.optString(PropertyConstant.TYPE))) { + if (StringUtils.isNotEmpty(request.optString(PropertyConstant.ENVIRONMENT))) { + //记录运行环境ID + projectEnvMap.put(projectId, request.optString(PropertyConstant.ENVIRONMENT)); + } + } + if (StringUtils.equals(ElementConstants.JDBC_SAMPLER, request.optString(PropertyConstant.TYPE))) { + if (request.has(PropertyConstant.ENVIRONMENT) && request.has(PropertyConstant.DATASOURCE_ID)) { + ApiTestEnvironmentWithBLOBs environment = baseEnvironmentService.get(request.optString(PropertyConstant.ENVIRONMENT)); + if (environment != null && environment.getConfig() != null) { + EnvironmentConfig envConfig = JSON.parseObject(environment.getConfig(), EnvironmentConfig.class); + if (org.apache.commons.collections.CollectionUtils.isNotEmpty(envConfig.getDatabaseConfigs())) { + for (DatabaseConfig item : envConfig.getDatabaseConfigs()) { + if (StringUtils.equals(item.getId(), request.optString(PropertyConstant.DATASOURCE_ID))) { + //记录运行环境ID + projectEnvMap.put(projectId, request.optString(PropertyConstant.ENVIRONMENT)); + } + } + } + } + } + } + return projectEnvMap; + } + public void run(String testId, String reportId) { TestPlanApiCase testPlanApiCase = testPlanApiCaseMapper.selectByPrimaryKey(testId); if (testPlanApiCase == null) { @@ -796,9 +832,12 @@ public class TestPlanApiCaseService { runModeConfigDTO.setEnvMap(new HashMap<>() {{ this.put(result.getProjectId(), testPlanApiCase.getEnvironmentId()); }}); - runModeConfigDTO.setResourcePoolId(runModeConfigDTO.getResourcePoolId()); - result.setEnvConfig(JSON.toJSONString(runModeConfigDTO)); + } else { + JSONObject jsonObject = new JSONObject(apiCase.getRequest()); + runModeConfigDTO.setEnvMap(this.getEnvMap(jsonObject, apiCase.getProjectId())); } + runModeConfigDTO.setResourcePoolId(runModeConfigDTO.getResourcePoolId()); + result.setEnvConfig(JSON.toJSONString(runModeConfigDTO)); result.setActuator(runModeConfigDTO.getResourcePoolId()); apiCaseResultService.batchSave(result); apiCase.setId(testId); diff --git a/api-test/backend/src/main/java/io/metersphere/service/plan/TestPlanScenarioCaseService.java b/api-test/backend/src/main/java/io/metersphere/service/plan/TestPlanScenarioCaseService.java index 71e9c9a176..ce91f7e813 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/plan/TestPlanScenarioCaseService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/plan/TestPlanScenarioCaseService.java @@ -3,9 +3,9 @@ package io.metersphere.service.plan; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import io.metersphere.api.dto.ApiCaseRelevanceRequest; +import io.metersphere.api.dto.EnvironmentCheckDTO; import io.metersphere.api.dto.EnvironmentType; import io.metersphere.api.dto.RelevanceScenarioRequest; -import io.metersphere.api.dto.EnvironmentCheckDTO; import io.metersphere.api.dto.automation.*; import io.metersphere.api.dto.plan.*; import io.metersphere.api.exec.scenario.ApiScenarioEnvService; @@ -240,7 +240,11 @@ public class TestPlanScenarioCaseService { EnvironmentCheckDTO scenarioEnv = apiAutomationService.getApiScenarioProjectId(id); list = new ArrayList<>(scenarioEnv.getProjectIds()); } - list.forEach(l -> newEnvMap.put(l, envMap == null ? StringUtils.EMPTY : envMap.getOrDefault(l, StringUtils.EMPTY))); + for (String s : list) { + if (envMap != null && StringUtils.isNotEmpty(envMap.get(s))) { + newEnvMap.put(s, envMap.get(s)); + } + } TestPlanApiScenario testPlanApiScenario = new TestPlanApiScenario(); testPlanApiScenario.setId(UUID.randomUUID().toString()); testPlanApiScenario.setCreateUser(SessionUtils.getUserId()); @@ -305,17 +309,6 @@ public class TestPlanScenarioCaseService { if (CollectionUtils.isEmpty(planCaseIdList)) { MSException.throwException("未找到执行场景!"); } - RunModeConfigDTO config = testPlanScenarioRequest.getConfig(); - if (config != null) { - String envType = config.getEnvironmentType(); - String envGroupId = config.getEnvironmentGroupId(); - Map envMap = config.getEnvMap(); - if ((StringUtils.equals(envType, EnvironmentType.JSON.toString()) && envMap != null && !envMap.isEmpty()) - || (StringUtils.equals(envType, EnvironmentType.GROUP.toString()) && StringUtils.isNotBlank(envGroupId))) { - // 更新场景用例环境信息,运行时从数据库读取最新环境 - this.setScenarioEnv(new ArrayList<>(), planCaseIdList, testPlanScenarioRequest.getConfig()); - } - } jMeterService.verifyPool(testPlanScenarioRequest.getProjectId(), testPlanScenarioRequest.getConfig()); planCaseIdList.forEach(item -> {