Merge "Fix: Make bookstore data consistent"
[cps.git] / cps-service / src / test / groovy / org / onap / cps / yang / YangTextSchemaSourceSetBuilderSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2020-2021 Pantheon.tech
4  *  Modifications Copyright (C) 2020-2022 Nordix Foundation
5  *  Modifications Copyright (C) 2021 Bell Canada.
6  *  Modifications Copyright (C) 2022 TechMahindra Ltd.
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.yang
25
26 import org.onap.cps.TestUtils
27 import org.onap.cps.spi.exceptions.ModelValidationException
28 import org.opendaylight.yangtools.yang.common.Revision
29 import spock.lang.Specification
30
31 import java.nio.charset.StandardCharsets
32
33 class YangTextSchemaSourceSetBuilderSpec extends Specification {
34
35     def 'Building a valid YangTextSchemaSourceSet using #filenameCase filename.'() {
36         given: 'a yang model (file)'
37             def yangResourceNameToContent = [filename: TestUtils.getResourceFileContent('bookstore.yang')]
38         when: 'the content is parsed'
39             def result = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
40         then: 'it can be validated successfully'
41             YangTextSchemaSourceSetBuilder.validate(yangResourceNameToContent)
42         and: 'the result contains 1 module of the correct name and revision'
43             result.modules.size() == 1
44             def optionalModule = result.findModule('stores', Revision.of('2020-09-15'))
45             optionalModule.isPresent()
46         where:
47             filenameCase           | filename
48             'generic'              | 'bookstore'
49             'RFC-6020 recommended' | 'bookstore-test@2020-09-15.YANG'
50     }
51
52     def 'Building YangTextSchemaSourceSet error case: #description.'() {
53         given: 'a file with #description'
54             def yangResourceNameToContent = TestUtils.getYangResourcesAsMap(filename)
55         when: 'the content is parsed'
56             YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent)
57         then: 'an exception is thrown'
58             thrown(expectedException)
59         where: 'the following parameters are used'
60             filename                      | description            || expectedException
61             'invalid.yang'                | 'invalid content'      || ModelValidationException
62             'invalid-empty.yang'          | 'no valid content'     || ModelValidationException
63             'invalid-missing-import.yang' | 'no dependency module' || ModelValidationException
64     }
65
66     def 'Convert yang source to a YangTextSchemaSource.'() {
67         given: 'a yang source text'
68             def yangSourceText = TestUtils.getResourceFileContent('bookstore.yang')
69         when: 'convert it to a YangTextSchemaSource'
70             def result = YangTextSchemaSourceSetBuilder.toYangTextSchemaSource('some name', yangSourceText)
71         then: 'the converted object has correct properties'
72             assert result.toString() == '{identifier=RevisionSourceIdentifier [name=some name]}'
73             assert new String(result.openStream().readAllBytes(), StandardCharsets.UTF_8) ==  yangSourceText
74         and: 'it has no symbolic name'
75             assert result.getSymbolicName().isEmpty()
76     }
77 }