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