2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation
4 * Modifications Copyright (C) 2021 Pantheon.tech
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
10 * 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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.utils
24 import org.onap.cps.TestUtils
25 import org.onap.cps.spi.exceptions.DataValidationException
26 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
27 import org.opendaylight.yangtools.yang.common.QName
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode
29 import spock.lang.Specification
31 class YangUtilsSpec extends Specification {
32 def 'Parsing a valid Json String.'() {
33 given: 'a yang model (file)'
34 def jsonData = org.onap.cps.TestUtils.getResourceFileContent('bookstore.json')
35 and: 'a model for that data'
36 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('bookstore.yang')
37 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
38 when: 'the json data is parsed'
39 NormalizedNode<?, ?> result = YangUtils.parseJsonData(jsonData, schemaContext)
40 then: 'the result is a normalized node of the correct type'
41 result.nodeType == QName.create('org:onap:ccsdk:sample', '2020-09-15', 'bookstore')
44 def 'Parsing invalid data: #description.'() {
45 given: 'a yang model (file)'
46 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('bookstore.yang')
47 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
48 when: 'invalid data is parsed'
49 YangUtils.parseJsonData(invalidJson, schemaContext)
50 then: 'an exception is thrown'
51 thrown(DataValidationException)
52 where: 'the following invalid json is provided'
53 invalidJson | description
54 '{incomplete json' | 'incomplete json'
55 '{"test:bookstore": {"address": "Parnell st." }}' | 'json with un-modelled data'
56 '{" }' | 'json with syntax exception'
59 def 'Parsing json data fragment by xpath for #scenario.'() {
60 given: 'schema context'
61 def yangResourcesMap = TestUtils.getYangResourcesAsMap('test-tree.yang')
62 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourcesMap).getSchemaContext()
63 when: 'json string is parsed'
64 def result = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath)
65 then: 'result represents a node of expected type'
66 result.nodeType == QName.create('org:onap:cps:test:test-tree', '2020-02-02', nodeName)
68 scenario | jsonData | parentNodeXpath || nodeName
69 'list element as container' | '{ "branch": { "name": "B", "nest": { "name": "N", "birds": ["bird"] } } }' | '/test-tree' || 'branch'
70 'list element within list' | '{ "branch": [{ "name": "B", "nest": { "name": "N", "birds": ["bird"] } }] }' | '/test-tree' || 'branch'
71 'container element' | '{ "nest": { "name": "N", "birds": ["bird"] } }' | '/test-tree/branch[@name=\'Branch\']' || 'nest'
74 def 'Parsing json data fragment by xpath error scenario: #scenario.'() {
75 given: 'schema context'
76 def yangResourcesMap = TestUtils.getYangResourcesAsMap('test-tree.yang')
77 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourcesMap).getSchemaContext()
78 when: 'json string is parsed'
79 YangUtils.parseJsonData('{"nest": {"name" : "Nest", "birds": ["bird"]}}', schemaContext,
81 then: 'expected exception is thrown'
82 thrown(DataValidationException)
84 scenario | parentNodeXpath
85 'xpath has no identifiers' | '/'
86 'xpath has no valid identifiers' | '/[@name=\'Name\']'
87 'invalid parent path' | '/test-bush'
88 'another invalid parent path' | '/test-tree/branch[@name=\'Branch\']/nest/name/last-name'
89 'fragment does not belong to parent' | '/test-tree/'
92 def 'Parsing json data with invalid json string: #description.'() {
93 given: 'schema context'
94 def yangResourcesMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
95 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourcesMap).getSchemaContext()
96 when: 'malformed json string is parsed'
97 YangUtils.parseJsonData(invalidJson, schemaContext)
98 then: 'an exception is thrown'
99 thrown(DataValidationException)
100 where: 'the following malformed json is provided'
101 description | invalidJson
102 'malformed json string with unterminated array data' | '{bookstore={categories=[{books=[{authors=[Iain M. Banks]}]}]}}'
103 'incorrect json' | '{" }'
106 def 'Parsing json data with space.'() {
107 given: 'schema context'
108 def yangResourcesMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
109 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourcesMap).getSchemaContext()
110 and: 'some json data with space in the array elements'
111 def jsonDataWithSpacesInArrayElement = TestUtils.getResourceFileContent('bookstore.json')
112 when: 'that json data is parsed'
113 YangUtils.parseJsonData(jsonDataWithSpacesInArrayElement, schemaContext)
114 then: 'no exception thrown'