From 95261f22a000f3a3f45059e6e7ddc5437396950c Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Thu, 6 Jul 2023 18:41:01 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=96=87=E6=A1=A3=E6=96=AD=E8=A8=80=E7=A9=BA?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E7=9A=84=E9=95=BF=E5=BA=A6=3D0=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E7=9A=84=E7=BC=BA=E9=99=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1027583 --user=王孝刚 【接口定义】github#25361响应结果中 dataInfos 字段为空,使用 JSON 文档结构校验-断言dataInfos 长度大于0,最终断言成功。 https://www.tapd.cn/55049933/s/1389963 --- .../io/metersphere/utils/DocumentUtils.java | 20 +++++--------- .../jmeter/assertions/JSONPathAssertion.java | 26 ++++++++++++++++--- .../jmeter/assertions/XMLAssertion.java | 2 +- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/framework/sdk-parent/jmeter/src/main/java/io/metersphere/utils/DocumentUtils.java b/framework/sdk-parent/jmeter/src/main/java/io/metersphere/utils/DocumentUtils.java index 2aed6d5fb9..910e88602b 100644 --- a/framework/sdk-parent/jmeter/src/main/java/io/metersphere/utils/DocumentUtils.java +++ b/framework/sdk-parent/jmeter/src/main/java/io/metersphere/utils/DocumentUtils.java @@ -35,7 +35,7 @@ public class DocumentUtils { isTrue = StringUtils.contains(resValue, expectedValue); break; case "length_eq": - isTrue = getLength(subj, decimalFormatter) == numberOf(item.getValue()); + isTrue = getLength(resValue, decimalFormatter) == numberOf(item.getValue()); break; case "length_not_eq": isTrue = getLength(subj, decimalFormatter) != numberOf(item.getValue()); @@ -116,18 +116,12 @@ public class DocumentUtils { } - private static int getLength(Object value) { - if (value != null) { - if (value instanceof List) { - return ((List) value).size(); - } - return value.toString().length(); - } - return 0; - } - private static int getLength(Object value, ThreadLocal decimalFormatter) { if (value != null) { + String resValue = objectToString(value, decimalFormatter); + if (StringUtils.equals(resValue, "[[]]")) { + return 0; + } if (value instanceof Map) { return ((Map) value).size(); } else if (value instanceof List) { @@ -167,7 +161,7 @@ public class DocumentUtils { return getType(value); } - public static String documentMsg(String name, Object resValue, String condition) { + public static String documentMsg(String name, Object resValue, String condition, ThreadLocal decimalFormatter) { String msg = ""; if (StringUtils.isNotEmpty(condition)) { ElementCondition elementCondition = JsonUtils.parseObject(condition, ElementCondition.class); @@ -176,7 +170,7 @@ public class DocumentUtils { if (StringUtils.equalsAny(item.getKey(), "value_eq", "value_not_eq", "value_in")) { msg = resValue != null ? resValue.toString() : ""; } else if (StringUtils.equalsAny(item.getKey(), "length_eq", "length_not_eq", "length_gt", "length_lt")) { - msg = "长度是:" + getLength(resValue) + ""; + msg = "长度是:" + getLength(resValue, decimalFormatter) + ""; } else { msg = resValue != null ? resValue.toString() : ""; } diff --git a/framework/sdk-parent/jmeter/src/main/java/org/apache/jmeter/assertions/JSONPathAssertion.java b/framework/sdk-parent/jmeter/src/main/java/org/apache/jmeter/assertions/JSONPathAssertion.java index 0dfb27a774..b3f18d14fc 100644 --- a/framework/sdk-parent/jmeter/src/main/java/org/apache/jmeter/assertions/JSONPathAssertion.java +++ b/framework/sdk-parent/jmeter/src/main/java/org/apache/jmeter/assertions/JSONPathAssertion.java @@ -19,6 +19,9 @@ package org.apache.jmeter.assertions; import com.jayway.jsonpath.JsonPath; import io.metersphere.utils.DocumentUtils; +import io.metersphere.utils.JsonUtils; +import io.metersphere.vo.Condition; +import io.metersphere.vo.ElementCondition; import net.minidev.json.JSONArray; import net.minidev.json.JSONObject; import net.minidev.json.JSONValue; @@ -126,6 +129,7 @@ public class JSONPathAssertion extends AbstractTestElement implements Serializab public boolean isUseRegex() { return getPropertyAsBoolean(ISREGEX, true); } + private static final String KEY_PRE = "[]"; private void doAssert(String jsonString) { @@ -179,7 +183,7 @@ public class JSONPathAssertion extends AbstractTestElement implements Serializab msg = "Value < '%s', but found '%s'"; break; case "DOCUMENT": - msg = DocumentUtils.documentMsg(this.getName(), value, this.getElementCondition()); + msg = DocumentUtils.documentMsg(this.getName(), value, this.getElementCondition(),decimalFormatter); break; } } else { @@ -193,8 +197,24 @@ public class JSONPathAssertion extends AbstractTestElement implements Serializab private boolean arrayMatched(JSONArray value) { List result = new ArrayList<>(); + + boolean isDocument = false; + if (StringUtils.isNotEmpty(this.getElementCondition())) { + ElementCondition elementCondition = JsonUtils.parseObject(this.getElementCondition(), ElementCondition.class); + if (CollectionUtils.isNotEmpty(elementCondition.getConditions()) && StringUtils.equals(this.getOption(), "DOCUMENT")) { + for (Condition item : elementCondition.getConditions()) { + if (StringUtils.equalsAnyIgnoreCase(item.getKey(), "length_eq", "length_not_eq", "length_gt", "length_lt")) { + isDocument = true; + } + } + } + } + if (isDocument) { + return isEquals(value); + } + for (Object subj : value.toArray()) { - if (!StringUtils.equalsAnyIgnoreCase(getOption(), "NOT_CONTAINS","EQUALS")) { + if (!StringUtils.equalsAnyIgnoreCase(getOption(), "NOT_CONTAINS", "EQUALS")) { if (subj == null && this.isExpectNull() || isEquals(subj)) { return true; } @@ -235,7 +255,7 @@ public class JSONPathAssertion extends AbstractTestElement implements Serializab private boolean isEquals(Object subj) { String str = DocumentUtils.objectToString(subj, decimalFormatter); - if (StringUtils.equals(str,KEY_PRE)) { + if (StringUtils.equals(str, KEY_PRE)) { return false; } if (isUseRegex()) { diff --git a/framework/sdk-parent/jmeter/src/main/java/org/apache/jmeter/assertions/XMLAssertion.java b/framework/sdk-parent/jmeter/src/main/java/org/apache/jmeter/assertions/XMLAssertion.java index e3b3453161..2ca16b1bf4 100644 --- a/framework/sdk-parent/jmeter/src/main/java/org/apache/jmeter/assertions/XMLAssertion.java +++ b/framework/sdk-parent/jmeter/src/main/java/org/apache/jmeter/assertions/XMLAssertion.java @@ -136,7 +136,7 @@ public class XMLAssertion extends AbstractTestElement implements Serializable, A } } if (!this.isEquals(value)) { - String msg = DocumentUtils.documentMsg(this.getName(), value, this.getCondition()); + String msg = DocumentUtils.documentMsg(this.getName(), value, this.getCondition(), decimalFormatter); throw new IllegalStateException(String.format(msg, this.getExpectedValue(), DocumentUtils.objectToString(value, decimalFormatter))); } }