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.tree.impl;
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertFalse;
12 import static org.junit.jupiter.api.Assertions.assertNotNull;
13 import static org.junit.jupiter.api.Assertions.assertSame;
14 import static org.junit.jupiter.api.Assertions.assertTrue;
16 import java.util.Optional;
17 import org.junit.jupiter.api.BeforeEach;
18 import org.junit.jupiter.api.Test;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
22 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
24 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
25 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
26 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
27 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
28 import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
29 import org.opendaylight.yangtools.yang.data.tree.impl.node.Version;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 * Schema structure of document is
55 class ModificationMetadataTreeTest extends AbstractTestModelTest {
57 private static final Short ONE_ID = 1;
58 private static final Short TWO_ID = 2;
59 private static final String TWO_ONE_NAME = "one";
60 private static final String TWO_TWO_NAME = "two";
62 private static final YangInstanceIdentifier OUTER_LIST_2_PATH =
63 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
64 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
67 private static final YangInstanceIdentifier TWO_TWO_PATH = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
68 .node(TestModel.INNER_LIST_QNAME)
69 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME)
72 private static final YangInstanceIdentifier TWO_TWO_VALUE_PATH = YangInstanceIdentifier.builder(TWO_TWO_PATH)
73 .node(TestModel.VALUE_QNAME)
76 private static final MapEntryNode BAR_NODE = ImmutableNodes.newMapEntryBuilder()
77 .withNodeIdentifier(NodeIdentifierWithPredicates.of(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID))
78 .withChild(ImmutableNodes.leafNode(TestModel.ID_QNAME, TWO_ID))
79 .withChild(ImmutableNodes.newSystemMapBuilder()
80 .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_LIST_QNAME))
81 .withChild(ImmutableNodes.newMapEntryBuilder()
83 NodeIdentifierWithPredicates.of(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_ONE_NAME))
84 .withChild(ImmutableNodes.leafNode(TestModel.NAME_QNAME, TWO_ONE_NAME))
86 .withChild(ImmutableNodes.newMapEntryBuilder()
88 NodeIdentifierWithPredicates.of(TestModel.INNER_LIST_QNAME,TestModel.NAME_QNAME, TWO_TWO_NAME))
89 .withChild(ImmutableNodes.leafNode(TestModel.NAME_QNAME, TWO_TWO_NAME))
94 private RootApplyStrategy rootOper;
97 void prepare() throws ExcludedDataSchemaNodeException {
98 rootOper = RootApplyStrategy.from(SchemaAwareApplyOperation.from(SCHEMA_CONTEXT,
99 DataTreeConfiguration.DEFAULT_OPERATIONAL));
103 * Returns a test document.
117 * @return a test document
119 public ContainerNode createDocumentOne() {
120 return ImmutableNodes.newContainerBuilder()
121 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME))
122 .withChild(createTestContainer())
126 private static ContainerNode createTestContainer() {
127 return ImmutableNodes.newContainerBuilder()
128 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
129 .withChild(ImmutableNodes.newSystemMapBuilder()
130 .withNodeIdentifier(new NodeIdentifier(TestModel.OUTER_LIST_QNAME))
131 .withChild(ImmutableNodes.newMapEntryBuilder()
133 NodeIdentifierWithPredicates.of(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
134 .withChild(ImmutableNodes.leafNode(TestModel.ID_QNAME, ONE_ID))
136 .withChild(BAR_NODE).build())
141 void basicReadWrites() {
142 final var modificationTree = new InMemoryDataTreeModification(
143 new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
144 TreeNode.of(createDocumentOne(), Version.initial()), rootOper), rootOper);
145 final var originalBarNode = modificationTree.readNode(OUTER_LIST_2_PATH);
146 assertTrue(originalBarNode.isPresent());
147 assertSame(BAR_NODE, originalBarNode.orElseThrow());
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 var barTwoCModified = modificationTree.readNode(TWO_TWO_VALUE_PATH);
155 assertTrue(barTwoCModified.isPresent());
156 assertEquals(ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "test"), barTwoCModified.orElseThrow());
158 // delete node to /outer-list/1/inner_list/two/value
159 modificationTree.delete(TWO_TWO_VALUE_PATH);
160 final var 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 var t = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
174 * Creates Mutable Data Tree based on provided snapshot and schema
178 return t.takeSnapshot().newModification();
182 void createFromEmptyState() {
184 final var modificationTree = createEmptyModificationTree();
185 // Writes empty container node to /test
186 modificationTree.write(TestModel.TEST_PATH,
187 ImmutableNodes.newContainerBuilder().withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)).build());
189 // Writes empty list node to /test/outer-list
190 modificationTree.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.newSystemMapBuilder()
191 .withNodeIdentifier(new NodeIdentifier(TestModel.OUTER_LIST_QNAME))
194 // Reads list node from /test/outer-list.
195 final var potentialOuterList = modificationTree.readNode(TestModel.OUTER_LIST_PATH);
196 assertFalse(potentialOuterList.isPresent());
198 // Reads container node from /test and verifies that it contains test node.
199 final var potentialTest = modificationTree.readNode(TestModel.TEST_PATH);
200 assertPresentAndType(potentialTest, ContainerNode.class);
204 void writeSubtreeReadChildren() {
205 final var modificationTree = createEmptyModificationTree();
206 modificationTree.write(TestModel.TEST_PATH, createTestContainer());
207 final var potential = modificationTree.readNode(TWO_TWO_PATH);
208 assertPresentAndType(potential, MapEntryNode.class);
212 void writeSubtreeDeleteChildren() {
213 final var modificationTree = createEmptyModificationTree();
214 modificationTree.write(TestModel.TEST_PATH, createTestContainer());
216 // We verify data are present
217 final var potentialBeforeDelete = modificationTree.readNode(TWO_TWO_PATH);
218 assertPresentAndType(potentialBeforeDelete, MapEntryNode.class);
220 modificationTree.delete(TWO_TWO_PATH);
221 final var potentialAfterDelete = modificationTree.readNode(TWO_TWO_PATH);
222 assertFalse(potentialAfterDelete.isPresent());
226 private static <T> T assertPresentAndType(final Optional<?> potential, final Class<T> type) {
227 assertNotNull(potential);
228 assertTrue(potential.isPresent());
229 assertTrue(type.isInstance(potential.orElseThrow()));
230 return type.cast(potential.orElseThrow());