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