fix(接口测试): 修复接口测试批量执行时不展示运行环境的缺陷

--bug=1014997 --user=宋天阳 【接口测试】批量执行接口、场景用例,生成的独立报告里没有运行环境
https://www.tapd.cn/55049933/s/1205151
This commit is contained in:
song-tianyang 2022-07-20 11:24:36 +08:00 committed by 建国
parent c5d28e436d
commit 0601771608
6 changed files with 94 additions and 15 deletions

View File

@ -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<String, String> getEnvMap(ApiTestCaseWithBLOBs apiCase) {
Map<String, String> 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());

View File

@ -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<String, List<String>> 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<String, String> 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<String, String> 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;
}
}

View File

@ -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<String, List<String>> 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<String, List<String>> 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(",");

View File

@ -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

View File

@ -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

View File

@ -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