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.impl.schema.ImmutableNodes;
31 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
32 import org.opendaylight.yangtools.yang.data.spi.tree.TreeNodeFactory;
33 import org.opendaylight.yangtools.yang.data.spi.tree.Version;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 * Schema structure of document is
59 public class ModificationMetadataTreeTest extends AbstractTestModelTest {
61 private static final Short ONE_ID = 1;
62 private static final Short TWO_ID = 2;
63 private static final String TWO_ONE_NAME = "one";
64 private static final String TWO_TWO_NAME = "two";
66 private static final YangInstanceIdentifier OUTER_LIST_1_PATH =
67 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
68 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID)
71 private static final YangInstanceIdentifier OUTER_LIST_2_PATH =
72 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
73 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
76 private static final YangInstanceIdentifier TWO_TWO_PATH = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
77 .node(TestModel.INNER_LIST_QNAME)
78 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME)
81 private static final YangInstanceIdentifier TWO_TWO_VALUE_PATH = YangInstanceIdentifier.builder(TWO_TWO_PATH)
82 .node(TestModel.VALUE_QNAME)
85 private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
86 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME)
87 .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_ONE_NAME))
88 .withChild(mapEntry(TestModel.INNER_LIST_QNAME,TestModel.NAME_QNAME, TWO_TWO_NAME))
92 private RootApplyStrategy rootOper;
95 public void prepare() throws ExcludedDataSchemaNodeException {
96 rootOper = RootApplyStrategy.from(SchemaAwareApplyOperation.from(SCHEMA_CONTEXT,
97 DataTreeConfiguration.DEFAULT_OPERATIONAL));
101 * Returns a test document.
115 * @return a test document
117 public NormalizedNode createDocumentOne() {
118 return ImmutableContainerNodeBuilder
120 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME))
121 .withChild(createTestContainer()).build();
125 private static ContainerNode createTestContainer() {
126 return ImmutableContainerNodeBuilder
128 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
130 mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
131 .withChild(mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
132 .withChild(BAR_NODE).build()).build();
136 public void basicReadWrites() {
137 final DataTreeModification modificationTree = new InMemoryDataTreeModification(
138 new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
139 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper), rootOper);
140 final Optional<NormalizedNode> originalBarNode = modificationTree.readNode(OUTER_LIST_2_PATH);
141 assertTrue(originalBarNode.isPresent());
142 assertSame(BAR_NODE, originalBarNode.get());
144 // writes node to /outer-list/1/inner_list/two/value
145 modificationTree.write(TWO_TWO_VALUE_PATH, ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "test"));
147 // reads node to /outer-list/1/inner_list/two/value
148 // and checks if node is already present
149 final Optional<NormalizedNode> barTwoCModified = modificationTree.readNode(TWO_TWO_VALUE_PATH);
150 assertTrue(barTwoCModified.isPresent());
151 assertEquals(ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "test"), barTwoCModified.get());
153 // delete node to /outer-list/1/inner_list/two/value
154 modificationTree.delete(TWO_TWO_VALUE_PATH);
155 final Optional<NormalizedNode> barTwoCAfterDelete = modificationTree.readNode(TWO_TWO_VALUE_PATH);
156 assertFalse(barTwoCAfterDelete.isPresent());
160 public DataTreeModification createEmptyModificationTree() {
162 * Creates empty Snapshot with associated schema context.
164 final DataTree t = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
169 * Creates Mutable Data Tree based on provided snapshot and schema
173 return t.takeSnapshot().newModification();
177 public void createFromEmptyState() {
179 final DataTreeModification modificationTree = createEmptyModificationTree();
180 // Writes empty container node to /test
181 modificationTree.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
183 // Writes empty list node to /test/outer-list
184 modificationTree.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
187 // Reads list node from /test/outer-list.
188 final Optional<NormalizedNode> potentialOuterList = modificationTree.readNode(TestModel.OUTER_LIST_PATH);
189 assertFalse(potentialOuterList.isPresent());
191 // Reads container node from /test and verifies that it contains test node.
192 final Optional<NormalizedNode> potentialTest = modificationTree.readNode(TestModel.TEST_PATH);
193 assertPresentAndType(potentialTest, ContainerNode.class);
197 public void writeSubtreeReadChildren() {
198 final DataTreeModification modificationTree = createEmptyModificationTree();
199 modificationTree.write(TestModel.TEST_PATH, createTestContainer());
200 final Optional<NormalizedNode> potential = modificationTree.readNode(TWO_TWO_PATH);
201 assertPresentAndType(potential, MapEntryNode.class);
205 public void writeSubtreeDeleteChildren() {
206 final DataTreeModification modificationTree = createEmptyModificationTree();
207 modificationTree.write(TestModel.TEST_PATH, createTestContainer());
209 // We verify data are present
210 final Optional<NormalizedNode> potentialBeforeDelete = modificationTree.readNode(TWO_TWO_PATH);
211 assertPresentAndType(potentialBeforeDelete, MapEntryNode.class);
213 modificationTree.delete(TWO_TWO_PATH);
214 final Optional<NormalizedNode> potentialAfterDelete = modificationTree.readNode(TWO_TWO_PATH);
215 assertFalse(potentialAfterDelete.isPresent());
219 private static <T> T assertPresentAndType(final Optional<?> potential, final Class<T> type) {
220 assertNotNull(potential);
221 assertTrue(potential.isPresent());
222 assertTrue(type.isInstance(potential.get()));
223 return type.cast(potential.get());