bc27c551558b91e2b47582bdbac9db63a1eeacd7
[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.SchemaAwareApplyOperationRoot;
30 import org.opendaylight.controller.md.sal.dom.store.impl.TestModel;
31 import org.opendaylight.controller.md.sal.dom.store.impl.tree.DataTree;
32 import org.opendaylight.controller.md.sal.dom.store.impl.tree.DataTreeModification;
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
106     @Before
107     public void prepare() {
108         schemaContext = TestModel.createTestContext();
109         assertNotNull("Schema context must not be null.", schemaContext);
110     }
111
112     /**
113      * Returns a test document
114      *
115      * <pre>
116      * test
117      *     outer-list
118      *          id 1
119      *     outer-list
120      *          id 2
121      *          inner-list
122      *                  name "one"
123      *          inner-list
124      *                  name "two"
125      *
126      * </pre>
127      *
128      * @return
129      */
130     public NormalizedNode<?, ?> createDocumentOne() {
131         return ImmutableContainerNodeBuilder
132                 .create()
133                 .withNodeIdentifier(new NodeIdentifier(schemaContext.getQName()))
134                 .withChild(createTestContainer()).build();
135
136     }
137
138     private ContainerNode createTestContainer() {
139         return ImmutableContainerNodeBuilder
140                 .create()
141                 .withNodeIdentifier(new NodeIdentifier(TEST_QNAME))
142                 .withChild(
143                         mapNodeBuilder(OUTER_LIST_QNAME)
144                                 .withChild(mapEntry(OUTER_LIST_QNAME, ID_QNAME, ONE_ID))
145                                 .withChild(BAR_NODE).build()).build();
146     }
147
148     @Test
149     public void basicReadWrites() {
150         DataTreeModification modificationTree = new InMemoryDataTreeModification(new InMemoryDataTreeSnapshot(schemaContext,
151                 StoreMetadataNode.createRecursively(createDocumentOne(), UnsignedLong.valueOf(5))),
152                 new SchemaAwareApplyOperationRoot(schemaContext));
153         Optional<NormalizedNode<?, ?>> originalBarNode = modificationTree.readNode(OUTER_LIST_2_PATH);
154         assertTrue(originalBarNode.isPresent());
155         assertSame(BAR_NODE, originalBarNode.get());
156
157         // writes node to /outer-list/1/inner_list/two/value
158         modificationTree.write(TWO_TWO_VALUE_PATH, ImmutableNodes.leafNode(VALUE_QNAME, "test"));
159
160         // reads node to /outer-list/1/inner_list/two/value
161         // and checks if node is already present
162         Optional<NormalizedNode<?, ?>> barTwoCModified = modificationTree.readNode(TWO_TWO_VALUE_PATH);
163         assertTrue(barTwoCModified.isPresent());
164         assertEquals(ImmutableNodes.leafNode(VALUE_QNAME, "test"), barTwoCModified.get());
165
166         // delete node to /outer-list/1/inner_list/two/value
167         modificationTree.delete(TWO_TWO_VALUE_PATH);
168         Optional<NormalizedNode<?, ?>> barTwoCAfterDelete = modificationTree.readNode(TWO_TWO_VALUE_PATH);
169         assertFalse(barTwoCAfterDelete.isPresent());
170     }
171
172
173     public DataTreeModification createEmptyModificationTree() {
174         /**
175          * Creates empty Snapshot with associated schema context.
176          */
177         DataTree t = InMemoryDataTreeFactory.getInstance().create();
178         t.setSchemaContext(schemaContext);
179
180         /**
181          *
182          * Creates Mutable Data Tree based on provided snapshot and schema
183          * context.
184          *
185          */
186         return t.takeSnapshot().newModification(new SchemaAwareApplyOperationRoot(schemaContext));
187     }
188
189     @Test
190     public void createFromEmptyState() {
191
192         DataTreeModification modificationTree = createEmptyModificationTree();
193         /**
194          * Writes empty container node to /test
195          *
196          */
197         modificationTree.write(TEST_PATH, ImmutableNodes.containerNode(TEST_QNAME));
198
199         /**
200          * Writes empty list node to /test/outer-list
201          */
202         modificationTree.write(OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(OUTER_LIST_QNAME).build());
203
204         /**
205          * Reads list node from /test/outer-list
206          */
207         Optional<NormalizedNode<?, ?>> potentialOuterList = modificationTree.readNode(OUTER_LIST_PATH);
208         assertTrue(potentialOuterList.isPresent());
209
210         /**
211          * Reads container node from /test and verifies that it contains test
212          * node
213          */
214         Optional<NormalizedNode<?, ?>> potentialTest = modificationTree.readNode(TEST_PATH);
215         ContainerNode containerTest = assertPresentAndType(potentialTest, ContainerNode.class);
216
217         /**
218          *
219          * Gets list from returned snapshot of /test and verifies it contains
220          * outer-list
221          *
222          */
223         assertPresentAndType(containerTest.getChild(new NodeIdentifier(OUTER_LIST_QNAME)), MapNode.class);
224
225     }
226
227     @Test
228     public void writeSubtreeReadChildren() {
229         DataTreeModification modificationTree = createEmptyModificationTree();
230         modificationTree.write(TEST_PATH, createTestContainer());
231         Optional<NormalizedNode<?, ?>> potential = modificationTree.readNode(TWO_TWO_PATH);
232         assertPresentAndType(potential, MapEntryNode.class);
233     }
234
235     @Test
236     public void writeSubtreeDeleteChildren() {
237         DataTreeModification modificationTree = createEmptyModificationTree();
238         modificationTree.write(TEST_PATH, createTestContainer());
239
240         // We verify data are present
241         Optional<NormalizedNode<?, ?>> potentialBeforeDelete = modificationTree.readNode(TWO_TWO_PATH);
242         assertPresentAndType(potentialBeforeDelete, MapEntryNode.class);
243
244         modificationTree.delete(TWO_TWO_PATH);
245         Optional<NormalizedNode<?, ?>> potentialAfterDelete = modificationTree.readNode(TWO_TWO_PATH);
246         assertFalse(potentialAfterDelete.isPresent());
247
248     }
249
250     private static <T> T assertPresentAndType(final Optional<?> potential, final Class<T> type) {
251         assertNotNull(potential);
252         assertTrue(potential.isPresent());
253         assertTrue(type.isInstance(potential.get()));
254         return type.cast(potential.get());
255     }
256
257 }