Populate model/ hierarchy
[yangtools.git] / model / yang-model-util / src / test / java / org / opendaylight / yangtools / yang / model / util / YT1233Test.java
1 /*
2  * Copyright (c) 2021 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.Assert.assertSame;
13 import static org.junit.Assert.assertThrows;
14 import static org.junit.Assert.assertTrue;
15
16 import java.util.NoSuchElementException;
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.stmt.DataTreeEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.GroupingEffectiveStatement;
23 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
24
25 public class YT1233Test {
26     private static EffectiveModelContext context;
27
28     private final SchemaInferenceStack stack = SchemaInferenceStack.of(context);
29
30     @BeforeClass
31     public static void beforeClass() {
32         context = YangParserTestUtils.parseYangResource("/yt1233.yang");
33     }
34
35     @Test
36     public void testExitToDataTree() {
37         final DataTreeEffectiveStatement<?> foo = stack.enterDataTree(QName.create("foo", "foo"));
38         assertSame(foo, stack.exitToDataTree());
39         assertTrue(stack.isEmpty());
40         assertSame(foo, stack.enterDataTree(foo.argument()));
41     }
42
43     @Test
44     public void testExitToGrouping() {
45         final GroupingEffectiveStatement baz = stack.enterGrouping(QName.create("foo", "baz"));
46         final DataTreeEffectiveStatement<?> xyzzy = stack.enterDataTree(QName.create("foo", "xyzzy"));
47         assertSame(xyzzy, stack.exitToDataTree());
48         assertSame(baz, stack.currentStatement());
49         assertSame(xyzzy, stack.enterDataTree(xyzzy.argument()));
50     }
51
52     @Test
53     public void testEmptyExitToDataTree() {
54         assertThrows(NoSuchElementException.class, stack::exitToDataTree);
55     }
56
57     @Test
58     public void testSchemaExitToDataTree() {
59         stack.enterSchemaTree(QName.create("foo", "bar"));
60         final IllegalStateException ex = assertThrows(IllegalStateException.class, stack::exitToDataTree);
61         assertThat(ex.getMessage(), startsWith("Unexpected current "));
62     }
63 }