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 com.google.common.base.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.DataTreeModification;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
34 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
35 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
41 * Schema structure of document is
66 public class ModificationMetadataTreeTest {
68 private static final Short ONE_ID = 1;
69 private static final Short TWO_ID = 2;
70 private static final String TWO_ONE_NAME = "one";
71 private static final String TWO_TWO_NAME = "two";
73 private static final YangInstanceIdentifier OUTER_LIST_1_PATH = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
74 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID) //
77 private static final YangInstanceIdentifier OUTER_LIST_2_PATH = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
78 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID) //
81 private static final YangInstanceIdentifier TWO_TWO_PATH = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
82 .node(TestModel.INNER_LIST_QNAME) //
83 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME) //
86 private static final YangInstanceIdentifier TWO_TWO_VALUE_PATH = YangInstanceIdentifier.builder(TWO_TWO_PATH)
87 .node(TestModel.VALUE_QNAME) //
90 private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID) //
91 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME) //
92 .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_ONE_NAME)) //
93 .withChild(mapEntry(TestModel.INNER_LIST_QNAME,TestModel.NAME_QNAME, TWO_TWO_NAME)) //
97 private SchemaContext schemaContext;
98 private RootModificationApplyOperation rootOper;
101 public void prepare() throws ReactorException {
102 schemaContext = TestModel.createTestContext();
103 assertNotNull("Schema context must not be null.", schemaContext);
104 rootOper = RootModificationApplyOperation.from(SchemaAwareApplyOperation.from(schemaContext, DataTreeConfiguration.DEFAULT_OPERATIONAL));
108 * Returns a test document
125 public NormalizedNode<?, ?> createDocumentOne() {
126 return ImmutableContainerNodeBuilder
128 .withNodeIdentifier(new NodeIdentifier(schemaContext.getQName()))
129 .withChild(createTestContainer()).build();
133 private static ContainerNode createTestContainer() {
134 return ImmutableContainerNodeBuilder
136 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
138 mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
139 .withChild(mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
140 .withChild(BAR_NODE).build()).build();
144 public void basicReadWrites() {
145 final DataTreeModification modificationTree = new InMemoryDataTreeModification(new InMemoryDataTreeSnapshot(schemaContext,
146 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper),
148 final Optional<NormalizedNode<?, ?>> originalBarNode = modificationTree.readNode(OUTER_LIST_2_PATH);
149 assertTrue(originalBarNode.isPresent());
150 assertSame(BAR_NODE, originalBarNode.get());
152 // writes node to /outer-list/1/inner_list/two/value
153 modificationTree.write(TWO_TWO_VALUE_PATH, ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "test"));
155 // reads node to /outer-list/1/inner_list/two/value
156 // and checks if node is already present
157 final Optional<NormalizedNode<?, ?>> barTwoCModified = modificationTree.readNode(TWO_TWO_VALUE_PATH);
158 assertTrue(barTwoCModified.isPresent());
159 assertEquals(ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "test"), barTwoCModified.get());
161 // delete node to /outer-list/1/inner_list/two/value
162 modificationTree.delete(TWO_TWO_VALUE_PATH);
163 final Optional<NormalizedNode<?, ?>> barTwoCAfterDelete = modificationTree.readNode(TWO_TWO_VALUE_PATH);
164 assertFalse(barTwoCAfterDelete.isPresent());
168 public DataTreeModification createEmptyModificationTree() {
170 * Creates empty Snapshot with associated schema context.
172 final DataTree t = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
173 t.setSchemaContext(schemaContext);
177 * Creates Mutable Data Tree based on provided snapshot and schema
181 return t.takeSnapshot().newModification();
185 public void createFromEmptyState() {
187 final DataTreeModification modificationTree = createEmptyModificationTree();
189 * Writes empty container node to /test
192 modificationTree.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
195 * Writes empty list node to /test/outer-list
197 modificationTree.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
200 * Reads list node from /test/outer-list
202 final Optional<NormalizedNode<?, ?>> potentialOuterList = modificationTree.readNode(TestModel.OUTER_LIST_PATH);
203 assertTrue(potentialOuterList.isPresent());
206 * Reads container node from /test and verifies that it contains test
209 final Optional<NormalizedNode<?, ?>> potentialTest = modificationTree.readNode(TestModel.TEST_PATH);
210 final ContainerNode containerTest = assertPresentAndType(potentialTest, ContainerNode.class);
214 * Gets list from returned snapshot of /test and verifies it contains
218 assertPresentAndType(containerTest.getChild(new NodeIdentifier(TestModel.OUTER_LIST_QNAME)), MapNode.class);
223 public void writeSubtreeReadChildren() {
224 final DataTreeModification modificationTree = createEmptyModificationTree();
225 modificationTree.write(TestModel.TEST_PATH, createTestContainer());
226 final Optional<NormalizedNode<?, ?>> potential = modificationTree.readNode(TWO_TWO_PATH);
227 assertPresentAndType(potential, MapEntryNode.class);
231 public void writeSubtreeDeleteChildren() {
232 final DataTreeModification modificationTree = createEmptyModificationTree();
233 modificationTree.write(TestModel.TEST_PATH, createTestContainer());
235 // We verify data are present
236 final Optional<NormalizedNode<?, ?>> potentialBeforeDelete = modificationTree.readNode(TWO_TWO_PATH);
237 assertPresentAndType(potentialBeforeDelete, MapEntryNode.class);
239 modificationTree.delete(TWO_TWO_PATH);
240 final Optional<NormalizedNode<?, ?>> potentialAfterDelete = modificationTree.readNode(TWO_TWO_PATH);
241 assertFalse(potentialAfterDelete.isPresent());
245 private static <T> T assertPresentAndType(final Optional<?> potential, final Class<T> type) {
246 assertNotNull(potential);
247 assertTrue(potential.isPresent());
248 assertTrue(type.isInstance(potential.get()));
249 return type.cast(potential.get());