From: Robert Varga Date: Mon, 30 Aug 2021 10:30:16 +0000 (+0200) Subject: Improve SchemaInferenceStack test coverage X-Git-Tag: v7.0.8~9 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=75c23294be3cc26e104dbcfbbfb5ca361f4b5944;p=yangtools.git Improve SchemaInferenceStack test coverage Add a few unit tests to improve coverage. Also fix error reporting and javadoc. Change-Id: I8338829a89e3fe07e944cf4eb3c576a48f87d3fc Signed-off-by: Robert Varga --- diff --git a/model/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaInferenceStack.java b/model/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaInferenceStack.java index 91610e6788..2281db9bb4 100644 --- a/model/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaInferenceStack.java +++ b/model/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaInferenceStack.java @@ -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; } diff --git a/model/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/SchemaInferenceStackTest.java b/model/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/SchemaInferenceStackTest.java index 65f2fc52c7..48422d2372 100644 --- a/model/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/SchemaInferenceStackTest.java +++ b/model/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/SchemaInferenceStackTest.java @@ -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)) { diff --git a/model/yang-model-util/src/test/resources/schema-context-util/my-module.yang b/model/yang-model-util/src/test/resources/schema-context-util/my-module.yang index 1bb1d21c20..f4b1d99f41 100644 --- a/model/yang-model-util/src/test/resources/schema-context-util/my-module.yang +++ b/model/yang-model-util/src/test/resources/schema-context-util/my-module.yang @@ -49,6 +49,9 @@ module my-module { type string; } } + typedef my-typedef-in-container { + type string; + } } rpc my-rpc {