From 2c2761776c90d36b375b7fee4957bfa4f900ae85 Mon Sep 17 00:00:00 2001 From: "illia.ihushev" Date: Thu, 30 Jul 2020 18:41:06 +0300 Subject: [PATCH] Add a test for catching path excapes 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 Signed-off-by: Robert Varga --- .../yang/model/util/SchemaInferenceStack.java | 2 +- .../yangtools/yang/model/util/YT1127Test.java | 73 +++++++++++++++++++ .../src/test/resources/yt1127.yang | 19 +++++ 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/YT1127Test.java create mode 100644 yang/yang-model-util/src/test/resources/yt1127.yang diff --git a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaInferenceStack.java b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaInferenceStack.java index 40f0203c63..b8f50c4752 100644 --- a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaInferenceStack.java +++ b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaInferenceStack.java @@ -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 index 0000000000..b83a1c13f1 --- /dev/null +++ b/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/YT1127Test.java @@ -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 index 0000000000..81ba1805aa --- /dev/null +++ b/yang/yang-model-util/src/test/resources/yt1127.yang @@ -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"; + } + } + } +} -- 2.36.6