Improve SchemaInferenceStack error reporting
[yangtools.git] / model / yang-model-util / src / test / java / org / opendaylight / yangtools / yang / model / util / YT1231Test.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.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertSame;
14 import static org.junit.Assert.assertThrows;
15
16 import org.junit.BeforeClass;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
20 import org.opendaylight.yangtools.yang.model.api.stmt.CaseEffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.ContainerEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
24 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
25
26 public class YT1231Test {
27     private static final QName FOO = QName.create("foo", "foo");
28     private static final QName BAR = QName.create("foo", "bar");
29     private static final QName BAZ = QName.create("foo", "baz");
30     private static final QName XYZZY = QName.create("foo", "xyzzy");
31
32     private static EffectiveModelContext CONTEXT;
33
34     private final SchemaInferenceStack stack = SchemaInferenceStack.of(CONTEXT);
35
36     @BeforeClass
37     public static void beforeClass() {
38         CONTEXT = YangParserTestUtils.parseYangResource("/yt1231.yang");
39     }
40
41     @Test
42     public void testEnterDataTree() {
43         // Trivial
44         assertThat(stack.enterDataTree(FOO), instanceOf(ContainerEffectiveStatement.class));
45         assertSame(CONTEXT.getModuleStatement(FOO.getModule()), stack.currentModule());
46         assertEquals(Absolute.of(FOO), stack.toSchemaNodeIdentifier());
47         assertThat(stack.enterDataTree(FOO), instanceOf(ContainerEffectiveStatement.class));
48         assertEquals(Absolute.of(FOO, FOO), stack.toSchemaNodeIdentifier());
49         stack.exit();
50
51         // Has to cross four layers of choice/case
52         assertThat(stack.enterDataTree(XYZZY), instanceOf(ContainerEffectiveStatement.class));
53         assertEquals(Absolute.of(FOO, BAR, BAR, BAZ, BAZ, XYZZY), stack.toSchemaNodeIdentifier());
54
55         stack.exit();
56         assertThat(stack.enterSchemaTree(BAR), instanceOf(ChoiceEffectiveStatement.class));
57         assertThat(stack.enterSchemaTree(BAR), instanceOf(CaseEffectiveStatement.class));
58         assertEquals(Absolute.of(FOO, BAR, BAR), stack.toSchemaNodeIdentifier());
59     }
60
61     @Test
62     public void testEnterChoice() {
63         // Simple
64         assertThat(stack.enterDataTree(FOO), instanceOf(ContainerEffectiveStatement.class));
65         assertEquals(Absolute.of(FOO), stack.toSchemaNodeIdentifier());
66         assertThat(stack.enterChoice(BAR), instanceOf(ChoiceEffectiveStatement.class));
67         assertEquals(Absolute.of(FOO, BAR), stack.toSchemaNodeIdentifier());
68
69         // Has to cross choice -> case -> choice
70         assertThat(stack.enterChoice(BAZ), instanceOf(ChoiceEffectiveStatement.class));
71         assertEquals(Absolute.of(FOO, BAR, BAR, BAZ), stack.toSchemaNodeIdentifier());
72
73         // Now the same with just case -> choice
74         stack.exit();
75         assertThat(stack.enterSchemaTree(BAR), instanceOf(CaseEffectiveStatement.class));
76         assertThat(stack.enterChoice(BAZ), instanceOf(ChoiceEffectiveStatement.class));
77         assertEquals(Absolute.of(FOO, BAR, BAR, BAZ), stack.toSchemaNodeIdentifier());
78     }
79
80     @Test
81     public void testEnterChoiceToRootContainer() {
82         assertEquals("Choice (foo)foo not present", assertEnterChoiceThrows(FOO));
83     }
84
85     @Test
86     public void testEnterChoiceToNestedContainer() {
87         assertThat(stack.enterDataTree(FOO), instanceOf(ContainerEffectiveStatement.class));
88         assertEquals(Absolute.of(FOO), stack.toSchemaNodeIdentifier());
89         assertEquals("Choice (foo)foo not present in schema parent (foo)foo", assertEnterChoiceThrows(FOO));
90     }
91
92     @Test
93     public void testEnterChoiceNonExistent() {
94         assertThat(stack.enterDataTree(FOO), instanceOf(ContainerEffectiveStatement.class));
95         assertEquals(Absolute.of(FOO), stack.toSchemaNodeIdentifier());
96         assertThat(stack.enterSchemaTree(BAR), instanceOf(ChoiceEffectiveStatement.class));
97
98         assertEquals("Choice (foo)foo not present in schema parent (foo)bar", assertEnterChoiceThrows(FOO));
99         assertEquals("Choice (foo)bar not present in schema parent (foo)bar", assertEnterChoiceThrows(BAR));
100     }
101
102     private String assertEnterChoiceThrows(final QName nodeIdentifier) {
103         return assertThrows(IllegalArgumentException.class, () -> stack.enterChoice(nodeIdentifier)).getMessage();
104     }
105 }