From 4f6f06108f276b735cf39bcb45f5aa33b160c653 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Thu, 20 Jan 2022 12:41:58 +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=E7=89=88=E6=9C=AC=E6=83=85=E5=86=B5?= =?UTF-8?q?=E4=B8=8B=E5=9C=BA=E6=99=AF=E5=BC=95=E7=94=A8=E5=A4=8D=E5=88=B6?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1009777 --user=赵勇 【接口自动化】-场景A中引用v1版本的场景B,删除场景B后场景A仍成功执行引用的场景B https://www.tapd.cn/55049933/s/1094931 --- .../controller/ApiAutomationController.java | 4 +- .../dto/definition/request/ElementUtil.java | 69 ++++++++++ .../dto/definition/request/MsScenario.java | 2 + .../request/sampler/MsDubboSampler.java | 12 +- .../request/sampler/MsHTTPSamplerProxy.java | 42 +++--- .../request/sampler/MsJDBCSampler.java | 22 +-- .../request/sampler/MsTCPSampler.java | 37 +++-- .../api/service/ApiAutomationService.java | 126 +++++++++++++----- .../mapper/ext/ExtApiDefinitionMapper.xml | 37 ++--- .../base/mapper/ext/ExtApiScenarioMapper.java | 2 +- .../base/mapper/ext/ExtApiScenarioMapper.xml | 8 +- .../base/mapper/ext/ExtApiTestCaseMapper.xml | 4 +- .../automation/scenario/EditApiScenario.vue | 4 + .../scenario/api/ScenarioRelevance.vue | 5 +- .../scenario/component/ApiComponent.vue | 70 +++++----- .../component/ApiScenarioComponent.vue | 2 +- 16 files changed, 299 insertions(+), 147 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java b/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java index dbda7d0a1a..dae2044a21 100644 --- a/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java +++ b/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java @@ -70,7 +70,7 @@ public class ApiAutomationController { @PostMapping("/list/all") @RequiresPermissions("PROJECT_API_SCENARIO:READ") - public List listAll(@RequestBody ApiScenarioBatchRequest request) { + public List listAll(@RequestBody ApiScenarioBatchRequest request) { return apiAutomationService.listAll(request); } @@ -194,7 +194,7 @@ public class ApiAutomationController { } @PostMapping("/getApiScenarios") - public List getApiScenarios(@RequestBody List ids) { + public List getApiScenarios(@RequestBody List ids) { return apiAutomationService.getApiScenarios(ids); } 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 fe06bd03b2..9125573648 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 @@ -495,6 +495,75 @@ public class ElementUtil { } } + public static void mergeHashTree(JSONObject element, JSONArray targetHashTree) { + try { + JSONArray sourceHashTree = element.getJSONArray("hashTree"); + if (CollectionUtils.isNotEmpty(sourceHashTree) && CollectionUtils.isNotEmpty(targetHashTree) && sourceHashTree.size() < targetHashTree.size()) { + element.put("hashTree", targetHashTree); + return; + } + List sourceIds = new ArrayList<>(); + List delIds = new ArrayList<>(); + Map updateMap = new HashMap<>(); + if (CollectionUtils.isEmpty(sourceHashTree)) { + if (CollectionUtils.isNotEmpty(targetHashTree)) { + element.put("hashTree", targetHashTree); + } + return; + } + if (CollectionUtils.isNotEmpty(targetHashTree)) { + for (int i = 0; i < targetHashTree.size(); i++) { + JSONObject item = targetHashTree.getJSONObject(i); + if (StringUtils.isNotEmpty(item.getString("id"))) { + updateMap.put(item.getString("id"), item); + } + } + } + // 找出待更新内容和源已经被删除的内容 + if (CollectionUtils.isNotEmpty(sourceHashTree)) { + for (int i = 0; i < sourceHashTree.size(); i++) { + JSONObject source = sourceHashTree.getJSONObject(i); + if (source != null) { + sourceIds.add(source.getString("id")); + if (!StringUtils.equals(source.getString("label"), "SCENARIO-REF-STEP") && StringUtils.isNotEmpty(source.getString("id"))) { + if (updateMap.containsKey(source.getString("id"))) { + sourceHashTree.set(i, updateMap.get(source.getString("id"))); + } else { + delIds.add(source.getString("id")); + } + } + // 历史数据兼容 + if (StringUtils.isEmpty(source.getString("id")) && !StringUtils.equals(source.getString("label"), "SCENARIO-REF-STEP") && i < targetHashTree.size()) { + sourceHashTree.set(i, targetHashTree.get(i)); + } + } + } + } + + // 删除多余的步骤 + for (int i = 0; i < sourceHashTree.size(); i++) { + JSONObject source = sourceHashTree.getJSONObject(i); + if (delIds.contains(source.getString("id"))) { + sourceHashTree.remove(i); + } + } + // 补充新增的源引用步骤 + if (CollectionUtils.isNotEmpty(targetHashTree)) { + for (int i = 0; i < targetHashTree.size(); i++) { + JSONObject item = sourceHashTree.getJSONObject(i); + if (!sourceIds.contains(item.getString("id"))) { + sourceHashTree.add(item); + } + } + } + if (CollectionUtils.isNotEmpty(sourceHashTree)) { + element.put("hashTree", sourceHashTree); + } + } catch (Exception e) { + element.put("hashTree", targetHashTree); + } + } + public static String hashTreeToString(HashTree hashTree) { try (ByteArrayOutputStream bas = new ByteArrayOutputStream()) { SaveService.saveTree(hashTree, bas); diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java index e92aa95b13..d7f5d8b65e 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java @@ -123,6 +123,8 @@ public class MsScenario extends MsTestElement { } this.setHashTree(sourceHashTree); hashTree = sourceHashTree; + } else { + return; } } catch (Exception ex) { 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 3d9e212334..c4f5ae67e8 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 @@ -26,6 +26,7 @@ import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.LogUtil; import io.metersphere.plugin.core.MsParameter; import io.metersphere.plugin.core.MsTestElement; +import io.metersphere.utils.LoggerUtil; import lombok.Data; import lombok.EqualsAndHashCode; import org.apache.commons.collections.CollectionUtils; @@ -88,7 +89,11 @@ public class MsDubboSampler extends MsTestElement { return; } if (this.getReferenced() != null && MsTestElementConstants.REF.name().equals(this.getReferenced())) { - this.setRefElement(); + boolean ref = this.setRefElement(); + if (!ref) { + LoggerUtil.debug("引用对象已经被删除:" + this.getId()); + return; + } hashTree = this.getHashTree(); } @@ -100,7 +105,7 @@ public class MsDubboSampler extends MsTestElement { } } - private void setRefElement() { + private boolean setRefElement() { try { ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class); ObjectMapper mapper = new ObjectMapper(); @@ -139,11 +144,12 @@ public class MsDubboSampler extends MsTestElement { this.setConsumerAndService(proxy.getConsumerAndService()); this.setRegistryCenter(proxy.getRegistryCenter()); this.setConfigCenter(proxy.getConfigCenter()); + return true; } } catch (Exception ex) { - ex.printStackTrace(); LogUtil.error(ex); } + return false; } private DubboSample dubboSample(ParameterConfig config) { 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 e18c652c50..f502c1fd2a 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 @@ -45,6 +45,7 @@ import io.metersphere.jmeter.utils.ScriptEngineUtils; import io.metersphere.plugin.core.MsParameter; import io.metersphere.plugin.core.MsTestElement; import io.metersphere.track.service.TestPlanApiCaseService; +import io.metersphere.utils.LoggerUtil; import lombok.Data; import lombok.EqualsAndHashCode; import org.apache.commons.collections.CollectionUtils; @@ -133,7 +134,7 @@ public class MsHTTPSamplerProxy extends MsTestElement { @JSONField(ordinal = 39) private boolean customizeReq; - private void setRefElement() { + private boolean setRefElement() { try { ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class); ObjectMapper mapper = new ObjectMapper(); @@ -171,11 +172,12 @@ public class MsHTTPSamplerProxy extends MsTestElement { this.setRest(proxy.getRest()); this.setArguments(proxy.getArguments()); this.setHeaders(proxy.getHeaders()); + return true; } } catch (Exception ex) { - ex.printStackTrace(); LogUtil.error(ex); } + return false; } @Override @@ -189,7 +191,11 @@ public class MsHTTPSamplerProxy extends MsTestElement { return; } if (this.getReferenced() != null && MsTestElementConstants.REF.name().equals(this.getReferenced())) { - this.setRefElement(); + boolean ref = this.setRefElement(); + if (!ref) { + LoggerUtil.debug("引用对象已经被删除:" + this.getId()); + return; + } hashTree = this.getHashTree(); } HTTPSamplerProxy sampler = new HTTPSamplerProxy(); @@ -749,22 +755,22 @@ public class MsHTTPSamplerProxy extends MsTestElement { list.stream(). filter(KeyValue::isValid). filter(KeyValue::isEnable).forEach(keyValue -> { - try { - String value = StringUtils.isNotEmpty(keyValue.getValue()) && keyValue.getValue().startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(keyValue.getValue()) : keyValue.getValue(); - HTTPArgument httpArgument = new HTTPArgument(keyValue.getName(), value); - if (keyValue.getValue() == null) { - httpArgument.setValue(""); - } - httpArgument.setAlwaysEncoded(keyValue.isUrlEncode()); - if (StringUtils.isNotBlank(keyValue.getContentType())) { - httpArgument.setContentType(keyValue.getContentType()); - } - arguments.addArgument(httpArgument); - } catch (Exception e) { - - } + try { + String value = StringUtils.isNotEmpty(keyValue.getValue()) && keyValue.getValue().startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(keyValue.getValue()) : keyValue.getValue(); + HTTPArgument httpArgument = new HTTPArgument(keyValue.getName(), value); + if (keyValue.getValue() == null) { + httpArgument.setValue(""); } - ); + httpArgument.setAlwaysEncoded(keyValue.isUrlEncode()); + if (StringUtils.isNotBlank(keyValue.getContentType())) { + httpArgument.setContentType(keyValue.getContentType()); + } + arguments.addArgument(httpArgument); + } catch (Exception e) { + + } + } + ); return arguments; } 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 e1194d2fce..7ffee269e4 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 @@ -29,6 +29,7 @@ import io.metersphere.commons.utils.LogUtil; import io.metersphere.constants.RunModeConstants; import io.metersphere.plugin.core.MsParameter; import io.metersphere.plugin.core.MsTestElement; +import io.metersphere.utils.LoggerUtil; import lombok.Data; import lombok.EqualsAndHashCode; import org.apache.commons.collections.CollectionUtils; @@ -86,7 +87,11 @@ public class MsJDBCSampler extends MsTestElement { return; } if (this.getReferenced() != null && MsTestElementConstants.REF.name().equals(this.getReferenced())) { - this.setRefElement(); + boolean ref = this.setRefElement(); + if (!ref) { + LoggerUtil.debug("引用对象已经被删除:" + this.getId()); + return; + } hashTree = this.getHashTree(); } if (config != null && config.getConfig() == null) { @@ -172,12 +177,12 @@ public class MsJDBCSampler extends MsTestElement { } //处理全局前后置脚本(步骤内) - String enviromentId = this.getEnvironmentId(); - if (enviromentId == null) { - enviromentId = this.useEnvironment; + String environmentId = this.getEnvironmentId(); + if (environmentId == null) { + environmentId = this.useEnvironment; } //根据配置将脚本放置在私有脚本之前 - JMeterScriptUtil.setScript(envConfig, samplerHashTree, GlobalScriptFilterRequest.JDBC.name(), enviromentId, config, false); + JMeterScriptUtil.setScript(envConfig, samplerHashTree, GlobalScriptFilterRequest.JDBC.name(), environmentId, config, false); HashTreeUtil hashTreeUtil = new HashTreeUtil(); @@ -192,7 +197,7 @@ public class MsJDBCSampler extends MsTestElement { }); } //根据配置将脚本放置在私有脚本之后 - JMeterScriptUtil.setScript(envConfig, samplerHashTree, GlobalScriptFilterRequest.JDBC.name(), enviromentId, config, true); + JMeterScriptUtil.setScript(envConfig, samplerHashTree, GlobalScriptFilterRequest.JDBC.name(), environmentId, config, true); } @@ -239,7 +244,7 @@ public class MsJDBCSampler extends MsTestElement { return ""; } - private void setRefElement() { + private boolean setRefElement() { try { ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class); ObjectMapper mapper = new ObjectMapper(); @@ -278,11 +283,12 @@ public class MsJDBCSampler extends MsTestElement { this.setVariableNames(proxy.getVariableNames()); this.setResultVariable(proxy.getResultVariable()); this.setQueryTimeout(proxy.getQueryTimeout()); + return true; } } catch (Exception ex) { - ex.printStackTrace(); LogUtil.error(ex); } + return false; } private EnvironmentConfig initDataSource() { 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 26f09c41d6..ef80d579e0 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 @@ -28,6 +28,7 @@ import io.metersphere.commons.utils.LogUtil; import io.metersphere.jmeter.utils.ScriptEngineUtils; import io.metersphere.plugin.core.MsParameter; import io.metersphere.plugin.core.MsTestElement; +import io.metersphere.utils.LoggerUtil; import lombok.Data; import lombok.EqualsAndHashCode; import org.apache.commons.collections.CollectionUtils; @@ -123,7 +124,11 @@ public class MsTCPSampler extends MsTestElement { return; } if (this.getReferenced() != null && MsTestElementConstants.REF.name().equals(this.getReferenced())) { - this.setRefElement(); + boolean ref = this.setRefElement(); + if (!ref) { + LoggerUtil.debug("引用对象已经被删除:" + this.getId()); + return; + } hashTree = this.getHashTree(); } if (config.getConfig() == null) { @@ -154,7 +159,7 @@ public class MsTCPSampler extends MsTestElement { //增加误报、全局断言 if (envConfig != null) { if (envConfig.isUseErrorCode()) { - List errorReportAssertion =HashTreeUtil.getErrorReportByProjectId(this.getProjectId()); + List errorReportAssertion = HashTreeUtil.getErrorReportByProjectId(this.getProjectId()); for (MsAssertions assertion : errorReportAssertion) { assertion.toHashTree(samplerHashTree, assertion.getHashTree(), config); } @@ -167,12 +172,12 @@ public class MsTCPSampler extends MsTestElement { } //处理全局前后置脚本(步骤内) - String enviromentId = this.getEnvironmentId(); - if (enviromentId == null) { - enviromentId = this.useEnvironment; + String environmentId = this.getEnvironmentId(); + if (environmentId == null) { + environmentId = this.useEnvironment; } //根据配置将脚本放置在私有脚本之前 - JMeterScriptUtil.setScript(envConfig, samplerHashTree, GlobalScriptFilterRequest.TCP.name(), enviromentId, config, false); + JMeterScriptUtil.setScript(envConfig, samplerHashTree, GlobalScriptFilterRequest.TCP.name(), environmentId, config, false); HashTreeUtil hashTreeUtil = new HashTreeUtil(); @@ -187,23 +192,10 @@ public class MsTCPSampler extends MsTestElement { }); } //根据配置将脚本放置在私有脚本之后 - JMeterScriptUtil.setScript(envConfig, samplerHashTree, GlobalScriptFilterRequest.TCP.name(), enviromentId, config, true); + JMeterScriptUtil.setScript(envConfig, samplerHashTree, GlobalScriptFilterRequest.TCP.name(), environmentId, config, true); } - private void addItemHashTree(MsTestElement element, HashTree samplerHashTree, ParameterConfig config) { - if (element != null) { - if (element.getEnvironmentId() == null) { - if (this.getEnvironmentId() == null) { - element.setEnvironmentId(useEnvironment); - } else { - element.setEnvironmentId(this.getEnvironmentId()); - } - } - element.toHashTree(samplerHashTree, element.getHashTree(), config); - } - } - - private void setRefElement() { + private boolean setRefElement() { try { ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class); ObjectMapper mapper = new ObjectMapper(); @@ -239,11 +231,12 @@ public class MsTCPSampler extends MsTestElement { this.setServer(proxy.getServer()); this.setPort(proxy.getPort()); this.setRequest(proxy.getRequest()); + return true; } } catch (Exception ex) { - ex.printStackTrace(); LogUtil.error(ex); } + return false; } private void parseEnvironment(EnvironmentConfig config) { 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 963a9284df..e085e09787 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -9,6 +9,8 @@ import io.metersphere.api.dto.automation.parse.ScenarioImport; import io.metersphere.api.dto.automation.parse.ScenarioImportParserFactory; import io.metersphere.api.dto.datacount.ApiDataCountResult; import io.metersphere.api.dto.datacount.ApiMethodUrlDTO; +import io.metersphere.api.dto.definition.ApiDefinitionResult; +import io.metersphere.api.dto.definition.ApiTestCaseInfo; import io.metersphere.api.dto.definition.RunDefinitionRequest; import io.metersphere.api.dto.definition.request.*; import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy; @@ -39,7 +41,10 @@ import io.metersphere.log.vo.DetailColumn; import io.metersphere.log.vo.OperatingLogDetails; import io.metersphere.log.vo.api.AutomationReference; import io.metersphere.plugin.core.MsTestElement; -import io.metersphere.service.*; +import io.metersphere.service.EnvironmentGroupProjectService; +import io.metersphere.service.QuotaService; +import io.metersphere.service.RelationshipEdgeService; +import io.metersphere.service.ScheduleService; import io.metersphere.track.dto.TestPlanDTO; import io.metersphere.track.request.testcase.ApiCaseRelevanceRequest; import io.metersphere.track.request.testcase.QueryTestPlanRequest; @@ -166,10 +171,10 @@ public class ApiAutomationService { return list; } - public List listAll(ApiScenarioBatchRequest request) { + public List listAll(ApiScenarioBatchRequest request) { ServiceUtils.getSelectAllIds(request, request.getCondition(), (query) -> extApiScenarioMapper.selectIdsByQuery(query)); - List list = extApiScenarioMapper.selectIds(request.getIds()); + List list = extApiScenarioMapper.selectIds(request.getIds()); return list; } @@ -675,25 +680,84 @@ public class ApiAutomationService { return scenarioWithBLOBs; } + private final static List requests = new ArrayList() {{ + this.add("HTTPSamplerProxy"); + this.add("DubboSampler"); + this.add("JDBCSampler"); + this.add("TCPSampler"); + }}; + + private void setElement(JSONObject element, Integer num, boolean enable, String versionName, boolean versionEnable) { + element.put("num", num); + element.put("enable", enable); + element.put("versionName", versionName); + element.put("versionEnable", versionEnable); + } + + private JSONObject setRequest(JSONObject element) { + boolean enable = element.getBoolean("enable"); + boolean isExist = false; + if (StringUtils.equalsIgnoreCase(element.getString("refType"), "CASE")) { + ApiTestCaseInfo apiTestCase = apiTestCaseService.get(element.getString("id")); + if (apiTestCase != null) { + JSONObject refElement = JSON.parseObject(apiTestCase.getRequest()); + ElementUtil.dataFormatting(refElement); + if (refElement.get("hashTree") != null && StringUtils.equalsIgnoreCase(element.getString("referenced"), "REF")) { + ElementUtil.mergeHashTree(element, refElement.getJSONArray("hashTree")); + element.put("referenced", "REF"); + element.put("name", apiTestCase.getName()); + } else { + element = refElement; + } + isExist = true; + this.setElement(element, apiTestCase.getNum(), enable, apiTestCase.getVersionName(), apiTestCase.getVersionEnable()); + } + } else { + ApiDefinitionResult definitionWithBLOBs = apiDefinitionService.getById(element.getString("id")); + if (definitionWithBLOBs != null) { + this.setElement(element, definitionWithBLOBs.getNum(), enable, definitionWithBLOBs.getVersionName(), definitionWithBLOBs.getVersionEnable()); + isExist = true; + } + } + if (!isExist) { + if (StringUtils.equalsIgnoreCase(element.getString("referenced"), "REF")) { + element.put("enable", false); + } + element.put("num", ""); + } + return element; + } + + private JSONObject setRefScenario(JSONObject element) { + boolean enable = element.getBoolean("enable"); + ApiScenarioDTO scenarioWithBLOBs = extApiScenarioMapper.selectById(element.getString("id")); + if (scenarioWithBLOBs != null && StringUtils.isNotEmpty(scenarioWithBLOBs.getScenarioDefinition())) { + boolean environmentEnable = element.getBoolean("environmentEnable"); + if (StringUtils.equalsIgnoreCase(element.getString("referenced"), "REF")) { + element = JSON.parseObject(scenarioWithBLOBs.getScenarioDefinition()); + element.put("referenced", "REF"); + element.put("name", scenarioWithBLOBs.getName()); + } + element.put("environmentEnable", environmentEnable); + this.setElement(element, scenarioWithBLOBs.getNum(), enable, scenarioWithBLOBs.getVersionName(), scenarioWithBLOBs.getVersionEnable()); + } else { + if (StringUtils.equalsIgnoreCase(element.getString("referenced"), "REF")) { + element.put("enable", false); + } + element.put("num", ""); + } + return element; + } + public void dataFormatting(JSONArray hashTree) { for (int i = 0; i < hashTree.size(); i++) { JSONObject element = hashTree.getJSONObject(i); if (element != null && StringUtils.equalsIgnoreCase(element.getString("type"), "scenario")) { - ApiScenarioDTO scenarioWithBLOBs = extApiScenarioMapper.selectById(element.getString("id")); - if (scenarioWithBLOBs != null && StringUtils.isNotEmpty(scenarioWithBLOBs.getScenarioDefinition())) { - boolean enable = element.getBoolean("enable"); - boolean environmentEnable = element.getBoolean("environmentEnable"); - if (StringUtils.equalsIgnoreCase(element.getString("referenced"), "REF")) { - element = JSON.parseObject(scenarioWithBLOBs.getScenarioDefinition()); - element.put("referenced", "REF"); - } - element.put("num", scenarioWithBLOBs.getNum()); - element.put("enable", enable); - element.put("environmentEnable", environmentEnable); - element.put("versionName", scenarioWithBLOBs.getVersionName()); - element.put("versionEnable", scenarioWithBLOBs.getVersionEnable()); - hashTree.set(i, element); - } + element = this.setRefScenario(element); + hashTree.set(i, element); + } else if (element != null && requests.contains(element.getString("type"))) { + element = this.setRequest(element); + hashTree.set(i, element); } if (element.containsKey("hashTree")) { JSONArray elementJSONArray = element.getJSONArray("hashTree"); @@ -704,20 +768,9 @@ public class ApiAutomationService { public void dataFormatting(JSONObject element) { if (element != null && StringUtils.equalsIgnoreCase(element.getString("type"), "scenario")) { - ApiScenarioDTO scenarioWithBLOBs = extApiScenarioMapper.selectById(element.getString("id")); - if (scenarioWithBLOBs != null && StringUtils.isNotEmpty(scenarioWithBLOBs.getScenarioDefinition())) { - boolean enable = element.getBoolean("enable"); - boolean environmentEnable = element.getBoolean("environmentEnable"); - if (StringUtils.equalsIgnoreCase(element.getString("referenced"), "REF")) { - element = JSON.parseObject(scenarioWithBLOBs.getScenarioDefinition()); - element.put("referenced", "REF"); - } - element.put("enable", enable); - element.put("environmentEnable", environmentEnable); - element.put("num", scenarioWithBLOBs.getNum()); - element.put("versionName", scenarioWithBLOBs.getVersionName()); - element.put("versionEnable", scenarioWithBLOBs.getVersionEnable()); - } + element = this.setRefScenario(element); + } else if (element != null && requests.contains(element.getString("type"))) { + element = this.setRequest(element); } if (element != null && element.containsKey("hashTree")) { JSONArray elementJSONArray = element.getJSONArray("hashTree"); @@ -761,7 +814,7 @@ public class ApiAutomationService { } - public List getApiScenarios(List ids) { + public List getApiScenarios(List ids) { if (CollectionUtils.isNotEmpty(ids)) { return extApiScenarioMapper.selectIds(ids); } @@ -1050,9 +1103,8 @@ public class ApiAutomationService { } public JmxInfoDTO genPerformanceTestJmx(GenScenarioRequest request) { - List apiScenarios = null; List ids = request.getIds(); - apiScenarios = extApiScenarioMapper.selectIds(ids); + List apiScenarios = extApiScenarioMapper.selectIds(ids); String testName = ""; String id = ""; if (!apiScenarios.isEmpty()) { @@ -1821,7 +1873,7 @@ public class ApiAutomationService { List returnList = new ArrayList<>(); List ids = request.getIds(); - List apiScenarioList = extApiScenarioMapper.selectIds(ids); + List apiScenarioList = extApiScenarioMapper.selectIds(ids); if (CollectionUtils.isEmpty(apiScenarioList)) { return returnList; } else { @@ -1845,7 +1897,7 @@ public class ApiAutomationService { (query) -> extApiScenarioMapper.selectIdsByQuery(query)); List ids = request.getIds(); if (CollectionUtils.isEmpty(ids)) return; - List apiScenarioList = extApiScenarioMapper.selectIds(ids); + List apiScenarioList = extApiScenarioMapper.selectIds(ids); SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); ApiScenarioMapper mapper = sqlSession.getMapper(ApiScenarioMapper.class); Long nextOrder = ServiceUtils.getNextOrder(request.getProjectId(), extApiScenarioMapper::getLastOrder); diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionMapper.xml index 145b0d00ba..ae16145f9c 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionMapper.xml @@ -89,12 +89,12 @@ select count(DISTINCT ref_id) from api_definition - + diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioMapper.java b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioMapper.java index 58586eba08..e7cbac3f9f 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioMapper.java +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioMapper.java @@ -23,7 +23,7 @@ public interface ExtApiScenarioMapper { List selectByTagId(@Param("id") String id); - List selectIds(@Param("ids") List ids); + List selectIds(@Param("ids") List ids); int selectTrash(@Param("projectId") String projectId); diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioMapper.xml index 13f025215d..4b59c9f53e 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioMapper.xml @@ -351,8 +351,12 @@ select * from api_scenario where tags like CONCAT('%', #{id},'%') - + select api_scenario.*, project_version.name as version_name, project.version_enable + from api_scenario + LEFT JOIN project_version on project_version.project_id = api_scenario.project_id and project_version.id = version_id + LEFT JOIN project on api_scenario.project_id = project.id + WHERE api_scenario.id in #{v} diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestCaseMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestCaseMapper.xml index ee37e58703..cf07dc32fa 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestCaseMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestCaseMapper.xml @@ -600,9 +600,11 @@