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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
21 package org.oran.smo.yangtools.parser.model.statements.yang.test;
23 import static org.junit.Assert.assertTrue;
25 import java.util.Arrays;
27 import org.junit.Test;
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;
39 public class AugmentTest extends YangTestCommon {
42 public void testAugmentIntoEmptyActionAndRpc() {
44 severityCalculator.suppressFinding(ParserFindingType.P152_AUGMENT_TARGET_NODE_IN_SAME_MODULE.toString());
46 parseRelativeImplementsYangModels(Arrays.asList("augment-test/augment-test-module.yang",
47 "augment-test/augment-test-module2.yang"));
49 final YModule augmentTestModule = getModule("augment-test-module");
50 assertTrue(augmentTestModule != null);
52 assertTrue(augmentTestModule.getRpcs().size() == 1);
53 assertTrue(augmentTestModule.getRpcs().get(0).getInput() != null);
54 assertTrue(augmentTestModule.getRpcs().get(0).getOutput() != null);
56 assertTrue(augmentTestModule.getRpcs().get(0).getInput().getLeafs().size() == 1);
57 assertTrue(augmentTestModule.getRpcs().get(0).getOutput().getLeafs().size() == 1);
59 final YContainer cont1 = getContainer(augmentTestModule, "cont1");
61 assertTrue(cont1.getActions().size() == 1);
62 assertTrue(cont1.getActions().get(0).getInput() != null);
63 assertTrue(cont1.getActions().get(0).getOutput() != null);
65 assertTrue(cont1.getActions().get(0).getInput().getLeafs().size() == 1);
66 assertTrue(cont1.getActions().get(0).getOutput().getLeafs().size() == 1);
68 // Tests added to handle augments into a choice
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");
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);
80 // - - - - some other different target nodes - - - - - -
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);
87 assertTrue(getLeaf(getNotification(cont2, "notification1"), "leaf18") != null);
89 // - - - - - status - - - - - -
91 final YContainer cont3 = getContainer(augmentTestModule, "cont3");
92 assertTrue(cont3.getEffectiveStatus().equals(YStatus.CURRENT));
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));
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));
114 // - - - - - strange path syntax - - - - - -
116 assertTrue(getContainer(cont1, "cont18") != null);
117 assertNoFindingsOnStatement(getAugment(augmentTestModule, "/this:cont1/"));
119 assertTrue(getContainer(cont1, "cont19") != null);
120 assertNoFindingsOnStatement(getAugment(augmentTestModule, "/:cont1"));
122 assertTrue(getContainer(cont1, "cont20") != null);
123 assertNoFindingsOnStatement(getAugment(augmentTestModule, "/cont1"));
125 assertTrue(getContainer(cont1, "cont21") == null);
126 assertStatementHasFindingOfType(getAugment(augmentTestModule, "//this:cont1"),
127 ParserFindingType.P054_UNRESOLVABLE_PATH.toString());
129 assertTrue(getContainer(cont1, "cont22") == null);
130 assertStatementHasFindingOfType(getAugment(augmentTestModule, "//this:cont1/"),
131 ParserFindingType.P054_UNRESOLVABLE_PATH.toString());
133 assertTrue(getContainer(cont1, "cont23") == null);
134 assertStatementHasFindingOfType(getAugment(augmentTestModule, "/"), ParserFindingType.P054_UNRESOLVABLE_PATH
137 assertTrue(getContainer(cont1, "cont24") == null);
138 assertStatementHasFindingOfType(getAugment(augmentTestModule, null),
139 ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT.toString());
141 assertTrue(getContainer(cont1, "cont25") == null);
142 assertStatementHasFindingOfType(getAugment(augmentTestModule, null),
143 ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT.toString());
145 // - - - - - - - - - issues with prefixes - - - - - -
147 assertHasFindingOfType(ParserFindingType.P034_UNRESOLVABLE_IMPORT.toString());
148 assertStatementHasFindingOfType(getAugment(augmentTestModule, "/nsm:some-cont"),
149 ParserFindingType.P054_UNRESOLVABLE_PATH.toString());
151 assertStatementHasFindingOfType(getAugment(augmentTestModule, "/this:cont1/nsm:some-cont"),
152 ParserFindingType.P054_UNRESOLVABLE_PATH.toString());
154 assertStatementHasFindingOfType(getAugment(augmentTestModule, "/this:cont1/unknown-prefix:some-cont"),
155 ParserFindingType.P054_UNRESOLVABLE_PATH.toString());
157 assertStatementHasFindingOfType(getAugment(augmentTestModule, "/this:cont1/:some-cont"),
158 ParserFindingType.P054_UNRESOLVABLE_PATH.toString());
162 public void testAugment3() {
164 parseRelativeImplementsYangModels(Arrays.asList("augment-test/augment-test-module3.yang"));
166 final YModule module = getModule("augment-test-module3");
167 assertTrue(module != null);
169 final YContainer cont1 = getContainer(module, "cont1");
170 assertTrue(cont1 != null);
172 assertTrue(getAction(cont1, "action1") != null);
173 assertTrue(getAction(cont1, "action1").getInput() != null);
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"));
180 final YList list2 = getList(cont1, "list2");
181 assertTrue(list2 != null);
183 assertTrue(getLeaf(list2, "leaf6") != null);
184 assertTrue(getLeaf(list2, "leaf7") != null);
185 assertTrue(getAction(list2, "action3") != null);
187 final YChoice choice8 = getChoice(cont1, "choice8");
188 assertTrue(choice8 != null);
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);
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);
199 // ------------------------ failures ------------------------
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
207 assertStatementHasFindingOfType(getAugment(module, ""), ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT
209 assertStatementHasFindingOfType(getAugment(module, "cont1"), ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT
211 assertStatementHasFindingOfType(getAugment(module, null), ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT
214 // -------------------------- not-allowed statements -------------------------
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());
223 public void testAugmentIntoSubModule() {
225 // severityCalculator.suppressFinding(ParserFindingType.P152_AUGMENT_TARGET_NODE_IN_SAME_MODULE.toString());
227 parseRelativeImplementsYangModels(Arrays.asList("augment-test/augment-intosub-module.yang",
228 "augment-test/augment-intosub-submodule.yang", "augment-test/augment-intosub-augmenting-module.yang"));
230 final YModule module = getModule("augment-intosub-module");
231 assertTrue(module != null);
233 final YContainer cont1 = getContainer(module, "cont1");
234 assertTrue(cont1 != null);
236 assertTrue(getLeaf(cont1, "leaf11") != null);
237 assertTrue(getLeaf(cont1, "leaf11").getEffectiveNamespace().equals("test:augment-intosub-augmenting-module"));
239 final YContainer cont2 = getContainer(module, "cont2");
240 assertTrue(cont2 != null);
242 assertTrue(getLeaf(cont2, "leaf12") != null);
243 assertTrue(getLeaf(cont2, "leaf12").getEffectiveNamespace().equals("test:augment-intosub-augmenting-module"));