Migrate yang-data-tree-ri to JUnit5
[yangtools.git] / data / yang-data-tree-ri / src / test / java / org / opendaylight / yangtools / yang / data / tree / impl / ModificationMetadataTreeTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.tree.impl;
9
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertFalse;
12 import static org.junit.jupiter.api.Assertions.assertNotNull;
13 import static org.junit.jupiter.api.Assertions.assertSame;
14 import static org.junit.jupiter.api.Assertions.assertTrue;
15 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntry;
16 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntryBuilder;
17 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapNodeBuilder;
18
19 import java.util.Optional;
20 import org.junit.jupiter.api.BeforeEach;
21 import org.junit.jupiter.api.Test;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
26 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
27 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
28 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
29 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
30 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
31 import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
32 import org.opendaylight.yangtools.yang.data.tree.impl.node.Version;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34
35 /*
36  * Schema structure of document is
37  *
38  * container root { 
39  *      list list-a {
40  *              key leaf-a;
41  *              leaf leaf-a;
42  *              choice choice-a {
43  *                      case one {
44  *                              leaf one;
45  *                      }
46  *                      case two-three {
47  *                              leaf two;
48  *                              leaf three;
49  *                      }
50  *              }
51  *              list list-b {
52  *                      key leaf-b;
53  *                      leaf leaf-b;
54  *              }
55  *      }
56  * }
57  */
58 class ModificationMetadataTreeTest extends AbstractTestModelTest {
59
60     private static final Short ONE_ID = 1;
61     private static final Short TWO_ID = 2;
62     private static final String TWO_ONE_NAME = "one";
63     private static final String TWO_TWO_NAME = "two";
64
65     private static final YangInstanceIdentifier OUTER_LIST_2_PATH =
66             YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
67             .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
68             .build();
69
70     private static final YangInstanceIdentifier TWO_TWO_PATH = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
71             .node(TestModel.INNER_LIST_QNAME)
72             .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME)
73             .build();
74
75     private static final YangInstanceIdentifier TWO_TWO_VALUE_PATH = YangInstanceIdentifier.builder(TWO_TWO_PATH)
76             .node(TestModel.VALUE_QNAME)
77             .build();
78
79     private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
80             .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME)
81                     .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_ONE_NAME))
82                     .withChild(mapEntry(TestModel.INNER_LIST_QNAME,TestModel.NAME_QNAME, TWO_TWO_NAME))
83                     .build())
84                     .build();
85
86     private RootApplyStrategy rootOper;
87
88     @BeforeEach
89     void prepare() throws ExcludedDataSchemaNodeException {
90         rootOper = RootApplyStrategy.from(SchemaAwareApplyOperation.from(SCHEMA_CONTEXT,
91             DataTreeConfiguration.DEFAULT_OPERATIONAL));
92     }
93
94     /**
95      * Returns a test document.
96      * <pre>
97      * test
98      *     outer-list
99      *          id 1
100      *     outer-list
101      *          id 2
102      *          inner-list
103      *                  name "one"
104      *          inner-list
105      *                  name "two"
106      *
107      * </pre>
108      *
109      * @return a test document
110      */
111     public ContainerNode createDocumentOne() {
112         return Builders.containerBuilder()
113             .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME))
114             .withChild(createTestContainer())
115             .build();
116     }
117
118     private static ContainerNode createTestContainer() {
119         return Builders.containerBuilder()
120             .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
121             .withChild(mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
122                 .withChild(mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
123                 .withChild(BAR_NODE).build())
124             .build();
125     }
126
127     @Test
128     void basicReadWrites() {
129         final var modificationTree = new InMemoryDataTreeModification(
130             new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
131                 TreeNode.of(createDocumentOne(), Version.initial()), rootOper), rootOper);
132         final var originalBarNode = modificationTree.readNode(OUTER_LIST_2_PATH);
133         assertTrue(originalBarNode.isPresent());
134         assertSame(BAR_NODE, originalBarNode.orElseThrow());
135
136         // writes node to /outer-list/1/inner_list/two/value
137         modificationTree.write(TWO_TWO_VALUE_PATH, ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "test"));
138
139         // reads node to /outer-list/1/inner_list/two/value
140         // and checks if node is already present
141         final var barTwoCModified = modificationTree.readNode(TWO_TWO_VALUE_PATH);
142         assertTrue(barTwoCModified.isPresent());
143         assertEquals(ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "test"), barTwoCModified.orElseThrow());
144
145         // delete node to /outer-list/1/inner_list/two/value
146         modificationTree.delete(TWO_TWO_VALUE_PATH);
147         final var barTwoCAfterDelete = modificationTree.readNode(TWO_TWO_VALUE_PATH);
148         assertFalse(barTwoCAfterDelete.isPresent());
149     }
150
151
152     public DataTreeModification createEmptyModificationTree() {
153         /**
154          * Creates empty Snapshot with associated schema context.
155          */
156         final var t = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
157             SCHEMA_CONTEXT);
158
159         /**
160          *
161          * Creates Mutable Data Tree based on provided snapshot and schema
162          * context.
163          *
164          */
165         return t.takeSnapshot().newModification();
166     }
167
168     @Test
169     void createFromEmptyState() {
170
171         final var modificationTree = createEmptyModificationTree();
172         // Writes empty container node to /test
173         modificationTree.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
174
175         // Writes empty list node to /test/outer-list
176         modificationTree.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
177             .build());
178
179         // Reads list node from /test/outer-list.
180         final var potentialOuterList = modificationTree.readNode(TestModel.OUTER_LIST_PATH);
181         assertFalse(potentialOuterList.isPresent());
182
183         // Reads container node from /test and verifies that it contains test node.
184         final var potentialTest = modificationTree.readNode(TestModel.TEST_PATH);
185         assertPresentAndType(potentialTest, ContainerNode.class);
186     }
187
188     @Test
189     void writeSubtreeReadChildren() {
190         final var modificationTree = createEmptyModificationTree();
191         modificationTree.write(TestModel.TEST_PATH, createTestContainer());
192         final var potential = modificationTree.readNode(TWO_TWO_PATH);
193         assertPresentAndType(potential, MapEntryNode.class);
194     }
195
196     @Test
197     void writeSubtreeDeleteChildren() {
198         final var modificationTree = createEmptyModificationTree();
199         modificationTree.write(TestModel.TEST_PATH, createTestContainer());
200
201         // We verify data are present
202         final var potentialBeforeDelete = modificationTree.readNode(TWO_TWO_PATH);
203         assertPresentAndType(potentialBeforeDelete, MapEntryNode.class);
204
205         modificationTree.delete(TWO_TWO_PATH);
206         final var potentialAfterDelete = modificationTree.readNode(TWO_TWO_PATH);
207         assertFalse(potentialAfterDelete.isPresent());
208
209     }
210
211     private static <T> T assertPresentAndType(final Optional<?> potential, final Class<T> type) {
212         assertNotNull(potential);
213         assertTrue(potential.isPresent());
214         assertTrue(type.isInstance(potential.orElseThrow()));
215         return type.cast(potential.orElseThrow());
216     }
217 }