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.controller.md.sal.dom.store.impl.tree.spi.TreeNodeFactory;
33 import org.opendaylight.controller.md.sal.dom.store.impl.tree.spi.Version;
34 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
40 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
41 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
44 import com.google.common.base.Optional;
48 * Schema structure of document is
73 public class ModificationMetadataTreeTest {
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";
80 private static final InstanceIdentifier OUTER_LIST_1_PATH = InstanceIdentifier.builder(OUTER_LIST_PATH)
81 .nodeWithKey(OUTER_LIST_QNAME, ID_QNAME, ONE_ID) //
84 private static final InstanceIdentifier OUTER_LIST_2_PATH = InstanceIdentifier.builder(OUTER_LIST_PATH)
85 .nodeWithKey(OUTER_LIST_QNAME, ID_QNAME, TWO_ID) //
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) //
93 private static final InstanceIdentifier TWO_TWO_VALUE_PATH = InstanceIdentifier.builder(TWO_TWO_PATH)
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)) //
104 private SchemaContext schemaContext;
105 private ModificationApplyOperation applyOper;
108 public void prepare() {
109 schemaContext = TestModel.createTestContext();
110 assertNotNull("Schema context must not be null.", schemaContext);
111 applyOper = SchemaAwareApplyOperation.from(schemaContext);
115 * Returns a test document
132 public NormalizedNode<?, ?> createDocumentOne() {
133 return ImmutableContainerNodeBuilder
135 .withNodeIdentifier(new NodeIdentifier(schemaContext.getQName()))
136 .withChild(createTestContainer()).build();
140 private ContainerNode createTestContainer() {
141 return ImmutableContainerNodeBuilder
143 .withNodeIdentifier(new NodeIdentifier(TEST_QNAME))
145 mapNodeBuilder(OUTER_LIST_QNAME)
146 .withChild(mapEntry(OUTER_LIST_QNAME, ID_QNAME, ONE_ID))
147 .withChild(BAR_NODE).build()).build();
151 public void basicReadWrites() {
152 DataTreeModification modificationTree = new InMemoryDataTreeModification(new InMemoryDataTreeSnapshot(schemaContext,
153 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), 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());
159 // writes node to /outer-list/1/inner_list/two/value
160 modificationTree.write(TWO_TWO_VALUE_PATH, ImmutableNodes.leafNode(VALUE_QNAME, "test"));
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());
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());
175 public DataTreeModification createEmptyModificationTree() {
177 * Creates empty Snapshot with associated schema context.
179 DataTree t = InMemoryDataTreeFactory.getInstance().create();
180 t.setSchemaContext(schemaContext);
184 * Creates Mutable Data Tree based on provided snapshot and schema
188 return t.takeSnapshot().newModification();
192 public void createFromEmptyState() {
194 DataTreeModification modificationTree = createEmptyModificationTree();
196 * Writes empty container node to /test
199 modificationTree.write(TEST_PATH, ImmutableNodes.containerNode(TEST_QNAME));
202 * Writes empty list node to /test/outer-list
204 modificationTree.write(OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(OUTER_LIST_QNAME).build());
207 * Reads list node from /test/outer-list
209 Optional<NormalizedNode<?, ?>> potentialOuterList = modificationTree.readNode(OUTER_LIST_PATH);
210 assertTrue(potentialOuterList.isPresent());
213 * Reads container node from /test and verifies that it contains test
216 Optional<NormalizedNode<?, ?>> potentialTest = modificationTree.readNode(TEST_PATH);
217 ContainerNode containerTest = assertPresentAndType(potentialTest, ContainerNode.class);
221 * Gets list from returned snapshot of /test and verifies it contains
225 assertPresentAndType(containerTest.getChild(new NodeIdentifier(OUTER_LIST_QNAME)), MapNode.class);
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);
238 public void writeSubtreeDeleteChildren() {
239 DataTreeModification modificationTree = createEmptyModificationTree();
240 modificationTree.write(TEST_PATH, createTestContainer());
242 // We verify data are present
243 Optional<NormalizedNode<?, ?>> potentialBeforeDelete = modificationTree.readNode(TWO_TWO_PATH);
244 assertPresentAndType(potentialBeforeDelete, MapEntryNode.class);
246 modificationTree.delete(TWO_TWO_PATH);
247 Optional<NormalizedNode<?, ?>> potentialAfterDelete = modificationTree.readNode(TWO_TWO_PATH);
248 assertFalse(potentialAfterDelete.isPresent());
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());