Merge "Fixing SonarQube violations"
[cps.git] / cps-service / src / test / groovy / org / onap / cps / spi / model / DataNodeBuilderSpec.groovy
index ce54ead..e46147c 100644 (file)
@@ -22,6 +22,7 @@ package org.onap.cps.spi.model
 
 import org.onap.cps.TestUtils
 import org.onap.cps.spi.model.DataNodeBuilder
+import org.onap.cps.utils.DataMapUtils
 import org.onap.cps.utils.YangUtils
 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
 import org.opendaylight.yangtools.yang.common.QName
@@ -31,7 +32,7 @@ import spock.lang.Specification
 
 class DataNodeBuilderSpec extends Specification {
 
-    Map<String, Map<String, Object>> expectedLeavesByXpathMap = [
+    Map<String, Map<String, Serializable>> expectedLeavesByXpathMap = [
             '/test-tree'                                            : [],
             '/test-tree/branch[@name=\'Left\']'                     : [name: 'Left'],
             '/test-tree/branch[@name=\'Left\']/nest'                : [name: 'Small', birds: ['Sparrow', 'Robin', 'Finch']],
@@ -139,6 +140,25 @@ class DataNodeBuilderSpec extends Specification {
             assert result.leaves['source-tp'] == '1-2-1'
     }
 
+    def 'Converting NormalizedNode (tree) to a DataNode (tree) -- with ChoiceNode.'() {
+        given: 'a schema context for expected model'
+            def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('yang-with-choice-node.yang')
+            def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent) getSchemaContext()
+        and: 'the json data fragment parsed into normalized node object'
+            def jsonData = TestUtils.getResourceFileContent('data-with-choice-node.json')
+            def normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext)
+        when: 'the normalized node is converted to a data node'
+            def result = new DataNodeBuilder().withNormalizedNodeTree(normalizedNode).build()
+            def mappedResult = TestUtils.getFlattenMapByXpath(result)
+        then: 'the resulting data node contains only one xpath with 3 leaves'
+            mappedResult.keySet().containsAll([
+                "/container-with-choice-leaves"
+            ])
+            assert result.leaves['leaf-1'] == "test"
+            assert result.leaves['choice-case1-leaf-a'] == "test"
+            assert result.leaves['choice-case1-leaf-b'] == "test"
+    }
+
     def 'Converting NormalizedNode into DataNode collection: #scenario.'() {
         given: 'a schema context for expected model'
             def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('test-tree.yang')
@@ -172,6 +192,21 @@ class DataNodeBuilderSpec extends Specification {
             'NormalizedNode is an unsupported type' | 'not supported' | Mock(NormalizedNode) | 0            | [ ]
     }
 
+    def 'Use of adding the module name prefix attribute of data node.'() {
+        when: 'data node is built with a prefix'
+            def testDataNode = new DataNodeBuilder()
+                    .withXpath(xPath)
+                    .withLeaves(sampleLeaves)
+                    .build()
+        then: 'the result when node request is a #scenario includes the correct prefix'
+            def result = new DataMapUtils().toDataMapWithIdentifier(testDataNode, 'sampleModuleNamePrefix')
+            result.toString() == expectedResult
+        where: 'the following parameters are used'
+            scenario          | xPath                                       | sampleLeaves                   | expectedResult
+            'list attribute'  | '/test-tree/branch[@name=\'Right\']/nest'   | [name: 'Big', birds: ['Owl']]  | '{sampleModuleNamePrefix:nest={name=Big, birds=[Owl]}}'
+            'container xpath' | '/test-tree/branch[@name=\'Left\']'         | [name: 'Left']                 | '{sampleModuleNamePrefix:branch={name=Left}}'
+    }
+
     def static assertLeavesMaps(actualLeavesMap, expectedLeavesMap) {
         expectedLeavesMap.each { key, value ->
             {