cc6a517814945edb3a39ebe68c13e15bb03704b6
[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.math.BigDecimal;
26 import java.util.Arrays;
27
28 import org.junit.Test;
29
30 import org.oran.smo.yangtools.parser.findings.ParserFindingType;
31 import org.oran.smo.yangtools.parser.model.statements.yang.CY;
32 import org.oran.smo.yangtools.parser.model.statements.yang.YChoice;
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.model.statements.yang.YWhen;
36 import org.oran.smo.yangtools.parser.testutils.YangTestCommon;
37
38 public class GroupingTest extends YangTestCommon {
39
40     @Test
41     public void testGrouping() {
42
43         severityCalculator.suppressFinding(ParserFindingType.P133_GROUPING_USED_ONCE_ONLY.toString());
44
45         parseRelativeImplementsYangModels(Arrays.asList("grouping-test/uses-module.yang",
46                 "grouping-test/grouping-module.yang"));
47
48         assertNoFindings();
49
50         final YModule usesModule = getModule("uses-module");
51         assertTrue(usesModule != null);
52
53         final YContainer cont1 = getContainer(usesModule, "cont1");
54         assertTrue(cont1 != null);
55
56         assertTrue(getLeaf(cont1, "leaf11") != null);
57         assertTrue(getLeaf(cont1, "leaf11").getIfFeatures().size() == 0);
58         assertTrue(getContainer(cont1, "contgroup1") != null);
59         assertTrue(getContainer(cont1, "contgroup1").getIfFeatures().size() == 1);
60         assertTrue(getContainer(cont1, "contgroup1").getIfFeatures().get(0).getValue().equals("feature1 or feature2"));
61
62         assertTrue(getLeafUnderContainer(usesModule, "cont2", "leaf21") != null);
63         assertTrue(getLeafFromContainerContainer(usesModule, "cont2", "contgroup1", "leafgroup1") != null);
64
65         final YWhen when = getContainerUnderContainer(usesModule, "cont2", "contgroup1").getWhens().get(0);
66         assertTrue(when != null);
67         assertTrue(when.appliesToParentSchemaNode() == true);
68
69         final YContainer cont3 = getContainer(usesModule, "cont3");
70         assertTrue(cont3 != null);
71         assertTrue(cont3.getEffectiveNamespace().equals("urn:o-ran:yang:uses-module"));
72
73         assertTrue(getLeaf(cont3, "leaf31") != null);
74         assertTrue(getContainer(cont3, "contgroup1") != null);
75         assertTrue(getContainer(cont3, "contgroup1").getEffectiveNamespace().equals("urn:o-ran:yang:uses-module"));
76         assertTrue(getContainer(cont3, "contgroup1").getWhens().size() == 0);
77         assertTrue(getLeaf(getContainer(cont3, "contgroup1"), "leaf32") != null);
78         assertTrue(getLeaf(getContainer(cont3, "contgroup1"), "leaf32").getEffectiveNamespace().equals(
79                 "urn:o-ran:yang:uses-module"));
80         assertTrue(getLeaf(getContainer(cont3, "contgroup1"), "leaf32").getWhens().size() == 1);
81         assertTrue(getLeaf(getContainer(cont3, "contgroup1"), "leaf32").getWhens().get(0).getValue().equals("abc > 10"));
82
83         // - - - augmenting a choice
84
85         final YContainer cont4 = getContainer(usesModule, "cont4");
86         assertTrue(cont4 != null);
87
88         assertTrue(getLeaf(cont4, "leaf21") != null);
89         assertTrue(getLeaf(cont4, "leaf22") != null);
90
91         final YChoice choice23 = getChoice(cont4, "choice23");
92         assertTrue(choice23 != null);
93
94         assertTrue(getCase(choice23, "leaf41") != null);
95         assertTrue(getCase(choice23, "leaf42") != null);
96         assertTrue(getCase(choice23, "case43") != null);
97         assertTrue(getCase(choice23, "cont44") != null);
98
99         // - - - - refine handling (without refine first, then with refine)
100
101         {
102             final YContainer cont5 = getContainer(usesModule, "cont5");
103             assertTrue(cont5 != null);
104
105             final YContainer cont31 = getContainer(cont5, "cont31");
106             assertTrue(cont31 != null);
107
108             assertTrue(getLeaf(cont31, "leaf35").getDefault().getValue().equals("hello"));
109             assertTrue(getLeaf(cont31, "leaf35").getMandatory() == null);
110
111             assertTrue(getLeaf(cont31, "leaf36").getConfig().getValue().equals("true"));
112             assertTrue(getLeaf(cont31, "leaf36").getMandatory().isMandatoryTrue());
113
114             assertTrue(getLeafList(cont31, "leaflist37").getDefaults().size() == 3);
115             assertTrue(getLeafList(cont31, "leaflist37").getDefaults().get(0).getDecimalDefaultValue().compareTo(BigDecimal
116                     .valueOf(10L)) == 0);
117             assertTrue(getLeafList(cont31, "leaflist37").getDefaults().get(1).getDecimalDefaultValue().compareTo(BigDecimal
118                     .valueOf(20L)) == 0);
119             assertTrue(getLeafList(cont31, "leaflist37").getDefaults().get(2).getDecimalDefaultValue().compareTo(BigDecimal
120                     .valueOf(30L)) == 0);
121             assertTrue(getLeafList(cont31, "leaflist37").getMinElements().getMinValue() == 2);
122             assertTrue(getLeafList(cont31, "leaflist37").getMaxElements().getMaxValue() == 5);
123
124             final YContainer cont32 = getContainer(cont5, "cont32");
125             assertTrue(cont32 != null);
126
127             assertTrue(cont32.getPresence() == null);
128             assertTrue(cont32.getIfFeatures().size() == 1);
129             assertTrue(cont32.getIfFeatures().get(0).getValue().equals("feature1"));
130             assertTrue(cont32.getMusts().size() == 1);
131             assertTrue(cont32.getMusts().get(0).getXpathExpression().equals("abc > 10"));
132         }
133
134         {
135             final YContainer cont6 = getContainer(usesModule, "cont6");
136             assertTrue(cont6 != null);
137
138             final YContainer cont31 = getContainer(cont6, "cont31");
139             assertTrue(cont31 != null);
140
141             assertTrue(getLeaf(cont31, "leaf35").getDefault().getValue().equals("world"));
142             assertTrue(getLeaf(cont31, "leaf35").getMandatory().isMandatoryTrue());
143
144             assertTrue(getLeaf(cont31, "leaf36").getConfig().getValue().equals("false"));
145             assertTrue(getLeaf(cont31, "leaf36").getMandatory().isMandatoryFalse());
146
147             assertTrue(getLeafList(cont31, "leaflist37").getDefaults().size() == 2);
148             assertTrue(getLeafList(cont31, "leaflist37").getDefaults().get(0).getDecimalDefaultValue().compareTo(BigDecimal
149                     .valueOf(80L)) == 0);
150             assertTrue(getLeafList(cont31, "leaflist37").getDefaults().get(1).getDecimalDefaultValue().compareTo(BigDecimal
151                     .valueOf(90L)) == 0);
152             assertTrue(getLeafList(cont31, "leaflist37").getMinElements().getMinValue() == 1);
153             assertTrue(getLeafList(cont31, "leaflist37").getMaxElements().getMaxValue() == 6);
154
155             final YContainer cont32 = getContainer(cont6, "cont32");
156             assertTrue(cont32 != null);
157
158             assertTrue(cont32.getPresence() != null);
159             assertTrue(cont32.getIfFeatures().size() == 2);
160             assertTrue(cont32.getIfFeatures().get(0).getValue().equals("feature1"));
161             assertTrue(cont32.getIfFeatures().get(1).getValue().equals("feature99"));
162             assertTrue(cont32.getMusts().size() == 2);
163             assertTrue(cont32.getMusts().get(0).getXpathExpression().equals("abc > 10"));
164             assertTrue(cont32.getMusts().get(1).getXpathExpression().equals("xyz > 99"));
165         }
166
167         // - - - - refine handling (extensions)
168
169         final YContainer cont7 = getContainer(usesModule, "cont7");
170         assertTrue(cont7 != null);
171
172         final YContainer cont41 = getContainer(cont7, "cont41");
173         assertTrue(cont41 != null);
174
175         assertTrue(getExtension(cont41, "grouping-module", "extension1", null) != null);
176         assertTrue(getExtension(cont41, "grouping-module", "extension2", "hello world") != null);
177         assertTrue(getExtension(cont41, "grouping-module", "extension2", "hello") == null);
178         assertTrue(getExtension(cont41, "grouping-module", "extension2", "world") == null);
179         assertTrue(getExtension(cont41, "grouping-module", "extension3", null) != null);
180
181         final YContainer cont42 = getContainer(cont7, "cont42");
182         assertTrue(cont42 != null);
183
184         assertTrue(getExtension(cont42, "grouping-module", "extension1", null) != null);
185         assertTrue(getExtension(cont42, "grouping-module", "extension2", "abc") != null);
186         assertTrue(getExtension(cont42, "grouping-module", "extension2", "def") != null);
187         assertTrue(getExtension(cont42, "grouping-module", "extension3", null) != null);
188     }
189
190     @Test
191     public void testGroupingFindings___do_not_suppress_unused() {
192
193         severityCalculator.suppressFinding(ParserFindingType.P133_GROUPING_USED_ONCE_ONLY.toString());
194         context.setSuppressFindingsOnUnusedSchemaNodes(false);
195         parseRelativeImplementsYangModels(Arrays.asList("grouping-test/faulty-grouping-module.yang"));
196
197         final YModule module = getModule("faulty-grouping-module");
198
199         assertStatementHasFindingOfType(getGrouping(module, "grouping1").getUses().get(0),
200                 ParserFindingType.P121_CIRCULAR_USES_REFERENCES.toString());
201
202         // - - - - circular usage
203
204         assertTrue(getGrouping(module, "grouping2") != null);
205         assertStatementHasFindingOfType(getGrouping(module, "grouping2").getUses().get(0),
206                 ParserFindingType.P121_CIRCULAR_USES_REFERENCES.toString());
207
208         assertTrue(getGrouping(module, "grouping3") != null);
209         assertStatementHasFindingOfType(getGrouping(module, "grouping3").getUses().get(0),
210                 ParserFindingType.P121_CIRCULAR_USES_REFERENCES.toString());
211
212         // ------ augment path cannot be found or syntax error
213
214         assertTrue(getContainer(module, "cont5") != null);
215         assertHasFindingOfTypeAndContainsMessage(ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT.toString(),
216                 "'/absolute-path'");
217
218         assertTrue(getContainer(module, "cont6") != null);
219         assertHasFindingOfTypeAndContainsMessage(ParserFindingType.P054_UNRESOLVABLE_PATH.toString(),
220                 "'container-that-does-not-exist-in-simple-grouping'");
221
222         // - - - - - - Invalid paths - - - - -
223
224         //        printFindings();
225
226         assertStatementHasFindingOfType(getChild(module, CY.USES, null), ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT
227                 .toString());
228
229         assertStatementHasFindingOfType(getChild(module, CY.USES, ""), ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT
230                 .toString());
231
232         assertStatementHasFindingOfType(getChild(module, CY.USES, " "), ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT
233                 .toString());
234
235         assertStatementHasFindingOfType(getChild(module, CY.USES, "nsm:non-existing-grouping"),
236                 ParserFindingType.P131_UNRESOLVABLE_GROUPING.toString());
237
238         final YContainer cont8 = getContainer(module, "cont8");
239
240         assertDomElementHasFindingOfType(cont8.getDomElement().getChildren().get(0).getChildren().get(0),
241                 ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT.toString());
242         assertDomElementHasFindingOfType(cont8.getDomElement().getChildren().get(0).getChildren().get(1),
243                 ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT.toString());
244         assertDomElementHasFindingOfType(cont8.getDomElement().getChildren().get(0).getChildren().get(2),
245                 ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT.toString());
246         assertDomElementHasFindingOfType(cont8.getDomElement().getChildren().get(0).getChildren().get(3),
247                 ParserFindingType.P054_UNRESOLVABLE_PATH.toString());
248
249         assertDomElementHasFindingOfType(cont8.getDomElement().getChildren().get(1).getChildren().get(0),
250                 ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT.toString());
251         assertDomElementHasFindingOfType(cont8.getDomElement().getChildren().get(1).getChildren().get(1),
252                 ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT.toString());
253         assertDomElementHasFindingOfType(cont8.getDomElement().getChildren().get(1).getChildren().get(2),
254                 ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT.toString());
255         assertDomElementHasFindingOfType(cont8.getDomElement().getChildren().get(1).getChildren().get(3),
256                 ParserFindingType.P054_UNRESOLVABLE_PATH.toString());
257
258         // - - - - - - Nested unresolvable uses - - - - -
259
260         assertTrue(getContainer(module, "cont9") != null);
261         assertStatementHasFindingOfType(getContainer(module, "cont9").getUses().get(0),
262                 ParserFindingType.P134_NESTED_USES_NOT_RESOLVABLE.toString());
263         assertFindingCountOnStatement(getContainer(module, "cont9").getUses().get(0), 2);
264     }
265
266     @Test
267     public void test_uses_refine() {
268
269         severityCalculator.suppressFinding(ParserFindingType.P133_GROUPING_USED_ONCE_ONLY.toString());
270
271         parseRelativeImplementsYangModels(Arrays.asList("grouping-test/uses-refine-module.yang"));
272
273         final YModule module = getModule("uses-refine-module");
274         assertTrue(module != null);
275
276         // - - - - should be all ok - - - - - -
277
278         final YContainer cont1 = getContainer(module, "cont1");
279
280         assertTrue(getLeaf(cont1, "leaf1").getDescription().getValue().equals("new description"));
281         assertTrue(getLeaf(cont1, "leaf1").getReference().getValue().equals("new reference"));
282         assertTrue(getLeaf(cont1, "leaf1").getConfig().isConfigTrue());
283         assertTrue(getLeaf(cont1, "leaf1").getDefault().getValue().equals("world"));
284         assertTrue(getLeaf(cont1, "leaf1").getMandatory().isMandatoryTrue());
285         assertTrue(getLeaf(cont1, "leaf1").getIfFeatures().size() == 1);
286         assertTrue(getLeaf(cont1, "leaf1").getIfFeatures().get(0).getValue().equals("feature2"));
287
288         assertTrue(getLeaf(cont1, "leaf2").getDescription().getValue().equals("old description"));
289         assertTrue(getLeaf(cont1, "leaf2").getReference().getValue().equals("old reference"));
290         assertTrue(getLeaf(cont1, "leaf2").getConfig().isConfigFalse());
291         assertTrue(getLeaf(cont1, "leaf2").getDefault().getValue().equals("world"));
292         assertTrue(getLeaf(cont1, "leaf2").getMandatory().isMandatoryFalse());
293         assertTrue(getLeaf(cont1, "leaf2").getIfFeatures().size() == 1);
294         assertTrue(getLeaf(cont1, "leaf2").getIfFeatures().get(0).getValue().equals("feature1"));
295
296         assertTrue(getLeaf(cont1, "leaf5").getMusts().size() == 2);
297         assertTrue(getLeaf(cont1, "leaf5").getMusts().get(0).getXpathExpression().equals(". > ../leaf3"));
298         assertTrue(getLeaf(cont1, "leaf5").getMusts().get(1).getXpathExpression().equals(". > ../leaf4"));
299
300         assertTrue(getLeafList(cont1, "leaflist14").getDefaults().size() == 2);
301         assertTrue(getLeafList(cont1, "leaflist14").getDefaults().get(0).getDecimalDefaultValue().compareTo(BigDecimal
302                 .valueOf(50L)) == 0);
303         assertTrue(getLeafList(cont1, "leaflist14").getDefaults().get(1).getDecimalDefaultValue().compareTo(BigDecimal
304                 .valueOf(60L)) == 0);
305         assertTrue(getLeafList(cont1, "leaflist14").getMinElements().getMinValue() == 2);
306         assertTrue(getLeafList(cont1, "leaflist14").getMaxElements().getMaxValue() == 8);
307         assertTrue(getLeafList(cont1, "leaflist14").getIfFeatures().size() == 2);
308         assertTrue(getLeafList(cont1, "leaflist14").getIfFeatures().get(0).getValue().equals("feature1"));
309         assertTrue(getLeafList(cont1, "leaflist14").getIfFeatures().get(1).getValue().equals("feature2"));
310
311         assertTrue(getLeafList(cont1, "leaflist15").getDefaults().size() == 3);
312         assertTrue(getLeafList(cont1, "leaflist15").getDefaults().get(0).getDecimalDefaultValue().compareTo(BigDecimal
313                 .valueOf(50L)) == 0);
314         assertTrue(getLeafList(cont1, "leaflist15").getDefaults().get(1).getDecimalDefaultValue().compareTo(BigDecimal
315                 .valueOf(60L)) == 0);
316         assertTrue(getLeafList(cont1, "leaflist15").getDefaults().get(2).getDecimalDefaultValue().compareTo(BigDecimal
317                 .valueOf(70L)) == 0);
318         assertTrue(getLeafList(cont1, "leaflist15").getMinElements().getMinValue() == 3);
319         assertTrue(getLeafList(cont1, "leaflist15").getMaxElements().getMaxValue() == 9);
320
321         assertTrue(getContainer(cont1, "cont74").getPresence().getValue().equals("meaningful!"));
322
323         // - - - - - - error scenarios - - - - - -
324
325         final YContainer cont2 = getContainer(module, "cont2");
326
327         assertStatementHasFindingOfType(cont2, ParserFindingType.P124_INVALID_REFINE_TARGET_NODE.toString());
328         assertStatementHasFindingOfType(cont2, ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT.toString());
329     }
330 }