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
30 class YangUtilsSpec extends Specification {
31 def 'Parsing a valid Json String.'() {
32 given: 'a yang model (file)'
33 def jsonData = org.onap.cps.TestUtils.getResourceFileContent('bookstore.json')
34 and: 'a model for that data'
35 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('bookstore.yang')
36 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
37 when: 'the json data is parsed'
38 NormalizedNode<?, ?> result = YangUtils.parseJsonData(jsonData, schemaContext)
39 then: 'the result is a normalized node of the correct type'
40 result.nodeType == QName.create('org:onap:ccsdk:sample', '2020-09-15', 'bookstore')
43 def 'Parsing invalid data: #description.'() {
44 given: 'a yang model (file)'
45 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('bookstore.yang')
46 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
47 when: 'invalid data is parsed'
48 YangUtils.parseJsonData(invalidJson, schemaContext)
49 then: 'an exception is thrown'
50 thrown(DataValidationException)
51 where: 'the following invalid json is provided'
52 invalidJson | description
53 '{incomplete json' | 'incomplete json'
54 '{"test:bookstore": {"address": "Parnell st." }}' | 'json with un-modelled data'
55 '{" }' | 'json with syntax exception'
58 def 'Parsing json data fragment by xpath for #scenario.'() {
59 given: 'schema context'
60 def yangResourcesMap = TestUtils.getYangResourcesAsMap('test-tree.yang')
61 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourcesMap).getSchemaContext()
62 when: 'json string is parsed'
63 def result = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath)
64 then: 'result represents a node of expected type'
65 result.nodeType == QName.create('org:onap:cps:test:test-tree', '2020-02-02', nodeName)
67 scenario | jsonData | parentNodeXpath || nodeName
68 'list element as container' | '{ "branch": { "name": "B", "nest": { "name": "N", "birds": ["bird"] } } }' | '/test-tree' || 'branch'
69 'list element within list' | '{ "branch": [{ "name": "B", "nest": { "name": "N", "birds": ["bird"] } }] }' | '/test-tree' || 'branch'
70 'container element' | '{ "nest": { "name": "N", "birds": ["bird"] } }' | '/test-tree/branch[@name=\'Branch\']' || 'nest'
73 def 'Parsing json data fragment by xpath error scenario: #scenario.'() {
74 given: 'schema context'
75 def yangResourcesMap = TestUtils.getYangResourcesAsMap('test-tree.yang')
76 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourcesMap).getSchemaContext()
77 when: 'json string is parsed'
78 YangUtils.parseJsonData('{"nest": {"name" : "Nest", "birds": ["bird"]}}', schemaContext,
80 then: 'expected exception is thrown'
81 thrown(DataValidationException)
83 scenario | parentNodeXpath
84 'xpath has no identifiers' | '/'
85 'xpath has no valid identifiers' | '/[@name=\'Name\']'
86 'invalid parent path' | '/test-bush'
87 'another invalid parent path' | '/test-tree/branch[@name=\'Branch\']/nest/name/last-name'
88 'fragment does not belong to parent' | '/test-tree/'