Merge "BUG-509: migrate to TreeNodes"
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / test / java / org / opendaylight / controller / md / sal / dom / store / impl / tree / data / ModificationMetadataTreeTest.java
1 /*
2  * Copyright (c) 2014 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.controller.md.sal.dom.store.impl.tree.data;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertSame;
14 import static org.junit.Assert.assertTrue;
15 import static org.opendaylight.controller.md.sal.dom.store.impl.TestModel.ID_QNAME;
16 import static org.opendaylight.controller.md.sal.dom.store.impl.TestModel.INNER_LIST_QNAME;
17 import static org.opendaylight.controller.md.sal.dom.store.impl.TestModel.NAME_QNAME;
18 import static org.opendaylight.controller.md.sal.dom.store.impl.TestModel.OUTER_LIST_PATH;
19 import static org.opendaylight.controller.md.sal.dom.store.impl.TestModel.OUTER_LIST_QNAME;
20 import static org.opendaylight.controller.md.sal.dom.store.impl.TestModel.TEST_PATH;
21 import static org.opendaylight.controller.md.sal.dom.store.impl.TestModel.TEST_QNAME;
22 import static org.opendaylight.controller.md.sal.dom.store.impl.TestModel.VALUE_QNAME;
23 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntry;
24 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntryBuilder;
25 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapNodeBuilder;
26
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.opendaylight.controller.md.sal.dom.store.impl.TestModel;
30 import org.opendaylight.controller.md.sal.dom.store.impl.tree.DataTree;
31 import org.opendaylight.controller.md.sal.dom.store.impl.tree.DataTreeModification;
32 import org.opendaylight.controller.md.sal.dom.store.impl.tree.spi.TreeNodeFactory;
33 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
39 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
40 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42
43 import com.google.common.base.Optional;
44 import com.google.common.primitives.UnsignedLong;
45
46 /**
47  *
48  * Schema structure of document is
49  *
50  * <pre>
51  * container root { 
52  *      list list-a {
53  *              key leaf-a;
54  *              leaf leaf-a;
55  *              choice choice-a {
56  *                      case one {
57  *                              leaf one;
58  *                      }
59  *                      case two-three {
60  *                              leaf two;
61  *                              leaf three;
62  *                      }
63  *              }
64  *              list list-b {
65  *                      key leaf-b;
66  *                      leaf leaf-b;
67  *              }
68  *      }
69  * }
70  * </pre>
71  *
72  */
73 public class ModificationMetadataTreeTest {
74
75     private static final Short ONE_ID = 1;
76     private static final Short TWO_ID = 2;
77     private static final String TWO_ONE_NAME = "one";
78     private static final String TWO_TWO_NAME = "two";
79
80     private static final InstanceIdentifier OUTER_LIST_1_PATH = InstanceIdentifier.builder(OUTER_LIST_PATH)
81             .nodeWithKey(OUTER_LIST_QNAME, ID_QNAME, ONE_ID) //
82             .build();
83
84     private static final InstanceIdentifier OUTER_LIST_2_PATH = InstanceIdentifier.builder(OUTER_LIST_PATH)
85             .nodeWithKey(OUTER_LIST_QNAME, ID_QNAME, TWO_ID) //
86             .build();
87
88     private static final InstanceIdentifier TWO_TWO_PATH = InstanceIdentifier.builder(OUTER_LIST_2_PATH)
89             .node(INNER_LIST_QNAME) //
90             .nodeWithKey(INNER_LIST_QNAME, NAME_QNAME, TWO_TWO_NAME) //
91             .build();
92
93     private static final InstanceIdentifier TWO_TWO_VALUE_PATH = InstanceIdentifier.builder(TWO_TWO_PATH)
94             .node(VALUE_QNAME) //
95             .build();
96
97     private static final MapEntryNode BAR_NODE = mapEntryBuilder(OUTER_LIST_QNAME, ID_QNAME, TWO_ID) //
98             .withChild(mapNodeBuilder(INNER_LIST_QNAME) //
99                     .withChild(mapEntry(INNER_LIST_QNAME, NAME_QNAME, TWO_ONE_NAME)) //
100                     .withChild(mapEntry(INNER_LIST_QNAME, NAME_QNAME, TWO_TWO_NAME)) //
101                     .build()) //
102                     .build();
103
104     private SchemaContext schemaContext;
105     private ModificationApplyOperation applyOper;
106
107     @Before
108     public void prepare() {
109         schemaContext = TestModel.createTestContext();
110         assertNotNull("Schema context must not be null.", schemaContext);
111         applyOper = SchemaAwareApplyOperation.from(schemaContext);
112     }
113
114     /**
115      * Returns a test document
116      *
117      * <pre>
118      * test
119      *     outer-list
120      *          id 1
121      *     outer-list
122      *          id 2
123      *          inner-list
124      *                  name "one"
125      *          inner-list
126      *                  name "two"
127      *
128      * </pre>
129      *
130      * @return
131      */
132     public NormalizedNode<?, ?> createDocumentOne() {
133         return ImmutableContainerNodeBuilder
134                 .create()
135                 .withNodeIdentifier(new NodeIdentifier(schemaContext.getQName()))
136                 .withChild(createTestContainer()).build();
137
138     }
139
140     private ContainerNode createTestContainer() {
141         return ImmutableContainerNodeBuilder
142                 .create()
143                 .withNodeIdentifier(new NodeIdentifier(TEST_QNAME))
144                 .withChild(
145                         mapNodeBuilder(OUTER_LIST_QNAME)
146                         .withChild(mapEntry(OUTER_LIST_QNAME, ID_QNAME, ONE_ID))
147                         .withChild(BAR_NODE).build()).build();
148     }
149
150     @Test
151     public void basicReadWrites() {
152         DataTreeModification modificationTree = new InMemoryDataTreeModification(new InMemoryDataTreeSnapshot(schemaContext,
153                 TreeNodeFactory.createTreeNode(createDocumentOne(), UnsignedLong.valueOf(5)), applyOper),
154                 new SchemaAwareApplyOperationRoot(schemaContext));
155         Optional<NormalizedNode<?, ?>> originalBarNode = modificationTree.readNode(OUTER_LIST_2_PATH);
156         assertTrue(originalBarNode.isPresent());
157         assertSame(BAR_NODE, originalBarNode.get());
158
159         // writes node to /outer-list/1/inner_list/two/value
160         modificationTree.write(TWO_TWO_VALUE_PATH, ImmutableNodes.leafNode(VALUE_QNAME, "test"));
161
162         // reads node to /outer-list/1/inner_list/two/value
163         // and checks if node is already present
164         Optional<NormalizedNode<?, ?>> barTwoCModified = modificationTree.readNode(TWO_TWO_VALUE_PATH);
165         assertTrue(barTwoCModified.isPresent());
166         assertEquals(ImmutableNodes.leafNode(VALUE_QNAME, "test"), barTwoCModified.get());
167
168         // delete node to /outer-list/1/inner_list/two/value
169         modificationTree.delete(TWO_TWO_VALUE_PATH);
170         Optional<NormalizedNode<?, ?>> barTwoCAfterDelete = modificationTree.readNode(TWO_TWO_VALUE_PATH);
171         assertFalse(barTwoCAfterDelete.isPresent());
172     }
173
174
175     public DataTreeModification createEmptyModificationTree() {
176         /**
177          * Creates empty Snapshot with associated schema context.
178          */
179         DataTree t = InMemoryDataTreeFactory.getInstance().create();
180         t.setSchemaContext(schemaContext);
181
182         /**
183          *
184          * Creates Mutable Data Tree based on provided snapshot and schema
185          * context.
186          *
187          */
188         return t.takeSnapshot().newModification();
189     }
190
191     @Test
192     public void createFromEmptyState() {
193
194         DataTreeModification modificationTree = createEmptyModificationTree();
195         /**
196          * Writes empty container node to /test
197          *
198          */
199         modificationTree.write(TEST_PATH, ImmutableNodes.containerNode(TEST_QNAME));
200
201         /**
202          * Writes empty list node to /test/outer-list
203          */
204         modificationTree.write(OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(OUTER_LIST_QNAME).build());
205
206         /**
207          * Reads list node from /test/outer-list
208          */
209         Optional<NormalizedNode<?, ?>> potentialOuterList = modificationTree.readNode(OUTER_LIST_PATH);
210         assertTrue(potentialOuterList.isPresent());
211
212         /**
213          * Reads container node from /test and verifies that it contains test
214          * node
215          */
216         Optional<NormalizedNode<?, ?>> potentialTest = modificationTree.readNode(TEST_PATH);
217         ContainerNode containerTest = assertPresentAndType(potentialTest, ContainerNode.class);
218
219         /**
220          *
221          * Gets list from returned snapshot of /test and verifies it contains
222          * outer-list
223          *
224          */
225         assertPresentAndType(containerTest.getChild(new NodeIdentifier(OUTER_LIST_QNAME)), MapNode.class);
226
227     }
228
229     @Test
230     public void writeSubtreeReadChildren() {
231         DataTreeModification modificationTree = createEmptyModificationTree();
232         modificationTree.write(TEST_PATH, createTestContainer());
233         Optional<NormalizedNode<?, ?>> potential = modificationTree.readNode(TWO_TWO_PATH);
234         assertPresentAndType(potential, MapEntryNode.class);
235     }
236
237     @Test
238     public void writeSubtreeDeleteChildren() {
239         DataTreeModification modificationTree = createEmptyModificationTree();
240         modificationTree.write(TEST_PATH, createTestContainer());
241
242         // We verify data are present
243         Optional<NormalizedNode<?, ?>> potentialBeforeDelete = modificationTree.readNode(TWO_TWO_PATH);
244         assertPresentAndType(potentialBeforeDelete, MapEntryNode.class);
245
246         modificationTree.delete(TWO_TWO_PATH);
247         Optional<NormalizedNode<?, ?>> potentialAfterDelete = modificationTree.readNode(TWO_TWO_PATH);
248         assertFalse(potentialAfterDelete.isPresent());
249
250     }
251
252     private static <T> T assertPresentAndType(final Optional<?> potential, final Class<T> type) {
253         assertNotNull(potential);
254         assertTrue(potential.isPresent());
255         assertTrue(type.isInstance(potential.get()));
256         return type.cast(potential.get());
257     }
258
259 }