fix(接口测试): 修复文档结构断言校验类型失败问题

--bug=1009686 --user=赵勇 【科锐数字科技 】这家公司的测试在用json文档结构断言时发现,发现类型校验没有生效,比如期望是array类型的,或者String类型的,但是实际类型不是这个。 https://www.tapd.cn/55049933/s/1095061
This commit is contained in:
fit2-zhao 2022-01-20 16:49:53 +08:00 committed by fit2-zhao
parent 903f2ef4a9
commit f3ef2727be
1 changed files with 20 additions and 8 deletions

View File

@ -25,6 +25,8 @@ public class Document {
private List<DocumentElement> xml; private List<DocumentElement> xml;
private String assertionName; private String assertionName;
private static final String delimiter = "split==";
public void parseJson(HashTree hashTree, String name) { public void parseJson(HashTree hashTree, String name) {
this.assertionName = name; this.assertionName = name;
// 提取出合并的权限 // 提取出合并的权限
@ -56,20 +58,24 @@ public class Document {
private void conditions(List<DocumentElement> dataList, Map<String, ElementCondition> conditionMap) { private void conditions(List<DocumentElement> dataList, Map<String, ElementCondition> conditionMap) {
dataList.forEach(item -> { dataList.forEach(item -> {
if (StringUtils.isEmpty(item.getGroupId())) { if (StringUtils.isEmpty(item.getGroupId())) {
conditionMap.put(item.getId(), ElementCondition elementCondition =
new ElementCondition(item.isInclude(), item.isTypeVerification(), item.isArrayVerification(), new ElementCondition(item.isInclude(), item.isTypeVerification(), item.isArrayVerification(),
new LinkedList<Condition>() {{ new LinkedList<Condition>() {{
this.add(new Condition(item.getContentVerification(), item.getExpectedOutcome())); this.add(new Condition(item.getContentVerification(), item.getExpectedOutcome()));
}})); }});
elementCondition.setType(item.getType());
conditionMap.put(item.getId(), elementCondition);
} else { } else {
if (conditionMap.containsKey(item.getGroupId())) { if (conditionMap.containsKey(item.getGroupId())) {
conditionMap.get(item.getGroupId()).getConditions().add(new Condition(item.getContentVerification(), item.getExpectedOutcome())); conditionMap.get(item.getGroupId()).getConditions().add(new Condition(item.getContentVerification(), item.getExpectedOutcome()));
} else { } else {
conditionMap.put(item.getGroupId(), ElementCondition elementCondition =
new ElementCondition(item.isInclude(), item.isTypeVerification(), item.isArrayVerification(), new ElementCondition(item.isInclude(), item.isTypeVerification(), item.isArrayVerification(),
new LinkedList<Condition>() {{ new LinkedList<Condition>() {{
this.add(new Condition(item.getContentVerification(), item.getExpectedOutcome())); this.add(new Condition(item.getContentVerification(), item.getExpectedOutcome()));
}})); }});
elementCondition.setType(item.getType());
conditionMap.put(item.getGroupId(), elementCondition);
} }
} }
if (CollectionUtils.isNotEmpty(item.getChildren())) { if (CollectionUtils.isNotEmpty(item.getChildren())) {
@ -147,9 +153,15 @@ public class Document {
private String getConditionStr(DocumentElement item, ElementCondition elementCondition) { private String getConditionStr(DocumentElement item, ElementCondition elementCondition) {
StringBuilder conditionStr = new StringBuilder(); StringBuilder conditionStr = new StringBuilder();
if (elementCondition != null && CollectionUtils.isNotEmpty(elementCondition.getConditions())) { if (elementCondition != null && CollectionUtils.isNotEmpty(elementCondition.getConditions())) {
elementCondition.getConditions().forEach(condition -> { if (elementCondition.isTypeVerification()) {
conditionStr.append(item.getLabel(condition.getKey()).replace("'%'", (condition.getValue() != null ? condition.getValue().toString() : ""))); conditionStr.append("类型:[ " + item.getType() + " ]");
conditionStr.append(" and "); conditionStr.append(" and ");
}
elementCondition.getConditions().forEach(condition -> {
if (!StringUtils.equals("none", condition.getKey())) {
conditionStr.append(item.getLabel(condition.getKey()).replace("'%'", (condition.getValue() != null ? condition.getValue().toString() : "")));
conditionStr.append(" and ");
}
}); });
} }
String label = ""; String label = "";
@ -166,7 +178,7 @@ public class Document {
assertion.setExpectNull(false); assertion.setExpectNull(false);
assertion.setInvert(false); assertion.setInvert(false);
assertion.setName((StringUtils.isNotEmpty(assertionName) ? assertionName : "DocumentAssertion") + ("==" + item.getJsonPath() + " " + getConditionStr(item, elementCondition))); assertion.setName((StringUtils.isNotEmpty(assertionName) ? assertionName : "DocumentAssertion") + (delimiter + item.getJsonPath() + " " + getConditionStr(item, elementCondition)));
assertion.setProperty(TestElement.TEST_CLASS, JSONPathAssertion.class.getName()); assertion.setProperty(TestElement.TEST_CLASS, JSONPathAssertion.class.getName());
assertion.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("JSONPathAssertionGui")); assertion.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("JSONPathAssertionGui"));
assertion.setJsonPath(item.getJsonPath()); assertion.setJsonPath(item.getJsonPath());
@ -185,7 +197,7 @@ public class Document {
private XMLAssertion newXMLAssertion(DocumentElement item, ElementCondition elementCondition) { private XMLAssertion newXMLAssertion(DocumentElement item, ElementCondition elementCondition) {
XMLAssertion assertion = new XMLAssertion(); XMLAssertion assertion = new XMLAssertion();
assertion.setEnabled(true); assertion.setEnabled(true);
assertion.setName((StringUtils.isNotEmpty(assertionName) ? assertionName : "XMLAssertion") + "==" + (item.getJsonPath() + " " + getConditionStr(item, elementCondition))); assertion.setName((StringUtils.isNotEmpty(assertionName) ? assertionName : "XMLAssertion") + delimiter + (item.getJsonPath() + " " + getConditionStr(item, elementCondition)));
assertion.setProperty(TestElement.TEST_CLASS, XMLAssertion.class.getName()); assertion.setProperty(TestElement.TEST_CLASS, XMLAssertion.class.getName());
assertion.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("XMLAssertionGui")); assertion.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("XMLAssertionGui"));
assertion.setProperty("XML_PATH", item.getJsonPath()); assertion.setProperty("XML_PATH", item.getJsonPath());