Add a test for catching path excapes 75/91775/7
authorillia.ihushev <illia.ihushev@pantheon.tech>
Thu, 30 Jul 2020 15:41:06 +0000 (18:41 +0300)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 16 Feb 2021 09:06:04 +0000 (10:06 +0100)
Leafref resolution has been reworked in a previous patch, now add
a test case to ensure we are covered.

JIRA: YANGTOOLS-1127
Change-Id: I46e76966f553cb031119593d0e405a739bd27e4e
Signed-off-by: illia.ihushev <illia.ihushev@pantheon.tech>
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaInferenceStack.java
yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/YT1127Test.java [new file with mode: 0644]
yang/yang-model-util/src/test/resources/yt1127.yang [new file with mode: 0644]

index 40f0203c63b832e65b905fae6b94327acecf80ad..b8f50c47528b814a8e4db0a468a77e1dd9696393 100644 (file)
@@ -524,7 +524,7 @@ public final class SchemaInferenceStack implements Mutable, EffectiveModelContex
                     verify(step instanceof AxisStep, "Unexpected parent step %s", step);
                     try {
                         current = exitToDataTree();
-                    } catch (IllegalStateException e) {
+                    } catch (IllegalStateException | NoSuchElementException e) {
                         throw new IllegalArgumentException("Illegal parent access in " + path, e);
                     }
                     break;
diff --git a/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/YT1127Test.java b/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/YT1127Test.java
new file mode 100644 (file)
index 0000000..b83a1c1
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.model.util;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.startsWith;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertThrows;
+
+import java.util.NoSuchElementException;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
+
+public class YT1127Test {
+    private static EffectiveModelContext context;
+
+    @BeforeClass
+    public static void beforeClass() {
+        context = YangParserTestUtils.parseYangResource("/yt1127.yang");
+    }
+
+    @AfterClass
+    public static void afterClass() {
+        context = null;
+    }
+
+    @Test
+    public void testGroupingLeafRef() {
+        final SchemaInferenceStack stack = SchemaInferenceStack.of(context);
+        stack.enterGrouping(QName.create("foo", "grp"));
+        final SchemaTreeEffectiveStatement<?> leaf1 = stack.enterSchemaTree(QName.create("foo", "leaf1"));
+        assertThat(leaf1, instanceOf(LeafSchemaNode.class));
+        final TypeDefinition<?> type = ((LeafSchemaNode) leaf1).getType();
+        assertThat(type, instanceOf(LeafrefTypeDefinition.class));
+
+        final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
+            () -> stack.resolveLeafref((LeafrefTypeDefinition) type));
+        assertThat(ex.getMessage(), startsWith("Illegal parent access in YangLocationPath"));
+        final Throwable cause = ex.getCause();
+        assertThat(cause, instanceOf(IllegalStateException.class));
+        assertThat(cause.getMessage(), startsWith("Unexpected current Grouping"));
+    }
+
+    @Test
+    public void testContainerLeafRef() {
+        final SchemaInferenceStack stack = SchemaInferenceStack.ofDataTreePath(context,
+            QName.create("foo", "cont"), QName.create("foo", "leaf2"));
+
+        final EffectiveStatement<?, ?> leaf2 = stack.currentStatement();
+        assertThat(leaf2, instanceOf(LeafSchemaNode.class));
+        final TypeDefinition<?> type = ((LeafSchemaNode) leaf2).getType();
+        assertThat(type, instanceOf(LeafrefTypeDefinition.class));
+
+        final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
+            () -> stack.resolveLeafref((LeafrefTypeDefinition) type));
+        assertThat(ex.getMessage(), startsWith("Illegal parent access in YangLocationPath"));
+        assertThat(ex.getCause(), instanceOf(NoSuchElementException.class));
+    }
+}
diff --git a/yang/yang-model-util/src/test/resources/yt1127.yang b/yang/yang-model-util/src/test/resources/yt1127.yang
new file mode 100644 (file)
index 0000000..81ba180
--- /dev/null
@@ -0,0 +1,19 @@
+module foo {
+  namespace foo;
+  prefix foo;
+
+  grouping grp {
+    leaf leaf1 {
+      type leafref {
+        path "../../foo:foo_cont/foo:name";
+      }
+    }
+  }
+  container cont {
+    leaf leaf2 {
+       type leafref {
+        path "../../../foo:foo_cont/foo:name";
+      }
+    }
+  }
+}