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.assertThrows;
15 import java.util.NoSuchElementException;
16 import org.junit.AfterClass;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
21 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
26 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
28 public class YT1127Test {
29 private static EffectiveModelContext context;
32 public static void beforeClass() {
33 context = YangParserTestUtils.parseYangResource("/yt1127.yang");
37 public static void afterClass() {
42 public void testGroupingLeafRef() {
43 final SchemaInferenceStack stack = SchemaInferenceStack.of(context);
44 stack.enterGrouping(QName.create("foo", "grp"));
45 final SchemaTreeEffectiveStatement<?> leaf1 = stack.enterSchemaTree(QName.create("foo", "leaf1"));
46 assertThat(leaf1, instanceOf(LeafSchemaNode.class));
47 final TypeDefinition<?> type = ((LeafSchemaNode) leaf1).getType();
48 assertThat(type, instanceOf(LeafrefTypeDefinition.class));
50 final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
51 () -> stack.resolveLeafref((LeafrefTypeDefinition) type));
52 assertThat(ex.getMessage(), startsWith("Illegal parent access in YangLocationPath"));
53 final Throwable cause = ex.getCause();
54 assertThat(cause, instanceOf(IllegalStateException.class));
55 assertThat(cause.getMessage(), startsWith("Unexpected current Grouping"));
59 public void testContainerLeafRef() {
60 final SchemaInferenceStack stack = SchemaInferenceStack.ofDataTreePath(context,
61 QName.create("foo", "cont"), QName.create("foo", "leaf2"));
63 final EffectiveStatement<?, ?> leaf2 = stack.currentStatement();
64 assertThat(leaf2, instanceOf(LeafSchemaNode.class));
65 final TypeDefinition<?> type = ((LeafSchemaNode) leaf2).getType();
66 assertThat(type, instanceOf(LeafrefTypeDefinition.class));
68 final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
69 () -> stack.resolveLeafref((LeafrefTypeDefinition) type));
70 assertThat(ex.getMessage(), startsWith("Illegal parent access in YangLocationPath"));
71 assertThat(ex.getCause(), instanceOf(NoSuchElementException.class));