+ @Sql([CLEAR_DATA, SET_DATA])
+ def 'Get data node by xpath with all descendants.'() {
+ when: 'data node is requested with all descendants'
+ def result = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES,
+ XPATH_DATA_NODE_WITH_LEAVES, INCLUDE_ALL_DESCENDANTS)
+ def mappedResult = treeToFlatMapByXpath(new HashMap<>(), result)
+ then: 'data node is returned with all the descendants populated'
+ assert mappedResult.size() == 4
+ assert result.getChildDataNodes().size() == 2
+ assert mappedResult.get('/parent-100/child-001').getChildDataNodes().size() == 0
+ assert mappedResult.get('/parent-100/child-002').getChildDataNodes().size() == 1
+ and: 'extracted leaves maps are matching expected'
+ mappedResult.forEach(
+ (xpath, dataNode) ->
+ assertLeavesMaps(dataNode.getLeaves(), expectedLeavesByXpathMap[xpath])
+ )
+ }
+
+ def static assertLeavesMaps(actualLeavesMap, expectedLeavesMap) {
+ expectedLeavesMap.forEach((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
+ }
+ }
+ )
+ return true
+ }
+
+ def static treeToFlatMapByXpath(Map<String, DataNode> flatMap, DataNode dataNodeTree) {
+ flatMap.put(dataNodeTree.getXpath(), dataNodeTree)
+ dataNodeTree.getChildDataNodes()
+ .forEach(childDataNode -> treeToFlatMapByXpath(flatMap, childDataNode))
+ return flatMap
+ }
+
+ @Unroll
+ @Sql([CLEAR_DATA, SET_DATA])
+ def 'Get data node error scenario: #scenario.'() {
+ when: 'attempt to get data node with #scenario'
+ objectUnderTest.getDataNode(dataspaceName, anchorName, xpath, OMIT_DESCENDANTS)
+ then: 'a #expectedException is thrown'
+ thrown(expectedException)
+ where: 'the following data is used'
+ scenario | dataspaceName | anchorName | xpath || expectedException
+ 'non-existing dataspace' | 'NO DATASPACE' | 'not relevant' | 'not relevant' || DataspaceNotFoundException
+ 'non-existing anchor' | DATASPACE_NAME | 'NO ANCHOR' | 'not relevant' || AnchorNotFoundException
+ 'non-existing xpath' | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | 'NO XPATH' || DataNodeNotFoundException
+ }