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 com.google.common.base.Optional;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
23 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
24 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertFalse;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertSame;
31 import static org.junit.Assert.assertTrue;
32 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntry;
33 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntryBuilder;
34 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapNodeBuilder;
43 * Schema structure of document is
68 public class ModificationMetadataTreeTest {
70 private static final Short ONE_ID = 1;
71 private static final Short TWO_ID = 2;
72 private static final String TWO_ONE_NAME = "one";
73 private static final String TWO_TWO_NAME = "two";
75 private static final InstanceIdentifier OUTER_LIST_1_PATH = InstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
76 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID) //
79 private static final InstanceIdentifier OUTER_LIST_2_PATH = InstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
80 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID) //
83 private static final InstanceIdentifier TWO_TWO_PATH = InstanceIdentifier.builder(OUTER_LIST_2_PATH)
84 .node(TestModel.INNER_LIST_QNAME) //
85 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME) //
88 private static final InstanceIdentifier TWO_TWO_VALUE_PATH = InstanceIdentifier.builder(TWO_TWO_PATH)
89 .node(TestModel.VALUE_QNAME) //
92 private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID) //
93 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME) //
94 .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_ONE_NAME)) //
95 .withChild(mapEntry(TestModel.INNER_LIST_QNAME,TestModel.NAME_QNAME, TWO_TWO_NAME)) //
99 private SchemaContext schemaContext;
100 private ModificationApplyOperation applyOper;
103 public void prepare() {
104 schemaContext = TestModel.createTestContext();
105 assertNotNull("Schema context must not be null.", schemaContext);
106 applyOper = SchemaAwareApplyOperation.from(schemaContext);
110 * Returns a test document
127 public NormalizedNode<?, ?> createDocumentOne() {
128 return ImmutableContainerNodeBuilder
130 .withNodeIdentifier(new NodeIdentifier(schemaContext.getQName()))
131 .withChild(createTestContainer()).build();
135 private ContainerNode createTestContainer() {
136 return ImmutableContainerNodeBuilder
138 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
140 mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
141 .withChild(mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
142 .withChild(BAR_NODE).build()).build();
146 public void basicReadWrites() {
147 DataTreeModification modificationTree = new InMemoryDataTreeModification(new InMemoryDataTreeSnapshot(schemaContext,
148 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), applyOper),
149 new SchemaAwareApplyOperationRoot(schemaContext));
150 Optional<NormalizedNode<?, ?>> originalBarNode = modificationTree.readNode(OUTER_LIST_2_PATH);
151 assertTrue(originalBarNode.isPresent());
152 assertSame(BAR_NODE, originalBarNode.get());
154 // writes node to /outer-list/1/inner_list/two/value
155 modificationTree.write(TWO_TWO_VALUE_PATH, ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "test"));
157 // reads node to /outer-list/1/inner_list/two/value
158 // and checks if node is already present
159 Optional<NormalizedNode<?, ?>> barTwoCModified = modificationTree.readNode(TWO_TWO_VALUE_PATH);
160 assertTrue(barTwoCModified.isPresent());
161 assertEquals(ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "test"), barTwoCModified.get());
163 // delete node to /outer-list/1/inner_list/two/value
164 modificationTree.delete(TWO_TWO_VALUE_PATH);
165 Optional<NormalizedNode<?, ?>> barTwoCAfterDelete = modificationTree.readNode(TWO_TWO_VALUE_PATH);
166 assertFalse(barTwoCAfterDelete.isPresent());
170 public DataTreeModification createEmptyModificationTree() {
172 * Creates empty Snapshot with associated schema context.
174 DataTree t = InMemoryDataTreeFactory.getInstance().create();
175 t.setSchemaContext(schemaContext);
179 * Creates Mutable Data Tree based on provided snapshot and schema
183 return t.takeSnapshot().newModification();
187 public void createFromEmptyState() {
189 DataTreeModification modificationTree = createEmptyModificationTree();
191 * Writes empty container node to /test
194 modificationTree.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
197 * Writes empty list node to /test/outer-list
199 modificationTree.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
202 * Reads list node from /test/outer-list
204 Optional<NormalizedNode<?, ?>> potentialOuterList = modificationTree.readNode(TestModel.OUTER_LIST_PATH);
205 assertTrue(potentialOuterList.isPresent());
208 * Reads container node from /test and verifies that it contains test
211 Optional<NormalizedNode<?, ?>> potentialTest = modificationTree.readNode(TestModel.TEST_PATH);
212 ContainerNode containerTest = assertPresentAndType(potentialTest, ContainerNode.class);
216 * Gets list from returned snapshot of /test and verifies it contains
220 assertPresentAndType(containerTest.getChild(new NodeIdentifier(TestModel.OUTER_LIST_QNAME)), MapNode.class);
225 public void writeSubtreeReadChildren() {
226 DataTreeModification modificationTree = createEmptyModificationTree();
227 modificationTree.write(TestModel.TEST_PATH, createTestContainer());
228 Optional<NormalizedNode<?, ?>> potential = modificationTree.readNode(TWO_TWO_PATH);
229 assertPresentAndType(potential, MapEntryNode.class);
233 public void writeSubtreeDeleteChildren() {
234 DataTreeModification modificationTree = createEmptyModificationTree();
235 modificationTree.write(TestModel.TEST_PATH, createTestContainer());
237 // We verify data are present
238 Optional<NormalizedNode<?, ?>> potentialBeforeDelete = modificationTree.readNode(TWO_TWO_PATH);
239 assertPresentAndType(potentialBeforeDelete, MapEntryNode.class);
241 modificationTree.delete(TWO_TWO_PATH);
242 Optional<NormalizedNode<?, ?>> potentialAfterDelete = modificationTree.readNode(TWO_TWO_PATH);
243 assertFalse(potentialAfterDelete.isPresent());
247 private static <T> T assertPresentAndType(final Optional<?> potential, final Class<T> type) {
248 assertNotNull(potential);
249 assertTrue(potential.isPresent());
250 assertTrue(type.isInstance(potential.get()));
251 return type.cast(potential.get());