2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.md.sal.dom.store.impl.tree.data;
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;
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;
42 import com.google.common.base.Optional;
43 import com.google.common.primitives.UnsignedLong;
47 * Schema structure of document is
72 public class ModificationMetadataTreeTest {
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";
79 private static final InstanceIdentifier OUTER_LIST_1_PATH = InstanceIdentifier.builder(OUTER_LIST_PATH)
80 .nodeWithKey(OUTER_LIST_QNAME, ID_QNAME, ONE_ID) //
83 private static final InstanceIdentifier OUTER_LIST_2_PATH = InstanceIdentifier.builder(OUTER_LIST_PATH)
84 .nodeWithKey(OUTER_LIST_QNAME, ID_QNAME, TWO_ID) //
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) //
92 private static final InstanceIdentifier TWO_TWO_VALUE_PATH = InstanceIdentifier.builder(TWO_TWO_PATH)
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)) //
103 private SchemaContext schemaContext;
104 private ModificationApplyOperation applyOper;
107 public void prepare() {
108 schemaContext = TestModel.createTestContext();
109 assertNotNull("Schema context must not be null.", schemaContext);
110 applyOper = SchemaAwareApplyOperation.from(schemaContext);
114 * Returns a test document
131 public NormalizedNode<?, ?> createDocumentOne() {
132 return ImmutableContainerNodeBuilder
134 .withNodeIdentifier(new NodeIdentifier(schemaContext.getQName()))
135 .withChild(createTestContainer()).build();
139 private ContainerNode createTestContainer() {
140 return ImmutableContainerNodeBuilder
142 .withNodeIdentifier(new NodeIdentifier(TEST_QNAME))
144 mapNodeBuilder(OUTER_LIST_QNAME)
145 .withChild(mapEntry(OUTER_LIST_QNAME, ID_QNAME, ONE_ID))
146 .withChild(BAR_NODE).build()).build();
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());
158 // writes node to /outer-list/1/inner_list/two/value
159 modificationTree.write(TWO_TWO_VALUE_PATH, ImmutableNodes.leafNode(VALUE_QNAME, "test"));
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());
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());
174 public DataTreeModification createEmptyModificationTree() {
176 * Creates empty Snapshot with associated schema context.
178 DataTree t = InMemoryDataTreeFactory.getInstance().create();
179 t.setSchemaContext(schemaContext);
183 * Creates Mutable Data Tree based on provided snapshot and schema
187 return t.takeSnapshot().newModification();
191 public void createFromEmptyState() {
193 DataTreeModification modificationTree = createEmptyModificationTree();
195 * Writes empty container node to /test
198 modificationTree.write(TEST_PATH, ImmutableNodes.containerNode(TEST_QNAME));
201 * Writes empty list node to /test/outer-list
203 modificationTree.write(OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(OUTER_LIST_QNAME).build());
206 * Reads list node from /test/outer-list
208 Optional<NormalizedNode<?, ?>> potentialOuterList = modificationTree.readNode(OUTER_LIST_PATH);
209 assertTrue(potentialOuterList.isPresent());
212 * Reads container node from /test and verifies that it contains test
215 Optional<NormalizedNode<?, ?>> potentialTest = modificationTree.readNode(TEST_PATH);
216 ContainerNode containerTest = assertPresentAndType(potentialTest, ContainerNode.class);
220 * Gets list from returned snapshot of /test and verifies it contains
224 assertPresentAndType(containerTest.getChild(new NodeIdentifier(OUTER_LIST_QNAME)), MapNode.class);
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);
237 public void writeSubtreeDeleteChildren() {
238 DataTreeModification modificationTree = createEmptyModificationTree();
239 modificationTree.write(TEST_PATH, createTestContainer());
241 // We verify data are present
242 Optional<NormalizedNode<?, ?>> potentialBeforeDelete = modificationTree.readNode(TWO_TWO_PATH);
243 assertPresentAndType(potentialBeforeDelete, MapEntryNode.class);
245 modificationTree.delete(TWO_TWO_PATH);
246 Optional<NormalizedNode<?, ?>> potentialAfterDelete = modificationTree.readNode(TWO_TWO_PATH);
247 assertFalse(potentialAfterDelete.isPresent());
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());