- then: 'the system creates a (root) fragment without a parent and 2 children (categories)'
- result.childDataNodes.size() == 2
- and: 'each child (category) has the root fragment (result) as parent and in turn as 1 child (a list of books)'
- result.childDataNodes.each { it.childDataNodes.size() == 1 }
- and: 'the fragments have the correct xpaths'
- assert result.xpath == '/bookstore'
- assert result.childDataNodes.collect { it.xpath }
- .containsAll(["/bookstore/categories[@code='01']", "/bookstore/categories[@code='02']"])
+ def mappedResult = treeToFlatMapByXpath(new HashMap<>(), result)
+ then: '5 DataNode objects with unique xpath were created in total'
+ mappedResult.size() == 5
+ and: 'all expected xpaths were built'
+ mappedResult.keySet().containsAll(expectedLeavesByXpathMap.keySet())
+ and: 'each data node contains the expected attributes'
+ mappedResult.each {
+ xpath, dataNode -> assertLeavesMaps(dataNode.getLeaves(), expectedLeavesByXpathMap[xpath])
+ }
+ }
+
+ def static assertLeavesMaps(actualLeavesMap, expectedLeavesMap) {
+ expectedLeavesMap.each { key, value ->
+ {
+ def actualValue = actualLeavesMap[key]
+ if (value instanceof Collection<?> && actualValue instanceof Collection<?>) {
+ assert value.size() == actualValue.size()
+ assert value.containsAll(actualValue)
+ } else {
+ assert value == actualValue
+ }
+ }
+ }
+ }
+
+ def treeToFlatMapByXpath(Map<String, DataNode> flatMap, DataNode dataNodeTree) {
+ flatMap.put(dataNodeTree.getXpath(), dataNodeTree)
+ dataNodeTree.getChildDataNodes()
+ .forEach(childDataNode -> treeToFlatMapByXpath(flatMap, childDataNode))
+ return flatMap