2 * Copyright (c) 2015 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;
19 import java.util.Optional;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
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.DataTreeConfiguration;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
32 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
33 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
36 * Schema structure of document is
58 public class ModificationMetadataTreeTest extends AbstractTestModelTest {
60 private static final Short ONE_ID = 1;
61 private static final Short TWO_ID = 2;
62 private static final String TWO_ONE_NAME = "one";
63 private static final String TWO_TWO_NAME = "two";
65 private static final YangInstanceIdentifier OUTER_LIST_1_PATH =
66 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
67 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID)
70 private static final YangInstanceIdentifier OUTER_LIST_2_PATH =
71 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
72 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
75 private static final YangInstanceIdentifier TWO_TWO_PATH = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
76 .node(TestModel.INNER_LIST_QNAME)
77 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME)
80 private static final YangInstanceIdentifier TWO_TWO_VALUE_PATH = YangInstanceIdentifier.builder(TWO_TWO_PATH)
81 .node(TestModel.VALUE_QNAME)
84 private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
85 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME)
86 .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_ONE_NAME))
87 .withChild(mapEntry(TestModel.INNER_LIST_QNAME,TestModel.NAME_QNAME, TWO_TWO_NAME))
91 private RootApplyStrategy rootOper;
94 public void prepare() throws ExcludedDataSchemaNodeException {
95 rootOper = RootApplyStrategy.from(SchemaAwareApplyOperation.from(SCHEMA_CONTEXT,
96 DataTreeConfiguration.DEFAULT_OPERATIONAL));
100 * Returns a test document.
114 * @return a test document
116 public NormalizedNode<?, ?> createDocumentOne() {
117 return ImmutableContainerNodeBuilder
119 .withNodeIdentifier(new NodeIdentifier(SCHEMA_CONTEXT.getQName()))
120 .withChild(createTestContainer()).build();
124 private static ContainerNode createTestContainer() {
125 return ImmutableContainerNodeBuilder
127 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
129 mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
130 .withChild(mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
131 .withChild(BAR_NODE).build()).build();
135 public void basicReadWrites() {
136 final DataTreeModification modificationTree = new InMemoryDataTreeModification(
137 new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
138 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper), rootOper);
139 final Optional<NormalizedNode<?, ?>> originalBarNode = modificationTree.readNode(OUTER_LIST_2_PATH);
140 assertTrue(originalBarNode.isPresent());
141 assertSame(BAR_NODE, originalBarNode.get());
143 // writes node to /outer-list/1/inner_list/two/value
144 modificationTree.write(TWO_TWO_VALUE_PATH, ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "test"));
146 // reads node to /outer-list/1/inner_list/two/value
147 // and checks if node is already present
148 final Optional<NormalizedNode<?, ?>> barTwoCModified = modificationTree.readNode(TWO_TWO_VALUE_PATH);
149 assertTrue(barTwoCModified.isPresent());
150 assertEquals(ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "test"), barTwoCModified.get());
152 // delete node to /outer-list/1/inner_list/two/value
153 modificationTree.delete(TWO_TWO_VALUE_PATH);
154 final Optional<NormalizedNode<?, ?>> barTwoCAfterDelete = modificationTree.readNode(TWO_TWO_VALUE_PATH);
155 assertFalse(barTwoCAfterDelete.isPresent());
159 public DataTreeModification createEmptyModificationTree() {
161 * Creates empty Snapshot with associated schema context.
163 final DataTree t = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
168 * Creates Mutable Data Tree based on provided snapshot and schema
172 return t.takeSnapshot().newModification();
176 public void createFromEmptyState() {
178 final DataTreeModification modificationTree = createEmptyModificationTree();
179 // Writes empty container node to /test
180 modificationTree.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
182 // Writes empty list node to /test/outer-list
183 modificationTree.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
186 // Reads list node from /test/outer-list.
187 final Optional<NormalizedNode<?, ?>> potentialOuterList = modificationTree.readNode(TestModel.OUTER_LIST_PATH);
188 assertFalse(potentialOuterList.isPresent());
190 // Reads container node from /test and verifies that it contains test node.
191 final Optional<NormalizedNode<?, ?>> potentialTest = modificationTree.readNode(TestModel.TEST_PATH);
192 assertPresentAndType(potentialTest, ContainerNode.class);
196 public void writeSubtreeReadChildren() {
197 final DataTreeModification modificationTree = createEmptyModificationTree();
198 modificationTree.write(TestModel.TEST_PATH, createTestContainer());
199 final Optional<NormalizedNode<?, ?>> potential = modificationTree.readNode(TWO_TWO_PATH);
200 assertPresentAndType(potential, MapEntryNode.class);
204 public void writeSubtreeDeleteChildren() {
205 final DataTreeModification modificationTree = createEmptyModificationTree();
206 modificationTree.write(TestModel.TEST_PATH, createTestContainer());
208 // We verify data are present
209 final Optional<NormalizedNode<?, ?>> potentialBeforeDelete = modificationTree.readNode(TWO_TWO_PATH);
210 assertPresentAndType(potentialBeforeDelete, MapEntryNode.class);
212 modificationTree.delete(TWO_TWO_PATH);
213 final Optional<NormalizedNode<?, ?>> potentialAfterDelete = modificationTree.readNode(TWO_TWO_PATH);
214 assertFalse(potentialAfterDelete.isPresent());
218 private static <T> T assertPresentAndType(final Optional<?> potential, final Class<T> type) {
219 assertNotNull(potential);
220 assertTrue(potential.isPresent());
221 assertTrue(type.isInstance(potential.get()));
222 return type.cast(potential.get());