diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/ms/HTTPSamplerConverter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/ms/HTTPSamplerConverter.java index caa2e3447c..46f508c1b6 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/ms/HTTPSamplerConverter.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/ms/HTTPSamplerConverter.java @@ -1,6 +1,7 @@ package io.metersphere.api.parser.ms; 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.MsHeader; 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.sdk.util.BeanUtils; 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.jmeter.protocol.http.control.HeaderManager; import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy; @@ -35,6 +36,13 @@ public class HTTPSamplerConverter extends AbstractMsElementConverter children = new LinkedList<>(); + children.add(msCommonElement); + msHTTPElement.setChildren(children); + parseChild(msHTTPElement, httpSampler, hashTree); } @@ -61,6 +69,7 @@ public class HTTPSamplerConverter extends AbstractMsElementConverter { + @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 assertions = new ArrayList<>(); + assertions.add(jsonPathAssertionItem); + + msResponseBodyAssertion.setJsonPathAssertion(new MsJSONPathAssertion() {{ + this.setAssertions(assertions); + }}); + + ConverterUtils.addAssertions(parent, msResponseBodyAssertion, "JSON_PATH"); + } +} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/ms/http/post/ResponseAssertionConverter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/ms/http/post/ResponseAssertionConverter.java deleted file mode 100644 index 14843fc009..0000000000 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/ms/http/post/ResponseAssertionConverter.java +++ /dev/null @@ -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 { - @Override - public void toMsElement(AbstractMsTestElement parent, ResponseAssertion element, HashTree hashTree) { - - } -} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/ms/http/post/XPath2ExtractorConverter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/ms/http/post/XPath2ExtractorConverter.java index 9d0fee5a45..a21dc26e99 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/ms/http/post/XPath2ExtractorConverter.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/ms/http/post/XPath2ExtractorConverter.java @@ -16,7 +16,6 @@ public class XPath2ExtractorConverter extends AbstractMsElementConverter { + @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 assertions = new ArrayList<>(); + assertions.add(xpathItem); + msXPathAssertion.setAssertions(assertions); + msResponseBodyAssertion.setXpathAssertion(msXPathAssertion); + ConverterUtils.addAssertions(parent, msResponseBodyAssertion, "XPATH"); + } +} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/utils/ConverterUtils.java b/backend/services/api-test/src/main/java/io/metersphere/api/utils/ConverterUtils.java index b8434d85a6..f5bb92cf47 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/utils/ConverterUtils.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/utils/ConverterUtils.java @@ -1,7 +1,10 @@ package io.metersphere.api.utils; +import io.metersphere.api.dto.assertion.MsAssertionConfig; import io.metersphere.api.dto.request.MsCommonElement; 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.MsProcessor; import io.metersphere.project.api.processor.SQLProcessor; @@ -101,4 +104,48 @@ public class ConverterUtils { msScriptElement.setVariableNames(element.getPropertyAsString("variableNames")); 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 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); + } + } + + } }