From 7652bca100105329bdc2bb0a99b011505ae040ab Mon Sep 17 00:00:00 2001 From: AgAngle <1323481023@qq.com> Date: Thu, 9 May 2024 20:01:16 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=8F=90=E5=89=8D=E5=92=8C=E6=96=AD=E8=A8=80?= =?UTF-8?q?=E7=BB=93=E6=9E=9C=E5=B1=95=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sdk/dto/api/result/ExtractResult.java | 15 ++++++++ .../api/result/ResponseAssertionResult.java | 34 +++++++++++++++++++ .../sdk/dto/api/result/ResponseResult.java | 5 +-- .../assertion/AssertionConverter.java | 13 +++++++ .../ResponseCodeAssertionConverter.java | 4 ++- .../ResponseHeaderAssertionConverter.java | 4 +++ .../ResponseTimeAssertionConverter.java | 2 ++ .../assertion/ScriptAssertionConverter.java | 6 +++- .../assertion/VariableAssertionConverter.java | 7 ++-- .../body/JSONPathAssertionConverter.java | 8 +++-- .../body/RegexAssertionConverter.java | 4 +++ .../ResponseBodyTypeAssertionConverter.java | 10 ++++++ .../body/XPathAssertionConverter.java | 13 ++++--- .../processor/extract/ExtractConverter.java | 7 ++++ .../extract/JSONPathExtractConverter.java | 1 + .../extract/RegexExtractConverter.java | 1 + .../extract/XPathExtractConverter.java | 2 ++ 17 files changed, 122 insertions(+), 14 deletions(-) create mode 100644 backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/ExtractResult.java diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/ExtractResult.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/ExtractResult.java new file mode 100644 index 0000000000..212b5a82e0 --- /dev/null +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/ExtractResult.java @@ -0,0 +1,15 @@ +package io.metersphere.sdk.dto.api.result; + +import lombok.Data; + +/** + * @Author: jianxing + * @CreateTime: 2024-05-09 17:08 + */ +@Data +public class ExtractResult { + private String name; + private String value; + private String type; + private String expression; +} diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/ResponseAssertionResult.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/ResponseAssertionResult.java index 5906b698e7..842b4ad084 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/ResponseAssertionResult.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/ResponseAssertionResult.java @@ -16,6 +16,7 @@ public class ResponseAssertionResult { /** * 断言内容 */ + @Deprecated private String content; /** @@ -23,6 +24,27 @@ public class ResponseAssertionResult { */ private String script; + /** + * 实际值 + */ + private String actualValue; + + /** + * 期望值 + */ + private String expectedValue; + + /** + * 断言类型 + * {@link AssertionResultType} + */ + private String assertionType; + + /** + * 断言条件 + */ + private String condition; + /** * 断言结果 */ @@ -32,4 +54,16 @@ public class ResponseAssertionResult { * 是否通过 */ private boolean pass; + + public enum AssertionResultType { + DOCUMENT, + RESPONSE_CODE, + RESPONSE_HEADER, + RESPONSE_TIME, + SCRIPT, + VARIABLE, + JSON_PATH, + XPATH, + REGEX + } } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/ResponseResult.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/ResponseResult.java index b61429b74d..db1d78bc44 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/ResponseResult.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/ResponseResult.java @@ -51,10 +51,7 @@ public class ResponseResult { */ private String contentType; - /** - * 过程变量处理信息 - */ - private String vars; + private List extractResults; /** * 图片内容 diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/AssertionConverter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/AssertionConverter.java index 414d8077be..0999db8a1a 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/AssertionConverter.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/AssertionConverter.java @@ -8,6 +8,7 @@ import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.StringUtils; import org.apache.jmeter.assertions.ResponseAssertion; import org.apache.jmeter.save.SaveService; +import org.apache.jmeter.testelement.AbstractTestElement; import org.apache.jmeter.testelement.TestElement; import org.apache.jorphan.collections.HashTree; @@ -64,4 +65,16 @@ public abstract class AssertionConverter { } return text; } + + public static void setMsAssertionInfoProperty(AbstractTestElement assertion, String assertionType, String name, String condition, String expectedValue) { + // 保存断言信息 + assertion.setProperty("name", name); + assertion.setProperty("assertionType", assertionType); + assertion.setProperty("condition", condition); + assertion.setProperty("expectedValue", expectedValue); + } + + public static void setMsAssertionInfoProperty(AbstractTestElement assertion, String assertionType, String name) { + AssertionConverter.setMsAssertionInfoProperty(assertion, assertionType, name, null, null); + } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/ResponseCodeAssertionConverter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/ResponseCodeAssertionConverter.java index b10f75f683..e5517f58f7 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/ResponseCodeAssertionConverter.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/ResponseCodeAssertionConverter.java @@ -2,6 +2,7 @@ package io.metersphere.api.parser.jmeter.processor.assertion; import io.metersphere.project.api.assertion.MsResponseCodeAssertion; import io.metersphere.plugin.api.dto.ParameterConfig; +import io.metersphere.sdk.dto.api.result.ResponseAssertionResult; import org.apache.commons.lang3.StringUtils; import org.apache.jmeter.assertions.ResponseAssertion; import org.apache.jorphan.collections.HashTree; @@ -27,7 +28,6 @@ public class ResponseCodeAssertionConverter extends AssertionConverter "((?:[\\r\\n]%key|^%key):.*%value)"; case EQUALS, NOT_EQUALS -> "((?:[\\r\\n]%key|^%key):\\s*(?:%value[\\r\\n]|%value$))"; diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/ResponseTimeAssertionConverter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/ResponseTimeAssertionConverter.java index 405b30296a..2721502443 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/ResponseTimeAssertionConverter.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/ResponseTimeAssertionConverter.java @@ -2,6 +2,7 @@ package io.metersphere.api.parser.jmeter.processor.assertion; import io.metersphere.project.api.assertion.MsResponseTimeAssertion; import io.metersphere.plugin.api.dto.ParameterConfig; +import io.metersphere.sdk.dto.api.result.ResponseAssertionResult; import org.apache.jmeter.assertions.DurationAssertion; import org.apache.jmeter.save.SaveService; import org.apache.jmeter.testelement.TestElement; @@ -31,6 +32,7 @@ public class ResponseTimeAssertionConverter extends AssertionConverter { } return false; } + + public void setMsAssertionInfoProperty(AbstractTestElement assertion, String assertionType, String name, String condition, String expectedValue) { + AssertionConverter.setMsAssertionInfoProperty(assertion, assertionType, name, condition, expectedValue); + } + + public void setMsAssertionInfoProperty(AbstractTestElement assertion, String assertionType, String name) { + AssertionConverter.setMsAssertionInfoProperty(assertion, assertionType, name); + } } \ No newline at end of file diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/body/XPathAssertionConverter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/body/XPathAssertionConverter.java index ea546a5803..272ce95e6c 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/body/XPathAssertionConverter.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/body/XPathAssertionConverter.java @@ -4,6 +4,7 @@ import io.metersphere.project.api.assertion.body.MsXPathAssertion; import io.metersphere.project.api.assertion.body.MsXPathAssertionItem; import io.metersphere.project.api.processor.extract.XPathExtract; import io.metersphere.plugin.api.dto.ParameterConfig; +import io.metersphere.sdk.dto.api.result.ResponseAssertionResult; import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.StringUtils; import org.apache.jmeter.assertions.XPath2Assertion; @@ -43,13 +44,15 @@ public class XPathAssertionConverter extends ResponseBodyTypeAssertionConverter< private XPathAssertion parse2XPathAssertion(MsXPathAssertionItem msAssertion, Boolean globalEnable) { XPathAssertion assertion = new XPathAssertion(); + String expression = msAssertion.getExpression(); + setMsAssertionInfoProperty(assertion, ResponseAssertionResult.AssertionResultType.XPATH.name(), expression); assertion.setEnabled(msAssertion.getEnable()); assertion.setTolerant(true); assertion.setValidating(false); - assertion.setName("Response data expect xpath " + msAssertion.getExpression()); + assertion.setName("Response data expect xpath " + expression); assertion.setProperty(TestElement.TEST_CLASS, XPathAssertion.class.getName()); assertion.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass(XPATH_ASSERTION_GUI)); - assertion.setXPathString(msAssertion.getExpression()); + assertion.setXPathString(expression); assertion.setNegated(false); assertion.setEnabled(msAssertion.getEnable()); if (BooleanUtils.isFalse(globalEnable)) { @@ -61,11 +64,13 @@ public class XPathAssertionConverter extends ResponseBodyTypeAssertionConverter< private XPath2Assertion parse2XPath2Assertion(MsXPathAssertionItem msAssertion, Boolean globalEnable) { XPath2Assertion assertion = new XPath2Assertion(); + String expression = msAssertion.getExpression(); + setMsAssertionInfoProperty(assertion, ResponseAssertionResult.AssertionResultType.XPATH.name(), expression); assertion.setEnabled(msAssertion.getEnable()); - assertion.setName("Response date expect xpath " + msAssertion.getExpression()); + assertion.setName("Response date expect xpath " +expression); assertion.setProperty(TestElement.TEST_CLASS, XPath2Assertion.class.getName()); assertion.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass(X_PATH_2_ASSERTION_GUI)); - assertion.setXPathString(msAssertion.getExpression()); + assertion.setXPathString(expression); assertion.setNegated(false); assertion.setEnabled(msAssertion.getEnable()); if (BooleanUtils.isFalse(globalEnable)) { diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/extract/ExtractConverter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/extract/ExtractConverter.java index 59c230faaa..15ad723d5f 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/extract/ExtractConverter.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/extract/ExtractConverter.java @@ -4,6 +4,7 @@ import io.metersphere.project.api.processor.extract.MsExtract; import io.metersphere.project.api.processor.extract.ResultMatchingExtract; import io.metersphere.plugin.api.dto.ParameterConfig; import org.apache.commons.lang3.StringUtils; +import org.apache.jmeter.testelement.AbstractTestElement; import org.apache.jorphan.collections.HashTree; /** @@ -36,4 +37,10 @@ public abstract class ExtractConverter { } return 1; } + + public static void setMsExtractInfoProperty(AbstractTestElement extract, String type, String expression) { + // 保存断言信息 + extract.setProperty("type", type); + extract.setProperty("expression", expression); + } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/extract/JSONPathExtractConverter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/extract/JSONPathExtractConverter.java index 3dfb1cdfc1..615b4b7e6e 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/extract/JSONPathExtractConverter.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/extract/JSONPathExtractConverter.java @@ -30,6 +30,7 @@ public class JSONPathExtractConverter extends ExtractConverter extractor.setComputeConcatenation(true); } extractor.setEnabled(msExtract.getEnable()); + setMsExtractInfoProperty(extractor, msExtract.getVariableType(), msExtract.getExpression()); hashTree.add(extractor); } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/extract/RegexExtractConverter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/extract/RegexExtractConverter.java index 96e47cd1b4..4c8c93014a 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/extract/RegexExtractConverter.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/extract/RegexExtractConverter.java @@ -30,6 +30,7 @@ public class RegexExtractConverter extends ExtractConverter { extractor.setTemplate(getTemplate(msExtract.getExpressionMatchingRule())); // 处理匹配多条等匹配规则 extractor.setMatchNumber(parseResultMatchingRule(msExtract)); + setMsExtractInfoProperty(extractor, msExtract.getVariableType(), msExtract.getExpression()); hashTree.add(extractor); } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/extract/XPathExtractConverter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/extract/XPathExtractConverter.java index 8d1281311f..1875b29cfa 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/extract/XPathExtractConverter.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/extract/XPathExtractConverter.java @@ -38,6 +38,7 @@ public class XPathExtractConverter extends ExtractConverter { extractor.setEnabled(msExtract.getEnable()); // 处理匹配多条等匹配规则 extractor.setMatchNumber(parseResultMatchingRule(msExtract)); + setMsExtractInfoProperty(extractor, msExtract.getVariableType(), msExtract.getExpression()); return extractor; } @@ -51,6 +52,7 @@ public class XPathExtractConverter extends ExtractConverter { extractor.setEnabled(msExtract.getEnable()); // 处理匹配多条等匹配规则 extractor.setMatchNumber(parseResultMatchingRule(msExtract)); + setMsExtractInfoProperty(extractor, msExtract.getVariableType(), msExtract.getExpression()); return extractor; } }