80e8db01b6ecb126b8e9faabd3466573c3b5980b
[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 import java.util.Collections;
27
28 import org.junit.Test;
29
30 import org.oran.smo.yangtools.parser.findings.ParserFindingType;
31 import org.oran.smo.yangtools.parser.model.ConformanceType;
32 import org.oran.smo.yangtools.parser.model.statements.yang.CY;
33 import org.oran.smo.yangtools.parser.model.statements.yang.YContainer;
34 import org.oran.smo.yangtools.parser.model.statements.yang.YModule;
35 import org.oran.smo.yangtools.parser.testutils.YangTestCommon;
36
37 public class ConformanceTypeTest extends YangTestCommon {
38
39     @Test
40     public void test2ModulesImports() {
41
42         context.setFailFast(false);
43         context.setIgnoreImportedProtocolAccessibleObjects(false);
44
45         parseRelativeYangModels(Collections.<String> emptyList(), Arrays.asList("conformance-type-test/module1.yang",
46                 "conformance-type-test/module2.yang"));
47
48         assertHasFindingOfType(ParserFindingType.P005_NO_IMPLEMENTS.toString());
49
50         final YModule module1 = getModule("module1");
51         final YModule module2 = getModule("module2");
52
53         assertTrue(module1 != null);
54         assertTrue(module2 != null);
55
56         final YContainer container1 = getContainer(module1, "cont1");
57         final YContainer container2 = getContainer(module2, "cont2");
58
59         assertTrue(container1.getEffectiveConformanceType() == ConformanceType.IMPORT);
60         assertTrue(container2.getEffectiveConformanceType() == ConformanceType.IMPORT);
61
62         final YContainer contGroup2InModule1 = getContainer(module1, "contgroup2");
63         final YContainer contGroup2InModule2 = getContainer(module2, "contgroup2");
64
65         assertTrue(contGroup2InModule1.getEffectiveConformanceType() == ConformanceType.IMPORT);
66         assertTrue(contGroup2InModule2.getEffectiveConformanceType() == ConformanceType.IMPORT);
67     }
68
69     @Test
70     public void test2ModulesImplements() {
71
72         parseRelativeImplementsYangModels(Arrays.asList("conformance-type-test/module1.yang",
73                 "conformance-type-test/module2.yang"));
74
75         assertHasNotFindingOfType(ParserFindingType.P005_NO_IMPLEMENTS.toString());
76
77         final YModule module1 = getModule("module1");
78         final YModule module2 = getModule("module2");
79
80         assertTrue(module1 != null);
81         assertTrue(module2 != null);
82
83         final YContainer container1 = getContainer(module1, "cont1");
84         final YContainer container2 = getContainer(module2, "cont2");
85
86         assertTrue(container1.getEffectiveConformanceType() == ConformanceType.IMPLEMENT);
87         assertTrue(container2.getEffectiveConformanceType() == ConformanceType.IMPLEMENT);
88
89         final YContainer contGroup2InModule1 = getContainer(module1, "contgroup2");
90         final YContainer contGroup2InModule2 = getContainer(module2, "contgroup2");
91
92         assertTrue(contGroup2InModule1.getEffectiveConformanceType() == ConformanceType.IMPLEMENT);
93         assertTrue(contGroup2InModule2.getEffectiveConformanceType() == ConformanceType.IMPLEMENT);
94     }
95
96     @Test
97     public void test2ModulesOneImplementsOneImports() {
98
99         context.setIgnoreImportedProtocolAccessibleObjects(false);
100         parseRelativeYangModels(Arrays.asList("conformance-type-test/module1.yang"), Arrays.asList(
101                 "conformance-type-test/module2.yang"));
102
103         final YModule module1 = getModule("module1");
104         final YModule module2 = getModule("module2");
105
106         assertTrue(module1 != null);
107         assertTrue(module2 != null);
108
109         final YContainer container1 = getContainer(module1, "cont1");
110         final YContainer container2 = getContainer(module2, "cont2");
111
112         assertTrue(container1.getEffectiveConformanceType() == ConformanceType.IMPLEMENT);
113         assertTrue(container2.getEffectiveConformanceType() == ConformanceType.IMPORT);
114
115         final YContainer contGroup2InModule1 = getContainer(module1, "contgroup2");
116         final YContainer contGroup2InModule2 = getContainer(module2, "contgroup2");
117
118         assertTrue(contGroup2InModule1.getEffectiveConformanceType() == ConformanceType.IMPLEMENT);
119         assertTrue(contGroup2InModule2.getEffectiveConformanceType() == ConformanceType.IMPORT);
120     }
121
122     /*
123      * Tests for "ignoreImportedProtocolAccessibleObjects"
124      */
125
126     @Test
127     public void test2ModulesImplementsIgnoreImportedProtocolAccessibleObjects() {
128
129         context.setIgnoreImportedProtocolAccessibleObjects(true);
130
131         parseRelativeImplementsYangModels(Arrays.asList("conformance-type-test/module1.yang",
132                 "conformance-type-test/module2.yang"));
133
134         printFindings();
135         assertNoFindings();
136
137         final YModule module1 = getModule("module1");
138         final YModule module2 = getModule("module2");
139
140         assertTrue(module1 != null);
141         assertTrue(module2 != null);
142
143         final YContainer container1 = getContainer(module1, "cont1");
144         final YContainer container2 = getContainer(module2, "cont2");
145         assertTrue(container1.getEffectiveConformanceType() == ConformanceType.IMPLEMENT);
146         assertTrue(container2.getEffectiveConformanceType() == ConformanceType.IMPLEMENT);
147
148         final YContainer contGroup2InModule1 = getContainer(module1, "contgroup2");
149         final YContainer contGroup2InModule2 = getContainer(module2, "contgroup2");
150         assertTrue(contGroup2InModule1.getEffectiveConformanceType() == ConformanceType.IMPLEMENT);
151         assertTrue(contGroup2InModule2.getEffectiveConformanceType() == ConformanceType.IMPLEMENT);
152
153         assertTrue(getChild(module2, CY.RPC, "rpc1") != null);
154         assertTrue(getChild(module2, CY.LIST, "list1") != null);
155
156         assertTrue(getChild(module2, CY.FEATURE, "feature1") != null);
157         assertTrue(getChild(module2, "this:ext", "ext1") != null);
158     }
159
160     @Test
161     public void test2ModulesOneImplementsOneImportsIgnoreImportedProtocolAccessibleObjects() {
162
163         context.setIgnoreImportedProtocolAccessibleObjects(true);
164
165         parseRelativeYangModels(Arrays.asList("conformance-type-test/module1.yang"), Arrays.asList(
166                 "conformance-type-test/module2.yang"));
167
168         final YModule module1 = getModule("module1");
169         final YModule module2 = getModule("module2");
170
171         assertTrue(module1 != null);
172         assertTrue(module2 != null);
173
174         final YContainer container1 = getContainer(module1, "cont1");
175         final YContainer container2 = getContainer(module2, "cont2");
176
177         assertTrue(container1.getEffectiveConformanceType() == ConformanceType.IMPLEMENT);
178         assertTrue(container2 == null);
179
180         final YContainer contGroup2InModule1 = getContainer(module1, "contgroup2");
181         final YContainer contGroup2InModule2 = getContainer(module2, "contgroup2");
182
183         assertTrue(contGroup2InModule1.getEffectiveConformanceType() == ConformanceType.IMPLEMENT);
184         assertTrue(contGroup2InModule2 == null);
185
186         assertTrue(getChild(module2, CY.RPC, "rpc1") == null);
187         assertTrue(getChild(module2, CY.LIST, "list1") == null);
188
189         assertTrue(getChild(module2, CY.FEATURE, "feature1") != null);
190         assertTrue(getChild(module2, "this:ext", "ext1") != null);
191     }
192
193     // - - - - test crossing conformance type for module / submodule
194
195     @Test
196     public void test2ModuleAndSubmoduleSameConformance() {
197
198         parseRelativeYangModels(Arrays.asList("conformance-type-test/including-module.yang",
199                 "conformance-type-test/submodule.yang"), Collections.emptyList());
200
201         final YModule module = getModule("including-module");
202         assertTrue(module != null);
203
204         assertNoFindings();
205     }
206
207     @Test
208     public void test2ModuleAndSubmoduleDifferentConformance() {
209
210         parseRelativeYangModels(Arrays.asList("conformance-type-test/including-module.yang"), Arrays.asList(
211                 "conformance-type-test/submodule.yang"));
212
213         final YModule module = getModule("including-module");
214         assertTrue(module != null);
215
216         assertHasFindingOfType(ParserFindingType.P006_IMPLEMENT_IMPORT_MISMATCH.toString());
217     }
218
219 }