feat(接口自动化): 导入jmx 增加HashTree 有序处理

This commit is contained in:
fit2-zhao 2021-02-09 16:22:59 +08:00
parent b7cc737dd7
commit 39cffd9615
1 changed files with 40 additions and 15 deletions

View File

@ -31,13 +31,13 @@ import io.metersphere.api.dto.definition.request.timer.MsConstantTimer;
import io.metersphere.api.dto.definition.request.unknown.MsJmeterElement; import io.metersphere.api.dto.definition.request.unknown.MsJmeterElement;
import io.metersphere.api.dto.scenario.Body; import io.metersphere.api.dto.scenario.Body;
import io.metersphere.api.dto.scenario.KeyValue; import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.dto.scenario.request.BodyFile;
import io.metersphere.api.dto.scenario.request.RequestType; import io.metersphere.api.dto.scenario.request.RequestType;
import io.metersphere.base.domain.ApiScenarioModule; import io.metersphere.base.domain.ApiScenarioModule;
import io.metersphere.base.domain.ApiScenarioWithBLOBs; import io.metersphere.base.domain.ApiScenarioWithBLOBs;
import io.metersphere.commons.constants.LoopConstants; import io.metersphere.commons.constants.LoopConstants;
import io.metersphere.commons.utils.BeanUtils; import io.metersphere.commons.utils.BeanUtils;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.assertions.*; import org.apache.jmeter.assertions.*;
import org.apache.jmeter.config.ConfigTestElement; import org.apache.jmeter.config.ConfigTestElement;
import org.apache.jmeter.control.ForeachController; import org.apache.jmeter.control.ForeachController;
@ -121,14 +121,29 @@ public class MsJmeterParser extends ScenarioImportAbstractParser {
samplerProxy.getBody().setType(Body.FORM_DATA); samplerProxy.getBody().setType(Body.FORM_DATA);
List<KeyValue> keyValues = new LinkedList<>(); List<KeyValue> keyValues = new LinkedList<>();
for (HTTPFileArg arg : source.getHTTPFiles()) { for (HTTPFileArg arg : source.getHTTPFiles()) {
KeyValue keyValue = new KeyValue(arg.getProperty("Argument.name").toString(), arg.getProperty("Argument.value").toString()); List<BodyFile> files = new LinkedList<>();
BodyFile file = new BodyFile();
file.setId(arg.getParamName());
file.setName(arg.getPath());
files.add(file);
KeyValue keyValue = new KeyValue(arg.getParamName(), arg.getParamName());
keyValue.setContentType(arg.getProperty("HTTPArgument.content_type").toString()); keyValue.setContentType(arg.getProperty("HTTPArgument.content_type").toString());
keyValue.setType("file");
keyValue.setFiles(files);
keyValues.add(keyValue); keyValues.add(keyValue);
} }
samplerProxy.getBody().setKvs(keyValues); samplerProxy.getBody().setKvs(keyValues);
} }
samplerProxy.setProtocol(RequestType.HTTP); samplerProxy.setProtocol(RequestType.HTTP);
samplerProxy.setPort(source.getPort() + "");
if (source.getArguments() != null) { if (source.getArguments() != null) {
if (source.getPostBodyRaw()) {
samplerProxy.getBody().setType(Body.RAW);
source.getArguments().getArgumentsAsMap().forEach((k, v) -> {
samplerProxy.getBody().setRaw(v);
});
} else {
List<KeyValue> keyValues = new LinkedList<>(); List<KeyValue> keyValues = new LinkedList<>();
source.getArguments().getArgumentsAsMap().forEach((k, v) -> { source.getArguments().getArgumentsAsMap().forEach((k, v) -> {
KeyValue keyValue = new KeyValue(k, v); KeyValue keyValue = new KeyValue(k, v);
@ -138,6 +153,7 @@ public class MsJmeterParser extends ScenarioImportAbstractParser {
samplerProxy.setArguments(keyValues); samplerProxy.setArguments(keyValues);
} }
} }
}
samplerProxy.setPath(source.getPath()); samplerProxy.setPath(source.getPath());
samplerProxy.setMethod(source.getMethod()); samplerProxy.setMethod(source.getMethod());
if (source.getUrl() != null) { if (source.getUrl() != null) {
@ -151,7 +167,7 @@ public class MsJmeterParser extends ScenarioImportAbstractParser {
} }
private void convertTCPSampler(MsTCPSampler msTCPSampler, TCPSampler tcpSampler) { private void convertTCPSampler(MsTCPSampler msTCPSampler, TCPSampler tcpSampler) {
tcpSampler.setName(tcpSampler.getName()); msTCPSampler.setName(tcpSampler.getName());
msTCPSampler.setType("TCPSampler"); msTCPSampler.setType("TCPSampler");
msTCPSampler.setServer(tcpSampler.getServer()); msTCPSampler.setServer(tcpSampler.getServer());
msTCPSampler.setPort(tcpSampler.getPort() + ""); msTCPSampler.setPort(tcpSampler.getPort() + "");
@ -226,10 +242,11 @@ public class MsJmeterParser extends ScenarioImportAbstractParser {
msJDBCSampler.setType("JDBCSampler"); msJDBCSampler.setType("JDBCSampler");
msJDBCSampler.setName(jdbcSampler.getName()); msJDBCSampler.setName(jdbcSampler.getName());
msJDBCSampler.setProtocol("SQL"); msJDBCSampler.setProtocol("SQL");
msJDBCSampler.setQuery(jdbcSampler.getQuery()); msJDBCSampler.setQuery(jdbcSampler.getPropertyAsString("query"));
msJDBCSampler.setQueryTimeout(StringUtils.isNotEmpty(jdbcSampler.getQueryTimeout()) ? Long.parseLong(jdbcSampler.getQueryTimeout()) : 0L); msJDBCSampler.setQueryTimeout(jdbcSampler.getPropertyAsInt("queryTimeout"));
msJDBCSampler.setResultVariable(jdbcSampler.getResultVariable()); msJDBCSampler.setResultVariable(jdbcSampler.getPropertyAsString("resultVariable"));
msJDBCSampler.setVariableNames(jdbcSampler.getVariableNames()); msJDBCSampler.setVariableNames(jdbcSampler.getPropertyAsString("variableNames"));
msJDBCSampler.setVariables(new LinkedList<>());
} }
private void convertMsExtract(MsExtract extract, Object key) { private void convertMsExtract(MsExtract extract, Object key) {
@ -370,22 +387,30 @@ public class MsJmeterParser extends ScenarioImportAbstractParser {
elementNode = new MsJDBCSampler(); elementNode = new MsJDBCSampler();
JDBCSampler jdbcSampler = (JDBCSampler) key; JDBCSampler jdbcSampler = (JDBCSampler) key;
convertJDBCSampler((MsJDBCSampler) elementNode, jdbcSampler); convertJDBCSampler((MsJDBCSampler) elementNode, jdbcSampler);
} else if (key instanceof JSR223Sampler) { }
// JSR自定义脚本
else if (key instanceof JSR223Sampler) {
JSR223Sampler jsr223Sampler = (JSR223Sampler) key; JSR223Sampler jsr223Sampler = (JSR223Sampler) key;
elementNode = new MsJSR223Processor(); elementNode = new MsJSR223Processor();
BeanUtils.copyBean(elementNode, jsr223Sampler); BeanUtils.copyBean(elementNode, jsr223Sampler);
((MsJSR223Processor) elementNode).setScript(jsr223Sampler.getPropertyAsString("script"));
((MsJSR223Processor) elementNode).setScriptLanguage(jsr223Sampler.getPropertyAsString("scriptLanguage"));
} }
// 后置脚本 // 后置脚本
else if (key instanceof JSR223PostProcessor) { else if (key instanceof JSR223PostProcessor) {
JSR223PostProcessor jsr223Sampler = (JSR223PostProcessor) key; JSR223PostProcessor jsr223Sampler = (JSR223PostProcessor) key;
elementNode = new MsJSR223PostProcessor(); elementNode = new MsJSR223PostProcessor();
BeanUtils.copyBean(elementNode, jsr223Sampler); BeanUtils.copyBean(elementNode, jsr223Sampler);
((MsJSR223Processor) elementNode).setScript(jsr223Sampler.getPropertyAsString("script"));
((MsJSR223Processor) elementNode).setScriptLanguage(jsr223Sampler.getPropertyAsString("scriptLanguage"));
} }
// 前置脚本 // 前置脚本
else if (key instanceof JSR223PreProcessor) { else if (key instanceof JSR223PreProcessor) {
JSR223PreProcessor jsr223Sampler = (JSR223PreProcessor) key; JSR223PreProcessor jsr223Sampler = (JSR223PreProcessor) key;
elementNode = new MsJSR223PreProcessor(); elementNode = new MsJSR223PreProcessor();
BeanUtils.copyBean(elementNode, jsr223Sampler); BeanUtils.copyBean(elementNode, jsr223Sampler);
((MsJSR223Processor) elementNode).setScript(jsr223Sampler.getPropertyAsString("script"));
((MsJSR223Processor) elementNode).setScriptLanguage(jsr223Sampler.getPropertyAsString("scriptLanguage"));
} }
// 提取参数 // 提取参数
else if (key instanceof ResponseAssertion || key instanceof JSONPathAssertion || key instanceof XPath2Assertion || key instanceof JSR223Assertion || key instanceof DurationAssertion) { else if (key instanceof ResponseAssertion || key instanceof JSONPathAssertion || key instanceof XPath2Assertion || key instanceof JSR223Assertion || key instanceof DurationAssertion) {