fix(接口测试): 修复场景报告显示环境错误的缺陷

--bug=1027567 --user=王孝刚 【接口测试】场景列表-批量执行-选择新环境-独立报告-报告环境显示错误
https://www.tapd.cn/55049933/s/1391673
This commit is contained in:
wxg0103 2023-07-11 16:59:54 +08:00 committed by fit2-zhao
parent c737116c56
commit 6518430673

View File

@ -34,7 +34,10 @@ import io.metersphere.dto.*;
import io.metersphere.environment.service.BaseEnvGroupProjectService; import io.metersphere.environment.service.BaseEnvGroupProjectService;
import io.metersphere.i18n.Translator; import io.metersphere.i18n.Translator;
import io.metersphere.plugin.core.MsTestElement; import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.service.*; import io.metersphere.service.ApiExecutionQueueService;
import io.metersphere.service.RedisTemplateService;
import io.metersphere.service.ServiceUtils;
import io.metersphere.service.SystemParameterService;
import io.metersphere.service.definition.TcpApiParamService; import io.metersphere.service.definition.TcpApiParamService;
import io.metersphere.service.scenario.ApiScenarioReportService; import io.metersphere.service.scenario.ApiScenarioReportService;
import io.metersphere.service.scenario.ApiScenarioReportStructureService; import io.metersphere.service.scenario.ApiScenarioReportStructureService;
@ -48,12 +51,15 @@ import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.testelement.TestPlan; import org.apache.jmeter.testelement.TestPlan;
import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.HashTree;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.util.*; import java.util.*;
import java.util.function.Function; import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Service @Service
@ -363,13 +369,18 @@ public class ApiScenarioExecuteService {
RunModeConfigWithEnvironmentDTO runModeConfig = new RunModeConfigWithEnvironmentDTO(); RunModeConfigWithEnvironmentDTO runModeConfig = new RunModeConfigWithEnvironmentDTO();
BeanUtils.copyBean(runModeConfig, request.getConfig()); BeanUtils.copyBean(runModeConfig, request.getConfig());
List<String> projectIdLists = getProjectIds(item.getScenarioDefinition());
if (StringUtils.equals(runModeConfig.getEnvironmentType(), EnvironmentType.JSON.name()) && MapUtils.isEmpty(runModeConfig.getEnvMap())) { if (StringUtils.equals(runModeConfig.getEnvironmentType(), EnvironmentType.JSON.name()) && MapUtils.isEmpty(runModeConfig.getEnvMap())) {
Map<String, List<String>> projectEnvMap = apiScenarioEnvService.selectApiScenarioEnv(new ArrayList<>() {{ Map<String, List<String>> projectEnvMap = apiScenarioEnvService.selectApiScenarioEnv(new ArrayList<>() {{
this.add(item); this.add(item);
}}); }});
projectEnvMap = getProjectMap(projectIdLists, projectEnvMap);
runModeConfig.setExecutionEnvironmentMap(projectEnvMap); runModeConfig.setExecutionEnvironmentMap(projectEnvMap);
request.setConfig(runModeConfig); } else {
Map<String, String> stringListMap = getProjectEnvMap(projectIdLists, request.getConfig().getEnvMap());
runModeConfig.setEnvMap(stringListMap);
} }
request.setConfig(runModeConfig);
ApiScenarioReportResult report = apiScenarioReportService.initResult(reportId, item.getId(), item.getName(), request); ApiScenarioReportResult report = apiScenarioReportService.initResult(reportId, item.getId(), item.getName(), request);
report.setVersionId(item.getVersionId()); report.setVersionId(item.getVersionId());
@ -388,6 +399,36 @@ public class ApiScenarioExecuteService {
reportId = UUID.randomUUID().toString(); reportId = UUID.randomUUID().toString();
} }
} }
private static Map<String, String> getProjectEnvMap(List<String> projectIdLists, Map<String, String> projectEnvMap) {
if (CollectionUtils.isNotEmpty(projectIdLists)) {
projectEnvMap = projectEnvMap.entrySet().stream()
.filter(entry -> projectIdLists.contains(entry.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
return projectEnvMap;
}
@NotNull
private static List<String> getProjectIds(String scenarioDefinition) {
Pattern pattern = Pattern.compile("\"projectId\"\\s*:\\s*\"?([^\"]*)\"?,");
Matcher matcher = pattern.matcher(scenarioDefinition);
List<String> projectIdLists = new ArrayList<>();
while (matcher.find()) {
if (!projectIdLists.contains(matcher.group(1))){
projectIdLists.add(matcher.group(1));
}
}
return projectIdLists;
}
private static Map<String, List<String>> getProjectMap(List<String> projectIdLists, Map<String, List<String>> projectEnvMap) {
if (CollectionUtils.isNotEmpty(projectIdLists)) {
projectEnvMap = projectEnvMap.entrySet().stream()
.filter(entry -> projectIdLists.contains(entry.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
return projectEnvMap;
}
protected RunModeDataDTO getRunModeDataDTO(String testId, ApiScenarioReportResult report) { protected RunModeDataDTO getRunModeDataDTO(String testId, ApiScenarioReportResult report) {
RunModeDataDTO runModeDataDTO = new RunModeDataDTO(); RunModeDataDTO runModeDataDTO = new RunModeDataDTO();
@ -428,6 +469,10 @@ public class ApiScenarioExecuteService {
report.setVersionId(scenario.getVersionId()); report.setVersionId(scenario.getVersionId());
String scenarioDefinition = JSON.toJSONString(request.getTestElement().getHashTree().get(0).getHashTree().get(0)); String scenarioDefinition = JSON.toJSONString(request.getTestElement().getHashTree().get(0).getHashTree().get(0));
scenario.setScenarioDefinition(scenarioDefinition); scenario.setScenarioDefinition(scenarioDefinition);
List<String> projectIdLists = getProjectIds(scenarioDefinition);
Map<String, String> envMap = getProjectEnvMap(projectIdLists, request.getEnvironmentMap());
request.getConfig().setEnvMap(envMap);
report.setEnvConfig(JSON.toJSONString(request.getConfig()));
apiScenarioReportStructureService.save(scenario, report.getId(), request.getConfig().getReportType()); apiScenarioReportStructureService.save(scenario, report.getId(), request.getConfig().getReportType());
} else { } else {
if (request.getTestElement() != null && CollectionUtils.isNotEmpty(request.getTestElement().getHashTree())) { if (request.getTestElement() != null && CollectionUtils.isNotEmpty(request.getTestElement().getHashTree())) {