fix(接口测试): 修复接口测试导入jmx后无法更新的缺陷

--bug=1046216 --user=宋天阳 【接口测试】接口定义-导入jmeter文件编辑用例-提交更新失败 https://www.tapd.cn/55049933/s/1576729
This commit is contained in:
Jianguo-Genius 2024-09-11 15:26:51 +08:00 committed by Craftsman
parent 76c33c015e
commit 62fbdf8329
7 changed files with 148 additions and 18 deletions

View File

@ -1,6 +1,7 @@
package io.metersphere.api.parser.ms; package io.metersphere.api.parser.ms;
import io.metersphere.api.dto.ApiFile; import io.metersphere.api.dto.ApiFile;
import io.metersphere.api.dto.request.MsCommonElement;
import io.metersphere.api.dto.request.http.MsHTTPElement; import io.metersphere.api.dto.request.http.MsHTTPElement;
import io.metersphere.api.dto.request.http.MsHeader; import io.metersphere.api.dto.request.http.MsHeader;
import io.metersphere.api.dto.request.http.QueryParam; import io.metersphere.api.dto.request.http.QueryParam;
@ -10,7 +11,7 @@ import io.metersphere.plugin.api.spi.AbstractMsTestElement;
import io.metersphere.project.api.KeyValueEnableParam; import io.metersphere.project.api.KeyValueEnableParam;
import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.LogUtils; import io.metersphere.sdk.util.LogUtils;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.protocol.http.control.HeaderManager; import org.apache.jmeter.protocol.http.control.HeaderManager;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy; import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
@ -35,6 +36,13 @@ public class HTTPSamplerConverter extends AbstractMsElementConverter<HTTPSampler
public void toMsElement(AbstractMsTestElement parent, HTTPSamplerProxy httpSampler, HashTree hashTree) { public void toMsElement(AbstractMsTestElement parent, HTTPSamplerProxy httpSampler, HashTree hashTree) {
MsHTTPElement msHTTPElement = this.convertHttpSampler(httpSampler); MsHTTPElement msHTTPElement = this.convertHttpSampler(httpSampler);
parent.getChildren().add(msHTTPElement); parent.getChildren().add(msHTTPElement);
//默认生成一个空的body
MsCommonElement msCommonElement = new MsCommonElement();
LinkedList<AbstractMsTestElement> children = new LinkedList<>();
children.add(msCommonElement);
msHTTPElement.setChildren(children);
parseChild(msHTTPElement, httpSampler, hashTree); parseChild(msHTTPElement, httpSampler, hashTree);
} }
@ -61,6 +69,7 @@ public class HTTPSamplerConverter extends AbstractMsElementConverter<HTTPSampler
samplerProxy.setHeaders(headerKvList); samplerProxy.setHeaders(headerKvList);
// 初始化body // 初始化body
Body body = new Body(); Body body = new Body();
body.setBodyType(Body.BodyType.NONE.name());
body.setJsonBody(new JsonBody()); body.setJsonBody(new JsonBody());
body.setFormDataBody(new FormDataBody()); body.setFormDataBody(new FormDataBody());
body.setWwwFormBody(new WWWFormBody()); body.setWwwFormBody(new WWWFormBody());

View File

@ -45,6 +45,8 @@ public class MsElementConverterRegister {
register(RegexExtractorConverter.class); register(RegexExtractorConverter.class);
register(XPath2ExtractorConverter.class); register(XPath2ExtractorConverter.class);
register(XPathExtractorConverter.class); register(XPathExtractorConverter.class);
register(JSONPathAssertionConverter.class);
register(XPathAssertionConverter.class);
register(BeanShellPreProcessConverter.class); register(BeanShellPreProcessConverter.class);
register(JDBCPreProcessConverter.class); register(JDBCPreProcessConverter.class);

View File

@ -0,0 +1,43 @@
package io.metersphere.api.parser.ms.http.post;
import io.metersphere.api.utils.ConverterUtils;
import io.metersphere.plugin.api.spi.AbstractMsElementConverter;
import io.metersphere.plugin.api.spi.AbstractMsTestElement;
import io.metersphere.project.api.assertion.MsResponseBodyAssertion;
import io.metersphere.project.api.assertion.body.MsJSONPathAssertion;
import io.metersphere.project.api.assertion.body.MsJSONPathAssertionItem;
import io.metersphere.project.api.assertion.body.MsRegexAssertion;
import io.metersphere.project.api.assertion.body.MsXPathAssertion;
import io.metersphere.sdk.dto.CombineCondition;
import org.apache.jmeter.assertions.JSONPathAssertion;
import org.apache.jorphan.collections.HashTree;
import java.util.ArrayList;
import java.util.List;
public class JSONPathAssertionConverter extends AbstractMsElementConverter<JSONPathAssertion> {
@Override
public void toMsElement(AbstractMsTestElement parent, JSONPathAssertion element, HashTree hashTree) {
MsResponseBodyAssertion msResponseBodyAssertion = new MsResponseBodyAssertion();
msResponseBodyAssertion.setAssertionBodyType(MsResponseBodyAssertion.MsBodyAssertionType.JSON_PATH.name());
msResponseBodyAssertion.setXpathAssertion(new MsXPathAssertion());
msResponseBodyAssertion.setRegexAssertion(new MsRegexAssertion());
msResponseBodyAssertion.setName(element.getName());
msResponseBodyAssertion.setEnable(true);
MsJSONPathAssertionItem jsonPathAssertionItem = new MsJSONPathAssertionItem();
jsonPathAssertionItem.setCondition(CombineCondition.CombineConditionOperator.EQUALS.name());
jsonPathAssertionItem.setEnable(element.isEnabled());
jsonPathAssertionItem.setExpectedValue(element.getExpectedValue());
jsonPathAssertionItem.setExpression(element.getJsonPath());
List<MsJSONPathAssertionItem> assertions = new ArrayList<>();
assertions.add(jsonPathAssertionItem);
msResponseBodyAssertion.setJsonPathAssertion(new MsJSONPathAssertion() {{
this.setAssertions(assertions);
}});
ConverterUtils.addAssertions(parent, msResponseBodyAssertion, "JSON_PATH");
}
}

View File

@ -1,13 +0,0 @@
package io.metersphere.api.parser.ms.http.post;
import io.metersphere.plugin.api.spi.AbstractMsElementConverter;
import io.metersphere.plugin.api.spi.AbstractMsTestElement;
import org.apache.jmeter.assertions.ResponseAssertion;
import org.apache.jorphan.collections.HashTree;
public class ResponseAssertionConverter extends AbstractMsElementConverter<ResponseAssertion> {
@Override
public void toMsElement(AbstractMsTestElement parent, ResponseAssertion element, HashTree hashTree) {
}
}

View File

@ -16,7 +16,6 @@ public class XPath2ExtractorConverter extends AbstractMsElementConverter<XPath2E
xPathExtract.setResponseFormat(XPathExtract.ResponseFormat.XML.name()); xPathExtract.setResponseFormat(XPathExtract.ResponseFormat.XML.name());
xPathExtract.setVariableName(element.getRefName()); xPathExtract.setVariableName(element.getRefName());
xPathExtract.setExpression(element.getXPathQuery()); xPathExtract.setExpression(element.getXPathQuery());
if (element.getMatchNumber() == -1) { if (element.getMatchNumber() == -1) {
xPathExtract.setResultMatchingRule(ResultMatchingExtract.ResultMatchingRuleType.ALL.name()); xPathExtract.setResultMatchingRule(ResultMatchingExtract.ResultMatchingRuleType.ALL.name());
xPathExtract.setResultMatchingRuleNum(-1); xPathExtract.setResultMatchingRuleNum(-1);
@ -27,9 +26,6 @@ public class XPath2ExtractorConverter extends AbstractMsElementConverter<XPath2E
xPathExtract.setResultMatchingRule(ResultMatchingExtract.ResultMatchingRuleType.SPECIFIC.name()); xPathExtract.setResultMatchingRule(ResultMatchingExtract.ResultMatchingRuleType.SPECIFIC.name());
xPathExtract.setResultMatchingRuleNum(element.getMatchNumber()); xPathExtract.setResultMatchingRuleNum(element.getMatchNumber());
} }
// xPathExtract.setVariableType(element.getPropertyAsString("type"));
// xPathExtract.setExpression(element.getPropertyAsString("expression"));
ConverterUtils.addPostExtract(parent, xPathExtract); ConverterUtils.addPostExtract(parent, xPathExtract);
} }
} }

View File

@ -0,0 +1,46 @@
package io.metersphere.api.parser.ms.http.post;
import io.metersphere.api.utils.ConverterUtils;
import io.metersphere.plugin.api.spi.AbstractMsElementConverter;
import io.metersphere.plugin.api.spi.AbstractMsTestElement;
import io.metersphere.project.api.assertion.MsResponseBodyAssertion;
import io.metersphere.project.api.assertion.body.MsJSONPathAssertion;
import io.metersphere.project.api.assertion.body.MsRegexAssertion;
import io.metersphere.project.api.assertion.body.MsXPathAssertion;
import io.metersphere.project.api.assertion.body.MsXPathAssertionItem;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.assertions.XPathAssertion;
import org.apache.jorphan.collections.HashTree;
import java.util.ArrayList;
import java.util.List;
public class XPathAssertionConverter extends AbstractMsElementConverter<XPathAssertion> {
@Override
public void toMsElement(AbstractMsTestElement parent, XPathAssertion element, HashTree hashTree) {
MsResponseBodyAssertion msResponseBodyAssertion = new MsResponseBodyAssertion();
msResponseBodyAssertion.setAssertionBodyType(MsResponseBodyAssertion.MsBodyAssertionType.JSON_PATH.name());
msResponseBodyAssertion.setJsonPathAssertion(new MsJSONPathAssertion());
msResponseBodyAssertion.setRegexAssertion(new MsRegexAssertion());
msResponseBodyAssertion.setName(element.getName());
msResponseBodyAssertion.setEnable(true);
MsXPathAssertion msXPathAssertion = new MsXPathAssertion();
if (element.isValidating()) {
msXPathAssertion.setResponseFormat(MsXPathAssertion.ResponseFormat.XML.name());
} else {
msXPathAssertion.setResponseFormat(MsXPathAssertion.ResponseFormat.HTML.name());
}
MsXPathAssertionItem xpathItem = new MsXPathAssertionItem();
xpathItem.setEnable(element.isEnabled());
xpathItem.setExpectedValue(StringUtils.EMPTY);
xpathItem.setExpression(element.getXPathString());
List<MsXPathAssertionItem> assertions = new ArrayList<>();
assertions.add(xpathItem);
msXPathAssertion.setAssertions(assertions);
msResponseBodyAssertion.setXpathAssertion(msXPathAssertion);
ConverterUtils.addAssertions(parent, msResponseBodyAssertion, "XPATH");
}
}

View File

@ -1,7 +1,10 @@
package io.metersphere.api.utils; package io.metersphere.api.utils;
import io.metersphere.api.dto.assertion.MsAssertionConfig;
import io.metersphere.api.dto.request.MsCommonElement; import io.metersphere.api.dto.request.MsCommonElement;
import io.metersphere.plugin.api.spi.AbstractMsTestElement; import io.metersphere.plugin.api.spi.AbstractMsTestElement;
import io.metersphere.project.api.assertion.MsAssertion;
import io.metersphere.project.api.assertion.MsResponseBodyAssertion;
import io.metersphere.project.api.processor.ExtractPostProcessor; import io.metersphere.project.api.processor.ExtractPostProcessor;
import io.metersphere.project.api.processor.MsProcessor; import io.metersphere.project.api.processor.MsProcessor;
import io.metersphere.project.api.processor.SQLProcessor; import io.metersphere.project.api.processor.SQLProcessor;
@ -101,4 +104,48 @@ public class ConverterUtils {
msScriptElement.setVariableNames(element.getPropertyAsString("variableNames")); msScriptElement.setVariableNames(element.getPropertyAsString("variableNames"));
return msScriptElement; return msScriptElement;
} }
public static void addAssertions(AbstractMsTestElement parent, MsResponseBodyAssertion msResponseBodyAssertion, String assertionType) {
if (CollectionUtils.isEmpty(parent.getChildren())) {
MsAssertionConfig extractPostProcessor = new MsAssertionConfig();
extractPostProcessor.getAssertions().add(msResponseBodyAssertion);
MsCommonElement msCommonElement = new MsCommonElement();
msCommonElement.setAssertionConfig(extractPostProcessor);
LinkedList<AbstractMsTestElement> children = new LinkedList<>();
children.add(msCommonElement);
parent.setChildren(children);
} else {
AbstractMsTestElement child = parent.getChildren().getFirst();
if (child instanceof MsCommonElement msCommonElement) {
MsAssertionConfig assertionConfig = msCommonElement.getAssertionConfig();
if (assertionConfig == null) {
assertionConfig = new MsAssertionConfig();
assertionConfig.getAssertions().add(msResponseBodyAssertion);
msCommonElement.setAssertionConfig(assertionConfig);
} else {
boolean hasRepBodyAssertion = false;
for (MsAssertion assertion : assertionConfig.getAssertions()) {
if (assertion instanceof MsResponseBodyAssertion) {
hasRepBodyAssertion = true;
if ("JSON_PATH".equals(assertionType)) {
((MsResponseBodyAssertion) assertion).setJsonPathAssertion(msResponseBodyAssertion.getJsonPathAssertion());
} else if ("XPATH".equals(assertionType)) {
((MsResponseBodyAssertion) assertion).setXpathAssertion(msResponseBodyAssertion.getXpathAssertion());
}
}
}
if (!hasRepBodyAssertion) {
assertionConfig.getAssertions().add(msResponseBodyAssertion);
}
}
} else {
MsAssertionConfig assertionConfig = new MsAssertionConfig();
assertionConfig.getAssertions().add(msResponseBodyAssertion);
MsCommonElement msCommonElement = new MsCommonElement();
msCommonElement.setAssertionConfig(assertionConfig);
parent.getChildren().add(msCommonElement);
}
}
}
} }