Fix the datanode build logic (incorrect parsing of containers and mapped lists)
[cps.git] / cps-service / src / test / groovy / org / onap / cps / model / DataNodeBuilderSpec.groovy
1 package org.onap.cps.model
2
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
9
10 class DataNodeBuilderSpec extends Specification {
11
12     Map<String, Map<String, Object>> expectedLeavesByXpathMap = [
13             '/test-tree'                             : [],
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']]
18     ]
19
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'
35             mappedResult.each {
36                 xpath, dataNode -> assertLeavesMaps(dataNode.getLeaves(), expectedLeavesByXpathMap[xpath])
37             }
38     }
39
40     def static assertLeavesMaps(actualLeavesMap, expectedLeavesMap) {
41         expectedLeavesMap.each { key, value ->
42             {
43                 def actualValue = actualLeavesMap[key]
44                 if (value instanceof Collection<?> && actualValue instanceof Collection<?>) {
45                     assert value.size() == actualValue.size()
46                     assert value.containsAll(actualValue)
47                 } else {
48                     assert value == actualValue
49                 }
50             }
51         }
52     }
53
54     def treeToFlatMapByXpath(Map<String, DataNode> flatMap, DataNode dataNodeTree) {
55         flatMap.put(dataNodeTree.getXpath(), dataNodeTree)
56         dataNodeTree.getChildDataNodes()
57                 .forEach(childDataNode -> treeToFlatMapByXpath(flatMap, childDataNode))
58         return flatMap
59     }
60 }