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
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.cps.utils
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode
23 import org.opendaylight.yangtools.yang.common.QName
24 import org.opendaylight.yangtools.yang.common.Revision
25 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException
26 import spock.lang.Specification
27 import spock.lang.Unroll
29 class YangUtilsSpec extends Specification{
30 def 'Parsing a valid Yang Model'() {
31 given: 'a yang model (file)'
32 def file = new File(ClassLoader.getSystemClassLoader().getResource('bookstore.yang').getFile())
33 when: 'the file is parsed'
34 def result = YangUtils.parseYangModelFile(file)
35 then: 'the result contain 1 module of the correct name and revision'
36 result.modules.size() == 1
37 def optionalModule = result.findModule('bookstore', Revision.of('2020-09-15'))
38 optionalModule.isPresent()
42 def 'parsing invalid yang file (#description)'() {
43 given: 'a file with #description'
44 File file = new File(ClassLoader.getSystemClassLoader().getResource(filename).getFile());
45 when: 'the file is parsed'
46 YangUtils.parseYangModelFile(file)
47 then: 'an exception is thrown'
48 thrown(expectedException)
49 where: 'the following parameters are used'
50 filename | description || expectedException
51 'invalid.yang' | 'no valid content' || YangSyntaxErrorException
52 'someOtherFile.txt' | 'no .yang extension' || IllegalArgumentException
55 def 'Parsing a valid Json String'() {
56 given: 'a yang model (file)'
57 def jsonData = org.onap.cps.TestUtils.getResourceFileContent('bookstore.json')
58 and: 'a model for that data'
59 def file = new File(ClassLoader.getSystemClassLoader().getResource('bookstore.yang').getFile())
60 def schemaContext = YangUtils.parseYangModelFile(file)
61 when: 'the json data is parsed'
62 NormalizedNode<?, ?> result = YangUtils.parseJsonData(jsonData, schemaContext);
63 then: 'the result is a normalized node of the correct type'
64 result.nodeType == QName.create('org:onap:ccsdk:sample','2020-09-15','bookstore')
67 def 'Parsing an invalid Json String'() {
68 given: 'a yang model (file)'
69 def jsonData = '{incomplete json'
71 def file = new File(ClassLoader.getSystemClassLoader().getResource('bookstore.yang').getFile())
72 def schemaContext = YangUtils.parseYangModelFile(file)
73 when: 'the invalid json is parsed'
74 YangUtils.parseJsonData(jsonData, schemaContext);
75 then: ' an exception is thrown'
76 thrown(IllegalStateException)