Add SchemaInferenceStack.effectiveStatus()
[yangtools.git] / model / yang-model-util / src / test / java / org / opendaylight / yangtools / yang / model / util / SchemaInferenceStackTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  * Copyright (c) 2021 PANTHEON.tech, s.r.o.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9 package org.opendaylight.yangtools.yang.model.util;
10
11 import static org.junit.jupiter.api.Assertions.assertEquals;
12 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
13 import static org.junit.jupiter.api.Assertions.assertNotNull;
14 import static org.junit.jupiter.api.Assertions.assertSame;
15 import static org.junit.jupiter.api.Assertions.assertThrows;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18
19 import org.junit.jupiter.api.BeforeAll;
20 import org.junit.jupiter.api.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.Revision;
23 import org.opendaylight.yangtools.yang.common.XMLNamespace;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
26 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
27 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.PathExpression;
30 import org.opendaylight.yangtools.yang.model.api.PathExpression.LocationPathSteps;
31 import org.opendaylight.yangtools.yang.model.api.Status;
32 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
33 import org.opendaylight.yangtools.yang.xpath.api.YangLocationPath;
34 import org.opendaylight.yangtools.yang.xpath.api.YangXPathAxis;
35
36 class SchemaInferenceStackTest {
37     private static EffectiveModelContext context;
38     private static Module myModule;
39
40     @BeforeAll
41     static void beforeClass() {
42         context = YangParserTestUtils.parseYangResourceDirectory("/schema-context-util");
43         myModule = context.findModule(XMLNamespace.of("uri:my-module"), Revision.of("2014-10-07")).orElseThrow();
44     }
45
46     @Test
47     void findDataSchemaNodeTest() {
48         final var importedModule = context.findModule(XMLNamespace.of("uri:imported-module"),
49                 Revision.of("2014-10-07")).orElseThrow();
50
51         final QName myImportedContainer = QName.create(importedModule.getQNameModule(), "my-imported-container");
52         final QName myImportedLeaf = QName.create(importedModule.getQNameModule(), "my-imported-leaf");
53
54         final var testNode = assertInstanceOf(ContainerSchemaNode.class,
55             importedModule.getDataChildByName(myImportedContainer)).getDataChildByName(myImportedLeaf);
56
57         final var expr = mock(PathExpression.class);
58         doReturn(true).when(expr).isAbsolute();
59         doReturn(new LocationPathSteps(YangLocationPath.absolute(
60                 YangXPathAxis.CHILD.asStep(myImportedContainer), YangXPathAxis.CHILD.asStep(myImportedLeaf))))
61                 .when(expr).getSteps();
62
63         assertEquals(testNode, SchemaInferenceStack.of(context).resolvePathExpression(expr));
64     }
65
66     @Test
67     void findDataSchemaNodeTest2() {
68         final QName myLeafInGrouping2 = QName.create(myModule.getQNameModule(), "my-leaf-in-gouping2");
69         final var expr = mock(PathExpression.class);
70         doReturn(true).when(expr).isAbsolute();
71         doReturn(new LocationPathSteps(YangLocationPath.relative(YangXPathAxis.CHILD.asStep(myLeafInGrouping2))))
72                 .when(expr).getSteps();
73
74         final var grouping = getGroupingByName(myModule, "my-grouping");
75         final var stack = SchemaInferenceStack.of(context);
76         assertSame(grouping, stack.enterGrouping(grouping.getQName()));
77         assertEquals(grouping.getDataChildByName(myLeafInGrouping2), stack.resolvePathExpression(expr));
78     }
79
80     @Test
81     void enterGroupingNegativeTest() {
82         final var stack = SchemaInferenceStack.of(context);
83         assertNotExistentGrouping(stack, "module (uri:my-module?revision=2014-10-07)my-module");
84         stack.enterDataTree(QName.create(myModule.getQNameModule(), "my-container"));
85         assertNotExistentGrouping(stack, "schema parent (uri:my-module?revision=2014-10-07)my-container");
86     }
87
88     @Test
89     void enterNestedTypedefTest() {
90         final var stack = SchemaInferenceStack.of(context);
91         stack.enterDataTree(QName.create(myModule.getQNameModule(), "my-container"));
92         assertNotNull(stack.enterTypedef(QName.create(myModule.getQNameModule(), "my-typedef-in-container")));
93     }
94
95     @Test
96     void enterTypedefNegativeTest() {
97         final var stack = SchemaInferenceStack.of(context);
98         assertNotExistentTypedef(stack, "module (uri:my-module?revision=2014-10-07)my-module");
99         stack.enterDataTree(QName.create(myModule.getQNameModule(), "my-container"));
100         assertNotExistentTypedef(stack, "schema parent (uri:my-module?revision=2014-10-07)my-container");
101     }
102
103     @Test
104     void rootIsCurrent() {
105         final var stack = SchemaInferenceStack.of(context);
106         assertEquals(Status.CURRENT, stack.effectiveStatus());
107     }
108
109     @Test
110     void myGroupingIsCurrent() {
111         final var stack = SchemaInferenceStack.of(context);
112         stack.enterGrouping(QName.create(myModule.getQNameModule(), "my-grouping"));
113         assertEquals(Status.CURRENT, stack.effectiveStatus());
114     }
115
116     @Test
117     void myLeafInContainerIsDeprecated() {
118         final var stack = SchemaInferenceStack.of(context);
119         stack.enterDataTree(QName.create(myModule.getQNameModule(), "my-container"));
120         stack.enterDataTree(QName.create(myModule.getQNameModule(), "my-leaf-in-container"));
121         assertEquals(Status.DEPRECATED, stack.effectiveStatus());
122     }
123
124     @Test
125     void twoInGroupingIsObsolete() {
126         final var stack = SchemaInferenceStack.of(context);
127         stack.enterGrouping(QName.create(myModule.getQNameModule(), "my-name"));
128         stack.enterDataTree(QName.create(myModule.getQNameModule(), "two"));
129         assertEquals(Status.OBSOLETE, stack.effectiveStatus());
130     }
131
132     @Test
133     void twoInMyNameInputIsObsolete() {
134         final var stack = SchemaInferenceStack.of(context);
135         stack.enterSchemaTree(QName.create(myModule.getQNameModule(), "my-name"));
136         stack.enterSchemaTree(QName.create(myModule.getQNameModule(), "input"));
137         stack.enterSchemaTree(QName.create(myModule.getQNameModule(), "my-choice"));
138         stack.enterSchemaTree(QName.create(myModule.getQNameModule(), "case-two"));
139         stack.enterSchemaTree(QName.create(myModule.getQNameModule(), "two"));
140         assertEquals(Status.OBSOLETE, stack.effectiveStatus());
141     }
142
143     private static void assertNotExistentGrouping(final SchemaInferenceStack stack, final String parentDesc) {
144         final QName nonExistent = QName.create(myModule.getQNameModule(), "non-existent");
145         assertEquals("Grouping (uri:my-module?revision=2014-10-07)non-existent not present in " + parentDesc,
146             assertThrows(IllegalArgumentException.class, () -> stack.enterGrouping(nonExistent)).getMessage());
147     }
148
149     private static void assertNotExistentTypedef(final SchemaInferenceStack stack, final String parentDesc) {
150         final QName nonExistent = QName.create(myModule.getQNameModule(), "non-existent");
151         assertEquals("Typedef (uri:my-module?revision=2014-10-07)non-existent not present in " + parentDesc,
152             assertThrows(IllegalArgumentException.class, () -> stack.enterTypedef(nonExistent)).getMessage());
153     }
154
155     private static GroupingDefinition getGroupingByName(final DataNodeContainer dataNodeContainer, final String name) {
156         for (var grouping : dataNodeContainer.getGroupings()) {
157             if (grouping.getQName().getLocalName().equals(name)) {
158                 return grouping;
159             }
160         }
161         return null;
162     }
163 }