Fix yang resource parse failure if filename matches IETF recommended format
[cps.git] / cps-service / src / test / groovy / org / onap / cps / utils / YangTextSchemaSourceSetSpec.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.onap.cps.spi.exceptions.ModelValidationException
24 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
25 import org.opendaylight.yangtools.yang.common.Revision
26 import spock.lang.Specification
27 import spock.lang.Unroll
28
29 class YangTextSchemaSourceSetSpec extends Specification {
30
31     @Unroll
32     def 'Building a valid YangTextSchemaSourceSet using #filenameCase filename.'() {
33         given: 'a yang model (file)'
34             def yangResourceNameToContent = [filename: TestUtils.getResourceFileContent('bookstore.yang')]
35         when: 'the content is parsed'
36             def result = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext();
37         then: 'the result contains 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         where:
42             filenameCase           | filename
43             'generic'              | 'bookstore'
44             'RFC-6020 recommended' | 'bookstore-test@2020-09-15.YANG'
45     }
46
47     @Unroll
48     def 'Building YangTextSchemaSourceSet error case: #description.'() {
49         given: 'a file with #description'
50             def yangResourceNameToContent = TestUtils.getYangResourcesAsMap(filename)
51         when: 'the content is parsed'
52             YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent)
53         then: 'an exception is thrown'
54             thrown(expectedException)
55         where: 'the following parameters are used'
56             filename                      | description            || expectedException
57             'invalid.yang'                | 'invalid content'      || ModelValidationException
58             'invalid-empty.yang'          | 'no valid content'     || ModelValidationException
59             'invalid-missing-import.yang' | 'no dependency module' || ModelValidationException
60     }
61 }