Merge "Use PollingConditions to improve intermittent test failure"
[cps.git] / cps-service / src / test / groovy / org / onap / cps / utils / YangParserHelperSpec.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2024 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10
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.utils
22
23 import org.onap.cps.TestUtils
24 import org.onap.cps.spi.exceptions.DataValidationException
25 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
26 import org.opendaylight.yangtools.yang.common.QName
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode
28 import spock.lang.Specification
29
30 class YangParserHelperSpec extends Specification {
31
32     def objectUnderTest = new YangParserHelper()
33
34     def 'Parsing a valid multicontainer Json String.'() {
35         given: 'a yang model (file)'
36             def jsonData = TestUtils.getResourceFileContent('multiple-object-data.json')
37         and: 'a model for that data'
38             def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('multipleDataTree.yang')
39             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
40         when: 'the json data is parsed'
41             def result = objectUnderTest.parseData(ContentType.JSON, jsonData, schemaContext, '')
42         then: 'a ContainerNode holding collection of normalized nodes is returned'
43             result.body().getAt(index) instanceof NormalizedNode == true
44         then: 'qualified name of children created is as expected'
45             result.body().getAt(index).getIdentifier().nodeType == QName.create('org:onap:ccsdk:multiDataTree', '2020-09-15', nodeName)
46         where:
47             index | nodeName
48             0     | 'first-container'
49             1     | 'last-container'
50     }
51
52     def 'Parsing a valid #scenario String.'() {
53         given: 'a yang model (file)'
54             def fileData = TestUtils.getResourceFileContent(contentFile)
55         and: 'a model for that data'
56             def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('bookstore.yang')
57             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
58         when: 'the data is parsed'
59             NormalizedNode result = objectUnderTest.parseData(contentType, fileData, schemaContext, '')
60         then: 'the result is a normalized node of the correct type'
61             if (revision) {
62                 result.identifier.nodeType == QName.create(namespace, revision, localName)
63             } else {
64                 result.identifier.nodeType == QName.create(namespace, localName)
65             }
66         where:
67             scenario | contentFile      | contentType      | namespace                                 | revision     | localName
68             'JSON'   | 'bookstore.json' | ContentType.JSON | 'org:onap:ccsdk:sample'                   | '2020-09-15' | 'bookstore'
69             'XML'    | 'bookstore.xml'  | ContentType.XML  | 'urn:ietf:params:xml:ns:netconf:base:1.0' | ''           | 'bookstore'
70     }
71
72     def 'Parsing invalid data: #description.'() {
73         given: 'a yang model (file)'
74             def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('bookstore.yang')
75             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
76         when: 'invalid data is parsed'
77             objectUnderTest.parseData(contentType, invalidData, schemaContext, '')
78         then: 'an exception is thrown'
79             thrown(DataValidationException)
80         where: 'the following invalid data is provided'
81             invalidData                                                                          | contentType      | description
82             '{incomplete json'                                                                   | ContentType.JSON | 'incomplete json'
83             '{"test:bookstore": {"address": "Parnell st." }}'                                    | ContentType.JSON | 'json with un-modelled data'
84             '{" }'                                                                               | ContentType.JSON | 'json with syntax exception'
85             '<data>'                                                                             | ContentType.XML  | 'incomplete xml'
86             '<data><bookstore><bookstore-anything>blabla</bookstore-anything></bookstore</data>' | ContentType.XML  | 'xml with invalid model'
87             ''                                                                                   | ContentType.XML  | 'empty xml'
88     }
89
90     def 'Parsing data fragment by xpath for #scenario.'() {
91         given: 'schema context'
92             def yangResourcesMap = TestUtils.getYangResourcesAsMap('test-tree.yang')
93             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourcesMap).getSchemaContext()
94         when: 'json string is parsed'
95             def result = objectUnderTest.parseData(contentType, nodeData, schemaContext, parentNodeXpath)
96         then: 'a ContainerNode holding collection of normalized nodes is returned'
97             result.body().getAt(0) instanceof NormalizedNode == true
98         then: 'result represents a node of expected type'
99             result.body().getAt(0).getIdentifier().nodeType == QName.create('org:onap:cps:test:test-tree', '2020-02-02', nodeName)
100         where:
101             scenario                         | contentType      | nodeData                                                                                                                                                                                                      | parentNodeXpath                       || nodeName
102             'JSON list element as container' | ContentType.JSON | '{ "branch": { "name": "B", "nest": { "name": "N", "birds": ["bird"] } } }'                                                                                                                                   | '/test-tree'                          || 'branch'
103             'JSON list element within list'  | ContentType.JSON | '{ "branch": [{ "name": "B", "nest": { "name": "N", "birds": ["bird"] } }] }'                                                                                                                                 | '/test-tree'                          || 'branch'
104             'JSON container element'         | ContentType.JSON | '{ "nest": { "name": "N", "birds": ["bird"] } }'                                                                                                                                                              | '/test-tree/branch[@name=\'Branch\']' || 'nest'
105             'XML element test tree'          | ContentType.XML  | '<?xml version=\'1.0\' encoding=\'UTF-8\'?><branch xmlns="org:onap:cps:test:test-tree"><name>Left</name><nest><name>Small</name><birds>Sparrow</birds></nest></branch>'                                       | '/test-tree'                          || 'branch'
106             'XML element branch xpath'       | ContentType.XML  | '<?xml version=\'1.0\' encoding=\'UTF-8\'?><branch xmlns="org:onap:cps:test:test-tree"><name>Left</name><nest><name>Small</name><birds>Sparrow</birds><birds>Robin</birds></nest></branch>'                   | '/test-tree'                          || 'branch'
107             'XML container element'          | ContentType.XML  | '<?xml version=\'1.0\' encoding=\'UTF-8\'?><nest xmlns="org:onap:cps:test:test-tree"><name>Small</name><birds>Sparrow</birds></nest>'                                                                         | '/test-tree/branch[@name=\'Branch\']' || 'nest'
108     }
109
110     def 'Parsing json data fragment by xpath error scenario: #scenario.'() {
111         given: 'schema context'
112             def yangResourcesMap = TestUtils.getYangResourcesAsMap('test-tree.yang')
113             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourcesMap).getSchemaContext()
114         when: 'json string is parsed'
115             objectUnderTest.parseData(ContentType.JSON, '{"nest": {"name" : "Nest", "birds": ["bird"]}}', schemaContext, parentNodeXpath)
116         then: 'expected exception is thrown'
117             thrown(DataValidationException)
118         where:
119             scenario                             | parentNodeXpath
120             'xpath has no identifiers'           | '/'
121             'xpath has no valid identifiers'     | '/[@name=\'Name\']'
122             'invalid parent path'                | '/test-bush'
123             'another invalid parent path'        | '/test-tree/branch[@name=\'Branch\']/nest/name/last-name'
124             'fragment does not belong to parent' | '/test-tree/'
125     }
126
127     def 'Parsing json data with invalid json string: #description.'() {
128         given: 'schema context'
129             def yangResourcesMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
130             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourcesMap).getSchemaContext()
131         when: 'malformed json string is parsed'
132             objectUnderTest.parseData(ContentType.JSON, invalidJson, schemaContext, '')
133         then: 'an exception is thrown'
134             thrown(DataValidationException)
135         where: 'the following malformed json is provided'
136             description                                          | invalidJson
137             'malformed json string with unterminated array data' | '{bookstore={categories=[{books=[{authors=[Iain M. Banks]}]}]}}'
138             'incorrect json'                                     | '{" }'
139     }
140
141     def 'Parsing json data with space.'() {
142         given: 'schema context'
143             def yangResourcesMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
144             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourcesMap).getSchemaContext()
145         and: 'some json data with space in the array elements'
146             def jsonDataWithSpacesInArrayElement = TestUtils.getResourceFileContent('bookstore.json')
147         when: 'that json data is parsed'
148             objectUnderTest.parseData(ContentType.JSON, jsonDataWithSpacesInArrayElement, schemaContext, '')
149         then: 'no exception thrown'
150             noExceptionThrown()
151     }
152
153     def 'Converting xPath to nodeId for #scenario.'() {
154         when: 'xPath is parsed'
155             def result = objectUnderTest.xpathToNodeIdSequence(xPath)
156         then: 'result represents an array of expected identifiers'
157             assert result == expectedNodeIdentifier
158         where: 'the following parameters are used'
159             scenario                                       | xPath                                                               || expectedNodeIdentifier
160             'container xpath'                              | '/test-tree'                                                        || ['test-tree']
161             'xpath contains list attribute'                | '/test-tree/branch[@name=\'Branch\']'                               || ['test-tree','branch']
162             'xpath contains list attributes with /'        | '/test-tree/branch[@name=\'/Branch\']/categories[@id=\'/broken\']'  || ['test-tree','branch','categories']
163     }
164
165
166 }