e9de1186ceb00a82b89f1b92c65d94f38a763b77
[smo/teiv.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2024 Ericsson
4  *  Modifications Copyright (C) 2024 OpenInfra Foundation Europe
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21 package org.oran.smo.yangtools.parser.model.statements.yang.test;
22
23 import static org.junit.Assert.assertTrue;
24
25 import java.util.Arrays;
26
27 import org.junit.Test;
28
29 import org.oran.smo.yangtools.parser.findings.ParserFindingType;
30 import org.oran.smo.yangtools.parser.model.statements.yang.YModule;
31 import org.oran.smo.yangtools.parser.testutils.YangTestCommon;
32
33 public class ImportTest extends YangTestCommon {
34
35     private static final String TEST_DIR = "src/test/resources/model-statements-yang/import-test/";
36
37     @Test
38     public void testTwoImportsWithoutRevision() {
39
40         parseAbsoluteImplementsYangModels(Arrays.asList(TEST_DIR + "import-twice-no-revisions-test-module.yang",
41                 YANG_METADATA_PATH));
42
43         assertHasFindingOfType(ParserFindingType.P036_MODULE_IMPORTED_MULTIPLE_TIMES.toString());
44
45         final YModule module = getModule("import-twice-no-revisions-test-module");
46         assertTrue(module != null);
47
48         assertStatementHasFindingOfType(module.getImports().get(1), ParserFindingType.P036_MODULE_IMPORTED_MULTIPLE_TIMES
49                 .toString());
50     }
51
52     @Test
53     public void testFirstImportNoRevision() {
54
55         parseAbsoluteImplementsYangModels(Arrays.asList(TEST_DIR + "import-twice-first-no-revision-test-module.yang",
56                 YANG_METADATA_PATH));
57
58         assertHasFindingOfType(ParserFindingType.P036_MODULE_IMPORTED_MULTIPLE_TIMES.toString());
59
60         final YModule module = getModule("import-twice-first-no-revision-test-module");
61         assertTrue(module != null);
62
63         assertStatementHasFindingOfType(module.getImports().get(1), ParserFindingType.P036_MODULE_IMPORTED_MULTIPLE_TIMES
64                 .toString());
65     }
66
67     @Test
68     public void testSecondImportNoRevision() {
69
70         parseAbsoluteImplementsYangModels(Arrays.asList(TEST_DIR + "import-twice-second-no-revision-test-module.yang",
71                 YANG_METADATA_PATH));
72
73         assertHasFindingOfType(ParserFindingType.P036_MODULE_IMPORTED_MULTIPLE_TIMES.toString());
74
75         final YModule module = getModule("import-twice-second-no-revision-test-module");
76         assertTrue(module != null);
77
78         assertStatementHasFindingOfType(module.getImports().get(1), ParserFindingType.P036_MODULE_IMPORTED_MULTIPLE_TIMES
79                 .toString());
80     }
81
82     @Test
83     public void testBothHaveSameRevision() {
84
85         parseAbsoluteImplementsYangModels(Arrays.asList(TEST_DIR + "import-twice-same-revisions-test-module.yang",
86                 YANG_METADATA_PATH));
87
88         assertHasFindingOfType(ParserFindingType.P036_MODULE_IMPORTED_MULTIPLE_TIMES.toString());
89
90         final YModule module = getModule("import-twice-same-revisions-test-module");
91         assertTrue(module != null);
92
93         assertStatementHasFindingOfType(module.getImports().get(1), ParserFindingType.P036_MODULE_IMPORTED_MULTIPLE_TIMES
94                 .toString());
95     }
96
97     @Test
98     public void testImportsItself() {
99
100         parseAbsoluteImplementsYangModels(Arrays.asList(TEST_DIR + "import-itself-test-module.yang"));
101
102         assertHasFindingOfType(ParserFindingType.P034_UNRESOLVABLE_IMPORT.toString());
103
104         final YModule module = getModule("import-itself-test-module");
105         assertTrue(module != null);
106
107         assertStatementHasFindingOfType(module.getImports().get(0), ParserFindingType.P034_UNRESOLVABLE_IMPORT.toString());
108     }
109
110     @Test
111     public void testUnresolvableNoRevisionImportNotInInput() {
112
113         parseAbsoluteImplementsYangModels(Arrays.asList(TEST_DIR + "unresolvable-import-no-revision-test-module.yang"));
114
115         assertHasFindingOfType(ParserFindingType.P034_UNRESOLVABLE_IMPORT.toString());
116
117         final YModule module = getModule("unresolvable-import-no-revision-test-module");
118         assertTrue(module != null);
119
120         assertStatementHasFindingOfType(module.getImports().get(0), ParserFindingType.P034_UNRESOLVABLE_IMPORT.toString());
121     }
122
123     @Test
124     public void testAmbiguousNoRevisionImport() {
125
126         parseAbsoluteImplementsYangModels(Arrays.asList(TEST_DIR + "unresolvable-import-ambiguous-test-module.yang",
127                 ORIG_MODULES_PATH + "ietf-yang-types-2010-09-24.yang",
128                 ORIG_MODULES_PATH + "ietf-yang-types-2019-11-04.yang"));
129
130         assertHasFindingOfType(ParserFindingType.P035_AMBIGUOUS_IMPORT.toString());
131
132         final YModule module = getModule("unresolvable-import-ambiguous-test-module");
133         assertTrue(module != null);
134
135         assertStatementHasFindingOfType(module.getImports().get(0), ParserFindingType.P035_AMBIGUOUS_IMPORT.toString());
136     }
137
138     @Test
139     public void testUnresolvableWithRevisionImportNotInInput() {
140
141         parseAbsoluteImplementsYangModels(Arrays.asList(TEST_DIR + "unresolvable-import-with-revision-test-module.yang"));
142
143         assertHasFindingOfType(ParserFindingType.P034_UNRESOLVABLE_IMPORT.toString());
144
145         final YModule module = getModule("unresolvable-import-with-revision-test-module");
146         assertTrue(module != null);
147
148         assertStatementHasFindingOfType(module.getImports().get(0), ParserFindingType.P034_UNRESOLVABLE_IMPORT.toString());
149     }
150
151     @Test
152     public void testUnresolvableWithWrongRevisionImport() {
153
154         parseAbsoluteImplementsYangModels(Arrays.asList(TEST_DIR + "unresolvable-import-wrong-revision-test-module.yang",
155                 ORIG_MODULES_PATH + "ietf-yang-types-2010-09-24.yang"));
156
157         assertHasFindingOfType(ParserFindingType.P034_UNRESOLVABLE_IMPORT.toString());
158
159         final YModule module = getModule("unresolvable-import-wrong-revision-test-module");
160         assertTrue(module != null);
161
162         assertStatementHasFindingOfType(module.getImports().get(0), ParserFindingType.P034_UNRESOLVABLE_IMPORT.toString());
163     }
164
165 }