fix (接口测试): 修复json断言数组校验问题

--bug=1008219 --user=赵勇 【接口测试】文档结构断言,json结构,数组值没校验 https://www.tapd.cn/55049933/s/1073920
This commit is contained in:
fit2-zhao 2021-11-24 11:15:25 +08:00 committed by fit2-zhao
parent 1e719d1294
commit b4801f4ba0
3 changed files with 43 additions and 6 deletions

View File

@ -81,7 +81,16 @@ public class Document {
if (StringUtils.isEmpty(item.getGroupId())) {
if (!item.getId().equals("root")) {
if (parentNode != null) {
item.setJsonPath(parentNode.getJsonPath() + "." + item.getName());
if (parentNode.getType().equals("array") && item.getType().equals("string")) {
try {
int index = StringUtils.isNotEmpty(item.getName()) ? Integer.parseInt(item.getName()) : 0;
item.setJsonPath(parentNode.getJsonPath() + "[" + index + "]");
} catch (Exception e) {
item.setJsonPath(parentNode.getJsonPath() + "." + item.getName());
}
} else {
item.setJsonPath(parentNode.getJsonPath() + "." + item.getName());
}
} else {
item.setJsonPath("$." + item.getName());
}
@ -107,7 +116,16 @@ public class Document {
for (DocumentElement item : dataList) {
if (StringUtils.isEmpty(item.getGroupId())) {
if (parentNode != null) {
item.setJsonPath(parentNode.getJsonPath() + "." + item.getName());
if (StringUtils.equals(parentNode.getType(), "array") && item.getType().equals("string")) {
try {
int index = StringUtils.isNotEmpty(item.getName()) ? Integer.parseInt(item.getName()) : 0;
item.setJsonPath(parentNode.getJsonPath() + "[" + index + "]");
} catch (Exception e) {
item.setJsonPath(parentNode.getJsonPath() + "." + item.getName());
}
} else {
item.setJsonPath(parentNode.getJsonPath() + "." + item.getName());
}
} else {
item.setJsonPath("$." + item.getName());
}

View File

@ -46,7 +46,7 @@ public class DocumentElement {
}
}
public DocumentElement(String name, String type, Object expectedOutcome, boolean typeVerification, List<DocumentElement>children) {
public DocumentElement(String name, String type, Object expectedOutcome, boolean typeVerification, List<DocumentElement> children) {
this.id = UUID.randomUUID().toString();
this.name = name;
this.expectedOutcome = expectedOutcome;
@ -62,6 +62,25 @@ public class DocumentElement {
}
}
public DocumentElement(String name, String type, Object expectedOutcome, boolean typeVerification, boolean arrayVerification, List<DocumentElement> children) {
this.id = UUID.randomUUID().toString();
this.name = name;
this.expectedOutcome = expectedOutcome;
this.type = type;
this.typeVerification = typeVerification;
this.arrayVerification = arrayVerification;
this.children = children == null ? this.children = new LinkedList<>() : children;
this.rowspan = 1;
this.contentVerification = "value_eq";
if (StringUtils.equalsAny(type, "object", "array")) {
this.contentVerification = "none";
} else if (expectedOutcome == null || StringUtils.isEmpty(expectedOutcome.toString())) {
this.contentVerification = "none";
}
}
public DocumentElement(String id, String name, String type, Object expectedOutcome, List<DocumentElement> children) {
this.id = id;
this.name = name;

View File

@ -74,7 +74,7 @@ public class JSONSchemaToDocumentUtils {
Object value = null;
boolean required = requiredList.contains(propertyName);
if (object.has("default")) {
value = object.get("default") != null ? object.get("default").toString() : "";
value = object.get("default") != null ? object.get("default").getAsString() : "";
concept.add(new DocumentElement(propertyName, propertyObjType, value, required, null));
} else if (object.has("enum")) {
try {
@ -97,7 +97,7 @@ public class JSONSchemaToDocumentUtils {
} else if (propertyObjType.equals("string")) {
// 先设置空值
if (object.has("default")) {
value = object.get("default") != null ? object.get("default").toString() : "";
value = object.get("default") != null ? object.get("default").getAsString() : "";
}
if (object.has("mock") && object.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString()) && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString())) {
value = ScriptEngineUtils.buildFunctionCallString(object.get("mock").getAsJsonObject().get("mock").getAsString());
@ -140,7 +140,7 @@ public class JSONSchemaToDocumentUtils {
concept.add(new DocumentElement(propertyName, propertyObjType, value, required, null));
} else if (propertyObjType.equals("array")) {
List<DocumentElement> elements = new LinkedList<>();
concept.add(new DocumentElement(propertyName, propertyObjType, "", requiredList.contains(propertyName), elements));
concept.add(new DocumentElement(propertyName, propertyObjType, "", requiredList.contains(propertyName), true, elements));
JsonArray jsonArray = object.get("items").getAsJsonArray();
analyzeArray(propertyName, jsonArray, elements, requiredList);
} else if (propertyObjType.equals("object")) {