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);
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<DecimalFormat> 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<DecimalFormat> 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() : "";
}

View File

@ -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<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()) {
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()) {

View File

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