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.yangtools.yang.data.impl.schema.tree;
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.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntry;
16 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntryBuilder;
17 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapNodeBuilder;
18 import com.google.common.base.Optional;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
31 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 * Schema structure of document is
62 public class ModificationMetadataTreeTest {
64 private static final Short ONE_ID = 1;
65 private static final Short TWO_ID = 2;
66 private static final String TWO_ONE_NAME = "one";
67 private static final String TWO_TWO_NAME = "two";
69 private static final YangInstanceIdentifier OUTER_LIST_1_PATH = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
70 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID) //
73 private static final YangInstanceIdentifier OUTER_LIST_2_PATH = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
74 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID) //
77 private static final YangInstanceIdentifier TWO_TWO_PATH = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
78 .node(TestModel.INNER_LIST_QNAME) //
79 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME) //
82 private static final YangInstanceIdentifier TWO_TWO_VALUE_PATH = YangInstanceIdentifier.builder(TWO_TWO_PATH)
83 .node(TestModel.VALUE_QNAME) //
86 private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID) //
87 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME) //
88 .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_ONE_NAME)) //
89 .withChild(mapEntry(TestModel.INNER_LIST_QNAME,TestModel.NAME_QNAME, TWO_TWO_NAME)) //
93 private SchemaContext schemaContext;
94 private RootModificationApplyOperation rootOper;
97 public void prepare() {
98 schemaContext = TestModel.createTestContext();
99 assertNotNull("Schema context must not be null.", schemaContext);
100 rootOper = RootModificationApplyOperation.from(SchemaAwareApplyOperation.from(schemaContext));
104 * Returns a test document
121 public NormalizedNode<?, ?> createDocumentOne() {
122 return ImmutableContainerNodeBuilder
124 .withNodeIdentifier(new NodeIdentifier(schemaContext.getQName()))
125 .withChild(createTestContainer()).build();
129 private static ContainerNode createTestContainer() {
130 return ImmutableContainerNodeBuilder
132 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
134 mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
135 .withChild(mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
136 .withChild(BAR_NODE).build()).build();
140 public void basicReadWrites() {
141 DataTreeModification modificationTree = new InMemoryDataTreeModification(new InMemoryDataTreeSnapshot(schemaContext,
142 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper),
144 Optional<NormalizedNode<?, ?>> originalBarNode = modificationTree.readNode(OUTER_LIST_2_PATH);
145 assertTrue(originalBarNode.isPresent());
146 assertSame(BAR_NODE, originalBarNode.get());
148 // writes node to /outer-list/1/inner_list/two/value
149 modificationTree.write(TWO_TWO_VALUE_PATH, ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "test"));
151 // reads node to /outer-list/1/inner_list/two/value
152 // and checks if node is already present
153 Optional<NormalizedNode<?, ?>> barTwoCModified = modificationTree.readNode(TWO_TWO_VALUE_PATH);
154 assertTrue(barTwoCModified.isPresent());
155 assertEquals(ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "test"), barTwoCModified.get());
157 // delete node to /outer-list/1/inner_list/two/value
158 modificationTree.delete(TWO_TWO_VALUE_PATH);
159 Optional<NormalizedNode<?, ?>> barTwoCAfterDelete = modificationTree.readNode(TWO_TWO_VALUE_PATH);
160 assertFalse(barTwoCAfterDelete.isPresent());
164 public DataTreeModification createEmptyModificationTree() {
166 * Creates empty Snapshot with associated schema context.
168 DataTree t = InMemoryDataTreeFactory.getInstance().create();
169 t.setSchemaContext(schemaContext);
173 * Creates Mutable Data Tree based on provided snapshot and schema
177 return t.takeSnapshot().newModification();
181 public void createFromEmptyState() {
183 DataTreeModification modificationTree = createEmptyModificationTree();
185 * Writes empty container node to /test
188 modificationTree.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
191 * Writes empty list node to /test/outer-list
193 modificationTree.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
196 * Reads list node from /test/outer-list
198 Optional<NormalizedNode<?, ?>> potentialOuterList = modificationTree.readNode(TestModel.OUTER_LIST_PATH);
199 assertTrue(potentialOuterList.isPresent());
202 * Reads container node from /test and verifies that it contains test
205 Optional<NormalizedNode<?, ?>> potentialTest = modificationTree.readNode(TestModel.TEST_PATH);
206 ContainerNode containerTest = assertPresentAndType(potentialTest, ContainerNode.class);
210 * Gets list from returned snapshot of /test and verifies it contains
214 assertPresentAndType(containerTest.getChild(new NodeIdentifier(TestModel.OUTER_LIST_QNAME)), MapNode.class);
219 public void writeSubtreeReadChildren() {
220 DataTreeModification modificationTree = createEmptyModificationTree();
221 modificationTree.write(TestModel.TEST_PATH, createTestContainer());
222 Optional<NormalizedNode<?, ?>> potential = modificationTree.readNode(TWO_TWO_PATH);
223 assertPresentAndType(potential, MapEntryNode.class);
227 public void writeSubtreeDeleteChildren() {
228 DataTreeModification modificationTree = createEmptyModificationTree();
229 modificationTree.write(TestModel.TEST_PATH, createTestContainer());
231 // We verify data are present
232 Optional<NormalizedNode<?, ?>> potentialBeforeDelete = modificationTree.readNode(TWO_TWO_PATH);
233 assertPresentAndType(potentialBeforeDelete, MapEntryNode.class);
235 modificationTree.delete(TWO_TWO_PATH);
236 Optional<NormalizedNode<?, ?>> potentialAfterDelete = modificationTree.readNode(TWO_TWO_PATH);
237 assertFalse(potentialAfterDelete.isPresent());
241 private static <T> T assertPresentAndType(final Optional<?> potential, final Class<T> type) {
242 assertNotNull(potential);
243 assertTrue(potential.isPresent());
244 assertTrue(type.isInstance(potential.get()));
245 return type.cast(potential.get());