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;
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
18 import org.opendaylight.yangtools.yang.model.api.stmt.CaseEffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.ContainerEffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
22 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
24 public class YT1231Test {
25 private static final QName FOO = QName.create("foo", "foo");
26 private static final QName BAR = QName.create("foo", "bar");
27 private static final QName BAZ = QName.create("foo", "baz");
28 private static final QName XYZZY = QName.create("foo", "xyzzy");
31 public void testEnterDataTree() {
32 final EffectiveModelContext context = YangParserTestUtils.parseYangResource("/yt1231.yang");
33 final SchemaInferenceStack stack = SchemaInferenceStack.of(context);
36 assertThat(stack.enterDataTree(FOO), instanceOf(ContainerEffectiveStatement.class));
37 assertSame(context.getModuleStatement(FOO.getModule()), stack.currentModule());
38 assertEquals(Absolute.of(FOO), stack.toSchemaNodeIdentifier());
39 assertThat(stack.enterDataTree(FOO), instanceOf(ContainerEffectiveStatement.class));
40 assertEquals(Absolute.of(FOO, FOO), stack.toSchemaNodeIdentifier());
43 // Has to cross four layers of choice/case
44 assertThat(stack.enterDataTree(XYZZY), instanceOf(ContainerEffectiveStatement.class));
45 assertEquals(Absolute.of(FOO, BAR, BAR, BAZ, BAZ, XYZZY), stack.toSchemaNodeIdentifier());
48 assertThat(stack.enterSchemaTree(BAR), instanceOf(ChoiceEffectiveStatement.class));
49 assertThat(stack.enterSchemaTree(BAR), instanceOf(CaseEffectiveStatement.class));
50 assertEquals(Absolute.of(FOO, BAR, BAR), stack.toSchemaNodeIdentifier());
54 public void testEnterChoice() {
55 final EffectiveModelContext context = YangParserTestUtils.parseYangResource("/yt1231.yang");
56 final SchemaInferenceStack stack = SchemaInferenceStack.of(context);
59 assertThat(stack.enterDataTree(FOO), instanceOf(ContainerEffectiveStatement.class));
60 assertEquals(Absolute.of(FOO), stack.toSchemaNodeIdentifier());
61 assertThat(stack.enterChoice(BAR), instanceOf(ChoiceEffectiveStatement.class));
62 assertEquals(Absolute.of(FOO, BAR), stack.toSchemaNodeIdentifier());
64 // Has to cross choice -> case -> choice
65 assertThat(stack.enterChoice(BAZ), instanceOf(ChoiceEffectiveStatement.class));
66 assertEquals(Absolute.of(FOO, BAR, BAR, BAZ), stack.toSchemaNodeIdentifier());
68 // Now the same with just case -> choice
70 assertThat(stack.enterSchemaTree(BAR), instanceOf(CaseEffectiveStatement.class));
71 assertThat(stack.enterChoice(BAZ), instanceOf(ChoiceEffectiveStatement.class));
72 assertEquals(Absolute.of(FOO, BAR, BAR, BAZ), stack.toSchemaNodeIdentifier());