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))); } }