2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.model.util;
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.CoreMatchers.startsWith;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertThrows;
16 import java.util.NoSuchElementException;
17 import org.junit.AfterClass;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
22 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
27 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
29 public class YT1127Test {
30 private static EffectiveModelContext context;
33 public static void beforeClass() {
34 context = YangParserTestUtils.parseYangResource("/yt1127.yang");
38 public static void afterClass() {
43 public void testGroupingLeafRef() {
44 final SchemaInferenceStack stack = SchemaInferenceStack.of(context);
45 stack.enterGrouping(QName.create("foo", "grp"));
46 final SchemaTreeEffectiveStatement<?> leaf1 = stack.enterSchemaTree(QName.create("foo", "leaf1"));
47 assertThat(leaf1, instanceOf(LeafSchemaNode.class));
48 final TypeDefinition<?> type = ((LeafSchemaNode) leaf1).getType();
49 assertThat(type, instanceOf(LeafrefTypeDefinition.class));
51 final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
52 () -> stack.resolveLeafref((LeafrefTypeDefinition) type));
53 assertThat(ex.getMessage(), startsWith("Illegal parent access in YangLocationPath"));
54 final Throwable cause = ex.getCause();
55 assertThat(cause, instanceOf(IllegalStateException.class));
56 assertEquals("Unexpected current EmptyGroupingEffectiveStatement{argument=(foo)grp}", cause.getMessage());
60 public void testContainerLeafRef() {
61 final SchemaInferenceStack stack = SchemaInferenceStack.ofDataTreePath(context,
62 QName.create("foo", "cont"), QName.create("foo", "leaf2"));
64 final EffectiveStatement<?, ?> leaf2 = stack.currentStatement();
65 assertThat(leaf2, instanceOf(LeafSchemaNode.class));
66 final TypeDefinition<?> type = ((LeafSchemaNode) leaf2).getType();
67 assertThat(type, instanceOf(LeafrefTypeDefinition.class));
69 final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
70 () -> stack.resolveLeafref((LeafrefTypeDefinition) type));
71 assertThat(ex.getMessage(), startsWith("Illegal parent access in YangLocationPath"));
72 assertThat(ex.getCause(), instanceOf(NoSuchElementException.class));