1 package org.onap.cps.model
3 import org.onap.cps.TestUtils
4 import org.onap.cps.spi.model.DataNode
5 import org.onap.cps.spi.model.DataNodeBuilder
6 import org.onap.cps.utils.YangUtils
7 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
8 import spock.lang.Specification
10 class DataNodeBuilderSpec extends Specification {
12 Map<String, Map<String, Object>> expectedLeavesByXpathMap = [
14 '/test-tree/branch[@name=\'Left\']' : [name: 'Left'],
15 '/test-tree/branch[@name=\'Left\']/nest' : [name: 'Small', birds: ['Sparrow', 'Robin', 'Finch']],
16 '/test-tree/branch[@name=\'Right\']' : [name: 'Right'],
17 '/test-tree/branch[@name=\'Right\']/nest': [name: 'Big', birds: ['Owl', 'Raven', 'Crow']]
20 def 'Converting Normalized Node (tree) to a DataNode (tree).'() {
21 given: 'a Yang module'
22 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('test-tree.yang')
23 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent) getSchemaContext()
24 and: 'a normalized node for that model'
25 def jsonData = TestUtils.getResourceFileContent('test-tree.json')
26 def normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext)
27 when: 'the normalized node is converted to a DataNode (tree)'
28 def result = new DataNodeBuilder().withNormalizedNodeTree(normalizedNode).build()
29 def mappedResult = treeToFlatMapByXpath(new HashMap<>(), result)
30 then: '5 DataNode objects with unique xpath were created in total'
31 mappedResult.size() == 5
32 and: 'all expected xpaths were built'
33 mappedResult.keySet().containsAll(expectedLeavesByXpathMap.keySet())
34 and: 'each data node contains the expected attributes'
36 xpath, dataNode -> assertLeavesMaps(dataNode.getLeaves(), expectedLeavesByXpathMap[xpath])
40 def static assertLeavesMaps(actualLeavesMap, expectedLeavesMap) {
41 expectedLeavesMap.each { key, value ->
43 def actualValue = actualLeavesMap[key]
44 if (value instanceof Collection<?> && actualValue instanceof Collection<?>) {
45 assert value.size() == actualValue.size()
46 assert value.containsAll(actualValue)
48 assert value == actualValue
54 def treeToFlatMapByXpath(Map<String, DataNode> flatMap, DataNode dataNodeTree) {
55 flatMap.put(dataNodeTree.getXpath(), dataNodeTree)
56 dataNodeTree.getChildDataNodes()
57 .forEach(childDataNode -> treeToFlatMapByXpath(flatMap, childDataNode))