801e43079a6f7fbd8807c9b2ff88bae53a864df4
[cps.git] / cps-service / src / test / groovy / org / onap / cps / utils / YangUtilsSpec.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.cps.utils
21
22 import org.onap.cps.TestUtils
23 import org.opendaylight.yangtools.yang.common.QName
24 import org.opendaylight.yangtools.yang.common.Revision
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode
26 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException
27 import spock.lang.Specification
28 import spock.lang.Unroll
29
30 class YangUtilsSpec extends Specification{
31
32     def 'Parsing a valid Yang Model'() {
33         given: 'a yang model (file)'
34             def file = new File(ClassLoader.getSystemClassLoader().getResource('bookstore.yang').getFile())
35         when: 'the file is parsed'
36             def result = YangUtils.parseYangModelFile(file)
37         then: 'the result contain 1 module of the correct name and revision'
38             result.modules.size() == 1
39             def optionalModule = result.findModule('stores', Revision.of('2020-09-15'))
40             optionalModule.isPresent()
41     }
42
43     @Unroll
44     def 'Parsing invalid yang file (#description).'() {
45         given: 'a file with #description'
46             File file = new File(ClassLoader.getSystemClassLoader().getResource(filename).getFile())
47         when: 'the file is parsed'
48             YangUtils.parseYangModelFile(file)
49         then: 'an exception is thrown'
50             thrown(expectedException)
51         where: 'the following parameters are used'
52              filename           | description          || expectedException
53             'invalid.yang'      | 'no valid content'   || YangSyntaxErrorException
54             'someOtherFile.txt' | 'no .yang extension' || IllegalArgumentException
55     }
56
57     def 'Parsing a valid Json String.'() {
58         given: 'a yang model (file)'
59             def jsonData = org.onap.cps.TestUtils.getResourceFileContent('bookstore.json')
60         and: 'a model for that data'
61             def file = new File(ClassLoader.getSystemClassLoader().getResource('bookstore.yang').getFile())
62             def schemaContext = YangUtils.parseYangModelFile(file)
63         when: 'the json data is parsed'
64             NormalizedNode<?, ?> result = YangUtils.parseJsonData(jsonData, schemaContext)
65         then: 'the result is a normalized node of the correct type'
66             result.nodeType == QName.create('org:onap:ccsdk:sample', '2020-09-15', 'bookstore')
67     }
68
69     @Unroll
70     def 'Parsing invalid data: #description.'() {
71         given: 'a yang model (file)'
72             def file = new File(ClassLoader.getSystemClassLoader().getResource('bookstore.yang').getFile())
73             def schemaContext = YangUtils.parseYangModelFile(file)
74         when: 'invalid data is parsed'
75             YangUtils.parseJsonData(invalidJson, schemaContext)
76         then: 'an exception is thrown'
77             thrown(IllegalStateException)
78         where: 'the following invalid json is provided'
79             invalidJson                                       | description
80             '{incomplete json'                                | 'incomplete json'
81             '{"test:bookstore": {"address": "Parnell st." }}' | 'json with un-modelled data'
82     }
83
84     def 'Breaking a Json Data Object into fragments.'() {
85         given: 'a Yang module'
86             def file = new File(ClassLoader.getSystemClassLoader().getResource('bookstore.yang').getFile())
87             def schemaContext = YangUtils.parseYangModelFile(file)
88             def module = schemaContext.findModule('stores', Revision.of('2020-09-15')).get()
89         and: 'a normalized node for that model'
90             def jsonData = TestUtils.getResourceFileContent('bookstore.json')
91             def normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext)
92         when: 'the json data is fragmented'
93             def result = YangUtils.fragmentNormalizedNode(normalizedNode, module)
94         then: 'the system creates a (root) fragment without a parent and 2 children (categories)'
95             result.parentFragment == null
96             result.childFragments.size() == 2
97         and: 'each child (category) has the root fragment (result) as parent and in turn as 1 child (a list of books)'
98             result.childFragments.each { it.parentFragment == result && it.childFragments.size() == 1 }
99         and: 'the fragments have the correct xpaths'
100             assert result.xpath == '/bookstore'
101             assert result.childFragments.collect { it.xpath }
102                 .containsAll(["/bookstore/categories[@code='01']", "/bookstore/categories[@code='02']"])
103     }
104
105 }