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;
18 import com.google.common.base.Optional;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
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.DataTreeModification;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
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;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
39 * Schema structure of document is
64 public class ModificationMetadataTreeTest {
66 private static final Short ONE_ID = 1;
67 private static final Short TWO_ID = 2;
68 private static final String TWO_ONE_NAME = "one";
69 private static final String TWO_TWO_NAME = "two";
71 private static final YangInstanceIdentifier OUTER_LIST_1_PATH = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
72 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID) //
75 private static final YangInstanceIdentifier OUTER_LIST_2_PATH = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
76 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID) //
79 private static final YangInstanceIdentifier TWO_TWO_PATH = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
80 .node(TestModel.INNER_LIST_QNAME) //
81 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME) //
84 private static final YangInstanceIdentifier TWO_TWO_VALUE_PATH = YangInstanceIdentifier.builder(TWO_TWO_PATH)
85 .node(TestModel.VALUE_QNAME) //
88 private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID) //
89 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME) //
90 .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_ONE_NAME)) //
91 .withChild(mapEntry(TestModel.INNER_LIST_QNAME,TestModel.NAME_QNAME, TWO_TWO_NAME)) //
95 private SchemaContext schemaContext;
96 private RootModificationApplyOperation rootOper;
99 public void prepare() throws ReactorException {
100 schemaContext = TestModel.createTestContext();
101 assertNotNull("Schema context must not be null.", schemaContext);
102 rootOper = RootModificationApplyOperation.from(SchemaAwareApplyOperation.from(schemaContext, TreeType.OPERATIONAL));
106 * Returns a test document
123 public NormalizedNode<?, ?> createDocumentOne() {
124 return ImmutableContainerNodeBuilder
126 .withNodeIdentifier(new NodeIdentifier(schemaContext.getQName()))
127 .withChild(createTestContainer()).build();
131 private static ContainerNode createTestContainer() {
132 return ImmutableContainerNodeBuilder
134 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
136 mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
137 .withChild(mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
138 .withChild(BAR_NODE).build()).build();
142 public void basicReadWrites() {
143 final DataTreeModification modificationTree = new InMemoryDataTreeModification(new InMemoryDataTreeSnapshot(schemaContext,
144 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper),
146 final Optional<NormalizedNode<?, ?>> originalBarNode = modificationTree.readNode(OUTER_LIST_2_PATH);
147 assertTrue(originalBarNode.isPresent());
148 assertSame(BAR_NODE, originalBarNode.get());
150 // writes node to /outer-list/1/inner_list/two/value
151 modificationTree.write(TWO_TWO_VALUE_PATH, ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "test"));
153 // reads node to /outer-list/1/inner_list/two/value
154 // and checks if node is already present
155 final Optional<NormalizedNode<?, ?>> barTwoCModified = modificationTree.readNode(TWO_TWO_VALUE_PATH);
156 assertTrue(barTwoCModified.isPresent());
157 assertEquals(ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "test"), barTwoCModified.get());
159 // delete node to /outer-list/1/inner_list/two/value
160 modificationTree.delete(TWO_TWO_VALUE_PATH);
161 final Optional<NormalizedNode<?, ?>> barTwoCAfterDelete = modificationTree.readNode(TWO_TWO_VALUE_PATH);
162 assertFalse(barTwoCAfterDelete.isPresent());
166 public DataTreeModification createEmptyModificationTree() {
168 * Creates empty Snapshot with associated schema context.
170 final DataTree t = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
171 t.setSchemaContext(schemaContext);
175 * Creates Mutable Data Tree based on provided snapshot and schema
179 return t.takeSnapshot().newModification();
183 public void createFromEmptyState() {
185 final DataTreeModification modificationTree = createEmptyModificationTree();
187 * Writes empty container node to /test
190 modificationTree.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
193 * Writes empty list node to /test/outer-list
195 modificationTree.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
198 * Reads list node from /test/outer-list
200 final Optional<NormalizedNode<?, ?>> potentialOuterList = modificationTree.readNode(TestModel.OUTER_LIST_PATH);
201 assertTrue(potentialOuterList.isPresent());
204 * Reads container node from /test and verifies that it contains test
207 final Optional<NormalizedNode<?, ?>> potentialTest = modificationTree.readNode(TestModel.TEST_PATH);
208 final ContainerNode containerTest = assertPresentAndType(potentialTest, ContainerNode.class);
212 * Gets list from returned snapshot of /test and verifies it contains
216 assertPresentAndType(containerTest.getChild(new NodeIdentifier(TestModel.OUTER_LIST_QNAME)), MapNode.class);
221 public void writeSubtreeReadChildren() {
222 final DataTreeModification modificationTree = createEmptyModificationTree();
223 modificationTree.write(TestModel.TEST_PATH, createTestContainer());
224 final Optional<NormalizedNode<?, ?>> potential = modificationTree.readNode(TWO_TWO_PATH);
225 assertPresentAndType(potential, MapEntryNode.class);
229 public void writeSubtreeDeleteChildren() {
230 final DataTreeModification modificationTree = createEmptyModificationTree();
231 modificationTree.write(TestModel.TEST_PATH, createTestContainer());
233 // We verify data are present
234 final Optional<NormalizedNode<?, ?>> potentialBeforeDelete = modificationTree.readNode(TWO_TWO_PATH);
235 assertPresentAndType(potentialBeforeDelete, MapEntryNode.class);
237 modificationTree.delete(TWO_TWO_PATH);
238 final Optional<NormalizedNode<?, ?>> potentialAfterDelete = modificationTree.readNode(TWO_TWO_PATH);
239 assertFalse(potentialAfterDelete.isPresent());
243 private static <T> T assertPresentAndType(final Optional<?> potential, final Class<T> type) {
244 assertNotNull(potential);
245 assertTrue(potential.isPresent());
246 assertTrue(type.isInstance(potential.get()));
247 return type.cast(potential.get());