2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020-2022 Nordix Foundation
4 * Modifications Copyright (C) 2021 Pantheon.tech
5 * Modifications Copyright (C) 2022 TechMahindra Ltd.
6 * Modifications Copyright (C) 2022 Deutsche Telekom AG
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.cps.utils
26 import org.onap.cps.TestUtils
27 import org.onap.cps.spi.exceptions.DataValidationException
28 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
29 import org.opendaylight.yangtools.yang.common.QName
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode
31 import spock.lang.Specification
33 class YangUtilsSpec extends Specification {
34 def 'Parsing a valid multicontainer Json String.'() {
35 given: 'a yang model (file)'
36 def jsonData = org.onap.cps.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 = YangUtils.parseJsonData(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)
52 def 'Parsing a valid #scenario String.'() {
53 given: 'a yang model (file)'
54 def fileData = org.onap.cps.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 = YangUtils.parseData(contentType, fileData, schemaContext)
60 then: 'the result is a normalized node of the correct type'
62 result.identifier.nodeType == QName.create(namespace, revision, localName)
64 result.identifier.nodeType == QName.create(namespace, localName)
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'
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 YangUtils.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'
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 = YangUtils.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)
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'
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 YangUtils.parseJsonData('{"nest": {"name" : "Nest", "birds": ["bird"]}}', schemaContext,
117 then: 'expected exception is thrown'
118 thrown(DataValidationException)
120 scenario | parentNodeXpath
121 'xpath has no identifiers' | '/'
122 'xpath has no valid identifiers' | '/[@name=\'Name\']'
123 'invalid parent path' | '/test-bush'
124 'another invalid parent path' | '/test-tree/branch[@name=\'Branch\']/nest/name/last-name'
125 'fragment does not belong to parent' | '/test-tree/'
128 def 'Parsing json data with invalid json string: #description.'() {
129 given: 'schema context'
130 def yangResourcesMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
131 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourcesMap).getSchemaContext()
132 when: 'malformed json string is parsed'
133 YangUtils.parseJsonData(invalidJson, schemaContext)
134 then: 'an exception is thrown'
135 thrown(DataValidationException)
136 where: 'the following malformed json is provided'
137 description | invalidJson
138 'malformed json string with unterminated array data' | '{bookstore={categories=[{books=[{authors=[Iain M. Banks]}]}]}}'
139 'incorrect json' | '{" }'
142 def 'Parsing json data with space.'() {
143 given: 'schema context'
144 def yangResourcesMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
145 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourcesMap).getSchemaContext()
146 and: 'some json data with space in the array elements'
147 def jsonDataWithSpacesInArrayElement = TestUtils.getResourceFileContent('bookstore.json')
148 when: 'that json data is parsed'
149 YangUtils.parseJsonData(jsonDataWithSpacesInArrayElement, schemaContext)
150 then: 'no exception thrown'
154 def 'Parsing xPath to nodeId for #scenario.'() {
155 when: 'xPath is parsed'
156 def result = YangUtils.xpathToNodeIdSequence(xPath)
157 then: 'result represents an array of expected identifiers'
158 assert result == expectedNodeIdentifier
159 where: 'the following parameters are used'
160 scenario | xPath || expectedNodeIdentifier
161 'container xpath' | '/test-tree' || ['test-tree']
162 'xpath contains list attribute' | '/test-tree/branch[@name=\'Branch\']' || ['test-tree','branch']
163 'xpath contains list attributes with /' | '/test-tree/branch[@name=\'/Branch\']/categories[@id=\'/broken\']' || ['test-tree','branch','categories']