Improve SchemaInferenceStack test coverage 57/97357/2
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 30 Aug 2021 10:30:16 +0000 (12:30 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 30 Aug 2021 10:31:31 +0000 (12:31 +0200)
Add a few unit tests to improve coverage. Also fix error reporting and
javadoc.

Change-Id: I8338829a89e3fe07e944cf4eb3c576a48f87d3fc
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
model/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaInferenceStack.java
model/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/SchemaInferenceStackTest.java
model/yang-model-util/src/test/resources/schema-context-util/my-module.yang

index 91610e67880b98740ba212baeae3229fb408c143..2281db9bb48ceeca1aedd2825b596940fca502e7 100644 (file)
@@ -476,7 +476,7 @@ public final class SchemaInferenceStack implements Mutable, EffectiveModelContex
      * Lookup a {@code typedef} by its node identifier and push it to the stack.
      *
      * @param nodeIdentifier Node identifier of the typedef to enter
-     * @return Resolved choice
+     * @return Resolved typedef
      * @throws NullPointerException if {@code nodeIdentifier} is null
      * @throws IllegalArgumentException if the corresponding typedef cannot be found
      */
@@ -791,7 +791,7 @@ public final class SchemaInferenceStack implements Mutable, EffectiveModelContex
         final TypedefEffectiveStatement ret = parent.streamEffectiveSubstatements(TypedefEffectiveStatement.class)
             .filter(stmt -> nodeIdentifier.equals(stmt.argument()))
             .findFirst()
-            .orElseThrow(() -> new IllegalArgumentException("Grouping " + nodeIdentifier + " not present"));
+            .orElseThrow(() -> new IllegalArgumentException("Typedef " + nodeIdentifier + " not present"));
         deque.push(ret);
         return ret;
     }
index 65f2fc52c713d216f62aa870349a951479cd9001..48422d237293969bce185eb85e973169c28ae91a 100644 (file)
@@ -9,7 +9,9 @@
 package org.opendaylight.yangtools.yang.model.util;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThrows;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 
@@ -74,6 +76,43 @@ public class SchemaInferenceStackTest {
         assertEquals(grouping.getDataChildByName(myLeafInGrouping2), stack.resolvePathExpression(expr));
     }
 
+    @Test
+    public void enterGroupingNegativeTest() {
+        final SchemaInferenceStack stack = SchemaInferenceStack.of(context);
+        assertNotExistentGrouping(stack);
+        stack.enterDataTree(QName.create(myModule.getQNameModule(), "my-container"));
+        assertNotExistentGrouping(stack);
+    }
+
+    @Test
+    public void enterNestedTypedefTest() {
+        final SchemaInferenceStack stack = SchemaInferenceStack.of(context);
+        stack.enterDataTree(QName.create(myModule.getQNameModule(), "my-container"));
+        assertNotNull(stack.enterTypedef(QName.create(myModule.getQNameModule(), "my-typedef-in-container")));
+    }
+
+    @Test
+    public void enterTypedefNegativeTest() {
+        final SchemaInferenceStack stack = SchemaInferenceStack.of(context);
+        assertNotExistentTypedef(stack);
+        stack.enterDataTree(QName.create(myModule.getQNameModule(), "my-container"));
+        assertNotExistentTypedef(stack);
+    }
+
+    private static void assertNotExistentGrouping(final SchemaInferenceStack stack) {
+        final QName nonExistent = QName.create(myModule.getQNameModule(), "non-existent");
+        final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
+            () -> stack.enterGrouping(nonExistent));
+        assertEquals("Grouping (uri:my-module?revision=2014-10-07)non-existent not present", ex.getMessage());
+    }
+
+    private static void assertNotExistentTypedef(final SchemaInferenceStack stack) {
+        final QName nonExistent = QName.create(myModule.getQNameModule(), "non-existent");
+        final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
+            () -> stack.enterTypedef(nonExistent));
+        assertEquals("Typedef (uri:my-module?revision=2014-10-07)non-existent not present", ex.getMessage());
+    }
+
     private static GroupingDefinition getGroupingByName(final DataNodeContainer dataNodeContainer, final String name) {
         for (final GroupingDefinition grouping : dataNodeContainer.getGroupings()) {
             if (grouping.getQName().getLocalName().equals(name)) {
index 1bb1d21c2068b388526666c1f59b9a5d7f5b4e61..f4b1d99f4178b765b75665ec47df4d854442b1d7 100644 (file)
@@ -49,6 +49,9 @@ module my-module {
                 type string;
             }
         }
+        typedef my-typedef-in-container {
+            type string;
+        }
     }
 
     rpc my-rpc {