2 * Copyright (c) 2022 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.data.util;
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
15 import org.junit.AfterClass;
16 import org.junit.BeforeClass;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.common.QNameModule;
20 import org.opendaylight.yangtools.yang.common.XMLNamespace;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.ContainerEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.LeafEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.ListEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
29 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
30 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
32 public class YT1412Test {
33 private static final QNameModule MODULE = QNameModule.create(XMLNamespace.of("foo"));
34 private static final QName ONE = QName.create(MODULE, "one");
35 private static final QName TWO = QName.create(MODULE, "two");
36 private static final QName THREE = QName.create(MODULE, "three");
37 private static final QName FOUR = QName.create(MODULE, "four");
38 private static final QName FIVE = QName.create(MODULE, "five");
39 private static final QName SIX = QName.create(MODULE, "six");
41 private static DataSchemaContextTree CONTEXT;
43 private final SchemaInferenceStack stack = SchemaInferenceStack.of(CONTEXT.getEffectiveModelContext());
46 public static void init() {
47 CONTEXT = DataSchemaContextTree.from(YangParserTestUtils.parseYangResource("/yt1412.yang"));
51 public static void cleanup() {
56 public void testEnterThroughChoice() {
57 final var one = CONTEXT.getRoot().enterChild(stack, ONE);
58 assertThat(one, instanceOf(ContainerContextNode.class));
59 assertThat(stack.currentStatement(), instanceOf(ContainerEffectiveStatement.class));
61 final var two = one.enterChild(FOUR, stack);
62 assertThat(two, instanceOf(ChoiceNodeContextNode.class));
63 assertThat(stack.currentStatement(), instanceOf(ChoiceEffectiveStatement.class));
65 final var three = two.enterChild(FOUR, stack);
66 assertThat(three, instanceOf(ChoiceNodeContextNode.class));
67 assertThat(stack.currentStatement(), instanceOf(ChoiceEffectiveStatement.class));
69 final var four = three.enterChild(FOUR, stack);
70 assertThat(four, instanceOf(LeafContextNode.class));
71 assertThat(stack.currentStatement(), instanceOf(LeafEffectiveStatement.class));
73 assertEquals(Absolute.of(ONE, TWO, THREE, THREE, FOUR, FOUR), stack.toSchemaNodeIdentifier());
77 public void testEnterThroughAugment() {
78 final var one = CONTEXT.getRoot().enterChild(stack, ONE);
79 assertThat(one, instanceOf(ContainerContextNode.class));
80 assertThat(stack.currentStatement(), instanceOf(ContainerEffectiveStatement.class));
82 final var augment = one.enterChild(FIVE, stack);
83 assertThat(augment, instanceOf(AugmentationContextNode.class));
84 assertThat(stack.currentStatement(), instanceOf(ContainerEffectiveStatement.class));
86 final var five = augment.enterChild(FIVE, stack);
87 assertThat(five, instanceOf(UnkeyedListMixinContextNode.class));
88 assertThat(stack.currentStatement(), instanceOf(ListEffectiveStatement.class));
90 final var fiveItem = five.enterChild(FIVE, stack);
91 assertThat(fiveItem, instanceOf(UnkeyedListItemContextNode.class));
92 assertThat(stack.currentStatement(), instanceOf(ListEffectiveStatement.class));
94 assertEquals(Absolute.of(ONE, FIVE), stack.toSchemaNodeIdentifier());
98 public void testEnterThroughAugmentChoiceAugment() {
99 final var one = CONTEXT.getRoot().enterChild(stack, ONE);
100 assertThat(one, instanceOf(ContainerContextNode.class));
101 assertThat(stack.currentStatement(), instanceOf(ContainerEffectiveStatement.class));
103 final var two = one.enterChild(SIX, stack);
104 assertThat(two, instanceOf(ChoiceNodeContextNode.class));
105 assertThat(stack.currentStatement(), instanceOf(ChoiceEffectiveStatement.class));
107 final var three = two.enterChild(SIX, stack);
108 assertThat(three, instanceOf(ChoiceNodeContextNode.class));
109 assertThat(stack.currentStatement(), instanceOf(ChoiceEffectiveStatement.class));
111 final var six = three.enterChild(SIX, stack);
112 assertThat(six, instanceOf(LeafContextNode.class));
113 assertThat(stack.currentStatement(), instanceOf(LeafEffectiveStatement.class));
115 assertEquals(Absolute.of(ONE, TWO, THREE, THREE, SIX, SIX), stack.toSchemaNodeIdentifier());
119 public void testEnterChoicePath() {
120 final var result = CONTEXT.enterPath(YangInstanceIdentifier.create(
121 new NodeIdentifier(ONE),
122 new NodeIdentifier(TWO),
123 new NodeIdentifier(THREE),
124 new NodeIdentifier(FOUR)))
127 assertThat(result.node(), instanceOf(LeafContextNode.class));
128 assertEquals(Absolute.of(ONE, TWO, THREE, THREE, FOUR, FOUR), result.stack().toSchemaNodeIdentifier());
132 public void testEnterAugmentPath() {
133 final var result = CONTEXT.enterPath(YangInstanceIdentifier.create(
134 new NodeIdentifier(ONE),
135 new AugmentationIdentifier(Set.of(FIVE)),
136 new NodeIdentifier(FIVE),
137 new NodeIdentifier(FIVE)))
140 assertThat(result.node(), instanceOf(UnkeyedListItemContextNode.class));
141 assertEquals(Absolute.of(ONE, FIVE), result.stack().toSchemaNodeIdentifier());
145 public void testEnterAugmentChoicePath() {
146 final var result = CONTEXT.enterPath(YangInstanceIdentifier.create(
147 new NodeIdentifier(ONE),
148 new NodeIdentifier(TWO),
149 new NodeIdentifier(THREE),
150 new NodeIdentifier(SIX)))
153 assertThat(result.node(), instanceOf(LeafContextNode.class));
154 assertEquals(Absolute.of(ONE, TWO, THREE, THREE, SIX, SIX), result.stack().toSchemaNodeIdentifier());