From 454d84037c9c68fe559e8443f30e8e1f14e4fece Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Fri, 26 Nov 2021 16:44:35 +0800 Subject: [PATCH] =?UTF-8?q?fix(SQL=E7=94=A8=E4=BE=8B=E6=89=A7=E8=A1=8C):?= =?UTF-8?q?=20=E4=BF=AE=E5=A4=8DSQL=E7=94=A8=E4=BE=8B=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E6=97=B6=E8=87=AA=E5=8A=A8=E6=B7=BB=E5=8A=A0script=E7=A9=BA?= =?UTF-8?q?=E8=84=9A=E6=9C=AC=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1008370 --user=宋天阳 【接口测试】创建sql用例,有一个步骤,但是步骤里啥都没有,执行的时候还会报错 https://www.tapd.cn/55049933/s/1075620 --- .../definition/parse/JMeterScriptUtil.java | 92 +++++++++++++++++++ .../request/sampler/MsHTTPSamplerProxy.java | 40 ++++---- .../request/sampler/MsJDBCSampler.java | 51 +++------- .../request/sampler/MsTCPSampler.java | 53 +++-------- .../components/case/ApiCaseItem.vue | 2 +- 5 files changed, 138 insertions(+), 100 deletions(-) create mode 100644 backend/src/main/java/io/metersphere/api/dto/definition/parse/JMeterScriptUtil.java diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/parse/JMeterScriptUtil.java b/backend/src/main/java/io/metersphere/api/dto/definition/parse/JMeterScriptUtil.java new file mode 100644 index 0000000000..22b132536e --- /dev/null +++ b/backend/src/main/java/io/metersphere/api/dto/definition/parse/JMeterScriptUtil.java @@ -0,0 +1,92 @@ +package io.metersphere.api.dto.definition.parse; + +import io.metersphere.api.dto.definition.request.ParameterConfig; +import io.metersphere.api.dto.definition.request.processors.post.MsJSR223PostProcessor; +import io.metersphere.api.dto.definition.request.processors.pre.MsJSR223PreProcessor; +import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; +import io.metersphere.api.dto.scenario.environment.GlobalScriptConfig; +import io.metersphere.plugin.core.MsTestElement; +import org.apache.commons.lang3.StringUtils; +import org.apache.jorphan.collections.HashTree; +import org.springframework.util.CollectionUtils; + +import java.util.List; + +public class JMeterScriptUtil { + + /** + * 判断脚本是否被过滤 + * + * @param filterProtocals 要过滤掉的请求类型 + * @param protocal 当前请求类型 + * @return + */ + public static boolean isScriptFilter(List filterProtocals, String protocal) { + if (!CollectionUtils.isEmpty(filterProtocals)) { + return filterProtocals.contains(protocal); + } else { + return false; + } + } + + public static MsJSR223PreProcessor getPreScript(EnvironmentConfig envConfig) { + if (envConfig != null && envConfig.getPreProcessor() != null && StringUtils.isNotEmpty(envConfig.getPreProcessor().getScript())) { + return envConfig.getPreProcessor(); + } else { + return null; + } + } + + public static MsJSR223PostProcessor getPostScript(EnvironmentConfig envConfig) { + if (envConfig != null && envConfig.getPostProcessor() != null && StringUtils.isNotEmpty(envConfig.getPostProcessor().getScript())) { + return envConfig.getPostProcessor(); + } else { + return null; + } + } + + /** + * Samper中放置脚本 + * + * @param envConfig 环境配置信息 + * @param samplerHashTree sampler的hashtree + * @param isAfterPrivateScript 是否将脚本放置在sampler的私有脚本之后 + * @param protocal 请求类型 + * @param environmentId 环境ID + * @param config 参数配置 + */ + public static void setScript(EnvironmentConfig envConfig, HashTree samplerHashTree, String protocal, String environmentId, ParameterConfig config, boolean isAfterPrivateScript) { + GlobalScriptConfig globalScriptConfig = envConfig != null ? envConfig.getGlobalScriptConfig() : null; + + boolean isPreScriptExecAfterPrivateScript = globalScriptConfig == null ? false : globalScriptConfig.isPreScriptExecAfterPrivateScript(); + boolean isPostScriptExecAfterPrivateScript = globalScriptConfig == null ? false : globalScriptConfig.isPostScriptExecAfterPrivateScript(); + List preFilterProtocal = globalScriptConfig == null ? null : globalScriptConfig.getFilterRequestPreScript(); + List postFilterProtocal = globalScriptConfig == null ? null : globalScriptConfig.getFilterRequestPostScript(); + MsJSR223PreProcessor preProcessor = JMeterScriptUtil.getPreScript(envConfig); + MsJSR223PostProcessor postProcessor = JMeterScriptUtil.getPostScript(envConfig); + boolean globalPreScriptIsFilter = JMeterScriptUtil.isScriptFilter(preFilterProtocal, protocal); + boolean globalPostScriptIsFilter = JMeterScriptUtil.isScriptFilter(postFilterProtocal, protocal); + if (isAfterPrivateScript) { + if (isPreScriptExecAfterPrivateScript && !globalPreScriptIsFilter) { + addItemHashTree(preProcessor, samplerHashTree, config, environmentId); + } + if (isPostScriptExecAfterPrivateScript && !globalPostScriptIsFilter) { + addItemHashTree(postProcessor, samplerHashTree, config, environmentId); + } + } else { + if (!isPreScriptExecAfterPrivateScript && !globalPreScriptIsFilter) { + addItemHashTree(preProcessor, samplerHashTree, config, environmentId); + } + if (!isPostScriptExecAfterPrivateScript && !globalPostScriptIsFilter) { + addItemHashTree(postProcessor, samplerHashTree, config, environmentId); + } + } + } + + private static void addItemHashTree(MsTestElement element, HashTree samplerHashTree, ParameterConfig config, String enviromentId) { + if (element != null && element.getEnvironmentId() == null) { + element.setEnvironmentId(enviromentId); + element.toHashTree(samplerHashTree, element.getHashTree(), 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 7ce147b894..9e00a2263b 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 @@ -7,6 +7,7 @@ import com.alibaba.fastjson.annotation.JSONType; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; +import io.metersphere.api.dto.definition.parse.JMeterScriptUtil; import io.metersphere.api.dto.definition.request.ElementUtil; import io.metersphere.api.dto.definition.request.ParameterConfig; import io.metersphere.api.dto.definition.request.auth.MsAuthManager; @@ -307,12 +308,11 @@ public class MsHTTPSamplerProxy extends MsTestElement { MsJSR223PreProcessor preProcessor = httpConfig.getPreProcessor(); MsJSR223PostProcessor postProcessor = httpConfig.getPostProcessor(); GlobalScriptConfig globalScriptConfig = httpConfig.getGlobalScriptConfig(); - List filterPreProtocal = globalScriptConfig == null ? new ArrayList<>() : globalScriptConfig.getFilterRequestPreScript(); - List filterPostProtocal = globalScriptConfig == null ? new ArrayList<>() : globalScriptConfig.getFilterRequestPostScript(); - - boolean filterPre = filterPreProtocal.contains(GlobalScriptFilterRequest.HTTP.name()); - boolean filterPost = filterPostProtocal.contains(GlobalScriptFilterRequest.HTTP.name()); + List filterPreProtocal = globalScriptConfig == null ? null : globalScriptConfig.getFilterRequestPreScript(); + List filterPostProtocal = globalScriptConfig == null ? null : globalScriptConfig.getFilterRequestPostScript(); + boolean filterPre = JMeterScriptUtil.isScriptFilter(filterPreProtocal, GlobalScriptFilterRequest.HTTP.name()); + boolean filterPost = JMeterScriptUtil.isScriptFilter(filterPostProtocal, GlobalScriptFilterRequest.HTTP.name()); boolean isPreScriptExecAfterPrivateScript = globalScriptConfig == null ? false : globalScriptConfig.isPreScriptExecAfterPrivateScript(); boolean isPostScriptExecAfterPrivateScript = globalScriptConfig == null ? false : globalScriptConfig.isPostScriptExecAfterPrivateScript(); @@ -724,22 +724,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 82cf1125ec..fcaf9f56cc 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 @@ -7,14 +7,12 @@ import com.alibaba.fastjson.annotation.JSONType; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; +import io.metersphere.api.dto.definition.parse.JMeterScriptUtil; import io.metersphere.api.dto.definition.request.ElementUtil; import io.metersphere.api.dto.definition.request.ParameterConfig; -import io.metersphere.api.dto.definition.request.processors.post.MsJSR223PostProcessor; -import io.metersphere.api.dto.definition.request.processors.pre.MsJSR223PreProcessor; import io.metersphere.api.dto.scenario.DatabaseConfig; import io.metersphere.api.dto.scenario.KeyValue; import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; -import io.metersphere.api.dto.scenario.environment.GlobalScriptConfig; import io.metersphere.api.dto.scenario.environment.GlobalScriptFilterRequest; import io.metersphere.api.service.ApiDefinitionService; import io.metersphere.api.service.ApiTestCaseService; @@ -100,7 +98,7 @@ public class MsJDBCSampler extends MsTestElement { // 数据兼容处理 if (config.getConfig() != null && StringUtils.isNotEmpty(this.getProjectId()) && config.getConfig().containsKey(this.getProjectId())) { // 1.8 之后 当前正常数据 - } else if ( config.getConfig() != null && config.getConfig().containsKey(getParentProjectId())) { + } else if (config.getConfig() != null && config.getConfig().containsKey(getParentProjectId())) { // 1.8 前后 混合数据 this.setProjectId(getParentProjectId()); } else { @@ -152,57 +150,30 @@ public class MsJDBCSampler extends MsTestElement { if (arguments != null) { tree.add(arguments); } - // 环境通用请求头 Arguments envArguments = getConfigArguments(config); if (envArguments != null) { tree.add(envArguments); } - MsJSR223PreProcessor preProcessor = envConfig != null ? envConfig.getPreProcessor() : null; - MsJSR223PostProcessor postProcessor = envConfig != null ? envConfig.getPostProcessor() : null; - GlobalScriptConfig globalScriptConfig = envConfig != null ? envConfig.getGlobalScriptConfig() : null; - if (globalScriptConfig != null) { - boolean isPreScriptExecAfterPrivateScript = globalScriptConfig.isPreScriptExecAfterPrivateScript(); - boolean isPostScriptExecAfterPrivateScript = globalScriptConfig.isPostScriptExecAfterPrivateScript(); - List preFilters = globalScriptConfig == null ? new ArrayList<>() : globalScriptConfig.getFilterRequestPreScript(); - List postFilters = globalScriptConfig == null ? new ArrayList<>() : globalScriptConfig.getFilterRequestPostScript(); - boolean globalPreScriptIsFilter = preFilters.contains(GlobalScriptFilterRequest.JDBC.name()); - boolean globalPostScriptIsFilter = postFilters.contains(GlobalScriptFilterRequest.JDBC.name()); - - if (!isPreScriptExecAfterPrivateScript && !globalPreScriptIsFilter) { - this.addItemHashTree(preProcessor, samplerHashTree, config); - } - if (!isPostScriptExecAfterPrivateScript && !globalPostScriptIsFilter) { - this.addItemHashTree(postProcessor, samplerHashTree, config); - } - - if (isPreScriptExecAfterPrivateScript && !globalPreScriptIsFilter) { - this.addItemHashTree(preProcessor, samplerHashTree, config); - } - if (isPostScriptExecAfterPrivateScript && !globalPostScriptIsFilter) { - this.addItemHashTree(postProcessor, samplerHashTree, config); - } + //处理全局前后置脚本(步骤内) + String enviromentId = this.getEnvironmentId(); + if (enviromentId == null) { + enviromentId = this.useEnvironment; } + //根据配置将脚本放置在私有脚本之前 + JMeterScriptUtil.setScript(envConfig, samplerHashTree, GlobalScriptFilterRequest.JDBC.name(), enviromentId, config, false); + if (CollectionUtils.isNotEmpty(hashTree)) { hashTree.forEach(el -> { el.toHashTree(samplerHashTree, el.getHashTree(), config); }); } + //根据配置将脚本放置在私有脚本之后 + JMeterScriptUtil.setScript(envConfig, samplerHashTree, GlobalScriptFilterRequest.JDBC.name(), enviromentId, config, true); } - private void addItemHashTree(MsTestElement element, HashTree samplerHashTree, ParameterConfig config) { - if (element != null && element.getEnvironmentId() == null) { - if (this.getEnvironmentId() == null) { - element.setEnvironmentId(useEnvironment); - } else { - element.setEnvironmentId(this.getEnvironmentId()); - } - element.toHashTree(samplerHashTree, element.getHashTree(), config); - } - } - /** * 环境通用变量 */ 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 2ebc6111fc..c2a7ad5cab 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 @@ -9,13 +9,12 @@ import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import io.metersphere.api.dto.automation.EsbDataStruct; import io.metersphere.api.dto.automation.TcpTreeTableDataStruct; +import io.metersphere.api.dto.definition.parse.JMeterScriptUtil; import io.metersphere.api.dto.definition.request.ElementUtil; import io.metersphere.api.dto.definition.request.ParameterConfig; -import io.metersphere.api.dto.definition.request.processors.post.MsJSR223PostProcessor; import io.metersphere.api.dto.definition.request.processors.pre.MsJSR223PreProcessor; import io.metersphere.api.dto.scenario.KeyValue; import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; -import io.metersphere.api.dto.scenario.environment.GlobalScriptConfig; import io.metersphere.api.dto.scenario.environment.GlobalScriptFilterRequest; import io.metersphere.api.service.ApiDefinitionService; import io.metersphere.api.service.ApiTestCaseService; @@ -151,50 +150,25 @@ public class MsTCPSampler extends MsTestElement { samplerHashTree.add(tcpPreProcessor.getJSR223PreProcessor()); } - MsJSR223PreProcessor preProcessor = null; - MsJSR223PostProcessor postProcessor = null; - GlobalScriptConfig globalScriptConfig = null; - if(envConfig != null){ - preProcessor = envConfig.getPreProcessor(); - postProcessor = envConfig.getPostProcessor(); - globalScriptConfig = envConfig.getGlobalScriptConfig(); - } - - boolean isPreScriptExecAfterPrivateScript = globalScriptConfig == null? false : globalScriptConfig.isPreScriptExecAfterPrivateScript(); - boolean isPostScriptExecAfterPrivateScript = globalScriptConfig == null? false : globalScriptConfig.isPostScriptExecAfterPrivateScript(); - boolean globalPreScriptIsFilter = false; - boolean globalPostScriptIsFilter = false; - List preFilterProtocal = globalScriptConfig == null? new ArrayList<>() : globalScriptConfig.getFilterRequestPreScript(); - List postFilterProtocal = globalScriptConfig == null? new ArrayList<>() : globalScriptConfig.getFilterRequestPostScript(); - if(preFilterProtocal.contains(GlobalScriptFilterRequest.TCP.name())){ - globalPreScriptIsFilter = true; - } - if(postFilterProtocal.contains(GlobalScriptFilterRequest.TCP.name())){ - globalPostScriptIsFilter = true; - } - - if(!isPreScriptExecAfterPrivateScript && !globalPreScriptIsFilter){ - this.addItemHashTree(preProcessor,samplerHashTree,config); - } - if(!isPostScriptExecAfterPrivateScript && !globalPostScriptIsFilter){ - this.addItemHashTree(postProcessor,samplerHashTree,config); + //处理全局前后置脚本(步骤内) + String enviromentId = this.getEnvironmentId(); + if (enviromentId == null) { + enviromentId = this.useEnvironment; } + //根据配置将脚本放置在私有脚本之前 + JMeterScriptUtil.setScript(envConfig, samplerHashTree, GlobalScriptFilterRequest.TCP.name(), enviromentId, config, false); if (CollectionUtils.isNotEmpty(hashTree)) { hashTree.forEach(el -> { el.toHashTree(samplerHashTree, el.getHashTree(), config); }); } - - if(isPreScriptExecAfterPrivateScript && !globalPreScriptIsFilter){ - this.addItemHashTree(preProcessor,samplerHashTree,config); - } - if(isPostScriptExecAfterPrivateScript && !globalPostScriptIsFilter){ - this.addItemHashTree(postProcessor,samplerHashTree,config); - } + //根据配置将脚本放置在私有脚本之后 + JMeterScriptUtil.setScript(envConfig, samplerHashTree, GlobalScriptFilterRequest.TCP.name(), enviromentId, config, false); } - private void addItemHashTree(MsTestElement element, HashTree samplerHashTree,ParameterConfig config){ - if(element != null){ + + private void addItemHashTree(MsTestElement element, HashTree samplerHashTree, ParameterConfig config) { + if (element != null) { if (element.getEnvironmentId() == null) { if (this.getEnvironmentId() == null) { element.setEnvironmentId(useEnvironment); @@ -205,6 +179,7 @@ public class MsTCPSampler extends MsTestElement { element.toHashTree(samplerHashTree, element.getHashTree(), config); } } + private void setRefElement() { try { ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class); @@ -234,7 +209,7 @@ public class MsTCPSampler extends MsTestElement { if (proxy != null) { if (StringUtils.equals(this.getRefType(), "CASE")) { ElementUtil.mergeHashTree(this, proxy.getHashTree()); - }else { + } else { this.setHashTree(proxy.getHashTree()); } this.setClassname(proxy.getClassname()); diff --git a/frontend/src/business/components/api/definition/components/case/ApiCaseItem.vue b/frontend/src/business/components/api/definition/components/case/ApiCaseItem.vue index 70a572e95a..d7fd938142 100644 --- a/frontend/src/business/components/api/definition/components/case/ApiCaseItem.vue +++ b/frontend/src/business/components/api/definition/components/case/ApiCaseItem.vue @@ -124,7 +124,7 @@ - +