refactor(接口测试): Sampler相关代码优化

This commit is contained in:
fit2-zhao 2022-01-21 15:08:43 +08:00 committed by fit2-zhao
parent 70faafe07a
commit 0efab80872
12 changed files with 254 additions and 360 deletions

View File

@ -3,8 +3,10 @@ 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.HttpConfig;
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.plugin.core.MsTestElement;
import org.apache.commons.lang3.StringUtils;
import org.apache.jorphan.collections.HashTree;
@ -83,10 +85,48 @@ public class JMeterScriptUtil {
}
}
private static void addItemHashTree(MsTestElement element, HashTree samplerHashTree, ParameterConfig config, String enviromentId) {
private static void addItemHashTree(MsTestElement element, HashTree samplerHashTree, ParameterConfig config, String environmentId) {
if (element != null) {
element.setEnvironmentId(element.getEnvironmentId() == null ? enviromentId : element.getEnvironmentId());
element.setEnvironmentId(element.getEnvironmentId() == null ? environmentId : element.getEnvironmentId());
element.toHashTree(samplerHashTree, element.getHashTree(), config);
}
}
public static void setHttpScript(HttpConfig httpConfig, HashTree httpSamplerTree, ParameterConfig config, String useEnvironment, String environmentId) {
MsJSR223PreProcessor preProcessor = httpConfig.getPreProcessor();
MsJSR223PostProcessor postProcessor = httpConfig.getPostProcessor();
GlobalScriptConfig globalScriptConfig = httpConfig.getGlobalScriptConfig();
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();
if (!filterPre && preProcessor != null && StringUtils.isNotEmpty(preProcessor.getScript())) {
if ((isPreScriptExecAfterPrivateScript) || (!isPreScriptExecAfterPrivateScript)) {
if (preProcessor.getEnvironmentId() == null) {
if (environmentId == null) {
preProcessor.setEnvironmentId(useEnvironment);
} else {
preProcessor.setEnvironmentId(useEnvironment);
}
}
preProcessor.toHashTree(httpSamplerTree, preProcessor.getHashTree(), config);
}
}
if (!filterPost && postProcessor != null && StringUtils.isNotEmpty(postProcessor.getScript())) {
if ((isPostScriptExecAfterPrivateScript) || (!isPostScriptExecAfterPrivateScript)) {
if (postProcessor.getEnvironmentId() == null) {
if (environmentId == null) {
postProcessor.setEnvironmentId(useEnvironment);
} else {
postProcessor.setEnvironmentId(environmentId);
}
}
postProcessor.toHashTree(httpSamplerTree, postProcessor.getHashTree(), config);
}
}
}
}

View File

@ -28,6 +28,7 @@ import io.metersphere.constants.RunModeConstants;
import io.metersphere.plugin.core.MsParameter;
import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.service.EnvironmentGroupProjectService;
import io.metersphere.utils.LoggerUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.config.Arguments;
@ -36,6 +37,7 @@ import org.apache.jmeter.config.RandomVariableConfig;
import org.apache.jmeter.modifiers.CounterConfig;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.AbstractTestElement;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree;
@ -181,16 +183,6 @@ public class ElementUtil {
return getFullPath(element.getParent(), path);
}
public static void getScenarioSet(MsTestElement element, List<String> id_names) {
if (StringUtils.equals(element.getType(), "scenario")) {
id_names.add(element.getResourceId() + "_" + element.getName());
}
if (element.getParent() == null) {
return;
}
getScenarioSet(element.getParent(), id_names);
}
public static String getParentName(MsTestElement parent) {
if (parent != null) {
// 获取全路径以备后面使用
@ -581,4 +573,10 @@ public class ElementUtil {
}
return resourceId + "_" + ElementUtil.getFullIndexPath(parent, indexPath);
}
public static void setBaseParams(AbstractTestElement sampler, MsTestElement parent, ParameterConfig config, String id, String indexPath) {
sampler.setProperty("MS-ID", id);
sampler.setProperty("MS-RESOURCE-ID", ElementUtil.getResourceId(id, config, parent, indexPath));
LoggerUtil.debug("mqtt sampler resourceId :" + sampler.getPropertyAsString("MS-RESOURCE-ID"));
}
}

View File

@ -1,18 +1,30 @@
package io.metersphere.api.dto.definition.request;
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
import io.metersphere.api.dto.definition.request.variable.ScenarioVariable;
import io.metersphere.api.dto.scenario.HttpConfig;
import io.metersphere.api.dto.scenario.HttpConfigCondition;
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import io.metersphere.api.dto.ssl.MsKeyStore;
import io.metersphere.api.service.ApiDefinitionService;
import io.metersphere.api.service.ApiTestCaseService;
import io.metersphere.base.domain.ApiDefinition;
import io.metersphere.base.domain.ApiTestCaseWithBLOBs;
import io.metersphere.base.domain.TestPlanApiCase;
import io.metersphere.commons.constants.ConditionType;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.constants.RunModeConstants;
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 lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.config.Arguments;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
@Data
public class ParameterConfig extends MsParameter {
@ -74,4 +86,108 @@ public class ParameterConfig extends MsParameter {
return arguments;
}
public HttpConfig matchConfig(MsHTTPSamplerProxy samplerProxy) {
HttpConfig httpConfig = this.getConfig().get(samplerProxy.getProjectId()).getHttpConfig();
boolean isNext = true;
if (CollectionUtils.isNotEmpty(httpConfig.getConditions())) {
for (HttpConfigCondition item : httpConfig.getConditions()) {
if (item.getType().equals(ConditionType.PATH.name())) {
HttpConfig config = httpConfig.getPathCondition(samplerProxy.getPath(), item);
if (config != null) {
isNext = false;
httpConfig = config;
break;
}
} else if (item.getType().equals(ConditionType.MODULE.name())) {
ApiDefinition apiDefinition = null;
ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class);
ApiTestCaseService apiTestCaseService = CommonBeanFactory.getBean(ApiTestCaseService.class);
if (StringUtils.isNotEmpty(samplerProxy.getRefType()) && samplerProxy.getRefType().equals("CASE")) {
ApiTestCaseWithBLOBs caseWithBLOBs = apiTestCaseService.get(samplerProxy.getId());
if (caseWithBLOBs != null) {
apiDefinition = apiDefinitionService.get(caseWithBLOBs.getApiDefinitionId());
}
} else {
apiDefinition = apiDefinitionService.get(samplerProxy.getId());
ApiTestCaseWithBLOBs apiTestCaseWithBLOBs = apiTestCaseService.get(samplerProxy.getId());
if (apiTestCaseWithBLOBs == null) {
apiTestCaseWithBLOBs = apiTestCaseService.get(samplerProxy.getName());
}
if (apiTestCaseWithBLOBs != null) {
apiDefinition = apiDefinitionService.get(apiTestCaseWithBLOBs.getApiDefinitionId());
} else {
TestPlanApiCaseService testPlanApiCaseService = CommonBeanFactory.getBean(TestPlanApiCaseService.class);
TestPlanApiCase testPlanApiCase = testPlanApiCaseService.getById(samplerProxy.getId());
if (testPlanApiCase != null) {
ApiTestCaseWithBLOBs caseWithBLOBs = apiTestCaseService.get(testPlanApiCase.getApiCaseId());
if (caseWithBLOBs != null) {
apiDefinition = apiDefinitionService.get(caseWithBLOBs.getApiDefinitionId());
}
}
}
}
if (apiDefinition != null) {
HttpConfig config = httpConfig.getModuleCondition(apiDefinition.getModuleId(), item);
if (config != null) {
isNext = false;
httpConfig = config;
break;
}
}
}
}
if (isNext) {
for (HttpConfigCondition item : httpConfig.getConditions()) {
if (item.getType().equals(ConditionType.NONE.name())) {
httpConfig = httpConfig.initHttpConfig(item);
break;
}
}
}
}
return httpConfig;
}
private String getParentProjectId(MsHTTPSamplerProxy samplerProxy) {
MsTestElement parent = samplerProxy.getParent();
while (parent != null) {
if (StringUtils.isNotBlank(parent.getProjectId())) {
return parent.getProjectId();
}
parent = parent.getParent();
}
return "";
}
public void compatible(MsHTTPSamplerProxy samplerProxy) {
if (samplerProxy.isCustomizeReq() && samplerProxy.getIsRefEnvironment() == null) {
if (StringUtils.isNotBlank(samplerProxy.getUrl())) {
samplerProxy.setIsRefEnvironment(false);
} else {
samplerProxy.setIsRefEnvironment(true);
}
}
// 数据兼容处理
if (this.getConfig() != null && StringUtils.isNotEmpty(samplerProxy.getProjectId()) && this.getConfig().containsKey(samplerProxy.getProjectId())) {
// 1.8 之后 当前正常数据
} else if (this.getConfig() != null && this.getConfig().containsKey(getParentProjectId(samplerProxy))) {
// 1.8 前后 混合数据
samplerProxy.setProjectId(getParentProjectId(samplerProxy));
} else {
// 1.8 之前 数据
if (this.getConfig() != null) {
if (!this.getConfig().containsKey(RunModeConstants.HIS_PRO_ID.toString())) {
// 测试计划执行
Iterator<String> it = this.getConfig().keySet().iterator();
if (it.hasNext()) {
samplerProxy.setProjectId(it.next());
}
} else {
samplerProxy.setProjectId(RunModeConstants.HIS_PRO_ID.toString());
}
}
}
}
}

View File

@ -1,6 +1,5 @@
package io.metersphere.api.dto.definition.request.processors;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.annotation.JSONType;
import io.metersphere.api.dto.RunningParamKeys;
@ -20,7 +19,6 @@ import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
@Data
@ -78,13 +76,7 @@ public class MsJSR223Processor extends MsTestElement {
} else {
processor.setName("JSR223Processor");
}
processor.setProperty("MS-ID", this.getId());
String indexPath = this.getIndex();
processor.setProperty("MS-RESOURCE-ID", ElementUtil.getResourceId(this.getResourceId(), config, this.getParent(), indexPath));
List<String> id_names = new LinkedList<>();
ElementUtil.getScenarioSet(this, id_names);
processor.setProperty("MS-SCENARIO", JSON.toJSONString(id_names));
ElementUtil.setBaseParams(processor, this.getParent(), config, this.getId(), this.getIndex());
processor.setProperty(TestElement.TEST_CLASS, JSR223Sampler.class.getName());
processor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
processor.setProperty("scriptLanguage", this.getScriptLanguage());

View File

@ -37,7 +37,6 @@ import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
@ -245,13 +244,8 @@ public class MsJDBCPostProcessor extends MsTestElement {
jdbcPostProcessor.setName(this.getName() == null? "JDBCPostProcessor" : this.getName());
jdbcPostProcessor.setProperty(TestElement.TEST_CLASS, JDBCPostProcessor.class.getName());
jdbcPostProcessor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
jdbcPostProcessor.setProperty("MS-ID", this.getId());
String indexPath = this.getIndex();
jdbcPostProcessor.setProperty("MS-RESOURCE-ID", this.getResourceId() + "_" + ElementUtil.getFullIndexPath(this.getParent(), indexPath));
List<String> id_names = new LinkedList<>();
ElementUtil.getScenarioSet(this, id_names);
jdbcPostProcessor.setProperty("MS-SCENARIO", JSON.toJSONString(id_names));
ElementUtil.setBaseParams(jdbcPostProcessor, this.getParent(), config, this.getId(), this.getIndex());
// request.getDataSource() 是ID需要转换为Name
jdbcPostProcessor.setProperty("dataSource", this.dataSource.getName());
jdbcPostProcessor.setProperty("query", this.getQuery());

View File

@ -37,7 +37,6 @@ import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
@ -245,14 +244,9 @@ public class MsJDBCPreProcessor extends MsTestElement {
jdbcPreProcessor.setName(this.getName() == null? "JDBCPreProcessor" : this.getName());
jdbcPreProcessor.setProperty(TestElement.TEST_CLASS, JDBCPreProcessor.class.getName());
jdbcPreProcessor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
jdbcPreProcessor.setProperty("MS-ID", this.getId());
String indexPath = this.getIndex();
jdbcPreProcessor.setProperty("MS-RESOURCE-ID", this.getResourceId() + "_" + ElementUtil.getFullIndexPath(this.getParent(), indexPath));
List<String> id_names = new LinkedList<>();
ElementUtil.getScenarioSet(this, id_names);
jdbcPreProcessor.setProperty("MS-SCENARIO", JSON.toJSONString(id_names));
// request.getDataSource() 是ID需要转换为Name
ElementUtil.setBaseParams(jdbcPreProcessor, this.getParent(), config, this.getId(), this.getIndex());
jdbcPreProcessor.setProperty("dataSource", this.dataSource.getName());
jdbcPreProcessor.setProperty("query", this.getQuery());
jdbcPreProcessor.setProperty("queryTimeout", String.valueOf(this.getQueryTimeout()));

View File

@ -36,7 +36,6 @@ import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
@ -156,22 +155,17 @@ public class MsDubboSampler extends MsTestElement {
DubboSample sampler = new DubboSample();
sampler.setEnabled(this.isEnable());
sampler.setName(this.getName());
if(config.isOperating()){
if (config.isOperating()) {
String[] testNameArr = sampler.getName().split("<->");
if (testNameArr.length > 0) {
String testName = testNameArr[0];
sampler.setName(testName);
}
}else {
} else {
sampler.setProperty(TestElement.TEST_CLASS, DubboSample.class.getName());
sampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("DubboSampleGui"));
}
sampler.setProperty("MS-ID", this.getId());
String indexPath = this.getIndex();
sampler.setProperty("MS-RESOURCE-ID", ElementUtil.getResourceId(this.getId(), config, this.getParent(), indexPath));
List<String> id_names = new LinkedList<>();
ElementUtil.getScenarioSet(this, id_names);
sampler.setProperty("MS-SCENARIO", JSON.toJSONString(id_names));
ElementUtil.setBaseParams(sampler, this.getParent(), config, this.getId(), this.getIndex());
sampler.addTestElement(configCenter(this.getConfigCenter()));
sampler.addTestElement(registryCenter(this.getRegistryCenter()));
sampler.addTestElement(consumerAndService(this.getConsumerAndService()));

View File

@ -13,38 +13,28 @@ import io.metersphere.api.dto.definition.request.ParameterConfig;
import io.metersphere.api.dto.definition.request.assertions.MsAssertions;
import io.metersphere.api.dto.definition.request.auth.MsAuthManager;
import io.metersphere.api.dto.definition.request.dns.MsDNSCacheManager;
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.Body;
import io.metersphere.api.dto.scenario.HttpConfig;
import io.metersphere.api.dto.scenario.HttpConfigCondition;
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.dto.scenario.environment.CommonConfig;
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.dto.ssl.KeyStoreConfig;
import io.metersphere.api.dto.ssl.KeyStoreFile;
import io.metersphere.api.dto.ssl.MsKeyStore;
import io.metersphere.api.service.ApiDefinitionService;
import io.metersphere.api.service.ApiTestCaseService;
import io.metersphere.api.service.CommandService;
import io.metersphere.base.domain.ApiDefinition;
import io.metersphere.base.domain.ApiDefinitionWithBLOBs;
import io.metersphere.base.domain.ApiTestCaseWithBLOBs;
import io.metersphere.base.domain.TestPlanApiCase;
import io.metersphere.commons.constants.ConditionType;
import io.metersphere.commons.constants.MsTestElementConstants;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.FileUtils;
import io.metersphere.commons.utils.HashTreeUtil;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.constants.RunModeConstants;
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;
@ -64,7 +54,10 @@ import org.apache.jorphan.collections.HashTree;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@ -134,52 +127,6 @@ public class MsHTTPSamplerProxy extends MsTestElement {
@JSONField(ordinal = 39)
private boolean customizeReq;
private boolean setRefElement() {
try {
ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
MsHTTPSamplerProxy proxy = null;
if (StringUtils.equals(this.getRefType(), "CASE")) {
ApiTestCaseService apiTestCaseService = CommonBeanFactory.getBean(ApiTestCaseService.class);
ApiTestCaseWithBLOBs bloBs = apiTestCaseService.get(this.getId());
if (bloBs != null) {
this.setProjectId(bloBs.getProjectId());
JSONObject element = JSON.parseObject(bloBs.getRequest());
ElementUtil.dataFormatting(element);
proxy = mapper.readValue(element.toJSONString(), new TypeReference<MsHTTPSamplerProxy>() {
});
this.setName(bloBs.getName());
}
} else {
ApiDefinitionWithBLOBs apiDefinition = apiDefinitionService.getBLOBs(this.getId());
if (apiDefinition != null) {
this.setName(apiDefinition.getName());
this.setProjectId(apiDefinition.getProjectId());
proxy = mapper.readValue(apiDefinition.getRequest(), new TypeReference<MsHTTPSamplerProxy>() {
});
}
}
if (proxy != null) {
if (StringUtils.equals(this.getRefType(), "CASE")) {
ElementUtil.mergeHashTree(this, proxy.getHashTree());
} else {
this.setHashTree(proxy.getHashTree());
}
this.setPath(proxy.getPath());
this.setMethod(proxy.getMethod());
this.setBody(proxy.getBody());
this.setRest(proxy.getRest());
this.setArguments(proxy.getArguments());
this.setHeaders(proxy.getHeaders());
return true;
}
} catch (Exception ex) {
LogUtil.error(ex);
}
return false;
}
@Override
public void toHashTree(HashTree tree, List<MsTestElement> hashTree, MsParameter msParameter) {
ParameterConfig config = (ParameterConfig) msParameter;
@ -204,7 +151,7 @@ public class MsHTTPSamplerProxy extends MsTestElement {
if (StringUtils.isEmpty(this.getName())) {
sampler.setName("HTTPSamplerProxy");
}
if(config.isOperating()){
if (config.isOperating()) {
String[] testNameArr = sampler.getName().split("<->");
if (testNameArr.length > 0) {
String testName = testNameArr[0];
@ -213,13 +160,7 @@ public class MsHTTPSamplerProxy extends MsTestElement {
}
sampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
sampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("HttpTestSampleGui"));
sampler.setProperty("MS-ID", this.getId());
String indexPath = this.getIndex();
sampler.setProperty("MS-RESOURCE-ID", ElementUtil.getResourceId(this.getId(), config, this.getParent(), indexPath));
List<String> id_names = new LinkedList<>();
ElementUtil.getScenarioSet(this, id_names);
sampler.setProperty("MS-SCENARIO", JSON.toJSONString(id_names));
ElementUtil.setBaseParams(sampler, this.getParent(), config, this.getId(), this.getIndex());
sampler.setMethod(this.getMethod());
sampler.setContentEncoding("UTF-8");
sampler.setFollowRedirects(this.isFollowRedirects());
@ -231,7 +172,7 @@ public class MsHTTPSamplerProxy extends MsTestElement {
config.setConfig(ElementUtil.getEnvironmentConfig(this.useEnvironment, this.getProjectId(), this.isMockEnvironment()));
}
compatible(config);
config.compatible(this);
this.initConnectAndResponseTimeout(config);
sampler.setConnectTimeout(this.getConnectTimeout() == null ? "60000" : this.getConnectTimeout());
@ -239,7 +180,6 @@ public class MsHTTPSamplerProxy extends MsTestElement {
HttpConfig httpConfig = getHttpConfig(config);
setSamplerPath(config, httpConfig, sampler);
// 请求体处理
@ -291,14 +231,10 @@ public class MsHTTPSamplerProxy extends MsTestElement {
addCertificate(config, httpSamplerTree);
//根据配置增加全局前后至脚本
if (httpConfig != null) {
this.setScript(httpConfig, httpSamplerTree, config, false);
}
HashTreeUtil hashTreeUtil = new HashTreeUtil();
//增加误报断言
if (httpConfig != null) {
//根据配置增加全局前后至脚本
JMeterScriptUtil.setHttpScript(httpConfig, httpSamplerTree, config, useEnvironment, this.getEnvironmentId());
//增加误报断言
if (CollectionUtils.isNotEmpty(httpConfig.getErrorReportAssertions())) {
for (MsAssertions assertion : httpConfig.getErrorReportAssertions()) {
assertion.toHashTree(httpSamplerTree, assertion.getHashTree(), config);
@ -319,56 +255,60 @@ public class MsHTTPSamplerProxy extends MsTestElement {
el.setEnvironmentId(this.getEnvironmentId());
}
}
if (el instanceof MsAssertions) {
//断言设置需要和全局断言误报进行去重
el = hashTreeUtil.duplicateRegexInAssertions(httpConfig.getAssertions(), (MsAssertions) el);
}
el.toHashTree(httpSamplerTree, el.getHashTree(), config);
}
}
//根据配置增加全局前后至脚本
if (httpConfig != null) {
this.setScript(httpConfig, httpSamplerTree, config, true);
JMeterScriptUtil.setHttpScript(httpConfig, httpSamplerTree, config, useEnvironment, this.getEnvironmentId());
}
}
private void setScript(HttpConfig httpConfig, HashTree httpSamplerTree, ParameterConfig config, boolean isAfterPrivateScript) {
MsJSR223PreProcessor preProcessor = httpConfig.getPreProcessor();
MsJSR223PostProcessor postProcessor = httpConfig.getPostProcessor();
GlobalScriptConfig globalScriptConfig = httpConfig.getGlobalScriptConfig();
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();
if (!filterPre && preProcessor != null && StringUtils.isNotEmpty(preProcessor.getScript())) {
if ((isPreScriptExecAfterPrivateScript && isAfterPrivateScript) || (!isPreScriptExecAfterPrivateScript && !isAfterPrivateScript)) {
if (preProcessor.getEnvironmentId() == null) {
if (this.getEnvironmentId() == null) {
preProcessor.setEnvironmentId(useEnvironment);
} else {
preProcessor.setEnvironmentId(this.getEnvironmentId());
}
private boolean setRefElement() {
try {
ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
MsHTTPSamplerProxy proxy = null;
if (StringUtils.equals(this.getRefType(), "CASE")) {
ApiTestCaseService apiTestCaseService = CommonBeanFactory.getBean(ApiTestCaseService.class);
ApiTestCaseWithBLOBs bloBs = apiTestCaseService.get(this.getId());
if (bloBs != null) {
this.setProjectId(bloBs.getProjectId());
JSONObject element = JSON.parseObject(bloBs.getRequest());
ElementUtil.dataFormatting(element);
proxy = mapper.readValue(element.toJSONString(), new TypeReference<MsHTTPSamplerProxy>() {
});
this.setName(bloBs.getName());
}
preProcessor.toHashTree(httpSamplerTree, preProcessor.getHashTree(), config);
}
}
if (!filterPost && postProcessor != null && StringUtils.isNotEmpty(postProcessor.getScript())) {
if ((isPostScriptExecAfterPrivateScript && isAfterPrivateScript) || (!isPostScriptExecAfterPrivateScript && !isAfterPrivateScript)) {
if (postProcessor.getEnvironmentId() == null) {
if (this.getEnvironmentId() == null) {
postProcessor.setEnvironmentId(useEnvironment);
} else {
postProcessor.setEnvironmentId(this.getEnvironmentId());
}
} else {
ApiDefinitionWithBLOBs apiDefinition = apiDefinitionService.getBLOBs(this.getId());
if (apiDefinition != null) {
this.setName(apiDefinition.getName());
this.setProjectId(apiDefinition.getProjectId());
proxy = mapper.readValue(apiDefinition.getRequest(), new TypeReference<MsHTTPSamplerProxy>() {
});
}
postProcessor.toHashTree(httpSamplerTree, postProcessor.getHashTree(), config);
}
if (proxy != null) {
if (StringUtils.equals(this.getRefType(), "CASE")) {
ElementUtil.mergeHashTree(this, proxy.getHashTree());
} else {
this.setHashTree(proxy.getHashTree());
}
this.setPath(proxy.getPath());
this.setMethod(proxy.getMethod());
this.setBody(proxy.getBody());
this.setRest(proxy.getRest());
this.setArguments(proxy.getArguments());
this.setHeaders(proxy.getHeaders());
return true;
}
} catch (Exception ex) {
LogUtil.error(ex);
}
return false;
}
private void initConnectAndResponseTimeout(ParameterConfig config) {
@ -416,7 +356,7 @@ public class MsHTTPSamplerProxy extends MsTestElement {
if (StringUtils.isNotEmpty(useEvnId) && !StringUtils.equals(useEvnId, this.getEnvironmentId())) {
this.setEnvironmentId(useEvnId);
}
HttpConfig httpConfig = matchConfig(config);
HttpConfig httpConfig = config.matchConfig(this);
httpConfig.setPreProcessor(environmentConfig.getPreProcessor());
httpConfig.setPostProcessor(environmentConfig.getPostProcessor());
httpConfig.setGlobalScriptConfig(environmentConfig.getGlobalScriptConfig());
@ -630,38 +570,6 @@ public class MsHTTPSamplerProxy extends MsTestElement {
return false;
}
// 兼容旧数据
private void compatible(ParameterConfig config) {
if (this.isCustomizeReq() && this.isRefEnvironment == null) {
if (StringUtils.isNotBlank(this.url)) {
this.isRefEnvironment = false;
} else {
this.isRefEnvironment = true;
}
}
// 数据兼容处理
if (config.getConfig() != null && StringUtils.isNotEmpty(this.getProjectId()) && config.getConfig().containsKey(this.getProjectId())) {
// 1.8 之后 当前正常数据
} else if (config.getConfig() != null && config.getConfig().containsKey(getParentProjectId())) {
// 1.8 前后 混合数据
this.setProjectId(getParentProjectId());
} else {
// 1.8 之前 数据
if (config.getConfig() != null) {
if (!config.getConfig().containsKey(RunModeConstants.HIS_PRO_ID.toString())) {
// 测试计划执行
Iterator<String> it = config.getConfig().keySet().iterator();
if (it.hasNext()) {
this.setProjectId(it.next());
}
} else {
this.setProjectId(RunModeConstants.HIS_PRO_ID.toString());
}
}
}
}
private boolean isUrl() {
// 自定义字段没有引用环境则非url
if (this.isCustomizeReq()) {
@ -688,17 +596,6 @@ public class MsHTTPSamplerProxy extends MsTestElement {
return false;
}
private String getParentProjectId() {
MsTestElement parent = this.getParent();
while (parent != null) {
if (StringUtils.isNotBlank(parent.getProjectId())) {
return parent.getProjectId();
}
parent = parent.getParent();
}
return "";
}
private String getRestParameters(String path) {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(path);
@ -820,74 +717,6 @@ public class MsHTTPSamplerProxy extends MsTestElement {
}
}
/**
* 按照环境规则匹配环境
*
* @param parameterConfig
* @return
*/
private HttpConfig matchConfig(ParameterConfig parameterConfig) {
HttpConfig httpConfig = parameterConfig.getConfig().get(this.getProjectId()).getHttpConfig();
boolean isNext = true;
if (CollectionUtils.isNotEmpty(httpConfig.getConditions())) {
for (HttpConfigCondition item : httpConfig.getConditions()) {
if (item.getType().equals(ConditionType.PATH.name())) {
HttpConfig config = httpConfig.getPathCondition(this.getPath(), item);
if (config != null) {
isNext = false;
httpConfig = config;
break;
}
} else if (item.getType().equals(ConditionType.MODULE.name())) {
ApiDefinition apiDefinition = null;
ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class);
ApiTestCaseService apiTestCaseService = CommonBeanFactory.getBean(ApiTestCaseService.class);
if (StringUtils.isNotEmpty(this.getRefType()) && this.getRefType().equals("CASE")) {
ApiTestCaseWithBLOBs caseWithBLOBs = apiTestCaseService.get(this.getId());
if (caseWithBLOBs != null) {
apiDefinition = apiDefinitionService.get(caseWithBLOBs.getApiDefinitionId());
}
} else {
apiDefinition = apiDefinitionService.get(this.getId());
ApiTestCaseWithBLOBs apiTestCaseWithBLOBs = apiTestCaseService.get(this.getId());
if (apiTestCaseWithBLOBs == null) {
apiTestCaseWithBLOBs = apiTestCaseService.get(this.getName());
}
if (apiTestCaseWithBLOBs != null) {
apiDefinition = apiDefinitionService.get(apiTestCaseWithBLOBs.getApiDefinitionId());
} else {
TestPlanApiCaseService testPlanApiCaseService = CommonBeanFactory.getBean(TestPlanApiCaseService.class);
TestPlanApiCase testPlanApiCase = testPlanApiCaseService.getById(this.getId());
if (testPlanApiCase != null) {
ApiTestCaseWithBLOBs caseWithBLOBs = apiTestCaseService.get(testPlanApiCase.getApiCaseId());
if (caseWithBLOBs != null) {
apiDefinition = apiDefinitionService.get(caseWithBLOBs.getApiDefinitionId());
}
}
}
}
if (apiDefinition != null) {
HttpConfig config = httpConfig.getModuleCondition(apiDefinition.getModuleId(), item);
if (config != null) {
isNext = false;
httpConfig = config;
break;
}
}
}
}
if (isNext) {
for (HttpConfigCondition item : httpConfig.getConditions()) {
if (item.getType().equals(ConditionType.NONE.name())) {
httpConfig = httpConfig.initHttpConfig(item);
break;
}
}
}
}
return httpConfig;
}
/**
* 环境通用变量这里只适用用接口定义和用例场景自动化会加到场景中
*/

View File

@ -42,7 +42,6 @@ import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
@ -162,19 +161,7 @@ public class MsJDBCSampler extends MsTestElement {
}
//增加误报全局断言
if (envConfig != null) {
if (envConfig.isUseErrorCode()) {
List<MsAssertions> errorReportAssertion = HashTreeUtil.getErrorReportByProjectId(this.getProjectId());
for (MsAssertions assertion : errorReportAssertion) {
assertion.toHashTree(samplerHashTree, assertion.getHashTree(), config);
}
}
if (CollectionUtils.isNotEmpty(envConfig.getAssertions())) {
for (MsAssertions assertion : envConfig.getAssertions()) {
assertion.toHashTree(samplerHashTree, assertion.getHashTree(), config);
}
}
}
HashTreeUtil.addPositive(envConfig, samplerHashTree, config, this.getProjectId());
//处理全局前后置脚本(步骤内)
String environmentId = this.getEnvironmentId();
@ -184,15 +171,8 @@ public class MsJDBCSampler extends MsTestElement {
//根据配置将脚本放置在私有脚本之前
JMeterScriptUtil.setScript(envConfig, samplerHashTree, GlobalScriptFilterRequest.JDBC.name(), environmentId, config, false);
HashTreeUtil hashTreeUtil = new HashTreeUtil();
if (CollectionUtils.isNotEmpty(hashTree)) {
EnvironmentConfig finalEnvConfig = envConfig;
hashTree.forEach(el -> {
if (el instanceof MsAssertions) {
//断言设置需要和全局断言误报进行去重
el = hashTreeUtil.duplicateRegexInAssertions(finalEnvConfig.getAssertions(), (MsAssertions) el);
}
el.toHashTree(samplerHashTree, el.getHashTree(), config);
});
}
@ -328,7 +308,7 @@ public class MsJDBCSampler extends MsTestElement {
JDBCSampler sampler = new JDBCSampler();
sampler.setEnabled(this.isEnable());
sampler.setName(this.getName());
if(config.isOperating()){
if (config.isOperating()) {
String[] testNameArr = sampler.getName().split("<->");
if (testNameArr.length > 0) {
String testName = testNameArr[0];
@ -337,14 +317,7 @@ public class MsJDBCSampler extends MsTestElement {
}
sampler.setProperty(TestElement.TEST_CLASS, JDBCSampler.class.getName());
sampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
sampler.setProperty("MS-ID", this.getId());
String indexPath = this.getIndex();
sampler.setProperty("MS-RESOURCE-ID", ElementUtil.getResourceId(this.getId(), config, this.getParent(), indexPath));
List<String> id_names = new LinkedList<>();
ElementUtil.getScenarioSet(this, id_names);
sampler.setProperty("MS-SCENARIO", JSON.toJSONString(id_names));
// request.getDataSource() 是ID需要转换为Name
ElementUtil.setBaseParams(sampler, this.getParent(), config, this.getId(), this.getIndex());
sampler.setProperty("dataSource", this.dataSource.getName());
sampler.setProperty("query", this.getQuery());
sampler.setProperty("queryTimeout", String.valueOf(this.getQueryTimeout()));

View File

@ -46,7 +46,6 @@ import org.apache.jorphan.collections.ListedHashTree;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
@ -157,19 +156,7 @@ public class MsTCPSampler extends MsTestElement {
}
//增加误报全局断言
if (envConfig != null) {
if (envConfig.isUseErrorCode()) {
List<MsAssertions> errorReportAssertion = HashTreeUtil.getErrorReportByProjectId(this.getProjectId());
for (MsAssertions assertion : errorReportAssertion) {
assertion.toHashTree(samplerHashTree, assertion.getHashTree(), config);
}
}
if (CollectionUtils.isNotEmpty(envConfig.getAssertions())) {
for (MsAssertions assertion : envConfig.getAssertions()) {
assertion.toHashTree(samplerHashTree, assertion.getHashTree(), config);
}
}
}
HashTreeUtil.addPositive(envConfig, samplerHashTree, config, this.getProjectId());
//处理全局前后置脚本(步骤内)
String environmentId = this.getEnvironmentId();
@ -259,20 +246,14 @@ public class MsTCPSampler extends MsTestElement {
TCPSampler tcpSampler = new TCPSampler();
tcpSampler.setEnabled(this.isEnable());
tcpSampler.setName(this.getName());
if(config.isOperating()){
if (config.isOperating()) {
String[] testNameArr = tcpSampler.getName().split("<->");
if (testNameArr.length > 0) {
String testName = testNameArr[0];
tcpSampler.setName(testName);
}
}
tcpSampler.setProperty("MS-ID", this.getId());
String indexPath = this.getIndex();
tcpSampler.setProperty("MS-RESOURCE-ID", ElementUtil.getResourceId(this.getId(), config, this.getParent(), indexPath));
List<String> id_names = new LinkedList<>();
ElementUtil.getScenarioSet(this, id_names);
tcpSampler.setProperty("MS-SCENARIO", JSON.toJSONString(id_names));
ElementUtil.setBaseParams(tcpSampler, this.getParent(), config, this.getId(), this.getIndex());
tcpSampler.setProperty(TestElement.TEST_CLASS, TCPSampler.class.getName());
tcpSampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TCPSamplerGui"));
tcpSampler.setClassname(this.getClassname());

View File

@ -4,11 +4,8 @@ import io.metersphere.api.dto.definition.request.assertions.MsAssertions;
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.GlobalScriptConfig;
import io.metersphere.base.domain.ApiModule;
import io.metersphere.base.domain.ApiModuleExample;
import io.metersphere.base.mapper.ApiModuleMapper;
import io.metersphere.commons.utils.BeanUtils;
import io.metersphere.commons.utils.CommonBeanFactory;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
@ -53,39 +50,6 @@ public class HttpConfig {
return null;
}
private void getAllChild(String moduleId, List<String> allChild) {
// 找出所有子模块
if (apiModuleMapper == null) {
apiModuleMapper = CommonBeanFactory.getBean(ApiModuleMapper.class);
}
allChild.add(moduleId);
ApiModuleExample example = new ApiModuleExample();
example.createCriteria().andParentIdEqualTo(moduleId);
List<ApiModule> modules = apiModuleMapper.selectByExample(example);
for (ApiModule module : modules) {
getAllChild(module.getId(), allChild);
}
}
// public HttpConfig getModuleCondition(String moduleId, HttpConfigCondition configCondition) {
// List<String> moduleIds = new ArrayList<>();
// if (CollectionUtils.isNotEmpty(configCondition.getDetails())) {
// if (CollectionUtils.isEmpty(configCondition.getModuleIds())) {
// for (KeyValue keyValue : configCondition.getDetails()) {
// getAllChild(keyValue.getValue(), moduleIds);
// }
// configCondition.setModuleIds(moduleIds);
// } else {
// moduleIds = configCondition.getModuleIds();
// }
//
// if (moduleIds.contains(moduleId)) {
// return initHttpConfig(configCondition);
// }
// }
// return null;
// }
public HttpConfig getModuleCondition(String moduleId, HttpConfigCondition configCondition) {
if (CollectionUtils.isNotEmpty(configCondition.getDetails())) {
List<KeyValue> details = configCondition.getDetails().stream().filter(detail -> StringUtils.contains(detail.getValue(), moduleId)).collect(Collectors.toList());

View File

@ -3,8 +3,10 @@ package io.metersphere.commons.utils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.metersphere.api.dto.RunningParamKeys;
import io.metersphere.api.dto.definition.request.ParameterConfig;
import io.metersphere.api.dto.definition.request.assertions.MsAssertionRegex;
import io.metersphere.api.dto.definition.request.assertions.MsAssertions;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import io.metersphere.api.service.ApiTestEnvironmentService;
import io.metersphere.api.service.ExtErrorReportLibraryService;
import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs;
@ -237,4 +239,21 @@ public class HashTreeUtil {
ExtErrorReportLibraryService service = CommonBeanFactory.getBean(ExtErrorReportLibraryService.class);
return service.getAssertionByProjectIdAndStatusIsOpen(projectId);
}
public static void addPositive(EnvironmentConfig envConfig, HashTree samplerHashTree, ParameterConfig config, String projectId) {
if (envConfig != null) {
return;
}
if (envConfig.isUseErrorCode()) {
List<MsAssertions> errorReportAssertion = HashTreeUtil.getErrorReportByProjectId(projectId);
for (MsAssertions assertion : errorReportAssertion) {
assertion.toHashTree(samplerHashTree, assertion.getHashTree(), config);
}
}
if (CollectionUtils.isNotEmpty(envConfig.getAssertions())) {
for (MsAssertions assertion : envConfig.getAssertions()) {
assertion.toHashTree(samplerHashTree, assertion.getHashTree(), config);
}
}
}
}