fix(接口测试): 修复文档断言空数组的长度=0失败的缺陷

--bug=1027583 --user=王孝刚 【接口定义】github#25361响应结果中 dataInfos 字段为空,使用 JSON
文档结构校验-断言dataInfos 长度大于0,最终断言成功。 https://www.tapd.cn/55049933/s/1389963
This commit is contained in:
wxg0103 2023-07-06 18:41:01 +08:00 committed by 刘瑞斌
parent 75cf17d836
commit 95261f22a0
3 changed files with 31 additions and 17 deletions

View File

@ -35,7 +35,7 @@ public class DocumentUtils {
isTrue = StringUtils.contains(resValue, expectedValue); isTrue = StringUtils.contains(resValue, expectedValue);
break; break;
case "length_eq": case "length_eq":
isTrue = getLength(subj, decimalFormatter) == numberOf(item.getValue()); isTrue = getLength(resValue, decimalFormatter) == numberOf(item.getValue());
break; break;
case "length_not_eq": case "length_not_eq":
isTrue = getLength(subj, decimalFormatter) != numberOf(item.getValue()); 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<DecimalFormat> decimalFormatter) { private static int getLength(Object value, ThreadLocal<DecimalFormat> decimalFormatter) {
if (value != null) { if (value != null) {
String resValue = objectToString(value, decimalFormatter);
if (StringUtils.equals(resValue, "[[]]")) {
return 0;
}
if (value instanceof Map) { if (value instanceof Map) {
return ((Map) value).size(); return ((Map) value).size();
} else if (value instanceof List) { } else if (value instanceof List) {
@ -167,7 +161,7 @@ public class DocumentUtils {
return getType(value); return getType(value);
} }
public static String documentMsg(String name, Object resValue, String condition) { public static String documentMsg(String name, Object resValue, String condition, ThreadLocal<DecimalFormat> decimalFormatter) {
String msg = ""; String msg = "";
if (StringUtils.isNotEmpty(condition)) { if (StringUtils.isNotEmpty(condition)) {
ElementCondition elementCondition = JsonUtils.parseObject(condition, ElementCondition.class); 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")) { if (StringUtils.equalsAny(item.getKey(), "value_eq", "value_not_eq", "value_in")) {
msg = resValue != null ? resValue.toString() : ""; msg = resValue != null ? resValue.toString() : "";
} else if (StringUtils.equalsAny(item.getKey(), "length_eq", "length_not_eq", "length_gt", "length_lt")) { } else if (StringUtils.equalsAny(item.getKey(), "length_eq", "length_not_eq", "length_gt", "length_lt")) {
msg = "长度是:" + getLength(resValue) + ""; msg = "长度是:" + getLength(resValue, decimalFormatter) + "";
} else { } else {
msg = resValue != null ? resValue.toString() : ""; msg = resValue != null ? resValue.toString() : "";
} }

View File

@ -19,6 +19,9 @@ package org.apache.jmeter.assertions;
import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.JsonPath;
import io.metersphere.utils.DocumentUtils; 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.JSONArray;
import net.minidev.json.JSONObject; import net.minidev.json.JSONObject;
import net.minidev.json.JSONValue; import net.minidev.json.JSONValue;
@ -126,6 +129,7 @@ public class JSONPathAssertion extends AbstractTestElement implements Serializab
public boolean isUseRegex() { public boolean isUseRegex() {
return getPropertyAsBoolean(ISREGEX, true); return getPropertyAsBoolean(ISREGEX, true);
} }
private static final String KEY_PRE = "[]"; private static final String KEY_PRE = "[]";
private void doAssert(String jsonString) { private void doAssert(String jsonString) {
@ -179,7 +183,7 @@ public class JSONPathAssertion extends AbstractTestElement implements Serializab
msg = "Value < '%s', but found '%s'"; msg = "Value < '%s', but found '%s'";
break; break;
case "DOCUMENT": case "DOCUMENT":
msg = DocumentUtils.documentMsg(this.getName(), value, this.getElementCondition()); msg = DocumentUtils.documentMsg(this.getName(), value, this.getElementCondition(),decimalFormatter);
break; break;
} }
} else { } else {
@ -193,6 +197,22 @@ public class JSONPathAssertion extends AbstractTestElement implements Serializab
private boolean arrayMatched(JSONArray value) { private boolean arrayMatched(JSONArray value) {
List<Boolean> result = new ArrayList<>(); List<Boolean> 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()) { 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)) { if (subj == null && this.isExpectNull() || isEquals(subj)) {

View File

@ -136,7 +136,7 @@ public class XMLAssertion extends AbstractTestElement implements Serializable, A
} }
} }
if (!this.isEquals(value)) { 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))); throw new IllegalStateException(String.format(msg, this.getExpectedValue(), DocumentUtils.objectToString(value, decimalFormatter)));
} }
} }