fix(测试跟踪): 修复测试计划执行未选择环境时默认场景环境未生效问题

--bug=1029017 --user=赵勇 【接口测试】场景列表-批量执行-项目部分选择新环境、部分选择默认环境 -当前项目步骤获取默认环境失败 https://www.tapd.cn/55049933/s/1405696

Signed-off-by: fit2-zhao <yong.zhao@fit2cloud.com>
This commit is contained in:
fit2-zhao 2023-08-18 14:47:25 +08:00 committed by fit2-zhao
parent d83c7eff98
commit f6817aefb2
2 changed files with 54 additions and 37 deletions

View File

@ -66,8 +66,6 @@ public class ApiJMeterFileService {
@Lazy
@Resource
private TemporaryFileUtil temporaryFileUtil;
@Resource
private RemakeReportService remakeReportService;
// 接口测试 用例/接口
private static final List<String> CASE_MODES = new ArrayList<>() {{
@ -84,40 +82,44 @@ public class ApiJMeterFileService {
ApiRunMode.JENKINS_SCENARIO_PLAN.name(),
ApiRunMode.SCHEDULE_SCENARIO_PLAN.name());
private ApiScenarioWithBLOBs getScenario(String runMode, String remoteTestId, Map<String, String> planEnvMap, Map<String, String> envMap) {
private ApiScenarioWithBLOBs getScenario(String runMode, String remoteTestId, Map<String, String> executeEnvMap, Map<String, String> envMap) {
ApiScenarioWithBLOBs scenario = null;
if (PLAN_SCENARIO.contains(runMode)) {
// 获取场景用例单独的执行环境
TestPlanApiScenario planApiScenario = testPlanApiScenarioMapper.selectByPrimaryKey(remoteTestId);
if (planApiScenario != null) {
String envType = planApiScenario.getEnvironmentType();
String environmentGroupId = planApiScenario.getEnvironmentGroupId();
String environment = planApiScenario.getEnvironment();
if (StringUtils.equals(envType, EnvironmentType.JSON.name()) && StringUtils.isNotBlank(environment)) {
planEnvMap.putAll(JSON.parseObject(environment, Map.class));
} else if (StringUtils.equals(envType, EnvironmentType.GROUP.name()) && StringUtils.isNotBlank(environmentGroupId)) {
planEnvMap.putAll(environmentGroupProjectService.getEnvMap(environmentGroupId));
}
// 测试计划自身环境
executeEnvMap.putAll(extractScenarioEnv(planApiScenario.getEnvironmentType(), planApiScenario.getEnvironment(), planApiScenario.getEnvironmentGroupId()));
scenario = apiScenarioMapper.selectByPrimaryKey(planApiScenario.getApiScenarioId());
}
}
if (scenario == null) {
scenario = apiScenarioMapper.selectByPrimaryKey(remoteTestId);
}
// 获取场景用例单独的执行环境
Map<String, String> scenarioEnv = extractScenarioEnv(scenario.getEnvironmentType(), scenario.getEnvironmentJson(), scenario.getEnvironmentGroupId());
scenarioEnv.entrySet().stream()
.filter(entry -> StringUtils.isBlank(executeEnvMap.get(entry.getKey())))
.forEach(entry -> executeEnvMap.put(entry.getKey(), entry.getValue()));
// 当前执行时所选环境
if (MapUtils.isNotEmpty(envMap)) {
planEnvMap.putAll(envMap);
} else if (scenario != null && !PLAN_SCENARIO.contains(runMode)) {
String envType = scenario.getEnvironmentType();
String envJson = scenario.getEnvironmentJson();
String envGroupId = scenario.getEnvironmentGroupId();
if (StringUtils.equals(envType, EnvironmentType.JSON.name()) && StringUtils.isNotBlank(envJson)) {
planEnvMap.putAll(JSON.parseObject(envJson, Map.class));
} else if (StringUtils.equals(envType, EnvironmentType.GROUP.name()) && StringUtils.isNotBlank(envGroupId)) {
planEnvMap.putAll(environmentGroupProjectService.getEnvMap(envGroupId));
}
executeEnvMap.putAll(envMap);
}
return scenario;
}
private Map<String, String> extractScenarioEnv(String envType, String envJson, String envGroupId) {
Map<String, String> scenarioEnv = new LinkedHashMap<>();
if (envType.equals(EnvironmentType.JSON.name()) && !envJson.isBlank()) {
scenarioEnv = JSON.parseObject(envJson, Map.class);
} else if (envType.equals(EnvironmentType.GROUP.name()) && !envGroupId.isBlank()) {
scenarioEnv = environmentGroupProjectService.getEnvMap(envGroupId);
}
return scenarioEnv;
}
public byte[] downloadJmeterFiles(String runMode, String remoteTestId, String reportId, String reportType, String queueId) {
JmeterRunRequestDTO runRequest = new JmeterRunRequestDTO(remoteTestId, reportId, runMode, null);
runRequest.setReportType(reportType);
@ -140,6 +142,7 @@ public class ApiJMeterFileService {
runRequest.setRetryNum(detail.getRetryNumber());
runRequest.setRunType(detail.getType());
}
// 执行时选择的环境
Map<String, String> processEnvMap = new LinkedHashMap<>();
if (detail != null && StringUtils.isNotEmpty(detail.getEvnMap())) {
processEnvMap = JSONUtil.parseObject(detail.getEvnMap(), Map.class);
@ -151,9 +154,7 @@ public class ApiJMeterFileService {
} else {
Map<String, String> execEnvMap = new HashMap<>();
ApiScenarioWithBLOBs scenario = this.getScenario(runMode, remoteTestId, execEnvMap, processEnvMap);
if (scenario != null) {
hashTree = GenerateHashTreeUtil.generateHashTree(scenario, execEnvMap, runRequest);
}
hashTree = GenerateHashTreeUtil.generateHashTree(scenario, execEnvMap, runRequest);
}
if (MapUtils.isEmpty(processEnvMap)) {

View File

@ -22,6 +22,7 @@ import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.*;
import io.metersphere.constants.RunModeConstants;
import io.metersphere.dto.*;
import io.metersphere.environment.service.BaseEnvGroupProjectService;
import io.metersphere.i18n.Translator;
import io.metersphere.log.utils.ReflexObjectUtil;
import io.metersphere.log.vo.DetailColumn;
@ -91,6 +92,8 @@ public class ApiScenarioReportService {
BaseShareInfoService baseShareInfoService;
@Resource
private RedisTemplateService redisTemplateService;
@Resource
private BaseEnvGroupProjectService environmentGroupProjectService;
public void batchSaveResult(List<ResultDTO> dtos) {
apiScenarioReportResultService.batchSave(dtos);
@ -754,19 +757,6 @@ public class ApiScenarioReportService {
return null;
}
public List<ApiScenarioReport> getByIds(List<String> ids) {
if (org.apache.commons.collections.CollectionUtils.isNotEmpty(ids)) {
ApiScenarioReportExample example = new ApiScenarioReportExample();
example.createCriteria().andIdIn(ids);
return apiScenarioReportMapper.selectByExample(example);
}
return null;
}
public List<ApiReportCountDTO> countByApiScenarioId() {
return extApiScenarioReportMapper.countByApiScenarioId();
}
public Map<String, String> getReportStatusByReportIds(Collection<String> values) {
if (CollectionUtils.isEmpty(values)) {
return new HashMap<>();
@ -869,6 +859,16 @@ public class ApiScenarioReportService {
return projectEnvMap;
}
private Map<String, String> extractScenarioEnv(String envType, String envJson, String envGroupId) {
Map<String, String> scenarioEnv = new LinkedHashMap<>();
if (envType.equals(EnvironmentType.JSON.name()) && !envJson.isBlank()) {
scenarioEnv = JSON.parseObject(envJson, Map.class);
} else if (envType.equals(EnvironmentType.GROUP.name()) && !envGroupId.isBlank()) {
scenarioEnv = environmentGroupProjectService.getEnvMap(envGroupId);
}
return scenarioEnv;
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void batchSave(Map<String, RunModeDataDTO> executeQueue, String serialReportId, String runMode, List<MsExecResponseDTO> responseDTOS) {
List<ApiScenarioReportResult> list = new LinkedList<>();
@ -877,14 +877,30 @@ public class ApiScenarioReportService {
ApiScenarioReportResult report = executeQueue.get(reportId).getReport();
ApiScenarioWithBLOBs scenario = executeQueue.get(reportId).getScenario();
if (ObjectUtils.isNotEmpty(scenario)) {
// 当前场景环境
Map<String, String> scenarioEnv = extractScenarioEnv(scenario.getEnvironmentType(), scenario.getEnvironmentJson(), scenario.getEnvironmentGroupId());
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());
// 获取场景用例单独的执行环境
scenarioEnv.entrySet().stream()
.filter(entry -> StringUtils.isBlank(envMap.get(entry.getKey())))
.forEach(entry -> envMap.put(entry.getKey(), entry.getValue()));
config.setEnvMap(envMap);
} else if (MapUtils.isNotEmpty(config.getExecutionEnvironmentMap())) {
Map<String, List<String>> envMap = getProjectMap(projectIdLists, config.getExecutionEnvironmentMap());
// 获取场景用例单独的执行环境
scenarioEnv.forEach((k, v) -> {
if (!envMap.containsKey(k) || CollectionUtils.isEmpty(envMap.get(k))) {
envMap.put(k, Collections.singletonList(v));
} else {
envMap.get(k).add(v);
}
});
config.setExecutionEnvironmentMap(envMap);
}
report.setEnvConfig(JSON.toJSONString(config));