From fb63829555da9defa3b2da411a35f6f75735fff4 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Tue, 4 Jan 2022 17:31:32 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=9A=E5=B1=82=E5=9C=BA=E6=99=AF=E5=BC=95?= =?UTF-8?q?=E7=94=A8=E6=89=A7=E8=A1=8C=E6=8A=A5=E5=91=8A=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ApiJmeterFileController.java | 15 ------- .../io/metersphere/api/dto/StepTreeDTO.java | 1 + .../dto/definition/request/ElementUtil.java | 42 ++++--------------- .../definition/request/ParameterConfig.java | 4 ++ .../request/processors/MsJSR223Processor.java | 16 +++---- .../request/sampler/MsDubboSampler.java | 2 +- .../request/sampler/MsHTTPSamplerProxy.java | 2 +- .../request/sampler/MsJDBCSampler.java | 2 +- .../request/sampler/MsTCPSampler.java | 3 +- .../exec/scenario/ApiScenarioEnvService.java | 2 +- .../api/exec/utils/GenerateHashTreeUtil.java | 14 ++++--- .../api/service/ApiAutomationService.java | 2 +- .../api/service/ApiJmeterFileService.java | 33 +-------------- .../ApiScenarioReportResultService.java | 3 -- .../api/service/ApiScenarioReportService.java | 2 +- .../ApiScenarioReportStructureService.java | 31 +++++++++++++- .../websocket/ApiReportWebSocket.java | 10 +---- 17 files changed, 69 insertions(+), 115 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/controller/ApiJmeterFileController.java b/backend/src/main/java/io/metersphere/api/controller/ApiJmeterFileController.java index 254ed7c873..36ee43c558 100644 --- a/backend/src/main/java/io/metersphere/api/controller/ApiJmeterFileController.java +++ b/backend/src/main/java/io/metersphere/api/controller/ApiJmeterFileController.java @@ -1,16 +1,13 @@ package io.metersphere.api.controller; -import io.metersphere.api.dto.scenario.request.BodyFile; import io.metersphere.api.jmeter.JmeterThreadUtils; import io.metersphere.api.service.ApiJmeterFileService; -import org.apache.dubbo.common.utils.CollectionUtils; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; -import java.util.List; import java.util.UUID; @RestController @@ -25,18 +22,6 @@ public class ApiJmeterFileController { return JmeterThreadUtils.stop(name); } - @PostMapping("download/files") - public ResponseEntity downloadBodyFiles(@RequestBody List bodyFileList) { - byte[] bytes = new byte[10]; - if (CollectionUtils.isNotEmpty(bodyFileList)) { - bytes = apiJmeterFileService.downloadJmeterFiles(bodyFileList); - } - return ResponseEntity.ok() - .contentType(MediaType.parseMediaType("application/octet-stream")) - .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + UUID.randomUUID().toString() + ".zip\"") - .body(bytes); - } - @GetMapping("download/jar") public ResponseEntity downloadJmeterFiles() { byte[] bytes = apiJmeterFileService.downloadJmeterJar(); diff --git a/backend/src/main/java/io/metersphere/api/dto/StepTreeDTO.java b/backend/src/main/java/io/metersphere/api/dto/StepTreeDTO.java index db9f0ed827..e62a4da0b0 100644 --- a/backend/src/main/java/io/metersphere/api/dto/StepTreeDTO.java +++ b/backend/src/main/java/io/metersphere/api/dto/StepTreeDTO.java @@ -14,6 +14,7 @@ public class StepTreeDTO { private String resourceId; private String label; private RequestResult value; + private String allIndex; //误报库编码 private String errorCode; diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java index d329e60c5a..fe06bd03b2 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java @@ -359,41 +359,6 @@ public class ElementUtil { } } - public static void dataFormatting(JSONArray hashTree, String id, String reportType) { - for (int i = 0; i < hashTree.size(); i++) { - JSONObject element = hashTree.getJSONObject(i); - formatSampler(element); - if (element != null && element.get("clazzName") == null && clazzMap.containsKey(element.getString("type"))) { - element.fluentPut("clazzName", clazzMap.get(element.getString("type"))); - } - if (StringUtils.equals(reportType, RunModeConstants.SET_REPORT.toString())) { - if (element != null && requests.contains(element.getString("type")) && !element.getString("resourceId").contains(id)) { - element.fluentPut("resourceId", id + "=" + element.getString("resourceId")); - } - } - if (element.containsKey("hashTree")) { - JSONArray elementJSONArray = element.getJSONArray("hashTree"); - dataFormatting(elementJSONArray, id, reportType); - } - } - } - - public static void dataFormatting(JSONObject element, String id, String reportType) { - if (element != null && element.get("clazzName") == null && clazzMap.containsKey(element.getString("type"))) { - element.fluentPut("clazzName", clazzMap.get(element.getString("type"))); - } - if (StringUtils.equals(reportType, RunModeConstants.SET_REPORT.toString())) { - if (element != null && requests.contains(element.getString("type")) && !element.getString("resourceId").contains(id)) { - element.fluentPut("resourceId", id + "=" + element.getString("resourceId")); - } - } - formatSampler(element); - if (element != null && element.containsKey("hashTree")) { - JSONArray elementJSONArray = element.getJSONArray("hashTree"); - dataFormatting(elementJSONArray, id, reportType); - } - } - public static void dataSetDomain(JSONArray hashTree, MsParameter msParameter) { try { ObjectMapper mapper = new ObjectMapper(); @@ -540,4 +505,11 @@ public class ElementUtil { } return null; } + + public static String getResourceId(String resourceId, ParameterConfig config, MsTestElement parent, String indexPath) { + if (StringUtils.isNotEmpty(config.getScenarioId()) && StringUtils.equals(config.getReportType(), RunModeConstants.SET_REPORT.toString())) { + resourceId = config.getScenarioId() + "=" + resourceId; + } + return resourceId + "_" + ElementUtil.getFullIndexPath(parent, indexPath); + } } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/ParameterConfig.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/ParameterConfig.java index bc14915f58..a46fa0648c 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/ParameterConfig.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/ParameterConfig.java @@ -52,6 +52,10 @@ public class ParameterConfig extends MsParameter { */ private String projectId; + private String scenarioId; + + private String reportType; + private List csvFilePaths = new ArrayList<>(); diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/MsJSR223Processor.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/MsJSR223Processor.java index dc93cc95e7..ad99883a64 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/MsJSR223Processor.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/MsJSR223Processor.java @@ -39,14 +39,14 @@ public class MsJSR223Processor extends MsTestElement { public void toHashTree(HashTree tree, List hashTree, MsParameter msParameter) { ParameterConfig config = (ParameterConfig) msParameter; //替换Metersphere环境变量 - if(StringUtils.isEmpty(this.getEnvironmentId())){ - if(config.getConfig() != null){ - if(config.getProjectId() != null){ + if (StringUtils.isEmpty(this.getEnvironmentId())) { + if (config.getConfig() != null) { + if (config.getProjectId() != null) { String evnId = config.getConfig().get(config.getProjectId()).getApiEnvironmentid(); this.setEnvironmentId(evnId); - }else { + } else { Collection evnConfigList = config.getConfig().values(); - if(evnConfigList!=null && !evnConfigList.isEmpty()){ + if (evnConfigList != null && !evnConfigList.isEmpty()) { for (EnvironmentConfig configItem : evnConfigList) { String evnId = configItem.getApiEnvironmentid(); this.setEnvironmentId(evnId); @@ -57,7 +57,7 @@ public class MsJSR223Processor extends MsTestElement { } } - script = StringUtils.replace(script, RunningParamKeys.API_ENVIRONMENT_ID,"\""+RunningParamKeys.RUNNING_PARAMS_PREFIX+this.getEnvironmentId()+".\""); + script = StringUtils.replace(script, RunningParamKeys.API_ENVIRONMENT_ID, "\"" + RunningParamKeys.RUNNING_PARAMS_PREFIX + this.getEnvironmentId() + ".\""); // 非导出操作,且不是启用状态则跳过执行 if (!config.isOperating() && !this.isEnable()) { @@ -71,14 +71,14 @@ public class MsJSR223Processor extends MsTestElement { processor.setName("JSR223Processor"); } processor.setProperty("MS-ID", this.getId()); - processor.setProperty("MS-RESOURCE-ID", this.getResourceId()+ "_" + this.getIndex()); + String indexPath = this.getIndex(); + processor.setProperty("MS-RESOURCE-ID", ElementUtil.getResourceId(this.getResourceId(), config, this.getParent(), indexPath)); List id_names = new LinkedList<>(); ElementUtil.getScenarioSet(this, id_names); processor.setProperty("MS-SCENARIO", JSON.toJSONString(id_names)); processor.setProperty(TestElement.TEST_CLASS, JSR223Sampler.class.getName()); processor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI")); - /*processor.setProperty("cacheKey", "true");*/ processor.setProperty("scriptLanguage", this.getScriptLanguage()); if (StringUtils.isNotEmpty(this.getScriptLanguage()) && this.getScriptLanguage().equals("nashornScript")) { processor.setProperty("scriptLanguage", "nashorn"); diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsDubboSampler.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsDubboSampler.java index 94ad610b83..a0d1c4eb18 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsDubboSampler.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsDubboSampler.java @@ -154,7 +154,7 @@ public class MsDubboSampler extends MsTestElement { sampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("DubboSampleGui")); sampler.setProperty("MS-ID", this.getId()); String indexPath = this.getIndex(); - sampler.setProperty("MS-RESOURCE-ID", this.getResourceId() + "_" + ElementUtil.getFullIndexPath(this.getParent(), indexPath)); + sampler.setProperty("MS-RESOURCE-ID", ElementUtil.getResourceId(this.getResourceId(), config, this.getParent(), indexPath)); List id_names = new LinkedList<>(); ElementUtil.getScenarioSet(this, id_names); sampler.setProperty("MS-SCENARIO", JSON.toJSONString(id_names)); diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java index 259b9dfc50..e1a8c9dc44 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java @@ -200,7 +200,7 @@ public class MsHTTPSamplerProxy extends MsTestElement { sampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("HttpTestSampleGui")); sampler.setProperty("MS-ID", this.getId()); String indexPath = this.getIndex(); - sampler.setProperty("MS-RESOURCE-ID", this.getResourceId() + "_" + ElementUtil.getFullIndexPath(this.getParent(), indexPath)); + sampler.setProperty("MS-RESOURCE-ID", ElementUtil.getResourceId(this.getResourceId(), config, this.getParent(), indexPath)); List id_names = new LinkedList<>(); ElementUtil.getScenarioSet(this, id_names); sampler.setProperty("MS-SCENARIO", JSON.toJSONString(id_names)); diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java index b89cc64f27..3e50d472d2 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java @@ -302,7 +302,7 @@ public class MsJDBCSampler extends MsTestElement { sampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI")); sampler.setProperty("MS-ID", this.getId()); String indexPath = this.getIndex(); - sampler.setProperty("MS-RESOURCE-ID", this.getResourceId() + "_" + ElementUtil.getFullIndexPath(this.getParent(), indexPath)); + sampler.setProperty("MS-RESOURCE-ID", ElementUtil.getResourceId(this.getResourceId(), config, this.getParent(), indexPath)); List id_names = new LinkedList<>(); ElementUtil.getScenarioSet(this, id_names); sampler.setProperty("MS-SCENARIO", JSON.toJSONString(id_names)); diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java index 3d79dd00b4..5f76443206 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java @@ -20,7 +20,6 @@ import io.metersphere.api.service.ApiDefinitionService; import io.metersphere.api.service.ApiTestCaseService; import io.metersphere.base.domain.ApiDefinitionWithBLOBs; import io.metersphere.base.domain.ApiTestCaseWithBLOBs; -import io.metersphere.commons.constants.DelimiterConstants; import io.metersphere.commons.constants.MsTestElementConstants; import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.LogUtil; @@ -245,7 +244,7 @@ public class MsTCPSampler extends MsTestElement { tcpSampler.setName(this.getName()); tcpSampler.setProperty("MS-ID", this.getId()); String indexPath = this.getIndex(); - tcpSampler.setProperty("MS-RESOURCE-ID", this.getResourceId() + "_" + ElementUtil.getFullIndexPath(this.getParent(), indexPath)); + tcpSampler.setProperty("MS-RESOURCE-ID", ElementUtil.getResourceId(this.getResourceId(), config, this.getParent(), indexPath)); List id_names = new LinkedList<>(); ElementUtil.getScenarioSet(this, id_names); tcpSampler.setProperty("MS-SCENARIO", JSON.toJSONString(id_names)); 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 2aec9b4e19..5b9090ab50 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 @@ -225,7 +225,7 @@ public class ApiScenarioEnvService { } String definition = apiScenarioWithBLOBs.getScenarioDefinition(); MsScenario scenario = JSONObject.parseObject(definition, MsScenario.class); - GenerateHashTreeUtil.parse(definition, scenario, apiScenarioWithBLOBs.getId(), null); + GenerateHashTreeUtil.parse(definition, scenario); if (StringUtils.equals(environmentType, EnvironmentType.JSON.toString())) { scenario.setEnvironmentMap(JSON.parseObject(environmentJson, Map.class)); } else if (StringUtils.equals(environmentType, EnvironmentType.GROUP.toString())) { diff --git a/backend/src/main/java/io/metersphere/api/exec/utils/GenerateHashTreeUtil.java b/backend/src/main/java/io/metersphere/api/exec/utils/GenerateHashTreeUtil.java index 6f83201961..fec9d4659f 100644 --- a/backend/src/main/java/io/metersphere/api/exec/utils/GenerateHashTreeUtil.java +++ b/backend/src/main/java/io/metersphere/api/exec/utils/GenerateHashTreeUtil.java @@ -35,18 +35,18 @@ public class GenerateHashTreeUtil { public static MsScenario parseScenarioDefinition(String scenarioDefinition) { if (StringUtils.isNotEmpty(scenarioDefinition)) { MsScenario scenario = JSONObject.parseObject(scenarioDefinition, MsScenario.class); - parse(scenarioDefinition, scenario, scenario.getId(), null); + parse(scenarioDefinition, scenario); return scenario; } return null; } - public static void parse(String scenarioDefinition, MsScenario scenario, String id, String reportType) { + public static void parse(String scenarioDefinition, MsScenario scenario) { ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); try { JSONObject element = JSON.parseObject(scenarioDefinition); - ElementUtil.dataFormatting(element, id, reportType); + ElementUtil.dataFormatting(element); // 多态JSON普通转换会丢失内容,需要通过 ObjectMapper 获取 if (element != null && StringUtils.isNotEmpty(element.getString("hashTree"))) { LinkedList elements = mapper.readValue(element.getString("hashTree"), @@ -134,7 +134,7 @@ public class GenerateHashTreeUtil { } else { setScenarioEnv(scenario, item); } - GenerateHashTreeUtil.parse(item.getScenarioDefinition(), scenario, item.getId(), reportType); + GenerateHashTreeUtil.parse(item.getScenarioDefinition(), scenario); group.setEnableCookieShare(scenario.isEnableCookieShare()); LinkedList scenarios = new LinkedList<>(); @@ -145,8 +145,10 @@ public class GenerateHashTreeUtil { } catch (Exception ex) { MSException.throwException(ex.getMessage()); } - - testPlan.toHashTree(jmeterHashTree, testPlan.getHashTree(), new ParameterConfig()); + ParameterConfig config = new ParameterConfig(); + config.setScenarioId(item.getId()); + config.setReportType(reportType); + testPlan.toHashTree(jmeterHashTree, testPlan.getHashTree(), config); return jmeterHashTree; } diff --git a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java index 17263d003c..ea683728f1 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -740,7 +740,7 @@ public class ApiAutomationService { if (scenario == null) { return null; } - GenerateHashTreeUtil.parse(apiScenario.getScenarioDefinition(), scenario, apiScenario.getId(), null); + GenerateHashTreeUtil.parse(apiScenario.getScenarioDefinition(), scenario); String environmentType = apiScenario.getEnvironmentType(); String environmentJson = apiScenario.getEnvironmentJson(); String environmentGroupId = apiScenario.getEnvironmentGroupId(); diff --git a/backend/src/main/java/io/metersphere/api/service/ApiJmeterFileService.java b/backend/src/main/java/io/metersphere/api/service/ApiJmeterFileService.java index f11a6a212e..061eade52f 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiJmeterFileService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiJmeterFileService.java @@ -47,18 +47,6 @@ public class ApiJmeterFileService { @Resource private EnvironmentGroupProjectService environmentGroupProjectService; - public byte[] downloadJmeterFiles(List bodyFileList) { - Map files = new LinkedHashMap<>(); - Map multipartFiles = this.getMultipartFiles(bodyFileList); - if (!com.alibaba.excel.util.CollectionUtils.isEmpty(multipartFiles)) { - for (String k : multipartFiles.keySet()) { - byte[] v = multipartFiles.get(k); - files.put(k, v); - } - } - return listBytesToZip(files); - } - public byte[] downloadJmeterFiles(String runMode, String remoteTestId, String reportId, String reportType, String queueId) { Map planEnvMap = new HashMap<>(); ApiScenarioWithBLOBs scenario = null; @@ -193,24 +181,7 @@ public class ApiJmeterFileService { if (CollectionUtils.isNotEmpty(files)) { for (BodyFile bodyFile : files) { File file = new File(bodyFile.getName()); - if (file != null && !file.exists()) { - byte[] fileByte = FileUtils.fileToByte(file); - if (fileByte != null) { - multipartFiles.put(file.getName(), fileByte); - } - } - } - } - return multipartFiles; - } - - private Map getMultipartFiles(List files) { - Map multipartFiles = new LinkedHashMap<>(); - // 获取附件 - if (CollectionUtils.isNotEmpty(files)) { - for (BodyFile bodyFile : files) { - File file = new File(bodyFile.getName()); - if (file != null && !file.exists()) { + if (file != null && file.exists()) { byte[] fileByte = FileUtils.fileToByte(file); if (fileByte != null) { multipartFiles.put(file.getName(), fileByte); @@ -229,7 +200,7 @@ public class ApiJmeterFileService { files.put(fileName, jmx.getBytes(StandardCharsets.UTF_8)); // 获取JMX使用到的附件 Map multipartFiles = this.getMultipartFiles(hashTree); - if (!com.alibaba.excel.util.CollectionUtils.isEmpty(multipartFiles)) { + if (multipartFiles != null && !multipartFiles.isEmpty()) { for (String k : multipartFiles.keySet()) { byte[] v = multipartFiles.get(k); files.put(k, v); diff --git a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportResultService.java b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportResultService.java index 2c9bfa0f02..02fc763262 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportResultService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportResultService.java @@ -52,9 +52,6 @@ public class ApiScenarioReportResultService { } } String resourceId = result.getResourceId(); - if (StringUtils.isNotEmpty(resourceId) && resourceId.contains("_")) { - resourceId = StringUtils.substringBefore(result.getResourceId(), "_"); - } report.setResourceId(resourceId); report.setReportId(reportId); report.setTotalAssertions(Long.parseLong(result.getTotalAssertions() + "")); diff --git a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java index edfb397b07..eafaac89c0 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java @@ -362,7 +362,7 @@ public class ApiScenarioReportService { public String getEnvironment(ApiScenarioWithBLOBs apiScenario) { String environment = "未配置"; String environmentType = apiScenario.getEnvironmentType(); - if (StringUtils.equals(environmentType, EnvironmentType.JSON.name())) { + if (StringUtils.equals(environmentType, EnvironmentType.JSON.name()) && StringUtils.isNotEmpty(apiScenario.getEnvironmentJson())) { String environmentJson = apiScenario.getEnvironmentJson(); JSONObject jsonObject = JSON.parseObject(environmentJson); ApiTestEnvironmentExample example = new ApiTestEnvironmentExample(); diff --git a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportStructureService.java b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportStructureService.java index ddfb6f4580..b5e353ab25 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportStructureService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportStructureService.java @@ -6,8 +6,11 @@ import com.alibaba.fastjson.JSONObject; import io.metersphere.api.dto.ApiScenarioReportDTO; import io.metersphere.api.dto.StepTreeDTO; import io.metersphere.base.domain.*; +import io.metersphere.base.mapper.ApiScenarioMapper; import io.metersphere.base.mapper.ApiScenarioReportResultMapper; import io.metersphere.base.mapper.ApiScenarioReportStructureMapper; +import io.metersphere.commons.constants.MsTestElementConstants; +import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.constants.RunModeConstants; import io.metersphere.dto.RequestResult; import io.metersphere.utils.LoggerUtil; @@ -75,13 +78,23 @@ public class ApiScenarioReportStructureService { JSONObject element = JSON.parseObject(apiScenario.getScenarioDefinition()); StepTreeDTO dto = null; if (element != null && element.getBoolean("enable")) { + String referenced = element.getString("referenced"); + if (StringUtils.equals(referenced, MsTestElementConstants.REF.name())) { + if (StringUtils.equals(element.getString("type"), "scenario")) { + ApiScenarioWithBLOBs scenarioWithBLOBs = CommonBeanFactory.getBean(ApiScenarioMapper.class).selectByPrimaryKey(element.getString("id")); + if (scenarioWithBLOBs != null) { + element = JSON.parseObject(scenarioWithBLOBs.getScenarioDefinition()); + } + } + } String resourceId = element.getString("resourceId"); if (StringUtils.equals(reportType, RunModeConstants.SET_REPORT.toString())) { if (StringUtils.isNotEmpty(resourceId) && StringUtils.isNotEmpty(apiScenario.getId()) && !resourceId.contains(apiScenario.getId())) { resourceId = apiScenario.getId() + "=" + element.getString("resourceId"); } } - dto = new StepTreeDTO(apiScenario.getName(), resourceId, element.getString("type"), element.getIntValue("index")); + dto = new StepTreeDTO(apiScenario.getName(), resourceId, element.getString("type"), 1); + dto.setAllIndex(null); if (element.containsKey("hashTree") && !requests.contains(dto.getType())) { JSONArray elementJSONArray = element.getJSONArray("hashTree"); dataFormatting(elementJSONArray, dto, apiScenario.getId(), reportType); @@ -94,6 +107,15 @@ public class ApiScenarioReportStructureService { for (int i = 0; i < hashTree.size(); i++) { JSONObject element = hashTree.getJSONObject(i); if (element != null && element.getBoolean("enable")) { + String referenced = element.getString("referenced"); + if (StringUtils.equals(referenced, MsTestElementConstants.REF.name())) { + if (StringUtils.equals(element.getString("type"), "scenario")) { + ApiScenarioWithBLOBs scenarioWithBLOBs = CommonBeanFactory.getBean(ApiScenarioMapper.class).selectByPrimaryKey(element.getString("id")); + if (scenarioWithBLOBs != null) { + element = JSON.parseObject(scenarioWithBLOBs.getScenarioDefinition()); + } + } + } String resourceId = element.getString("resourceId"); if (StringUtils.equals(reportType, RunModeConstants.SET_REPORT.toString())) { if (StringUtils.isNotEmpty(resourceId) && StringUtils.isNotEmpty(id) && !resourceId.contains(id)) { @@ -101,6 +123,13 @@ public class ApiScenarioReportStructureService { } } StepTreeDTO children = new StepTreeDTO(element.getString("name"), resourceId, element.getString("type"), element.getIntValue("index")); + if (StringUtils.isNotEmpty(dto.getAllIndex())) { + children.setAllIndex(dto.getAllIndex() + "_" + (children.getIndex() == 0 ? (i + 1) : children.getIndex())); + children.setResourceId(resourceId + "_" + children.getAllIndex()); + } else { + children.setAllIndex("" + (children.getIndex() == 0 ? (i + 1) : children.getIndex())); + children.setResourceId(resourceId + "_" + children.getAllIndex()); + } dto.getChildren().add(children); if (element.containsKey("hashTree") && !requests.contains(children.getType())) { JSONArray elementJSONArray = element.getJSONArray("hashTree"); diff --git a/backend/src/main/java/io/metersphere/websocket/ApiReportWebSocket.java b/backend/src/main/java/io/metersphere/websocket/ApiReportWebSocket.java index cd66b337b2..1d3a61ab99 100644 --- a/backend/src/main/java/io/metersphere/websocket/ApiReportWebSocket.java +++ b/backend/src/main/java/io/metersphere/websocket/ApiReportWebSocket.java @@ -1,8 +1,6 @@ package io.metersphere.websocket; -import com.alibaba.fastjson.JSON; import io.metersphere.api.dto.APIReportResult; -import io.metersphere.api.jmeter.TestResult; import io.metersphere.api.service.ApiDefinitionService; import io.metersphere.commons.utils.LogUtil; import lombok.SneakyThrows; @@ -99,12 +97,8 @@ public class ApiReportWebSocket { if (!session.isOpen()) { return; } - if (report != null) { - if (StringUtils.isNotEmpty(report.getContent())) { - session.getBasicRemote().sendText(report.getContent()); - } else { - session.getBasicRemote().sendText(JSON.toJSONString(new TestResult())); - } + if (report != null && StringUtils.isNotEmpty(report.getContent())) { + session.getBasicRemote().sendText(report.getContent()); session.close(); } } catch (Exception e) {