Merge "Fix sonar code smells"
[cps.git] / cps-service / src / test / groovy / org / onap / cps / model / DataNodeBuilderSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Pantheon.tech
4  *  Modifications Copyright (C) 2021 Nordix Foundation.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.model
22
23 import org.onap.cps.TestUtils
24 import org.onap.cps.spi.model.DataNodeBuilder
25 import org.onap.cps.utils.YangUtils
26 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
27 import spock.lang.Specification
28
29 class DataNodeBuilderSpec extends Specification {
30
31     Map<String, Map<String, Object>> expectedLeavesByXpathMap = [
32             '/test-tree'                                            : [],
33             '/test-tree/branch[@name=\'Left\']'                     : [name: 'Left'],
34             '/test-tree/branch[@name=\'Left\']/nest'                : [name: 'Small', birds: ['Sparrow', 'Robin', 'Finch']],
35             '/test-tree/branch[@name=\'Right\']'                    : [name: 'Right'],
36             '/test-tree/branch[@name=\'Right\']/nest'               : [name: 'Big', birds: ['Owl', 'Raven', 'Crow']],
37             '/test-tree/fruit[@color=\'Green\' and @name=\'Apple\']': [color: 'Green', name: 'Apple']
38     ]
39
40     String[] networkTopologyModelRfc8345 = [
41             'ietf/ietf-yang-types@2013-07-15.yang',
42             'ietf/ietf-network-topology-state@2018-02-26.yang',
43             'ietf/ietf-network-topology@2018-02-26.yang',
44             'ietf/ietf-network-state@2018-02-26.yang',
45             'ietf/ietf-network@2018-02-26.yang',
46             'ietf/ietf-inet-types@2013-07-15.yang'
47     ]
48
49     def 'Converting NormalizedNode (tree) to a DataNode (tree).'() {
50         given: 'the schema context for expected model'
51             def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('test-tree.yang')
52             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent) getSchemaContext()
53         and: 'the json data parsed into normalized node object'
54             def jsonData = TestUtils.getResourceFileContent('test-tree.json')
55             def normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext)
56         when: 'the normalized node is converted to a data node'
57             def result = new DataNodeBuilder().withNormalizedNodeTree(normalizedNode).build()
58             def mappedResult = TestUtils.getFlattenMapByXpath(result)
59         then: '5 DataNode objects with unique xpath were created in total'
60             mappedResult.size() == 6
61         and: 'all expected xpaths were built'
62             mappedResult.keySet().containsAll(expectedLeavesByXpathMap.keySet())
63         and: 'each data node contains the expected attributes'
64             mappedResult.each {
65                 xpath, dataNode -> assertLeavesMaps(dataNode.getLeaves(), expectedLeavesByXpathMap[xpath])
66             }
67     }
68
69     def 'Converting NormalizedNode (tree) to a DataNode (tree) for known parent node.'() {
70         given: 'a schema context for expected model'
71             def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('test-tree.yang')
72             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent) getSchemaContext()
73         and: 'the json data parsed into normalized node object'
74             def jsonData = '{ "branch": [{ "name": "Branch", "nest": { "name": "Nest", "birds": ["bird"] } }] }'
75             def normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, "/test-tree")
76         when: 'the normalized node is converted to a data node with parent node xpath defined'
77             def result = new DataNodeBuilder()
78                     .withNormalizedNodeTree(normalizedNode)
79                     .withParentNodeXpath("/test-tree")
80                     .build()
81             def mappedResult = TestUtils.getFlattenMapByXpath(result)
82         then: '2 DataNode objects with unique xpath were created in total'
83             mappedResult.size() == 2
84         and: 'all expected xpaths were built'
85             mappedResult.keySet()
86                     .containsAll(['/test-tree/branch[@name=\'Branch\']', '/test-tree/branch[@name=\'Branch\']/nest'])
87     }
88
89     def 'Converting NormalizedNode (tree) to a DataNode (tree) -- augmentation case.'() {
90         given: 'a schema context for expected model'
91             def yangResourceNameToContent = TestUtils.getYangResourcesAsMap(networkTopologyModelRfc8345)
92             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent) getSchemaContext()
93         and: 'the json data parsed into normalized node object'
94             def jsonData = TestUtils.getResourceFileContent('ietf/data/ietf-network-topology-sample-rfc8345.json')
95             def normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext)
96         when: 'the normalized node is converted to a data node '
97             def result = new DataNodeBuilder().withNormalizedNodeTree(normalizedNode).build()
98             def mappedResult = TestUtils.getFlattenMapByXpath(result)
99         then: 'all expected data nodes are populated'
100             mappedResult.size() == 32
101             println(mappedResult.keySet().sort())
102         and: 'xpaths for augmentation nodes (link and termination-point nodes) were built correctly'
103             mappedResult.keySet().containsAll([
104                     "/networks/network[@network-id='otn-hc']/link[@link-id='D1,1-2-1,D2,2-1-1']",
105                     "/networks/network[@network-id='otn-hc']/link[@link-id='D1,1-3-1,D3,3-1-1']",
106                     "/networks/network[@network-id='otn-hc']/link[@link-id='D2,2-1-1,D1,1-2-1']",
107                     "/networks/network[@network-id='otn-hc']/link[@link-id='D2,2-3-1,D3,3-2-1']",
108                     "/networks/network[@network-id='otn-hc']/link[@link-id='D3,3-1-1,D1,1-3-1']",
109                     "/networks/network[@network-id='otn-hc']/link[@link-id='D3,3-2-1,D2,2-3-1']",
110                     "/networks/network[@network-id='otn-hc']/node[@node-id='D1']/termination-point[@tp-id='1-0-1']",
111                     "/networks/network[@network-id='otn-hc']/node[@node-id='D1']/termination-point[@tp-id='1-2-1']",
112                     "/networks/network[@network-id='otn-hc']/node[@node-id='D1']/termination-point[@tp-id='1-3-1']",
113                     "/networks/network[@network-id='otn-hc']/node[@node-id='D2']/termination-point[@tp-id='2-0-1']",
114                     "/networks/network[@network-id='otn-hc']/node[@node-id='D2']/termination-point[@tp-id='2-1-1']",
115                     "/networks/network[@network-id='otn-hc']/node[@node-id='D2']/termination-point[@tp-id='2-3-1']",
116                     "/networks/network[@network-id='otn-hc']/node[@node-id='D3']/termination-point[@tp-id='3-1-1']",
117                     "/networks/network[@network-id='otn-hc']/node[@node-id='D3']/termination-point[@tp-id='3-2-1']"
118             ])
119     }
120
121     def 'Converting NormalizedNode (tree) to a DataNode (tree) for known parent node -- augmentation case.'() {
122         given: 'a schema context for expected model'
123             def yangResourceNameToContent = TestUtils.getYangResourcesAsMap(networkTopologyModelRfc8345)
124             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent) getSchemaContext()
125         and: 'parent node xpath referencing augmentation node within a model'
126             def parentNodeXpath = "/networks/network[@network-id='otn-hc']/link[@link-id='D1,1-2-1,D2,2-1-1']"
127         and: 'the json data fragment parsed into normalized node object for given parent node xpath'
128             def jsonData = '{"source": {"source-node": "D1", "source-tp": "1-2-1"}}'
129             def normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath)
130         when: 'the normalized node is converted to a data node with given parent node xpath'
131             def result = new DataNodeBuilder().withNormalizedNodeTree(normalizedNode)
132                     .withParentNodeXpath(parentNodeXpath).build()
133         then: 'the resulting data node represents a child of augmentation node'
134             assert result.xpath == "/networks/network[@network-id='otn-hc']/link[@link-id='D1,1-2-1,D2,2-1-1']/source"
135             assert result.leaves['source-node'] == 'D1'
136             assert result.leaves['source-tp'] == '1-2-1'
137     }
138
139     def 'Converting NormalizedNode into DataNode collection: #scenario.'() {
140         given: 'a schema context for expected model'
141             def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('test-tree.yang')
142             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent) getSchemaContext()
143         and: 'parent node xpath referencing parent of list element'
144             def parentNodeXpath = "/test-tree"
145         and: 'the json data fragment (list element) parsed into normalized node object'
146             def normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath)
147         when: 'the normalized node is converted to a data node collection'
148             def result = new DataNodeBuilder().withNormalizedNodeTree(normalizedNode)
149                     .withParentNodeXpath(parentNodeXpath).buildCollection()
150             def resultXpaths = result.collect { it.getXpath() }
151         then: 'the resulting collection contains data nodes for expected list elements'
152             assert resultXpaths.size() == expectedSize
153             assert resultXpaths.containsAll(expectedXpaths)
154         where: 'following parameters are used'
155             scenario           | jsonData                                         | expectedSize | expectedXpaths
156             'single entry'     | '{"branch": [{"name": "One"}]}'                  | 1            | ['/test-tree/branch[@name=\'One\']']
157             'multiple entries' | '{"branch": [{"name": "One"}, {"name": "Two"}]}' | 2            | ['/test-tree/branch[@name=\'One\']', '/test-tree/branch[@name=\'Two\']']
158     }
159
160     def static assertLeavesMaps(actualLeavesMap, expectedLeavesMap) {
161         expectedLeavesMap.each { key, value ->
162             {
163                 def actualValue = actualLeavesMap[key]
164                 if (value instanceof Collection<?> && actualValue instanceof Collection<?>) {
165                     assert value.size() == actualValue.size()
166                     assert value.containsAll(actualValue)
167                 } else {
168                     assert value == actualValue
169                 }
170             }
171         }
172     }
173 }