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