ad7c58154fab4a67cac8231826cbdeef3269052a
[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.YChoice;
31 import org.oran.smo.yangtools.parser.model.statements.yang.YContainer;
32 import org.oran.smo.yangtools.parser.model.statements.yang.YInput;
33 import org.oran.smo.yangtools.parser.model.statements.yang.YList;
34 import org.oran.smo.yangtools.parser.model.statements.yang.YModule;
35 import org.oran.smo.yangtools.parser.model.statements.yang.YRpc;
36 import org.oran.smo.yangtools.parser.model.statements.yang.YStatus;
37 import org.oran.smo.yangtools.parser.testutils.YangTestCommon;
38
39 public class AugmentTest extends YangTestCommon {
40
41     @Test
42     public void testAugmentIntoEmptyActionAndRpc() {
43
44         severityCalculator.suppressFinding(ParserFindingType.P152_AUGMENT_TARGET_NODE_IN_SAME_MODULE.toString());
45
46         parseRelativeImplementsYangModels(Arrays.asList("augment-test/augment-test-module.yang",
47                 "augment-test/augment-test-module2.yang"));
48
49         final YModule augmentTestModule = getModule("augment-test-module");
50         assertTrue(augmentTestModule != null);
51
52         assertTrue(augmentTestModule.getRpcs().size() == 1);
53         assertTrue(augmentTestModule.getRpcs().get(0).getInput() != null);
54         assertTrue(augmentTestModule.getRpcs().get(0).getOutput() != null);
55
56         assertTrue(augmentTestModule.getRpcs().get(0).getInput().getLeafs().size() == 1);
57         assertTrue(augmentTestModule.getRpcs().get(0).getOutput().getLeafs().size() == 1);
58
59         final YContainer cont1 = getContainer(augmentTestModule, "cont1");
60
61         assertTrue(cont1.getActions().size() == 1);
62         assertTrue(cont1.getActions().get(0).getInput() != null);
63         assertTrue(cont1.getActions().get(0).getOutput() != null);
64
65         assertTrue(cont1.getActions().get(0).getInput().getLeafs().size() == 1);
66         assertTrue(cont1.getActions().get(0).getOutput().getLeafs().size() == 1);
67
68         // Tests added to handle augments into a choice
69
70         final YRpc rpc1 = getRpc(augmentTestModule, "rpc1");
71         final YInput rpc1input = rpc1.getInput();
72         final YContainer rpc1cont1 = getContainer(rpc1input, "cont1");
73         final YChoice choice1 = getChoice(rpc1cont1, "choice1");
74
75         assertTrue(getCase(choice1, "case1") != null);
76         assertTrue(getCase(choice1, "case2") != null);
77         assertTrue(getCase(choice1, "inserted-case-leaf") != null);
78         assertTrue(getCase(choice1, "inserted-case-leaf-from-second-module") != null);
79
80         // - - - - some other different target nodes - - - - - -
81
82         final YContainer cont2 = getContainer(augmentTestModule, "cont2");
83         assertTrue(getList(cont2, "list1") != null);
84         assertTrue(getLeaf(getList(cont2, "list1"), "leaf11") != null);
85         assertTrue(getLeaf(getList(cont2, "list1"), "leaf15") != null);
86
87         assertTrue(getLeaf(getNotification(cont2, "notification1"), "leaf18") != null);
88
89         // - - - - - status - - - - - -
90
91         final YContainer cont3 = getContainer(augmentTestModule, "cont3");
92         assertTrue(cont3.getEffectiveStatus().equals(YStatus.CURRENT));
93
94         assertTrue(getContainer(cont3, "cont11") != null);
95         assertTrue(getContainer(cont3, "cont11").getStatus().isDeprecated());
96         assertTrue(getContainer(cont3, "cont11").getEffectiveStatus().equals(YStatus.DEPRECATED));
97         assertTrue(getContainer(cont3, "cont12") != null);
98         assertTrue(getContainer(cont3, "cont12").getStatus().isDeprecated());
99         assertTrue(getContainer(cont3, "cont12").getEffectiveStatus().equals(YStatus.DEPRECATED));
100         assertTrue(getContainer(cont3, "cont13") != null);
101         assertTrue(getContainer(cont3, "cont13").getStatus().isObsolete());
102         assertTrue(getContainer(cont3, "cont13").getEffectiveStatus().equals(YStatus.OBSOLETE));
103
104         assertTrue(getContainer(cont3, "cont15") != null);
105         assertTrue(getContainer(cont3, "cont15").getStatus().isCurrent());
106         assertTrue(getContainer(cont3, "cont15").getEffectiveStatus().equals(YStatus.CURRENT));
107         assertTrue(getContainer(cont3, "cont16") != null);
108         assertTrue(getContainer(cont3, "cont16").getStatus().isDeprecated());
109         assertTrue(getContainer(cont3, "cont16").getEffectiveStatus().equals(YStatus.DEPRECATED));
110         assertTrue(getContainer(cont3, "cont17") != null);
111         assertTrue(getContainer(cont3, "cont17").getStatus().isObsolete());
112         assertTrue(getContainer(cont3, "cont17").getEffectiveStatus().equals(YStatus.OBSOLETE));
113
114         // - - - - - strange path syntax - - - - - -
115
116         assertTrue(getContainer(cont1, "cont18") != null);
117         assertNoFindingsOnStatement(getAugment(augmentTestModule, "/this:cont1/"));
118
119         assertTrue(getContainer(cont1, "cont19") != null);
120         assertNoFindingsOnStatement(getAugment(augmentTestModule, "/:cont1"));
121
122         assertTrue(getContainer(cont1, "cont20") != null);
123         assertNoFindingsOnStatement(getAugment(augmentTestModule, "/cont1"));
124
125         assertTrue(getContainer(cont1, "cont21") == null);
126         assertStatementHasFindingOfType(getAugment(augmentTestModule, "//this:cont1"),
127                 ParserFindingType.P054_UNRESOLVABLE_PATH.toString());
128
129         assertTrue(getContainer(cont1, "cont22") == null);
130         assertStatementHasFindingOfType(getAugment(augmentTestModule, "//this:cont1/"),
131                 ParserFindingType.P054_UNRESOLVABLE_PATH.toString());
132
133         assertTrue(getContainer(cont1, "cont23") == null);
134         assertStatementHasFindingOfType(getAugment(augmentTestModule, "/"), ParserFindingType.P054_UNRESOLVABLE_PATH
135                 .toString());
136
137         assertTrue(getContainer(cont1, "cont24") == null);
138         assertStatementHasFindingOfType(getAugment(augmentTestModule, null),
139                 ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT.toString());
140
141         assertTrue(getContainer(cont1, "cont25") == null);
142         assertStatementHasFindingOfType(getAugment(augmentTestModule, null),
143                 ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT.toString());
144
145         // - - - - - - - - - issues with prefixes - - - - - -
146
147         assertHasFindingOfType(ParserFindingType.P034_UNRESOLVABLE_IMPORT.toString());
148         assertStatementHasFindingOfType(getAugment(augmentTestModule, "/nsm:some-cont"),
149                 ParserFindingType.P054_UNRESOLVABLE_PATH.toString());
150
151         assertStatementHasFindingOfType(getAugment(augmentTestModule, "/this:cont1/nsm:some-cont"),
152                 ParserFindingType.P054_UNRESOLVABLE_PATH.toString());
153
154         assertStatementHasFindingOfType(getAugment(augmentTestModule, "/this:cont1/unknown-prefix:some-cont"),
155                 ParserFindingType.P054_UNRESOLVABLE_PATH.toString());
156
157         assertStatementHasFindingOfType(getAugment(augmentTestModule, "/this:cont1/:some-cont"),
158                 ParserFindingType.P054_UNRESOLVABLE_PATH.toString());
159     }
160
161     @Test
162     public void testAugment3() {
163
164         parseRelativeImplementsYangModels(Arrays.asList("augment-test/augment-test-module3.yang"));
165
166         final YModule module = getModule("augment-test-module3");
167         assertTrue(module != null);
168
169         final YContainer cont1 = getContainer(module, "cont1");
170         assertTrue(cont1 != null);
171
172         assertTrue(getAction(cont1, "action1") != null);
173         assertTrue(getAction(cont1, "action1").getInput() != null);
174
175         assertTrue(getContainer(getAction(cont1, "action1").getInput(), "input-container") != null);
176         assertTrue(getContainer(getAction(cont1, "action1").getInput(), "input-container").getWhens().size() == 1);
177         assertTrue(getContainer(getAction(cont1, "action1").getInput(), "input-container").getWhens().get(0).getValue()
178                 .equals("leaf-at-root = true"));
179
180         final YList list2 = getList(cont1, "list2");
181         assertTrue(list2 != null);
182
183         assertTrue(getLeaf(list2, "leaf6") != null);
184         assertTrue(getLeaf(list2, "leaf7") != null);
185         assertTrue(getAction(list2, "action3") != null);
186
187         final YChoice choice8 = getChoice(cont1, "choice8");
188         assertTrue(choice8 != null);
189
190         assertTrue(getCase(choice8, "shorthand-container") != null);
191         assertTrue(getCase(choice8, "shorthand-container").getIfFeatures().size() == 1);
192         assertTrue(getContainer(getCase(choice8, "shorthand-container"), "shorthand-container") != null);
193
194         assertTrue(getCase(choice8, "case3") != null);
195         assertTrue(getCase(choice8, "case3").getIfFeatures().size() == 2);
196         assertTrue(getContainer(getCase(choice8, "case3"), "case-container") != null);
197         assertTrue(getContainer(getCase(choice8, "case3"), "second-case-container") != null);
198
199         // ------------------------ failures ------------------------
200
201         assertStatementHasFindingOfType(getAugment(module, "/leaf-at-root"),
202                 ParserFindingType.P151_TARGET_NODE_CANNOT_BE_AUGMENTED.toString());
203         assertStatementHasFindingOfType(getAugment(module, "/unknownprefix:somenode"),
204                 ParserFindingType.P033_UNRESOLVEABLE_PREFIX.toString());
205         assertStatementHasFindingOfType(getAugment(module, "/this:unknown-node"), ParserFindingType.P054_UNRESOLVABLE_PATH
206                 .toString());
207         assertStatementHasFindingOfType(getAugment(module, ""), ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT
208                 .toString());
209         assertStatementHasFindingOfType(getAugment(module, "cont1"), ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT
210                 .toString());
211         assertStatementHasFindingOfType(getAugment(module, null), ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT
212                 .toString());
213
214         // -------------------------- not-allowed statements -------------------------
215
216         assertStatementHasFindingOfType(getAugment(module, "/this:cont1/choice8/case1").getActions().get(0),
217                 ParserFindingType.P151_TARGET_NODE_CANNOT_BE_AUGMENTED.toString());
218         assertStatementHasFindingOfType(getAugment(module, "/this:cont1/choice8/case2").getNotifications().get(0),
219                 ParserFindingType.P151_TARGET_NODE_CANNOT_BE_AUGMENTED.toString());
220     }
221
222     @Test
223     public void testAugmentIntoSubModule() {
224
225         //      severityCalculator.suppressFinding(ParserFindingType.P152_AUGMENT_TARGET_NODE_IN_SAME_MODULE.toString());
226
227         parseRelativeImplementsYangModels(Arrays.asList("augment-test/augment-intosub-module.yang",
228                 "augment-test/augment-intosub-submodule.yang", "augment-test/augment-intosub-augmenting-module.yang"));
229
230         final YModule module = getModule("augment-intosub-module");
231         assertTrue(module != null);
232
233         final YContainer cont1 = getContainer(module, "cont1");
234         assertTrue(cont1 != null);
235
236         assertTrue(getLeaf(cont1, "leaf11") != null);
237         assertTrue(getLeaf(cont1, "leaf11").getEffectiveNamespace().equals("test:augment-intosub-augmenting-module"));
238
239         final YContainer cont2 = getContainer(module, "cont2");
240         assertTrue(cont2 != null);
241
242         assertTrue(getLeaf(cont2, "leaf12") != null);
243         assertTrue(getLeaf(cont2, "leaf12").getEffectiveNamespace().equals("test:augment-intosub-augmenting-module"));
244
245         printFindings();
246     }
247
248 }