fix(接口测试): 修复场景报告显示环境问题

--bug=1027567 --user=王孝刚 【接口测试】场景列表-批量执行-选择新环境-独立报告-报告环境显示错误
https://www.tapd.cn/55049933/s/1393339
This commit is contained in:
wxg0103 2023-07-13 20:14:50 +08:00 committed by fit2-zhao
parent 1a777df55d
commit 2d933bb302
3 changed files with 59 additions and 46 deletions

View File

@ -66,6 +66,7 @@ import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jorphan.collections.HashTree;
import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONObject;
@ -73,6 +74,8 @@ import java.io.File;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -1174,4 +1177,25 @@ public class ElementUtil {
}
return false;
}
@NotNull
public 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;
}
public 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;
}
}

View File

@ -7,6 +7,7 @@ import io.metersphere.api.dto.automation.ApiScenarioReportResult;
import io.metersphere.api.dto.automation.ExecuteType;
import io.metersphere.api.dto.automation.RunScenarioRequest;
import io.metersphere.api.dto.definition.RunDefinitionRequest;
import io.metersphere.api.dto.definition.request.ElementUtil;
import io.metersphere.api.dto.definition.request.ParameterConfig;
import io.metersphere.api.dto.plan.TestPlanApiScenarioInfoDTO;
import io.metersphere.api.exec.api.ApiCaseExecuteService;
@ -51,15 +52,12 @@ import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jorphan.collections.HashTree;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.util.*;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@Service
@ -369,18 +367,13 @@ public class ApiScenarioExecuteService {
RunModeConfigWithEnvironmentDTO runModeConfig = new RunModeConfigWithEnvironmentDTO();
BeanUtils.copyBean(runModeConfig, request.getConfig());
List<String> projectIdLists = getProjectIds(item.getScenarioDefinition());
if (StringUtils.equals(runModeConfig.getEnvironmentType(), EnvironmentType.JSON.name()) && MapUtils.isEmpty(runModeConfig.getEnvMap())) {
Map<String, List<String>> projectEnvMap = apiScenarioEnvService.selectApiScenarioEnv(new ArrayList<>() {{
this.add(item);
}});
projectEnvMap = getProjectMap(projectIdLists, projectEnvMap);
runModeConfig.setExecutionEnvironmentMap(projectEnvMap);
} else {
Map<String, String> stringListMap = getProjectEnvMap(projectIdLists, request.getConfig().getEnvMap());
runModeConfig.setEnvMap(stringListMap);
request.setConfig(runModeConfig);
}
request.setConfig(runModeConfig);
ApiScenarioReportResult report = apiScenarioReportService.initResult(reportId, item.getId(), item.getName(), request);
report.setVersionId(item.getVersionId());
@ -399,36 +392,6 @@ public class ApiScenarioExecuteService {
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) {
RunModeDataDTO runModeDataDTO = new RunModeDataDTO();
@ -469,8 +432,8 @@ public class ApiScenarioExecuteService {
report.setVersionId(scenario.getVersionId());
String scenarioDefinition = JSON.toJSONString(request.getTestElement().getHashTree().get(0).getHashTree().get(0));
scenario.setScenarioDefinition(scenarioDefinition);
List<String> projectIdLists = getProjectIds(scenarioDefinition);
Map<String, String> envMap = getProjectEnvMap(projectIdLists, request.getEnvironmentMap());
List<String> projectIdLists = ElementUtil.getProjectIds(scenarioDefinition);
Map<String, String> envMap = ElementUtil.getProjectEnvMap(projectIdLists, request.getEnvironmentMap());
request.getConfig().setEnvMap(envMap);
report.setEnvConfig(JSON.toJSONString(request.getConfig()));
apiScenarioReportStructureService.save(scenario, report.getId(), request.getConfig().getReportType());
@ -482,8 +445,8 @@ public class ApiScenarioExecuteService {
if (testElement != null) {
apiScenario.setName(testElement.getName());
apiScenario.setScenarioDefinition(JSON.toJSONString(testElement));
List<String> projectIdLists = getProjectIds(apiScenario.getScenarioDefinition());
Map<String, String> envMap = getProjectEnvMap(projectIdLists, request.getEnvironmentMap());
List<String> projectIdLists =ElementUtil. getProjectIds(apiScenario.getScenarioDefinition());
Map<String, String> envMap = ElementUtil.getProjectEnvMap(projectIdLists, request.getEnvironmentMap());
request.getConfig().setEnvMap(envMap);
report.setEnvConfig(JSON.toJSONString(request.getConfig()));
apiScenarioReportStructureService.save(apiScenario, report.getId(), request.getConfig().getReportType());

View File

@ -7,8 +7,7 @@ import io.metersphere.api.dto.automation.ExecuteType;
import io.metersphere.api.dto.automation.RunScenarioRequest;
import io.metersphere.api.dto.datacount.ApiDataCountResult;
import io.metersphere.api.dto.definition.RunDefinitionRequest;
import io.metersphere.service.*;
import io.metersphere.utils.ReportStatusUtil;
import io.metersphere.api.dto.definition.request.ElementUtil;
import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.*;
import io.metersphere.base.mapper.ext.ExtApiDefinitionExecResultMapper;
@ -21,7 +20,6 @@ import io.metersphere.commons.enums.ApiReportStatus;
import io.metersphere.commons.enums.ExecutionExecuteTypeEnum;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.*;
import io.metersphere.vo.ResultVO;
import io.metersphere.constants.RunModeConstants;
import io.metersphere.dto.*;
import io.metersphere.i18n.Translator;
@ -31,10 +29,15 @@ import io.metersphere.log.vo.OperatingLogDetails;
import io.metersphere.log.vo.api.ModuleReference;
import io.metersphere.notice.sender.NoticeModel;
import io.metersphere.notice.service.NoticeSendService;
import io.metersphere.service.*;
import io.metersphere.utils.JsonUtils;
import io.metersphere.utils.ReportStatusUtil;
import io.metersphere.vo.ResultVO;
import jakarta.annotation.Resource;
import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
@ -857,12 +860,35 @@ public class ApiScenarioReportService {
}
}
private static Map<String, List<String>> getProjectMap(List<String> projectIdLists, Map<String, List<String>> projectEnvMap) {
if (org.apache.commons.collections.CollectionUtils.isNotEmpty(projectIdLists)) {
projectEnvMap = projectEnvMap.entrySet().stream()
.filter(entry -> projectIdLists.contains(entry.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
return projectEnvMap;
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void batchSave(Map<String, RunModeDataDTO> executeQueue, String serialReportId, String runMode, List<MsExecResponseDTO> responseDTOS) {
List<ApiScenarioReportResult> list = new LinkedList<>();
if (StringUtils.isEmpty(serialReportId)) {
for (String reportId : executeQueue.keySet()) {
ApiScenarioReportResult report = executeQueue.get(reportId).getReport();
ApiScenarioWithBLOBs scenario = executeQueue.get(reportId).getScenario();
if (ObjectUtils.isNotEmpty(scenario)) {
List<String> projectIdLists = ElementUtil.getProjectIds(scenario.getScenarioDefinition());
String envConfig = report.getEnvConfig();
RunModeConfigWithEnvironmentDTO config = JsonUtils.parseObject(envConfig, RunModeConfigWithEnvironmentDTO.class);
if (MapUtils.isNotEmpty(config.getEnvMap())) {
Map<String, String> envMap = ElementUtil.getProjectEnvMap(projectIdLists, config.getEnvMap());
config.setEnvMap(envMap);
} else if (MapUtils.isNotEmpty(config.getExecutionEnvironmentMap())) {
Map<String, List<String>> envMap = getProjectMap(projectIdLists, config.getExecutionEnvironmentMap());
config.setExecutionEnvironmentMap(envMap);
}
report.setEnvConfig(JSON.toJSONString(config));
}
list.add(report);
responseDTOS.add(new MsExecResponseDTO(executeQueue.get(reportId).getTestId(), reportId, runMode));
}