From 1e719d12940b1b8439c58ef33325002dd10f8298 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Wed, 24 Nov 2021 14:40:19 +0800 Subject: [PATCH] =?UTF-8?q?fix=20(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=20=E4=BF=AE=E5=A4=8DJDBC=E8=AF=B7=E6=B1=82=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=8F=98=E9=87=8F=E4=B8=8D=E7=94=9F=E6=95=88=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1008311 --user=赵勇 【github#8016】添加sql自定义请求-》未保存状态下,无法引用环境里配置的全局变量 https://www.tapd.cn/55049933/s/1074137 --- .../request/sampler/MsJDBCSampler.java | 104 ++++++++++-------- 1 file changed, 61 insertions(+), 43 deletions(-) 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 7148d353f2..82cf1125ec 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 @@ -84,14 +84,14 @@ public class MsJDBCSampler extends MsTestElement { public void toHashTree(HashTree tree, List hashTree, MsParameter msParameter) { ParameterConfig config = (ParameterConfig) msParameter; // 非导出操作,且不是启用状态则跳过执行 - if (!config.isOperating() && !this.isEnable()) { + if (config != null && !config.isOperating() && !this.isEnable()) { return; } if (this.getReferenced() != null && MsTestElementConstants.REF.name().equals(this.getReferenced())) { this.setRefElement(); hashTree = this.getHashTree(); } - if (config.getConfig() == null) { + if (config != null && config.getConfig() == null) { // 单独接口执行 this.setProjectId(config.getProjectId()); config.setConfig(ElementUtil.getEnvironmentConfig(StringUtils.isNotEmpty(useEnvironment) ? useEnvironment : environmentId, this.getProjectId(), this.isMockEnvironment())); @@ -100,7 +100,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 { @@ -153,62 +153,80 @@ public class MsJDBCSampler extends MsTestElement { tree.add(arguments); } - 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.JDBC.name())){ - globalPreScriptIsFilter = true; - } - if(postFilterProtocal.contains(GlobalScriptFilterRequest.JDBC.name())){ - globalPostScriptIsFilter = true; + // 环境通用请求头 + Arguments envArguments = getConfigArguments(config); + if (envArguments != null) { + tree.add(envArguments); } - if(!isPreScriptExecAfterPrivateScript && !globalPreScriptIsFilter){ - this.addItemHashTree(preProcessor,samplerHashTree,config); - } - if(!isPostScriptExecAfterPrivateScript && !globalPostScriptIsFilter){ - this.addItemHashTree(postProcessor,samplerHashTree,config); - } + 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); + } + } 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); - } - } - 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()); - } + 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); } } + /** + * 环境通用变量 + */ + private Arguments getConfigArguments(ParameterConfig config) { + Arguments arguments = new Arguments(); + arguments.setEnabled(true); + arguments.setName(StringUtils.isNotEmpty(this.getName()) ? this.getName() : "Arguments"); + arguments.setProperty(TestElement.TEST_CLASS, Arguments.class.getName()); + arguments.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("ArgumentsPanel")); + // 环境通用变量 + if (config.isEffective(this.getProjectId()) && config.getConfig().get(this.getProjectId()).getCommonConfig() != null + && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getCommonConfig().getVariables())) { + config.getConfig().get(this.getProjectId()).getCommonConfig().getVariables().stream().filter(KeyValue::isValid).filter(KeyValue::isEnable).forEach(keyValue -> + arguments.addArgument(keyValue.getName(), keyValue.getValue(), "=") + ); + // 清空变量,防止重复添加 + config.getConfig().get(this.getProjectId()).getCommonConfig().getVariables().clear(); + } + if (arguments.getArguments() != null && arguments.getArguments().size() > 0) { + return arguments; + } + return null; + } + private boolean isDataSource(List databaseConfigs) { List ids = databaseConfigs.stream().map(DatabaseConfig::getId).collect(Collectors.toList()); if (StringUtils.isNotEmpty(this.dataSourceId) && ids.contains(this.dataSourceId)) {