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.MapNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
33 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
34 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
39 * Schema structure of document is
61 public class ModificationMetadataTreeTest {
63 private static final Short ONE_ID = 1;
64 private static final Short TWO_ID = 2;
65 private static final String TWO_ONE_NAME = "one";
66 private static final String TWO_TWO_NAME = "two";
68 private static final YangInstanceIdentifier OUTER_LIST_1_PATH =
69 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 =
74 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
75 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
78 private static final YangInstanceIdentifier TWO_TWO_PATH = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
79 .node(TestModel.INNER_LIST_QNAME)
80 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME)
83 private static final YangInstanceIdentifier TWO_TWO_VALUE_PATH = YangInstanceIdentifier.builder(TWO_TWO_PATH)
84 .node(TestModel.VALUE_QNAME)
87 private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
88 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME)
89 .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_ONE_NAME))
90 .withChild(mapEntry(TestModel.INNER_LIST_QNAME,TestModel.NAME_QNAME, TWO_TWO_NAME))
94 private SchemaContext schemaContext;
95 private RootModificationApplyOperation rootOper;
98 public void prepare() throws ReactorException {
99 schemaContext = TestModel.createTestContext();
100 assertNotNull("Schema context must not be null.", schemaContext);
101 rootOper = RootModificationApplyOperation.from(SchemaAwareApplyOperation.from(schemaContext,
102 DataTreeConfiguration.DEFAULT_OPERATIONAL));
106 * Returns a test document.
120 * @return a test document
122 public NormalizedNode<?, ?> createDocumentOne() {
123 return ImmutableContainerNodeBuilder
125 .withNodeIdentifier(new NodeIdentifier(schemaContext.getQName()))
126 .withChild(createTestContainer()).build();
130 private static ContainerNode createTestContainer() {
131 return ImmutableContainerNodeBuilder
133 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
135 mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
136 .withChild(mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
137 .withChild(BAR_NODE).build()).build();
141 public void basicReadWrites() {
142 final DataTreeModification modificationTree = new InMemoryDataTreeModification(
143 new InMemoryDataTreeSnapshot(schemaContext,
144 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper), rootOper);
145 final Optional<NormalizedNode<?, ?>> originalBarNode = modificationTree.readNode(OUTER_LIST_2_PATH);
146 assertTrue(originalBarNode.isPresent());
147 assertSame(BAR_NODE, originalBarNode.get());
149 // writes node to /outer-list/1/inner_list/two/value
150 modificationTree.write(TWO_TWO_VALUE_PATH, ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "test"));
152 // reads node to /outer-list/1/inner_list/two/value
153 // and checks if node is already present
154 final Optional<NormalizedNode<?, ?>> barTwoCModified = modificationTree.readNode(TWO_TWO_VALUE_PATH);
155 assertTrue(barTwoCModified.isPresent());
156 assertEquals(ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "test"), barTwoCModified.get());
158 // delete node to /outer-list/1/inner_list/two/value
159 modificationTree.delete(TWO_TWO_VALUE_PATH);
160 final Optional<NormalizedNode<?, ?>> barTwoCAfterDelete = modificationTree.readNode(TWO_TWO_VALUE_PATH);
161 assertFalse(barTwoCAfterDelete.isPresent());
165 public DataTreeModification createEmptyModificationTree() {
167 * Creates empty Snapshot with associated schema context.
169 final DataTree t = InMemoryDataTreeFactory.getInstance().create(DataTreeConfiguration.DEFAULT_OPERATIONAL);
170 t.setSchemaContext(schemaContext);
174 * Creates Mutable Data Tree based on provided snapshot and schema
178 return t.takeSnapshot().newModification();
182 public void createFromEmptyState() {
184 final DataTreeModification modificationTree = createEmptyModificationTree();
185 // Writes empty container node to /test
186 modificationTree.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
188 // Writes empty list node to /test/outer-list
189 modificationTree.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
192 // Reads list node from /test/outer-list.
193 final Optional<NormalizedNode<?, ?>> potentialOuterList = modificationTree.readNode(TestModel.OUTER_LIST_PATH);
194 assertTrue(potentialOuterList.isPresent());
196 // Reads container node from /test and verifies that it contains test node.
197 final Optional<NormalizedNode<?, ?>> potentialTest = modificationTree.readNode(TestModel.TEST_PATH);
198 final ContainerNode containerTest = assertPresentAndType(potentialTest, ContainerNode.class);
202 * Gets list from returned snapshot of /test and verifies it contains
206 assertPresentAndType(containerTest.getChild(new NodeIdentifier(TestModel.OUTER_LIST_QNAME)), MapNode.class);
210 public void writeSubtreeReadChildren() {
211 final DataTreeModification modificationTree = createEmptyModificationTree();
212 modificationTree.write(TestModel.TEST_PATH, createTestContainer());
213 final Optional<NormalizedNode<?, ?>> potential = modificationTree.readNode(TWO_TWO_PATH);
214 assertPresentAndType(potential, MapEntryNode.class);
218 public void writeSubtreeDeleteChildren() {
219 final DataTreeModification modificationTree = createEmptyModificationTree();
220 modificationTree.write(TestModel.TEST_PATH, createTestContainer());
222 // We verify data are present
223 final Optional<NormalizedNode<?, ?>> potentialBeforeDelete = modificationTree.readNode(TWO_TWO_PATH);
224 assertPresentAndType(potentialBeforeDelete, MapEntryNode.class);
226 modificationTree.delete(TWO_TWO_PATH);
227 final Optional<NormalizedNode<?, ?>> potentialAfterDelete = modificationTree.readNode(TWO_TWO_PATH);
228 assertFalse(potentialAfterDelete.isPresent());
232 private static <T> T assertPresentAndType(final Optional<?> potential, final Class<T> type) {
233 assertNotNull(potential);
234 assertTrue(potential.isPresent());
235 assertTrue(type.isInstance(potential.get()));
236 return type.cast(potential.get());