Migrate YANG inputs for yang-model-util
[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.startsWith;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.jupiter.api.Assertions.assertEquals;
13 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
14 import static org.junit.jupiter.api.Assertions.assertThrows;
15
16 import java.util.NoSuchElementException;
17 import org.junit.jupiter.api.AfterAll;
18 import org.junit.jupiter.api.BeforeAll;
19 import org.junit.jupiter.api.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.type.LeafrefTypeDefinition;
24 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
25
26 class YT1127Test {
27     private static EffectiveModelContext context;
28
29     @BeforeAll
30     static void beforeClass() {
31         context = YangParserTestUtils.parseYang("""
32             module foo {
33               namespace foo;
34               prefix foo;
35
36               grouping grp {
37                 leaf leaf1 {
38                   type leafref {
39                     path "../../foo:foo_cont/foo:name";
40                   }
41                 }
42               }
43               container cont {
44                 leaf leaf2 {
45                    type leafref {
46                     path "../../../foo:foo_cont/foo:name";
47                   }
48                 }
49               }
50             }""");
51     }
52
53     @AfterAll
54     static void afterClass() {
55         context = null;
56     }
57
58     @Test
59     void testGroupingLeafRef() {
60         final var stack = SchemaInferenceStack.of(context);
61         stack.enterGrouping(QName.create("foo", "grp"));
62         final var leaf1 = assertInstanceOf(LeafSchemaNode.class, stack.enterSchemaTree(QName.create("foo", "leaf1")));
63         final var type = assertInstanceOf(LeafrefTypeDefinition.class, leaf1.getType());
64
65         final var ex = assertThrows(IllegalArgumentException.class, () -> stack.resolveLeafref(type));
66         assertThat(ex.getMessage(), startsWith("Illegal parent access in YangLocationPath"));
67         final var cause = assertInstanceOf(IllegalStateException.class, ex.getCause());
68         assertEquals("Unexpected current EmptyGroupingEffectiveStatement{argument=(foo)grp}", cause.getMessage());
69     }
70
71     @Test
72     void testContainerLeafRef() {
73         final var stack = SchemaInferenceStack.ofDataTreePath(context,
74             QName.create("foo", "cont"), QName.create("foo", "leaf2"));
75
76         final var leaf2 = assertInstanceOf(LeafSchemaNode.class, stack.currentStatement());
77         final var type = assertInstanceOf(LeafrefTypeDefinition.class, leaf2.getType());
78
79         final var ex = assertThrows(IllegalArgumentException.class, () -> stack.resolveLeafref(type));
80         assertThat(ex.getMessage(), startsWith("Illegal parent access in YangLocationPath"));
81         assertInstanceOf(NoSuchElementException.class, ex.getCause());
82     }
83 }