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
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.utils
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 import spock.lang.Unroll
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')
45 def 'Parsing invalid data: #description.'() {
46 given: 'a yang model (file)'
47 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('bookstore.yang')
48 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
49 when: 'invalid data is parsed'
50 YangUtils.parseJsonData(invalidJson, schemaContext)
51 then: 'an exception is thrown'
52 thrown(DataValidationException)
53 where: 'the following invalid json is provided'
54 invalidJson | description
55 '{incomplete json' | 'incomplete json'
56 '{"test:bookstore": {"address": "Parnell st." }}' | 'json with un-modelled data'
60 def 'Parsing json data fragment by xpath for #scenario.'() {
61 given: 'schema context'
62 def yangResourcesMap = TestUtils.getYangResourcesAsMap('test-tree.yang')
63 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourcesMap).getSchemaContext()
64 when: 'json string is parsed'
65 def result = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath)
66 then: 'result represents a node of expected type'
67 result.nodeType == QName.create('org:onap:cps:test:test-tree', '2020-02-02', nodeName)
69 scenario | jsonData | parentNodeXpath || nodeName
70 'list element as container' | '{ "branch": { "name": "B", "nest": { "name": "N", "birds": ["bird"] } } }' | '/test-tree' || 'branch'
71 'list element within list' | '{ "branch": [{ "name": "B", "nest": { "name": "N", "birds": ["bird"] } }] }' | '/test-tree' || 'branch'
72 'container element' | '{ "nest": { "name": "N", "birds": ["bird"] } }' | '/test-tree/branch[@name=\'Branch\']' || 'nest'
76 def 'Parsing json data fragment by xpath error scenario: #scenario.'() {
77 given: 'schema context'
78 def yangResourcesMap = TestUtils.getYangResourcesAsMap('test-tree.yang')
79 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourcesMap).getSchemaContext()
80 when: 'json string is parsed'
81 YangUtils.parseJsonData('{"nest": {"name" : "Nest", "birds": ["bird"]}}', schemaContext,
83 then: 'expected exception is thrown'
84 thrown(DataValidationException)
86 scenario | parentNodeXpath
87 'xpath has no identifiers' | '/'
88 'xpath has no valid identifiers' | '/[@name=\'Name\']'
89 'invalid parent path' | '/test-bush'
90 'another invalid parent path' | '/test-tree/branch[@name=\'Branch\']/nest/name/last-name'
91 'fragment does not belong to parent' | '/test-tree/'