fix(SQL用例执行): 修复SQL用例执行时自动添加script空脚本的问题

--bug=1008370 --user=宋天阳 【接口测试】创建sql用例,有一个步骤,但是步骤里啥都没有,执行的时候还会报错
https://www.tapd.cn/55049933/s/1075620
This commit is contained in:
song-tianyang 2021-11-26 16:44:35 +08:00 committed by song-tianyang
parent 7768845465
commit 454d84037c
5 changed files with 138 additions and 100 deletions

View File

@ -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<String> 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<String> preFilterProtocal = globalScriptConfig == null ? null : globalScriptConfig.getFilterRequestPreScript();
List<String> 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);
}
}
}

View File

@ -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<String> filterPreProtocal = globalScriptConfig == null ? new ArrayList<>() : globalScriptConfig.getFilterRequestPreScript();
List<String> filterPostProtocal = globalScriptConfig == null ? new ArrayList<>() : globalScriptConfig.getFilterRequestPostScript();
boolean filterPre = filterPreProtocal.contains(GlobalScriptFilterRequest.HTTP.name());
boolean filterPost = filterPostProtocal.contains(GlobalScriptFilterRequest.HTTP.name());
List<String> filterPreProtocal = globalScriptConfig == null ? null : globalScriptConfig.getFilterRequestPreScript();
List<String> 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();

View File

@ -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;
@ -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<String> preFilters = globalScriptConfig == null ? new ArrayList<>() : globalScriptConfig.getFilterRequestPreScript();
List<String> postFilters = globalScriptConfig == null ? new ArrayList<>() : globalScriptConfig.getFilterRequestPostScript();
boolean globalPreScriptIsFilter = preFilters.contains(GlobalScriptFilterRequest.JDBC.name());
boolean globalPostScriptIsFilter = postFilters.contains(GlobalScriptFilterRequest.JDBC.name());
//处理全局前后置脚本(步骤内)
String enviromentId = this.getEnvironmentId();
if (enviromentId == null) {
enviromentId = this.useEnvironment;
}
//根据配置将脚本放置在私有脚本之前
JMeterScriptUtil.setScript(envConfig, samplerHashTree, GlobalScriptFilterRequest.JDBC.name(), enviromentId, config, false);
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);
});
}
//根据配置将脚本放置在私有脚本之后
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);
}
}
/**
* 环境通用变量
*/

View File

@ -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,48 +150,23 @@ 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<String> preFilterProtocal = globalScriptConfig == null? new ArrayList<>() : globalScriptConfig.getFilterRequestPreScript();
List<String> 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);
});
}
//根据配置将脚本放置在私有脚本之后
JMeterScriptUtil.setScript(envConfig, samplerHashTree, GlobalScriptFilterRequest.TCP.name(), enviromentId, config, false);
}
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) {
@ -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);

View File

@ -124,7 +124,7 @@
<api-response-component :currentProtocol="apiCase.request.protocol" :api-item="apiCase" :result="runResult"/>
</div>
<ms-jmx-step :request="apiCase.request" :api-id="api.id" :response="apiCase.responseData"/>
<ms-jmx-step v-if="apiCase.request.hashTree && apiCase.request.hashTree.length > 0" :request="apiCase.request" :api-id="api.id" :response="apiCase.responseData"/>
<!-- 保存操作 -->
<el-button type="primary" size="small" style="margin: 20px; float: right" @click="saveTestCase(apiCase)"
v-if="type!=='detail'"