2 * Copyright (c) 2014 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 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.spi.TreeNodeFactory;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
31 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import com.google.common.base.Optional;
44 * Schema structure of document is
69 public class ModificationMetadataTreeTest {
71 private static final Short ONE_ID = 1;
72 private static final Short TWO_ID = 2;
73 private static final String TWO_ONE_NAME = "one";
74 private static final String TWO_TWO_NAME = "two";
76 private static final YangInstanceIdentifier OUTER_LIST_1_PATH = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
77 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID) //
80 private static final YangInstanceIdentifier OUTER_LIST_2_PATH = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
81 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID) //
84 private static final YangInstanceIdentifier TWO_TWO_PATH = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
85 .node(TestModel.INNER_LIST_QNAME) //
86 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME) //
89 private static final YangInstanceIdentifier TWO_TWO_VALUE_PATH = YangInstanceIdentifier.builder(TWO_TWO_PATH)
90 .node(TestModel.VALUE_QNAME) //
93 private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID) //
94 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME) //
95 .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_ONE_NAME)) //
96 .withChild(mapEntry(TestModel.INNER_LIST_QNAME,TestModel.NAME_QNAME, TWO_TWO_NAME)) //
100 private SchemaContext schemaContext;
101 private RootModificationApplyOperation rootOper;
104 public void prepare() {
105 schemaContext = TestModel.createTestContext();
106 assertNotNull("Schema context must not be null.", schemaContext);
107 rootOper = RootModificationApplyOperation.from(SchemaAwareApplyOperation.from(schemaContext));
111 * Returns a test document
128 public NormalizedNode<?, ?> createDocumentOne() {
129 return ImmutableContainerNodeBuilder
131 .withNodeIdentifier(new NodeIdentifier(schemaContext.getQName()))
132 .withChild(createTestContainer()).build();
136 private ContainerNode createTestContainer() {
137 return ImmutableContainerNodeBuilder
139 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
141 mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
142 .withChild(mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
143 .withChild(BAR_NODE).build()).build();
147 public void basicReadWrites() {
148 DataTreeModification modificationTree = new InMemoryDataTreeModification(new InMemoryDataTreeSnapshot(schemaContext,
149 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper),
151 Optional<NormalizedNode<?, ?>> originalBarNode = modificationTree.readNode(OUTER_LIST_2_PATH);
152 assertTrue(originalBarNode.isPresent());
153 assertSame(BAR_NODE, originalBarNode.get());
155 // writes node to /outer-list/1/inner_list/two/value
156 modificationTree.write(TWO_TWO_VALUE_PATH, ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "test"));
158 // reads node to /outer-list/1/inner_list/two/value
159 // and checks if node is already present
160 Optional<NormalizedNode<?, ?>> barTwoCModified = modificationTree.readNode(TWO_TWO_VALUE_PATH);
161 assertTrue(barTwoCModified.isPresent());
162 assertEquals(ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "test"), barTwoCModified.get());
164 // delete node to /outer-list/1/inner_list/two/value
165 modificationTree.delete(TWO_TWO_VALUE_PATH);
166 Optional<NormalizedNode<?, ?>> barTwoCAfterDelete = modificationTree.readNode(TWO_TWO_VALUE_PATH);
167 assertFalse(barTwoCAfterDelete.isPresent());
171 public DataTreeModification createEmptyModificationTree() {
173 * Creates empty Snapshot with associated schema context.
175 DataTree t = InMemoryDataTreeFactory.getInstance().create();
176 t.setSchemaContext(schemaContext);
180 * Creates Mutable Data Tree based on provided snapshot and schema
184 return t.takeSnapshot().newModification();
188 public void createFromEmptyState() {
190 DataTreeModification modificationTree = createEmptyModificationTree();
192 * Writes empty container node to /test
195 modificationTree.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
198 * Writes empty list node to /test/outer-list
200 modificationTree.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
203 * Reads list node from /test/outer-list
205 Optional<NormalizedNode<?, ?>> potentialOuterList = modificationTree.readNode(TestModel.OUTER_LIST_PATH);
206 assertTrue(potentialOuterList.isPresent());
209 * Reads container node from /test and verifies that it contains test
212 Optional<NormalizedNode<?, ?>> potentialTest = modificationTree.readNode(TestModel.TEST_PATH);
213 ContainerNode containerTest = assertPresentAndType(potentialTest, ContainerNode.class);
217 * Gets list from returned snapshot of /test and verifies it contains
221 assertPresentAndType(containerTest.getChild(new NodeIdentifier(TestModel.OUTER_LIST_QNAME)), MapNode.class);
226 public void writeSubtreeReadChildren() {
227 DataTreeModification modificationTree = createEmptyModificationTree();
228 modificationTree.write(TestModel.TEST_PATH, createTestContainer());
229 Optional<NormalizedNode<?, ?>> potential = modificationTree.readNode(TWO_TWO_PATH);
230 assertPresentAndType(potential, MapEntryNode.class);
234 public void writeSubtreeDeleteChildren() {
235 DataTreeModification modificationTree = createEmptyModificationTree();
236 modificationTree.write(TestModel.TEST_PATH, createTestContainer());
238 // We verify data are present
239 Optional<NormalizedNode<?, ?>> potentialBeforeDelete = modificationTree.readNode(TWO_TWO_PATH);
240 assertPresentAndType(potentialBeforeDelete, MapEntryNode.class);
242 modificationTree.delete(TWO_TWO_PATH);
243 Optional<NormalizedNode<?, ?>> potentialAfterDelete = modificationTree.readNode(TWO_TWO_PATH);
244 assertFalse(potentialAfterDelete.isPresent());
248 private static <T> T assertPresentAndType(final Optional<?> potential, final Class<T> type) {
249 assertNotNull(potential);
250 assertTrue(potential.isPresent());
251 assertTrue(type.isInstance(potential.get()));
252 return type.cast(potential.get());