Merge "Ncmp out event for REJECTED scenario"
[cps.git] / cps-service / src / test / groovy / org / onap / cps / spi / model / DataNodeBuilderSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Pantheon.tech
4  *  Modifications Copyright (C) 2021-2024 Nordix Foundation.
5  *  Modifications Copyright (C) 2022 TechMahindra Ltd.
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.spi.model
23
24 import org.onap.cps.TestUtils
25 import org.onap.cps.spi.exceptions.DataValidationException
26 import org.onap.cps.utils.ContentType
27 import org.onap.cps.utils.DataMapUtils
28 import org.onap.cps.utils.YangParserHelper
29 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
30 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode
31 import org.opendaylight.yangtools.yang.data.api.schema.ForeignDataNode
32 import spock.lang.Specification
33
34 class DataNodeBuilderSpec extends Specification {
35
36     def objectUnderTest = new DataNodeBuilder()
37     def yangParserHelper = new YangParserHelper()
38
39     def expectedLeavesByXpathMap = [
40             '/test-tree'                                            : [],
41             '/test-tree/branch[@name=\'Left\']'                     : [name: 'Left'],
42             '/test-tree/branch[@name=\'Left\']/nest'                : [name: 'Small', birds: ['Sparrow', 'Robin', 'Finch']],
43             '/test-tree/branch[@name=\'Right\']'                    : [name: 'Right'],
44             '/test-tree/branch[@name=\'Right\']/nest'               : [name: 'Big', birds: ['Owl', 'Raven', 'Crow']],
45             '/test-tree/fruit[@color=\'Green\' and @name=\'Apple\']': [color: 'Green', name: 'Apple']
46     ]
47
48     String[] networkTopologyModelRfc8345 = [
49             'ietf/ietf-yang-types@2013-07-15.yang',
50             'ietf/ietf-network-topology-state@2018-02-26.yang',
51             'ietf/ietf-network-topology@2018-02-26.yang',
52             'ietf/ietf-network-state@2018-02-26.yang',
53             'ietf/ietf-network@2018-02-26.yang',
54             'ietf/ietf-inet-types@2013-07-15.yang'
55     ]
56
57     def 'Converting ContainerNode (tree) to a DataNode (tree).'() {
58         given: 'the schema context for expected model'
59             def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('test-tree.yang')
60             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent) getSchemaContext()
61         and: 'the json data parsed into container node object'
62             def jsonData = TestUtils.getResourceFileContent('test-tree.json')
63             def containerNode = yangParserHelper.parseData(ContentType.JSON, jsonData, schemaContext, '')
64         when: 'the container node is converted to a data node'
65             def result = objectUnderTest.withContainerNode(containerNode).build()
66             def mappedResult = TestUtils.getFlattenMapByXpath(result)
67         then: '6 DataNode objects with unique xpath were created in total'
68             mappedResult.size() == 6
69         and: 'all expected xpaths were built'
70             mappedResult.keySet().containsAll(expectedLeavesByXpathMap.keySet())
71         and: 'each data node contains the expected attributes'
72             mappedResult.each {
73                 xpath, dataNode -> assertLeavesMaps(dataNode.getLeaves(), expectedLeavesByXpathMap[xpath])
74             }
75     }
76
77     def 'Converting ContainerNode (tree) to a DataNode (tree) for known parent node.'() {
78         given: 'a schema context for expected model'
79             def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('test-tree.yang')
80             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent) getSchemaContext()
81         and: 'the json data parsed into container node object'
82             def jsonData = '{ "branch": [{ "name": "Branch", "nest": { "name": "Nest", "birds": ["bird"] } }] }'
83             def containerNode = yangParserHelper.parseData(ContentType.JSON, jsonData, schemaContext, '/test-tree')
84         when: 'the container node is converted to a data node with parent node xpath defined'
85             def result = objectUnderTest.withContainerNode(containerNode).withParentNodeXpath('/test-tree').build()
86             def mappedResult = TestUtils.getFlattenMapByXpath(result)
87         then: '2 DataNode objects with unique xpath were created in total'
88             mappedResult.size() == 2
89         and: 'all expected xpaths were built'
90             mappedResult.keySet().containsAll(['/test-tree/branch[@name=\'Branch\']', '/test-tree/branch[@name=\'Branch\']/nest'])
91     }
92
93     def 'Converting ContainerNode (tree) to a DataNode (tree) -- augmentation case.'() {
94         given: 'a schema context for expected model'
95             def yangResourceNameToContent = TestUtils.getYangResourcesAsMap(networkTopologyModelRfc8345)
96             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent) getSchemaContext()
97         and: 'the json data parsed into container node object'
98             def jsonData = TestUtils.getResourceFileContent('ietf/data/ietf-network-topology-sample-rfc8345.json')
99             def containerNode = yangParserHelper.parseData(ContentType.JSON, jsonData, schemaContext, '')
100         when: 'the container node is converted to a data node '
101             def result = objectUnderTest.withContainerNode(containerNode).build()
102             def mappedResult = TestUtils.getFlattenMapByXpath(result)
103         then: 'all expected data nodes are populated'
104             mappedResult.size() == 32
105         and: 'xpaths for augmentation nodes (link and termination-point nodes) were built correctly'
106             mappedResult.keySet().containsAll([
107                     "/networks/network[@network-id='otn-hc']/link[@link-id='D1,1-2-1,D2,2-1-1']",
108                     "/networks/network[@network-id='otn-hc']/link[@link-id='D1,1-3-1,D3,3-1-1']",
109                     "/networks/network[@network-id='otn-hc']/link[@link-id='D2,2-1-1,D1,1-2-1']",
110                     "/networks/network[@network-id='otn-hc']/link[@link-id='D2,2-3-1,D3,3-2-1']",
111                     "/networks/network[@network-id='otn-hc']/link[@link-id='D3,3-1-1,D1,1-3-1']",
112                     "/networks/network[@network-id='otn-hc']/link[@link-id='D3,3-2-1,D2,2-3-1']",
113                     "/networks/network[@network-id='otn-hc']/node[@node-id='D1']/termination-point[@tp-id='1-0-1']",
114                     "/networks/network[@network-id='otn-hc']/node[@node-id='D1']/termination-point[@tp-id='1-2-1']",
115                     "/networks/network[@network-id='otn-hc']/node[@node-id='D1']/termination-point[@tp-id='1-3-1']",
116                     "/networks/network[@network-id='otn-hc']/node[@node-id='D2']/termination-point[@tp-id='2-0-1']",
117                     "/networks/network[@network-id='otn-hc']/node[@node-id='D2']/termination-point[@tp-id='2-1-1']",
118                     "/networks/network[@network-id='otn-hc']/node[@node-id='D2']/termination-point[@tp-id='2-3-1']",
119                     "/networks/network[@network-id='otn-hc']/node[@node-id='D3']/termination-point[@tp-id='3-1-1']",
120                     "/networks/network[@network-id='otn-hc']/node[@node-id='D3']/termination-point[@tp-id='3-2-1']"
121             ])
122     }
123
124     def 'Converting ContainerNode (tree) to a DataNode (tree) for known parent node -- augmentation case.'() {
125         given: 'a schema context for expected model'
126             def yangResourceNameToContent = TestUtils.getYangResourcesAsMap(networkTopologyModelRfc8345)
127             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent) getSchemaContext()
128         and: 'parent node xpath referencing augmentation node within a model'
129             def parentNodeXpath = "/networks/network[@network-id='otn-hc']/link[@link-id='D1,1-2-1,D2,2-1-1']"
130         and: 'the json data fragment parsed into container node object for given parent node xpath'
131             def jsonData = '{"source": {"source-node": "D1", "source-tp": "1-2-1"}}'
132             def containerNode = yangParserHelper.parseData(ContentType.JSON, jsonData, schemaContext,parentNodeXpath)
133         when: 'the container node is converted to a data node with given parent node xpath'
134             def result = objectUnderTest.withContainerNode(containerNode).withParentNodeXpath(parentNodeXpath).build()
135         then: 'the resulting data node represents a child of augmentation node'
136             assert result.xpath == "/networks/network[@network-id='otn-hc']/link[@link-id='D1,1-2-1,D2,2-1-1']/source"
137             assert result.leaves['source-node'] == 'D1'
138             assert result.leaves['source-tp'] == '1-2-1'
139     }
140
141     def 'Converting ContainerNode (tree) to a DataNode (tree) -- with ChoiceNode.'() {
142         given: 'a schema context for expected model'
143             def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('yang-with-choice-node.yang')
144             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent) getSchemaContext()
145         and: 'the json data fragment parsed into container node object'
146             def jsonData = TestUtils.getResourceFileContent('data-with-choice-node.json')
147             def containerNode = yangParserHelper.parseData(ContentType.JSON, jsonData, schemaContext, '')
148         when: 'the container node is converted to a data node'
149             def result = objectUnderTest.withContainerNode(containerNode).build()
150             def mappedResult = TestUtils.getFlattenMapByXpath(result)
151         then: 'the resulting data node contains only one xpath with 3 leaves'
152             mappedResult.keySet().containsAll([ '/container-with-choice-leaves' ])
153             assert result.leaves['leaf-1'] == 'test'
154             assert result.leaves['choice-case1-leaf-a'] == 'test'
155             assert result.leaves['choice-case1-leaf-b'] == 'test'
156     }
157
158     def 'Converting ContainerNode into DataNode collection: #scenario.'() {
159         given: 'a schema context for expected model'
160             def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('test-tree.yang')
161             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent) getSchemaContext()
162         and: 'parent node xpath referencing parent of list element'
163             def parentNodeXpath = '/test-tree'
164         and: 'the json data fragment (list element) parsed into container node object'
165             def containerNode = yangParserHelper.parseData(ContentType.JSON, jsonData, schemaContext, parentNodeXpath)
166         when: 'the container node is converted to a data node collection'
167             def result = objectUnderTest.withContainerNode(containerNode).withParentNodeXpath(parentNodeXpath).buildCollection()
168             def resultXpaths = result.collect { it.getXpath() }
169         then: 'the resulting collection contains data nodes for expected list elements'
170             assert resultXpaths.size() == expectedSize
171             assert resultXpaths.containsAll(expectedXpaths)
172         where: 'following parameters are used'
173             scenario           | jsonData                                         | expectedSize | expectedXpaths
174             'single entry'     | '{"branch": [{"name": "One"}]}'                  | 1            | ['/test-tree/branch[@name=\'One\']']
175             'multiple entries' | '{"branch": [{"name": "One"}, {"name": "Two"}]}' | 2            | ['/test-tree/branch[@name=\'One\']', '/test-tree/branch[@name=\'Two\']']
176     }
177
178     def 'Converting ContainerNode to a Collection with #scenario.'() {
179         expect: 'converting null to a collection returns an empty collection'
180             assert objectUnderTest.withContainerNode(containerNode).buildCollection().isEmpty()
181         where: 'the following container node is used'
182             scenario              | containerNode
183             'null object'         | null
184             'object without body' | Mock(ContainerNode)
185     }
186
187     def 'Converting ContainerNode to a DataNode with unsupported Normalized Node.'() {
188         given: 'a container node of an unsupported type'
189             def mockContainerNode = Mock(ContainerNode)
190             mockContainerNode.body() >> [ Mock(ForeignDataNode) ]
191         when: 'attempt to convert it'
192             objectUnderTest.withContainerNode(mockContainerNode).build()
193         then: 'a data validation exception is thrown'
194             thrown(DataValidationException)
195     }
196
197     def 'Build datanode from attributes.'() {
198         when: 'data node is built'
199             def result = new DataNodeBuilder()
200                 .withDataspace('my dataspace')
201                 .withAnchor('my anchor')
202                 .withModuleNamePrefix('my prefix')
203                 .withXpath('some xpath')
204                 .withLeaves([leaf1: 'value1'])
205                 .withChildDataNodes([Mock(DataNode)])
206                 .build()
207         then: 'the datanode has all the defined attributes'
208             assert result.dataspace == 'my dataspace'
209             assert result.anchorName == 'my anchor'
210             assert result.moduleNamePrefix == 'my prefix'
211             assert result.moduleNamePrefix == 'my prefix'
212             assert result.xpath == 'some xpath'
213             assert result.leaves == [leaf1: 'value1']
214             assert result.childDataNodes.size() == 1
215     }
216
217     def 'Use of adding the module name prefix attribute of data node.'() {
218         when: 'data node is built with a prefix'
219             def testDataNode = new DataNodeBuilder()
220                     .withXpath(xPath)
221                     .withLeaves(sampleLeaves)
222                     .build()
223         then: 'the result when node request is a #scenario includes the correct prefix'
224             def result = new DataMapUtils().toDataMapWithIdentifier(testDataNode, 'sampleModuleNamePrefix')
225             result.toString() == expectedResult
226         where: 'the following parameters are used'
227             scenario          | xPath                                       | sampleLeaves                   | expectedResult
228             'list attribute'  | '/test-tree/branch[@name=\'Right\']/nest'   | [name: 'Big', birds: ['Owl']]  | '{sampleModuleNamePrefix:nest={name=Big, birds=[Owl]}}'
229             'container xpath' | '/test-tree/branch[@name=\'Left\']'         | [name: 'Left']                 | '{sampleModuleNamePrefix:branch={name=Left}}'
230     }
231
232     def static assertLeavesMaps(actualLeavesMap, expectedLeavesMap) {
233         expectedLeavesMap.each { key, value ->
234             {
235                 def actualValue = actualLeavesMap[key]
236                 if (value instanceof Collection<?> && actualValue instanceof Collection<?>) {
237                     assert value.size() == actualValue.size()
238                     assert value.containsAll(actualValue)
239                 } else {
240                     assert value == actualValue
241                 }
242             }
243         }
244     }
245 }