Fix sonar code smells
[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  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  *  SPDX-License-Identifier: Apache-2.0
20  *  ============LICENSE_END=========================================================
21  */
22
23 package org.onap.cps.yang
24
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 class YangTextSchemaSourceSetBuilderSpec extends Specification {
32
33     def 'Building a valid YangTextSchemaSourceSet using #filenameCase filename.'() {
34         given: 'a yang model (file)'
35             def yangResourceNameToContent = [filename: TestUtils.getResourceFileContent('bookstore.yang')]
36         when: 'the content is parsed'
37             def result = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
38         then: 'it can be validated successfully'
39             YangTextSchemaSourceSetBuilder.validate(yangResourceNameToContent)
40         and: 'the result contains 1 module of the correct name and revision'
41             result.modules.size() == 1
42             def optionalModule = result.findModule('stores', Revision.of('2020-09-15'))
43             optionalModule.isPresent()
44         where:
45             filenameCase           | filename
46             'generic'              | 'bookstore'
47             'RFC-6020 recommended' | 'bookstore-test@2020-09-15.YANG'
48     }
49
50     def 'Building YangTextSchemaSourceSet error case: #description.'() {
51         given: 'a file with #description'
52             def yangResourceNameToContent = TestUtils.getYangResourcesAsMap(filename)
53         when: 'the content is parsed'
54             YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent)
55         then: 'an exception is thrown'
56             thrown(expectedException)
57         where: 'the following parameters are used'
58             filename                      | description            || expectedException
59             'invalid.yang'                | 'invalid content'      || ModelValidationException
60             'invalid-empty.yang'          | 'no valid content'     || ModelValidationException
61             'invalid-missing-import.yang' | 'no dependency module' || ModelValidationException
62     }
63 }