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;
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;
38 * Schema structure of document is
63 public class ModificationMetadataTreeTest {
65 private static final Short ONE_ID = 1;
66 private static final Short TWO_ID = 2;
67 private static final String TWO_ONE_NAME = "one";
68 private static final String TWO_TWO_NAME = "two";
70 private static final YangInstanceIdentifier OUTER_LIST_1_PATH = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
71 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID) //
74 private static final YangInstanceIdentifier OUTER_LIST_2_PATH = 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() {
99 schemaContext = TestModel.createTestContext();
100 assertNotNull("Schema context must not be null.", schemaContext);
101 rootOper = RootModificationApplyOperation.from(SchemaAwareApplyOperation.from(schemaContext, TreeType.OPERATIONAL));
105 * Returns 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(new InMemoryDataTreeSnapshot(schemaContext,
143 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), 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(TreeType.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();
186 * Writes empty container node to /test
189 modificationTree.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
192 * Writes empty list node to /test/outer-list
194 modificationTree.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
197 * Reads list node from /test/outer-list
199 final Optional<NormalizedNode<?, ?>> potentialOuterList = modificationTree.readNode(TestModel.OUTER_LIST_PATH);
200 assertTrue(potentialOuterList.isPresent());
203 * Reads container node from /test and verifies that it contains test
206 final Optional<NormalizedNode<?, ?>> potentialTest = modificationTree.readNode(TestModel.TEST_PATH);
207 final ContainerNode containerTest = assertPresentAndType(potentialTest, ContainerNode.class);
211 * Gets list from returned snapshot of /test and verifies it contains
215 assertPresentAndType(containerTest.getChild(new NodeIdentifier(TestModel.OUTER_LIST_QNAME)), MapNode.class);
220 public void writeSubtreeReadChildren() {
221 final DataTreeModification modificationTree = createEmptyModificationTree();
222 modificationTree.write(TestModel.TEST_PATH, createTestContainer());
223 final Optional<NormalizedNode<?, ?>> potential = modificationTree.readNode(TWO_TWO_PATH);
224 assertPresentAndType(potential, MapEntryNode.class);
228 public void writeSubtreeDeleteChildren() {
229 final DataTreeModification modificationTree = createEmptyModificationTree();
230 modificationTree.write(TestModel.TEST_PATH, createTestContainer());
232 // We verify data are present
233 final Optional<NormalizedNode<?, ?>> potentialBeforeDelete = modificationTree.readNode(TWO_TWO_PATH);
234 assertPresentAndType(potentialBeforeDelete, MapEntryNode.class);
236 modificationTree.delete(TWO_TWO_PATH);
237 final Optional<NormalizedNode<?, ?>> potentialAfterDelete = modificationTree.readNode(TWO_TWO_PATH);
238 assertFalse(potentialAfterDelete.isPresent());
242 private static <T> T assertPresentAndType(final Optional<?> potential, final Class<T> type) {
243 assertNotNull(potential);
244 assertTrue(potential.isPresent());
245 assertTrue(type.isInstance(potential.get()));
246 return type.cast(potential.get());