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