Populate model/ hierarchy
[yangtools.git] / model / yang-model-util / src / test / java / org / opendaylight / yangtools / yang / model / util / YT1127Test.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.model.util;
9
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;
14
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;
27
28 public class YT1127Test {
29     private static EffectiveModelContext context;
30
31     @BeforeClass
32     public static void beforeClass() {
33         context = YangParserTestUtils.parseYangResource("/yt1127.yang");
34     }
35
36     @AfterClass
37     public static void afterClass() {
38         context = null;
39     }
40
41     @Test
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));
49
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"));
56     }
57
58     @Test
59     public void testContainerLeafRef() {
60         final SchemaInferenceStack stack = SchemaInferenceStack.ofDataTreePath(context,
61             QName.create("foo", "cont"), QName.create("foo", "leaf2"));
62
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));
67
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));
72     }
73 }