diff --git a/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseExecuteService.java b/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseExecuteService.java index 405ca4af06..21336f696f 100644 --- a/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseExecuteService.java +++ b/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseExecuteService.java @@ -2,6 +2,7 @@ package io.metersphere.api.exec.api; import com.alibaba.fastjson.JSON; import io.metersphere.api.dto.ApiCaseRunRequest; +import io.metersphere.api.dto.EnvironmentType; import io.metersphere.api.dto.RunModeConfigWithEnvironmentDTO; import io.metersphere.api.dto.definition.ApiTestCaseRequest; import io.metersphere.api.dto.definition.BatchRunDefinitionRequest; @@ -234,6 +235,40 @@ public class ApiCaseExecuteService { return projectEnvMap; } + public Map getEnvMap(ApiTestCaseWithBLOBs apiCase) { + Map projectEnvMap = new HashMap<>(); + + JSONObject apiCaseNew = new JSONObject(apiCase.getRequest()); + if (apiCaseNew.has("type") && "HTTPSamplerProxy".equals(apiCaseNew.getString("type"))) { + if (apiCaseNew.has("useEnvironment") && StringUtils.isNotEmpty(apiCaseNew.getString("useEnvironment"))) { + //记录运行环境ID + String envId = apiCaseNew.getString("useEnvironment"); + projectEnvMap.put(apiCase.getProjectId(), envId); + } + } + if (apiCaseNew.has("type") && "JDBCSampler".equals(apiCaseNew.getString("type"))) { + if (apiCaseNew.has("useEnvironment") && apiCaseNew.has("dataSourceId")) { + String environmentId = apiCaseNew.getString("useEnvironment"); + String dataSourceId = apiCaseNew.getString("dataSourceId"); + ApiTestEnvironmentService environmentService = CommonBeanFactory.getBean(ApiTestEnvironmentService.class); + ApiTestEnvironmentWithBLOBs environment = environmentService.get(environmentId); + EnvironmentConfig envConfig = null; + if (environment != null && environment.getConfig() != null) { + envConfig = com.alibaba.fastjson.JSONObject.parseObject(environment.getConfig(), EnvironmentConfig.class); + if (CollectionUtils.isNotEmpty(envConfig.getDatabaseConfigs())) { + for (DatabaseConfig item : envConfig.getDatabaseConfigs()) { + if (item.getId().equals(dataSourceId)) { + //记录运行环境ID + projectEnvMap.put(apiCase.getProjectId(), environmentId); + } + } + } + } + } + } + return projectEnvMap; + } + /** * 接口定义case执行 * @@ -321,7 +356,14 @@ public class ApiCaseExecuteService { if (!request.isRerun()) { for (int i = 0; i < caseList.size(); i++) { ApiTestCaseWithBLOBs caseWithBLOBs = caseList.get(i); - ApiDefinitionExecResultWithBLOBs report = ApiDefinitionExecResultUtil.initBase(caseWithBLOBs.getId(), APITestStatus.Running.name(), null, request.getConfig()); + + RunModeConfigDTO config = new RunModeConfigDTO(); + BeanUtils.copyBean(config,request.getConfig()); + + if(StringUtils.equals(config.getEnvironmentType(), EnvironmentType.JSON.name()) && MapUtils.isEmpty(config.getEnvMap())){ + config.setEnvMap(this.getEnvMap(caseWithBLOBs)); + } + ApiDefinitionExecResultWithBLOBs report = ApiDefinitionExecResultUtil.initBase(caseWithBLOBs.getId(), APITestStatus.Running.name(), null, config); report.setStatus(status); report.setName(caseWithBLOBs.getName()); report.setProjectId(caseWithBLOBs.getProjectId()); diff --git a/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioEnvService.java b/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioEnvService.java index c723b20099..e3864996d3 100644 --- a/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioEnvService.java +++ b/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioEnvService.java @@ -24,8 +24,10 @@ import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.LogUtil; import io.metersphere.dto.RunModeConfigDTO; +import io.metersphere.i18n.Translator; import io.metersphere.plugin.core.MsTestElement; import io.metersphere.service.EnvironmentGroupProjectService; +import io.metersphere.service.EnvironmentGroupService; import io.metersphere.service.ProjectService; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; @@ -58,6 +60,9 @@ public class ApiScenarioEnvService { private ProjectService projectService; @Resource private ApiTestEnvironmentService apiTestEnvironmentService; + @Lazy + @Resource + private EnvironmentGroupService environmentGroupService; public ScenarioEnv getApiScenarioEnv(String definition) { ScenarioEnv env = new ScenarioEnv(); @@ -508,10 +513,14 @@ public class ApiScenarioEnvService { //执行时选择的环境信息 (一般在集合报告中会记录) Map> envMapByExecution = null; + String groupId = null; try { JSONObject jsonObject = JSONObject.parseObject(envConfig); if (jsonObject.containsKey("executionEnvironmentMap")) { RunModeConfigWithEnvironmentDTO configWithEnvironment = JSONObject.parseObject(envConfig, RunModeConfigWithEnvironmentDTO.class); + if (StringUtils.equals("GROUP", configWithEnvironment.getEnvironmentType()) && StringUtils.isNotEmpty(configWithEnvironment.getEnvironmentGroupId())) { + groupId = configWithEnvironment.getEnvironmentGroupId(); + } if (MapUtils.isNotEmpty(configWithEnvironment.getExecutionEnvironmentMap())) { envMapByExecution = configWithEnvironment.getExecutionEnvironmentMap(); } else { @@ -519,28 +528,37 @@ public class ApiScenarioEnvService { } } else { RunModeConfigDTO config = JSONObject.parseObject(envConfig, RunModeConfigDTO.class); + if (StringUtils.equals("GROUP", config.getEnvironmentType()) && StringUtils.isNotEmpty(config.getEnvironmentGroupId())) { + groupId = config.getEnvironmentGroupId(); + } envMapByRunConfig = config.getEnvMap(); } } catch (Exception e) { LogUtil.error("解析RunModeConfig失败!参数:" + envConfig, e); } - returnMap.putAll(this.selectProjectNameAndEnvName(envMapByExecution)); - if (MapUtils.isNotEmpty(envMapByRunConfig)) { - for (Map.Entry entry : envMapByRunConfig.entrySet()) { - String projectId = entry.getKey(); - String envId = entry.getValue(); - String projectName = projectService.selectNameById(projectId); - String envName = apiTestEnvironmentService.selectNameById(envId); - if (StringUtils.isNoneEmpty(projectName, envName)) { - returnMap.put(projectName, new ArrayList<>() {{ - this.add(envName); - }}); - } + if(StringUtils.isNotEmpty(groupId)){ + EnvironmentGroup environmentGroup = environmentGroupService.selectById(groupId); + if (StringUtils.isNotEmpty(environmentGroup.getName())) { + returnMap.put(Translator.get("environment_group"),new ArrayList<>(){{this.add(environmentGroup.getName());}}); } + }else { + returnMap.putAll(this.selectProjectNameAndEnvName(envMapByExecution)); + if (MapUtils.isNotEmpty(envMapByRunConfig)) { + for (Map.Entry entry : envMapByRunConfig.entrySet()) { + String projectId = entry.getKey(); + String envId = entry.getValue(); + String projectName = projectService.selectNameById(projectId); + String envName = apiTestEnvironmentService.selectNameById(envId); + if (StringUtils.isNoneEmpty(projectName, envName)) { + returnMap.put(projectName, new ArrayList<>() {{ + this.add(envName); + }}); + } + } + } } - return returnMap; } } diff --git a/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioExecuteService.java b/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioExecuteService.java index a983770f5c..4dd05bea74 100644 --- a/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioExecuteService.java +++ b/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioExecuteService.java @@ -341,8 +341,24 @@ public class ApiScenarioExecuteService { if (item.getStepTotal() == null || item.getStepTotal() == 0) { continue; } + + if (MapUtils.isEmpty(request.getConfig().getEnvMap())) { + RunModeConfigWithEnvironmentDTO runModeConfig = new RunModeConfigWithEnvironmentDTO(); + BeanUtils.copyBean(runModeConfig, request.getConfig()); + Map> projectEnvMap = apiScenarioEnvService.selectApiScenarioEnv(apiScenarios); + apiCaseExecuteService.setExecutionEnvironment(runModeConfig, projectEnvMap); + request.setConfig(runModeConfig); + } + + RunModeConfigWithEnvironmentDTO runModeConfig = new RunModeConfigWithEnvironmentDTO(); + BeanUtils.copyBean(runModeConfig, request.getConfig()); + if(StringUtils.equals(runModeConfig.getEnvironmentType(), EnvironmentType.JSON.name()) && MapUtils.isEmpty(runModeConfig.getEnvMap())){ + Map> projectEnvMap = apiScenarioEnvService.selectApiScenarioEnv(new ArrayList<>(){{this.add(item);}}); + runModeConfig.setExecutionEnvironmentMap(projectEnvMap); + } + APIScenarioReportResult report = apiScenarioReportService.init(reportId, item.getId(), item.getName(), request.getTriggerMode(), - request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig()); + request.getExecuteType(), item.getProjectId(), request.getReportUserID(), runModeConfig); scenarioIds.add(item.getId()); report.setVersionId(item.getVersionId()); scenarioNames.append(item.getName()).append(","); diff --git a/backend/src/main/resources/i18n/messages_en_US.properties b/backend/src/main/resources/i18n/messages_en_US.properties index 790567f497..5779f04f78 100644 --- a/backend/src/main/resources/i18n/messages_en_US.properties +++ b/backend/src/main/resources/i18n/messages_en_US.properties @@ -324,6 +324,7 @@ null_environment_group_name=Environment group name is null environment_group_name=Environment group name environment_group_exist=already exists environment_group_has_duplicate_project=Environment group has duplicate project +environment_group=Environment group #误报库 error_report_library=Error report issue_jira_info_error=Check the service integration information or Jira project ID diff --git a/backend/src/main/resources/i18n/messages_zh_CN.properties b/backend/src/main/resources/i18n/messages_zh_CN.properties index 470812ad2d..cd09e740ec 100644 --- a/backend/src/main/resources/i18n/messages_zh_CN.properties +++ b/backend/src/main/resources/i18n/messages_zh_CN.properties @@ -323,6 +323,7 @@ null_environment_group_name=环境组名称不存在 environment_group_name=环境组名称 environment_group_exist=已存在 environment_group_has_duplicate_project=每个项目只能选择一个环境! +environment_group=环境组 #误报库 error_report_library=误报库 issue_jira_info_error=请检查服务集成信息或Jira项目ID diff --git a/backend/src/main/resources/i18n/messages_zh_TW.properties b/backend/src/main/resources/i18n/messages_zh_TW.properties index 3aef7b604f..1d0ce37bfb 100644 --- a/backend/src/main/resources/i18n/messages_zh_TW.properties +++ b/backend/src/main/resources/i18n/messages_zh_TW.properties @@ -322,6 +322,7 @@ null_environment_group_name=環境組名稱不存在 environment_group_name=環境組名稱 environment_group_exist=已存在 environment_group_has_duplicate_project=每個項目只能選擇一個環境! +environment_group=環境組 #误报库 error_report_library=誤報庫 issue_jira_info_error=請檢查服務集成信息或Jira項目ID