Remove Augmentation{Identifier,Node}
[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.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
12
13 import org.junit.jupiter.api.AfterAll;
14 import org.junit.jupiter.api.BeforeAll;
15 import org.junit.jupiter.api.Test;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.common.XMLNamespace;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
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.LeafEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.ListEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
26 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
27 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
28
29 class YT1412Test {
30     private static final QNameModule MODULE = QNameModule.create(XMLNamespace.of("foo"));
31     private static final QName ONE = QName.create(MODULE, "one");
32     private static final QName TWO = QName.create(MODULE, "two");
33     private static final QName THREE = QName.create(MODULE, "three");
34     private static final QName FOUR = QName.create(MODULE, "four");
35     private static final QName FIVE = QName.create(MODULE, "five");
36     private static final QName SIX = QName.create(MODULE, "six");
37
38     private static DataSchemaContextTree CONTEXT;
39
40     private final SchemaInferenceStack stack = SchemaInferenceStack.of(CONTEXT.getEffectiveModelContext());
41
42     @BeforeAll
43     static void init() {
44         CONTEXT = DataSchemaContextTree.from(YangParserTestUtils.parseYangResource("/yt1412.yang"));
45     }
46
47     @AfterAll
48     static void cleanup() {
49         CONTEXT = null;
50     }
51
52     @Test
53     void testEnterThroughChoice() {
54         final var one = assertInstanceOf(ContainerContextNode.class, CONTEXT.getRoot().enterChild(stack, ONE));
55         assertInstanceOf(ContainerEffectiveStatement.class, stack.currentStatement());
56
57         final var two = assertInstanceOf(ChoiceNodeContextNode.class, one.enterChild(FOUR, stack));
58         assertInstanceOf(ChoiceEffectiveStatement.class, stack.currentStatement());
59
60         final var three = assertInstanceOf(ChoiceNodeContextNode.class, two.enterChild(FOUR, stack));
61         assertInstanceOf(ChoiceEffectiveStatement.class, stack.currentStatement());
62
63         assertInstanceOf(LeafContextNode.class, three.enterChild(FOUR, stack));
64         assertInstanceOf(LeafEffectiveStatement.class, stack.currentStatement());
65
66         assertEquals(Absolute.of(ONE, TWO, THREE, THREE, FOUR, FOUR), stack.toSchemaNodeIdentifier());
67     }
68
69     @Test
70     void testEnterThroughAugment() {
71         final var one = assertInstanceOf(ContainerContextNode.class, CONTEXT.getRoot().enterChild(stack, ONE));
72         assertInstanceOf(ContainerEffectiveStatement.class, stack.currentStatement());
73
74         final var five = assertInstanceOf(UnkeyedListMixinContextNode.class, one.enterChild(FIVE, stack));
75         assertInstanceOf(ListEffectiveStatement.class, stack.currentStatement());
76
77         assertInstanceOf(UnkeyedListItemContextNode.class, five.enterChild(FIVE, stack));
78         assertInstanceOf(ListEffectiveStatement.class, stack.currentStatement());
79
80         assertEquals(Absolute.of(ONE, FIVE), stack.toSchemaNodeIdentifier());
81     }
82
83     @Test
84     void testEnterThroughAugmentChoiceAugment() {
85         final var one = assertInstanceOf(ContainerContextNode.class, CONTEXT.getRoot().enterChild(stack, ONE));
86         assertInstanceOf(ContainerEffectiveStatement.class, stack.currentStatement());
87
88         final var two = assertInstanceOf(ChoiceNodeContextNode.class, one.enterChild(SIX, stack));
89         assertInstanceOf(ChoiceEffectiveStatement.class, stack.currentStatement());
90
91         final var three = assertInstanceOf(ChoiceNodeContextNode.class, two.enterChild(SIX, stack));
92         assertInstanceOf(ChoiceEffectiveStatement.class, stack.currentStatement());
93
94         assertInstanceOf(LeafContextNode.class, three.enterChild(SIX, stack));
95         assertInstanceOf(LeafEffectiveStatement.class, stack.currentStatement());
96
97         assertEquals(Absolute.of(ONE, TWO, THREE, THREE, SIX, SIX), stack.toSchemaNodeIdentifier());
98     }
99
100     @Test
101     void testEnterChoicePath() {
102         final var result = CONTEXT.enterPath(YangInstanceIdentifier.create(
103             new NodeIdentifier(ONE),
104             new NodeIdentifier(TWO),
105             new NodeIdentifier(THREE),
106             new NodeIdentifier(FOUR)))
107             .orElseThrow();
108
109         assertInstanceOf(LeafContextNode.class, result.node());
110         assertEquals(Absolute.of(ONE, TWO, THREE, THREE, FOUR, FOUR), result.stack().toSchemaNodeIdentifier());
111     }
112
113     @Test
114     void testEnterAugmentPath() {
115         final var result = CONTEXT.enterPath(YangInstanceIdentifier.create(
116             new NodeIdentifier(ONE),
117             new NodeIdentifier(FIVE),
118             new NodeIdentifier(FIVE)))
119             .orElseThrow();
120
121         assertInstanceOf(UnkeyedListItemContextNode.class, result.node());
122         assertEquals(Absolute.of(ONE, FIVE), result.stack().toSchemaNodeIdentifier());
123     }
124
125     @Test
126     void testEnterAugmentChoicePath() {
127         final var result = CONTEXT.enterPath(YangInstanceIdentifier.create(
128             new NodeIdentifier(ONE),
129             new NodeIdentifier(TWO),
130             new NodeIdentifier(THREE),
131             new NodeIdentifier(SIX)))
132             .orElseThrow();
133
134         assertInstanceOf(LeafContextNode.class, result.node());
135         assertEquals(Absolute.of(ONE, TWO, THREE, THREE, SIX, SIX), result.stack().toSchemaNodeIdentifier());
136     }
137 }