cca2ac6217e77144a04b87d686832bddb0476169
[cps.git] / cps-service / src / test / groovy / org / onap / cps / utils / YangUtilsSpec.groovy
1 /*
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.utils
23
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
30
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')
42     }
43
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'
57     }
58
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)
67         where:
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'
72     }
73
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,
80                     parentNodeXpath)
81         then: 'expected exception is thrown'
82             thrown(DataValidationException)
83         where:
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/'
90     }
91 }