2 * Copyright (c) 2021 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.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;
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;
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");
32 private static EffectiveModelContext CONTEXT;
34 private final SchemaInferenceStack stack = SchemaInferenceStack.of(CONTEXT);
37 public static void beforeClass() {
38 CONTEXT = YangParserTestUtils.parseYangResource("/yt1231.yang");
42 public void testEnterDataTree() {
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());
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());
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());
62 public void testEnterChoice() {
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());
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());
73 // Now the same with just case -> choice
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());
81 public void testEnterChoiceToRootContainer() {
82 assertEquals("Choice (foo)foo not present", assertEnterChoiceThrows(FOO));
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));
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));
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));
102 private String assertEnterChoiceThrows(final QName nodeIdentifier) {
103 return assertThrows(IllegalArgumentException.class, () -> stack.enterChoice(nodeIdentifier)).getMessage();