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.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;
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;
27 private static EffectiveModelContext context;
30 static void beforeClass() {
31 context = YangParserTestUtils.parseYang("""
39 path "../../foo:foo_cont/foo:name";
46 path "../../../foo:foo_cont/foo:name";
54 static void afterClass() {
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());
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());
72 void testContainerLeafRef() {
73 final var stack = SchemaInferenceStack.ofDataTreePath(context,
74 QName.create("foo", "cont"), QName.create("foo", "leaf2"));
76 final var leaf2 = assertInstanceOf(LeafSchemaNode.class, stack.currentStatement());
77 final var type = assertInstanceOf(LeafrefTypeDefinition.class, leaf2.getType());
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());