fix(接口测试): 兼容导入JMX历史数据

【【接口测试】导入jmx场景后直接点执行/调试,提示要先选环境】https://www.tapd.cn/55049933/bugtrace/bugs/view?bug_id=1155049933001026424

Signed-off-by: fit2-zhao <yong.zhao@fit2cloud.com>
This commit is contained in:
fit2-zhao 2023-05-22 19:22:43 +08:00 committed by fit2-zhao
parent 07f9a08275
commit 93853efce8
2 changed files with 24 additions and 1 deletions

View File

@ -335,9 +335,11 @@ public class JMeterParser extends ApiImportAbstractParser<ScenarioImport> {
samplerProxy.setUrl(this.getUrl(source)); samplerProxy.setUrl(this.getUrl(source));
samplerProxy.setPath(null); samplerProxy.setPath(null);
samplerProxy.setCustomizeReq(true); samplerProxy.setCustomizeReq(true);
samplerProxy.setIsRefEnvironment(false);
} }
samplerProxy.setId(UUID.randomUUID().toString()); samplerProxy.setId(UUID.randomUUID().toString());
samplerProxy.setType(ElementConstants.HTTP_SAMPLER); samplerProxy.setType(ElementConstants.HTTP_SAMPLER);
samplerProxy.setClazzName(MsHTTPSamplerProxy.class.getCanonicalName());
body.getKvs().add(new KeyValue()); body.getKvs().add(new KeyValue());
body.getBinary().add(new KeyValue()); body.getBinary().add(new KeyValue());
@ -375,6 +377,7 @@ public class JMeterParser extends ApiImportAbstractParser<ScenarioImport> {
msTCPSampler.setUsername(tcpSampler.getProperty(ConfigTestElement.USERNAME).getStringValue()); msTCPSampler.setUsername(tcpSampler.getProperty(ConfigTestElement.USERNAME).getStringValue());
msTCPSampler.setPassword(tcpSampler.getProperty(ConfigTestElement.PASSWORD).getStringValue()); msTCPSampler.setPassword(tcpSampler.getProperty(ConfigTestElement.PASSWORD).getStringValue());
msTCPSampler.setClassname(tcpSampler.getClassname()); msTCPSampler.setClassname(tcpSampler.getClassname());
msTCPSampler.setClazzName(MsTCPSampler.class.getCanonicalName());
} }
private void convertDubboSample(MsDubboSampler elementNode, DubboSample sampler) { private void convertDubboSample(MsDubboSampler elementNode, DubboSample sampler) {
@ -431,6 +434,7 @@ public class JMeterParser extends ApiImportAbstractParser<ScenarioImport> {
}); });
} }
elementNode.setAttachmentArgs(attachmentArgs); elementNode.setAttachmentArgs(attachmentArgs);
elementNode.setClazzName(MsDubboSampler.class.getCanonicalName());
} }
/** /**
@ -558,6 +562,10 @@ public class JMeterParser extends ApiImportAbstractParser<ScenarioImport> {
msJDBCSampler.setDataSourceId(dataPools.getDataSources().get(jdbcSampler.getPropertyAsString("dataSource")).getId()); msJDBCSampler.setDataSourceId(dataPools.getDataSources().get(jdbcSampler.getPropertyAsString("dataSource")).getId());
} }
msJDBCSampler.setVariables(new LinkedList<>()); msJDBCSampler.setVariables(new LinkedList<>());
msJDBCSampler.setCustomizeReq(true);
msJDBCSampler.setIsRefEnvironment(false);
msJDBCSampler.setReferenced(ElementConstants.STEP_CREATED);
msJDBCSampler.setClazzName(MsJDBCSampler.class.getCanonicalName());
} }
private void convertMsExtract(MsExtract extract, Object key) { private void convertMsExtract(MsExtract extract, Object key) {
@ -757,6 +765,7 @@ public class JMeterParser extends ApiImportAbstractParser<ScenarioImport> {
BeanUtils.copyBean(elementNode, jsr223Sampler); BeanUtils.copyBean(elementNode, jsr223Sampler);
((MsJSR223Processor) elementNode).setScript(jsr223Sampler.getPropertyAsString(ElementConstants.SCRIPT)); ((MsJSR223Processor) elementNode).setScript(jsr223Sampler.getPropertyAsString(ElementConstants.SCRIPT));
((MsJSR223Processor) elementNode).setScriptLanguage(jsr223Sampler.getPropertyAsString("scriptLanguage")); ((MsJSR223Processor) elementNode).setScriptLanguage(jsr223Sampler.getPropertyAsString("scriptLanguage"));
elementNode.setClazzName(MsJSR223Processor.class.getCanonicalName());
} }
// BeanShell自定义脚本 // BeanShell自定义脚本
else if (key instanceof BeanShellSampler) { else if (key instanceof BeanShellSampler) {
@ -766,6 +775,7 @@ public class JMeterParser extends ApiImportAbstractParser<ScenarioImport> {
((MsJSR223Processor) elementNode).setJsrEnable(false); ((MsJSR223Processor) elementNode).setJsrEnable(false);
((MsJSR223Processor) elementNode).setScript(jsr223Sampler.getPropertyAsString("BeanShellSampler.query")); ((MsJSR223Processor) elementNode).setScript(jsr223Sampler.getPropertyAsString("BeanShellSampler.query"));
((MsJSR223Processor) elementNode).setScriptLanguage(jsr223Sampler.getPropertyAsString("scriptLanguage")); ((MsJSR223Processor) elementNode).setScriptLanguage(jsr223Sampler.getPropertyAsString("scriptLanguage"));
elementNode.setClazzName(MsJSR223Processor.class.getCanonicalName());
} }
// 后置脚本 // 后置脚本
else if (key instanceof JSR223PostProcessor) { else if (key instanceof JSR223PostProcessor) {
@ -774,6 +784,7 @@ public class JMeterParser extends ApiImportAbstractParser<ScenarioImport> {
BeanUtils.copyBean(elementNode, jsr223Sampler); BeanUtils.copyBean(elementNode, jsr223Sampler);
((MsJSR223PostProcessor) elementNode).setScript(jsr223Sampler.getPropertyAsString(ElementConstants.SCRIPT)); ((MsJSR223PostProcessor) elementNode).setScript(jsr223Sampler.getPropertyAsString(ElementConstants.SCRIPT));
((MsJSR223PostProcessor) elementNode).setScriptLanguage(jsr223Sampler.getPropertyAsString("scriptLanguage")); ((MsJSR223PostProcessor) elementNode).setScriptLanguage(jsr223Sampler.getPropertyAsString("scriptLanguage"));
elementNode.setClazzName(MsJSR223PostProcessor.class.getCanonicalName());
} else if (key instanceof BeanShellPostProcessor) { } else if (key instanceof BeanShellPostProcessor) {
elementNode = getMsTestElement((BeanShellPostProcessor) key); elementNode = getMsTestElement((BeanShellPostProcessor) key);
} else if (key instanceof BeanShellPreProcessor) { } else if (key instanceof BeanShellPreProcessor) {
@ -786,22 +797,26 @@ public class JMeterParser extends ApiImportAbstractParser<ScenarioImport> {
BeanUtils.copyBean(elementNode, jsr223Sampler); BeanUtils.copyBean(elementNode, jsr223Sampler);
((MsJSR223PreProcessor) elementNode).setScript(jsr223Sampler.getPropertyAsString(ElementConstants.SCRIPT)); ((MsJSR223PreProcessor) elementNode).setScript(jsr223Sampler.getPropertyAsString(ElementConstants.SCRIPT));
((MsJSR223PreProcessor) elementNode).setScriptLanguage(jsr223Sampler.getPropertyAsString("scriptLanguage")); ((MsJSR223PreProcessor) elementNode).setScriptLanguage(jsr223Sampler.getPropertyAsString("scriptLanguage"));
elementNode.setClazzName(MsJSR223PreProcessor.class.getCanonicalName());
} }
// 断言规则 // 断言规则
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) {
elementNode = new MsAssertions(); elementNode = new MsAssertions();
convertMsAssertions((MsAssertions) elementNode, key); convertMsAssertions((MsAssertions) elementNode, key);
elementNode.setClazzName(MsAssertions.class.getCanonicalName());
} }
// 提取参数 // 提取参数
else if (key instanceof RegexExtractor || key instanceof XPath2Extractor || key instanceof JSONPostProcessor) { else if (key instanceof RegexExtractor || key instanceof XPath2Extractor || key instanceof JSONPostProcessor) {
elementNode = new MsExtract(); elementNode = new MsExtract();
convertMsExtract((MsExtract) elementNode, key); convertMsExtract((MsExtract) elementNode, key);
elementNode.setClazzName(MsExtract.class.getCanonicalName());
} }
// 定时器 // 定时器
else if (key instanceof ConstantTimer) { else if (key instanceof ConstantTimer) {
elementNode = new MsConstantTimer(); elementNode = new MsConstantTimer();
BeanUtils.copyBean(elementNode, key); BeanUtils.copyBean(elementNode, key);
elementNode.setType(ElementConstants.CONSTANT_TIMER); elementNode.setType(ElementConstants.CONSTANT_TIMER);
elementNode.setClazzName(MsConstantTimer.class.getCanonicalName());
} }
// 次数循环控制器 // 次数循环控制器
else if (key instanceof LoopController) { else if (key instanceof LoopController) {
@ -814,6 +829,7 @@ public class JMeterParser extends ApiImportAbstractParser<ScenarioImport> {
countController.setLoops(String.valueOf(loopController.getLoops())); countController.setLoops(String.valueOf(loopController.getLoops()));
countController.setProceed(true); countController.setProceed(true);
((MsLoopController) elementNode).setCountController(countController); ((MsLoopController) elementNode).setCountController(countController);
elementNode.setClazzName(MsLoopController.class.getCanonicalName());
} }
// While循环控制器 // While循环控制器
else if (key instanceof WhileController) { else if (key instanceof WhileController) {
@ -825,6 +841,7 @@ public class JMeterParser extends ApiImportAbstractParser<ScenarioImport> {
MsWhileController countController = new MsWhileController(); MsWhileController countController = new MsWhileController();
countController.setValue(whileController.getCondition()); countController.setValue(whileController.getCondition());
((MsLoopController) elementNode).setWhileController(countController); ((MsLoopController) elementNode).setWhileController(countController);
elementNode.setClazzName(MsWhileController.class.getCanonicalName());
} }
// Foreach 循环控制器 // Foreach 循环控制器
else if (key instanceof ForeachController) { else if (key instanceof ForeachController) {
@ -837,12 +854,14 @@ public class JMeterParser extends ApiImportAbstractParser<ScenarioImport> {
countController.setInputVal(foreachController.getInputValString()); countController.setInputVal(foreachController.getInputValString());
countController.setReturnVal(foreachController.getReturnValString()); countController.setReturnVal(foreachController.getReturnValString());
((MsLoopController) elementNode).setForEachController(countController); ((MsLoopController) elementNode).setForEachController(countController);
elementNode.setClazzName(ForeachController.class.getCanonicalName());
} else if (key instanceof TransactionController) { } else if (key instanceof TransactionController) {
TransactionController transactionController = (TransactionController) key; TransactionController transactionController = (TransactionController) key;
elementNode = new MsTransactionController(); elementNode = new MsTransactionController();
elementNode.setName(transactionController.getName()); elementNode.setName(transactionController.getName());
((MsTransactionController) elementNode).setGenerateParentSample(transactionController.isGenerateParentSample()); ((MsTransactionController) elementNode).setGenerateParentSample(transactionController.isGenerateParentSample());
((MsTransactionController) elementNode).setIncludeTimers(transactionController.isIncludeTimers()); ((MsTransactionController) elementNode).setIncludeTimers(transactionController.isIncludeTimers());
elementNode.setClazzName(MsTransactionController.class.getCanonicalName());
} else if (StringUtils.equals(key.getClass().getName(), "net.xmeter.samplers.ConnectSampler")) { } else if (StringUtils.equals(key.getClass().getName(), "net.xmeter.samplers.ConnectSampler")) {
elementNode = getMqttElement(key, "io.metersphere.plugin.mqtt.sampler.MqttConnectSampler"); elementNode = getMqttElement(key, "io.metersphere.plugin.mqtt.sampler.MqttConnectSampler");
} else if (StringUtils.equals(key.getClass().getName(), "net.xmeter.samplers.DisConnectSampler")) { } else if (StringUtils.equals(key.getClass().getName(), "net.xmeter.samplers.DisConnectSampler")) {
@ -864,6 +883,7 @@ public class JMeterParser extends ApiImportAbstractParser<ScenarioImport> {
elementNode.setName(testElement.getName()); elementNode.setName(testElement.getName());
((MsJmeterElement) elementNode).setJmeterElement(objToXml(key)); ((MsJmeterElement) elementNode).setJmeterElement(objToXml(key));
((MsJmeterElement) elementNode).setElementType(key.getClass().getSimpleName()); ((MsJmeterElement) elementNode).setElementType(key.getClass().getSimpleName());
elementNode.setClazzName(MsJmeterElement.class.getCanonicalName());
} }
elementNode.setEnable(((TestElement) key).isEnabled()); elementNode.setEnable(((TestElement) key).isEnabled());
elementNode.setResourceId(UUID.randomUUID().toString()); elementNode.setResourceId(UUID.randomUUID().toString());

View File

@ -65,6 +65,7 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.collections.ListedHashTree; import org.apache.jorphan.collections.ListedHashTree;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -868,6 +869,7 @@ public class ApiScenarioService {
public int getScenarioStep(List<String> ids) { public int getScenarioStep(List<String> ids) {
return extApiScenarioReferenceIdMapper.selectByScenarioIds(ids); return extApiScenarioReferenceIdMapper.selectByScenarioIds(ids);
} }
public List<ApiScenarioDTO> getScenarioDetail(List<String> ids) { public List<ApiScenarioDTO> getScenarioDetail(List<String> ids) {
if (CollectionUtils.isEmpty(ids)) { if (CollectionUtils.isEmpty(ids)) {
return new ArrayList<>(); return new ArrayList<>();
@ -2199,7 +2201,8 @@ public class ApiScenarioService {
} else { } else {
//将复制的或者类型不是引用case的步骤赋予当前项目id目的是为了运行的时候可以配置运行环境 //将复制的或者类型不是引用case的步骤赋予当前项目id目的是为了运行的时候可以配置运行环境
object.put("projectId", projectId); object.put("projectId", projectId);
if (StringUtils.isEmpty(object.optString("url"))) { if (StringUtils.isEmpty(object.optString("url"))
&& StringUtils.equals(object.optString("type"), HTTPSamplerProxy.class.getCanonicalName())) {
object.put("isRefEnvironment", true); object.put("isRefEnvironment", true);
} }
} }