Add DataSchemaContextNode/SchemaInferenceStack integration
[yangtools.git] / data / yang-data-util / src / test / java / org / opendaylight / yangtools / yang / data / util / YT1412Test.java
1 /*
2  * Copyright (c) 2022 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.data.util;
9
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13
14 import java.util.Set;
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;
31
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");
40
41     private static DataSchemaContextTree CONTEXT;
42
43     private final SchemaInferenceStack stack = SchemaInferenceStack.of(CONTEXT.getEffectiveModelContext());
44
45     @BeforeClass
46     public static void init() {
47         CONTEXT = DataSchemaContextTree.from(YangParserTestUtils.parseYangResource("/yt1412.yang"));
48     }
49
50     @AfterClass
51     public static void cleanup() {
52         CONTEXT = null;
53     }
54
55     @Test
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));
60
61         final var two = one.enterChild(FOUR, stack);
62         assertThat(two, instanceOf(ChoiceNodeContextNode.class));
63         assertThat(stack.currentStatement(), instanceOf(ChoiceEffectiveStatement.class));
64
65         final var three = two.enterChild(FOUR, stack);
66         assertThat(three, instanceOf(ChoiceNodeContextNode.class));
67         assertThat(stack.currentStatement(), instanceOf(ChoiceEffectiveStatement.class));
68
69         final var four = three.enterChild(FOUR, stack);
70         assertThat(four, instanceOf(LeafContextNode.class));
71         assertThat(stack.currentStatement(), instanceOf(LeafEffectiveStatement.class));
72
73         assertEquals(Absolute.of(ONE, TWO, THREE, THREE, FOUR, FOUR), stack.toSchemaNodeIdentifier());
74     }
75
76     @Test
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));
81
82         final var augment = one.enterChild(FIVE, stack);
83         assertThat(augment, instanceOf(AugmentationContextNode.class));
84         assertThat(stack.currentStatement(), instanceOf(ContainerEffectiveStatement.class));
85
86         final var five = augment.enterChild(FIVE, stack);
87         assertThat(five, instanceOf(UnkeyedListMixinContextNode.class));
88         assertThat(stack.currentStatement(), instanceOf(ListEffectiveStatement.class));
89
90         final var fiveItem = five.enterChild(FIVE, stack);
91         assertThat(fiveItem, instanceOf(UnkeyedListItemContextNode.class));
92         assertThat(stack.currentStatement(), instanceOf(ListEffectiveStatement.class));
93
94         assertEquals(Absolute.of(ONE, FIVE), stack.toSchemaNodeIdentifier());
95     }
96
97     @Test
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));
102
103         final var two = one.enterChild(SIX, stack);
104         assertThat(two, instanceOf(ChoiceNodeContextNode.class));
105         assertThat(stack.currentStatement(), instanceOf(ChoiceEffectiveStatement.class));
106
107         final var three = two.enterChild(SIX, stack);
108         assertThat(three, instanceOf(ChoiceNodeContextNode.class));
109         assertThat(stack.currentStatement(), instanceOf(ChoiceEffectiveStatement.class));
110
111         final var six = three.enterChild(SIX, stack);
112         assertThat(six, instanceOf(LeafContextNode.class));
113         assertThat(stack.currentStatement(), instanceOf(LeafEffectiveStatement.class));
114
115         assertEquals(Absolute.of(ONE, TWO, THREE, THREE, SIX, SIX), stack.toSchemaNodeIdentifier());
116     }
117
118     @Test
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)))
125             .orElseThrow();
126
127         assertThat(result.node(), instanceOf(LeafContextNode.class));
128         assertEquals(Absolute.of(ONE, TWO, THREE, THREE, FOUR, FOUR), result.stack().toSchemaNodeIdentifier());
129     }
130
131     @Test
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)))
138             .orElseThrow();
139
140         assertThat(result.node(), instanceOf(UnkeyedListItemContextNode.class));
141         assertEquals(Absolute.of(ONE, FIVE), result.stack().toSchemaNodeIdentifier());
142     }
143
144     @Test
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)))
151             .orElseThrow();
152
153         assertThat(result.node(), instanceOf(LeafContextNode.class));
154         assertEquals(Absolute.of(ONE, TWO, THREE, THREE, SIX, SIX), result.stack().toSchemaNodeIdentifier());
155     }
156 }