Increase code coverage in cps-service module
[cps.git] / cps-service / src / test / groovy / org / onap / cps / utils / YangUtilsSpec.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020-2023 Nordix Foundation
4  *  Modifications Copyright (C) 2021 Pantheon.tech
5  *  Modifications Copyright (C) 2022 TechMahindra Ltd.
6  *  Modifications Copyright (C) 2022 Deutsche Telekom AG
7  *  ================================================================================
8  *  Licensed under the Apache License, Version 2.0 (the "License");
9  *  you may not use this file except in compliance with the License.
10  *  You may obtain a copy of the License at
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License.
19  *
20  *  SPDX-License-Identifier: Apache-2.0
21  *  ============LICENSE_END=========================================================
22  */
23
24 package org.onap.cps.utils
25
26 import org.onap.cps.TestUtils
27 import org.onap.cps.spi.exceptions.DataValidationException
28 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
29 import org.opendaylight.yangtools.yang.common.QName
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode
32 import spock.lang.Specification
33
34 class YangUtilsSpec extends Specification {
35     def 'Parsing a valid multicontainer Json String.'() {
36         given: 'a yang model (file)'
37             def jsonData = org.onap.cps.TestUtils.getResourceFileContent('multiple-object-data.json')
38         and: 'a model for that data'
39             def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('multipleDataTree.yang')
40             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
41         when: 'the json data is parsed'
42             def result = YangUtils.parseJsonData(jsonData, schemaContext)
43         then: 'a ContainerNode holding collection of normalized nodes is returned'
44             result.body().getAt(index) instanceof NormalizedNode == true
45         then: 'qualified name of children created is as expected'
46             result.body().getAt(index).getIdentifier().nodeType == QName.create('org:onap:ccsdk:multiDataTree', '2020-09-15', nodeName)
47         where:
48             index | nodeName
49             0     | 'first-container'
50             1     | 'last-container'
51     }
52
53     def 'Parsing a valid #scenario String.'() {
54         given: 'a yang model (file)'
55             def fileData = org.onap.cps.TestUtils.getResourceFileContent(contentFile)
56         and: 'a model for that data'
57             def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('bookstore.yang')
58             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
59         when: 'the data is parsed'
60             NormalizedNode result = YangUtils.parseData(contentType, fileData, schemaContext)
61         then: 'the result is a normalized node of the correct type'
62             if (revision) {
63                 result.identifier.nodeType == QName.create(namespace, revision, localName)
64             } else {
65                 result.identifier.nodeType == QName.create(namespace, localName)
66             }
67         where:
68             scenario | contentFile      | contentType      | namespace                                 | revision     | localName
69             'JSON'   | 'bookstore.json' | ContentType.JSON | 'org:onap:ccsdk:sample'                   | '2020-09-15' | 'bookstore'
70             'XML'    | 'bookstore.xml'  | ContentType.XML  | 'urn:ietf:params:xml:ns:netconf:base:1.0' | ''           | 'bookstore'
71     }
72
73     def 'Parsing invalid data: #description.'() {
74         given: 'a yang model (file)'
75             def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('bookstore.yang')
76             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
77         when: 'invalid data is parsed'
78             YangUtils.parseData(contentType, invalidData, schemaContext)
79         then: 'an exception is thrown'
80             thrown(DataValidationException)
81         where: 'the following invalid data is provided'
82             invalidData                                                                          | contentType      | description
83             '{incomplete json'                                                                   | ContentType.JSON | 'incomplete json'
84             '{"test:bookstore": {"address": "Parnell st." }}'                                    | ContentType.JSON | 'json with un-modelled data'
85             '{" }'                                                                               | ContentType.JSON | 'json with syntax exception'
86             '<data>'                                                                             | ContentType.XML  | 'incomplete xml'
87             '<data><bookstore><bookstore-anything>blabla</bookstore-anything></bookstore</data>' | ContentType.XML  | 'xml with invalid model'
88             ''                                                                                   | ContentType.XML  | 'empty xml'
89     }
90
91     def 'Parsing data fragment by xpath for #scenario.'() {
92         given: 'schema context'
93             def yangResourcesMap = TestUtils.getYangResourcesAsMap('test-tree.yang')
94             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourcesMap).getSchemaContext()
95         when: 'json string is parsed'
96             def result = YangUtils.parseData(contentType, nodeData, schemaContext, parentNodeXpath)
97         then: 'a ContainerNode holding collection of normalized nodes is returned'
98             result.body().getAt(0) instanceof NormalizedNode == true
99         then: 'result represents a node of expected type'
100             result.body().getAt(0).getIdentifier().nodeType == QName.create('org:onap:cps:test:test-tree', '2020-02-02', nodeName)
101         where:
102             scenario                         | contentType      | nodeData                                                                                                                                                                                                      | parentNodeXpath                       || nodeName
103             'JSON list element as container' | ContentType.JSON | '{ "branch": { "name": "B", "nest": { "name": "N", "birds": ["bird"] } } }'                                                                                                                                   | '/test-tree'                          || 'branch'
104             'JSON list element within list'  | ContentType.JSON | '{ "branch": [{ "name": "B", "nest": { "name": "N", "birds": ["bird"] } }] }'                                                                                                                                 | '/test-tree'                          || 'branch'
105             'JSON container element'         | ContentType.JSON | '{ "nest": { "name": "N", "birds": ["bird"] } }'                                                                                                                                                              | '/test-tree/branch[@name=\'Branch\']' || 'nest'
106             'XML element test tree'          | ContentType.XML  | '<?xml version=\'1.0\' encoding=\'UTF-8\'?><branch xmlns="org:onap:cps:test:test-tree"><name>Left</name><nest><name>Small</name><birds>Sparrow</birds></nest></branch>'                                       | '/test-tree'                          || 'branch'
107             'XML element branch xpath'       | ContentType.XML  | '<?xml version=\'1.0\' encoding=\'UTF-8\'?><branch xmlns="org:onap:cps:test:test-tree"><name>Left</name><nest><name>Small</name><birds>Sparrow</birds><birds>Robin</birds></nest></branch>'                   | '/test-tree'                          || 'branch'
108             'XML container element'          | ContentType.XML  | '<?xml version=\'1.0\' encoding=\'UTF-8\'?><nest xmlns="org:onap:cps:test:test-tree"><name>Small</name><birds>Sparrow</birds></nest>'                                                                         | '/test-tree/branch[@name=\'Branch\']' || 'nest'
109     }
110
111     def 'Parsing json data fragment by xpath error scenario: #scenario.'() {
112         given: 'schema context'
113             def yangResourcesMap = TestUtils.getYangResourcesAsMap('test-tree.yang')
114             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourcesMap).getSchemaContext()
115         when: 'json string is parsed'
116             YangUtils.parseJsonData('{"nest": {"name" : "Nest", "birds": ["bird"]}}', schemaContext,
117                     parentNodeXpath)
118         then: 'expected exception is thrown'
119             thrown(DataValidationException)
120         where:
121             scenario                             | parentNodeXpath
122             'xpath has no identifiers'           | '/'
123             'xpath has no valid identifiers'     | '/[@name=\'Name\']'
124             'invalid parent path'                | '/test-bush'
125             'another invalid parent path'        | '/test-tree/branch[@name=\'Branch\']/nest/name/last-name'
126             'fragment does not belong to parent' | '/test-tree/'
127     }
128
129     def 'Parsing json data with invalid json string: #description.'() {
130         given: 'schema context'
131             def yangResourcesMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
132             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourcesMap).getSchemaContext()
133         when: 'malformed json string is parsed'
134             YangUtils.parseJsonData(invalidJson, schemaContext)
135         then: 'an exception is thrown'
136             thrown(DataValidationException)
137         where: 'the following malformed json is provided'
138             description                                          | invalidJson
139             'malformed json string with unterminated array data' | '{bookstore={categories=[{books=[{authors=[Iain M. Banks]}]}]}}'
140             'incorrect json'                                     | '{" }'
141     }
142
143     def 'Parsing json data with space.'() {
144         given: 'schema context'
145             def yangResourcesMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
146             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourcesMap).getSchemaContext()
147         and: 'some json data with space in the array elements'
148             def jsonDataWithSpacesInArrayElement = TestUtils.getResourceFileContent('bookstore.json')
149         when: 'that json data is parsed'
150             YangUtils.parseJsonData(jsonDataWithSpacesInArrayElement, schemaContext)
151         then: 'no exception thrown'
152             noExceptionThrown()
153     }
154
155     def 'Parsing xPath to nodeId for #scenario.'() {
156         when: 'xPath is parsed'
157             def result = YangUtils.xpathToNodeIdSequence(xPath)
158         then: 'result represents an array of expected identifiers'
159             assert result == expectedNodeIdentifier
160         where: 'the following parameters are used'
161             scenario                                       | xPath                                                               || expectedNodeIdentifier
162             'container xpath'                              | '/test-tree'                                                        || ['test-tree']
163             'xpath contains list attribute'                | '/test-tree/branch[@name=\'Branch\']'                               || ['test-tree','branch']
164             'xpath contains list attributes with /'        | '/test-tree/branch[@name=\'/Branch\']/categories[@id=\'/broken\']'  || ['test-tree','branch','categories']
165     }
166
167     def 'Get key attribute statement without key attributes'() {
168         given: 'a path argument without key attributes'
169             def mockPathArgument = Mock(YangInstanceIdentifier.NodeIdentifierWithPredicates)
170             mockPathArgument.entrySet() >> [ ]
171         expect: 'the result is an empty string'
172             YangUtils.getKeyAttributesStatement(mockPathArgument) == ''
173     }
174 }