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 * ================================================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.cps.utils
25 import org.onap.cps.TestUtils
26 import org.onap.cps.spi.exceptions.DataValidationException
27 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
28 import org.opendaylight.yangtools.yang.common.QName
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode
30 import spock.lang.Specification
32 class YangUtilsSpec extends Specification {
33 def 'Parsing a valid Json String.'() {
34 given: 'a yang model (file)'
35 def jsonData = org.onap.cps.TestUtils.getResourceFileContent('multiple-object-data.json')
36 and: 'a model for that data'
37 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('multipleDataTree.yang')
38 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
39 when: 'the json data is parsed'
40 def result = YangUtils.parseJsonData(jsonData, schemaContext)
41 then: 'a ContainerNode holding collection of normalized nodes is returned'
42 result.body().getAt(index) instanceof NormalizedNode == true
43 then: 'qualified name of children created is as expected'
44 result.body().getAt(index).getIdentifier().nodeType == QName.create('org:onap:ccsdk:multiDataTree', '2020-09-15', nodeName)
51 def 'Parsing invalid data: #description.'() {
52 given: 'a yang model (file)'
53 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('bookstore.yang')
54 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
55 when: 'invalid data is parsed'
56 YangUtils.parseJsonData(invalidJson, schemaContext)
57 then: 'an exception is thrown'
58 thrown(DataValidationException)
59 where: 'the following invalid json is provided'
60 invalidJson | description
61 '{incomplete json' | 'incomplete json'
62 '{"test:bookstore": {"address": "Parnell st." }}' | 'json with un-modelled data'
63 '{" }' | 'json with syntax exception'
66 def 'Parsing json data fragment by xpath for #scenario.'() {
67 given: 'schema context'
68 def yangResourcesMap = TestUtils.getYangResourcesAsMap('test-tree.yang')
69 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourcesMap).getSchemaContext()
70 when: 'json string is parsed'
71 def result = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath)
72 then: 'a ContainerNode holding collection of normalized nodes is returned'
73 result.body().getAt(0) instanceof NormalizedNode == true
74 then: 'result represents a node of expected type'
75 result.body().getAt(0).getIdentifier().nodeType == QName.create('org:onap:cps:test:test-tree', '2020-02-02', nodeName)
77 scenario | jsonData | parentNodeXpath || nodeName
78 'list element as container' | '{ "branch": { "name": "B", "nest": { "name": "N", "birds": ["bird"] } } }' | '/test-tree' || 'branch'
79 'list element within list' | '{ "branch": [{ "name": "B", "nest": { "name": "N", "birds": ["bird"] } }] }' | '/test-tree' || 'branch'
80 'container element' | '{ "nest": { "name": "N", "birds": ["bird"] } }' | '/test-tree/branch[@name=\'Branch\']' || 'nest'
83 def 'Parsing json data fragment by xpath error scenario: #scenario.'() {
84 given: 'schema context'
85 def yangResourcesMap = TestUtils.getYangResourcesAsMap('test-tree.yang')
86 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourcesMap).getSchemaContext()
87 when: 'json string is parsed'
88 YangUtils.parseJsonData('{"nest": {"name" : "Nest", "birds": ["bird"]}}', schemaContext,
90 then: 'expected exception is thrown'
91 thrown(DataValidationException)
93 scenario | parentNodeXpath
94 'xpath has no identifiers' | '/'
95 'xpath has no valid identifiers' | '/[@name=\'Name\']'
96 'invalid parent path' | '/test-bush'
97 'another invalid parent path' | '/test-tree/branch[@name=\'Branch\']/nest/name/last-name'
98 'fragment does not belong to parent' | '/test-tree/'
101 def 'Parsing json data with invalid json string: #description.'() {
102 given: 'schema context'
103 def yangResourcesMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
104 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourcesMap).getSchemaContext()
105 when: 'malformed json string is parsed'
106 YangUtils.parseJsonData(invalidJson, schemaContext)
107 then: 'an exception is thrown'
108 thrown(DataValidationException)
109 where: 'the following malformed json is provided'
110 description | invalidJson
111 'malformed json string with unterminated array data' | '{bookstore={categories=[{books=[{authors=[Iain M. Banks]}]}]}}'
112 'incorrect json' | '{" }'
115 def 'Parsing json data with space.'() {
116 given: 'schema context'
117 def yangResourcesMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
118 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourcesMap).getSchemaContext()
119 and: 'some json data with space in the array elements'
120 def jsonDataWithSpacesInArrayElement = TestUtils.getResourceFileContent('bookstore.json')
121 when: 'that json data is parsed'
122 YangUtils.parseJsonData(jsonDataWithSpacesInArrayElement, schemaContext)
123 then: 'no exception thrown'
127 def 'Parsing xPath to nodeId for #scenario.'() {
128 when: 'xPath is parsed'
129 def result = YangUtils.xpathToNodeIdSequence(xPath)
130 then: 'result represents an array of expected identifiers'
131 assert result == expectedNodeIdentifier
132 where: 'the following parameters are used'
133 scenario | xPath || expectedNodeIdentifier
134 'container xpath' | '/test-tree' || ['test-tree']
135 'xpath contains list attribute' | '/test-tree/branch[@name=\'Branch\']' || ['test-tree','branch']
136 'xpath contains list attributes with /' | '/test-tree/branch[@name=\'/Branch\']/categories[@id=\'/broken\']' || ['test-tree','branch','categories']