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;
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 java.util.Optional;
20 import org.junit.jupiter.api.BeforeEach;
21 import org.junit.jupiter.api.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.impl.schema.Builders;
27 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
28 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
29 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
30 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
31 import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
32 import org.opendaylight.yangtools.yang.data.tree.impl.node.Version;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 * Schema structure of document is
58 class ModificationMetadataTreeTest extends AbstractTestModelTest {
60 private static final Short ONE_ID = 1;
61 private static final Short TWO_ID = 2;
62 private static final String TWO_ONE_NAME = "one";
63 private static final String TWO_TWO_NAME = "two";
65 private static final YangInstanceIdentifier OUTER_LIST_2_PATH =
66 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
67 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
70 private static final YangInstanceIdentifier TWO_TWO_PATH = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
71 .node(TestModel.INNER_LIST_QNAME)
72 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME)
75 private static final YangInstanceIdentifier TWO_TWO_VALUE_PATH = YangInstanceIdentifier.builder(TWO_TWO_PATH)
76 .node(TestModel.VALUE_QNAME)
79 private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
80 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME)
81 .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_ONE_NAME))
82 .withChild(mapEntry(TestModel.INNER_LIST_QNAME,TestModel.NAME_QNAME, TWO_TWO_NAME))
86 private RootApplyStrategy rootOper;
89 void prepare() throws ExcludedDataSchemaNodeException {
90 rootOper = RootApplyStrategy.from(SchemaAwareApplyOperation.from(SCHEMA_CONTEXT,
91 DataTreeConfiguration.DEFAULT_OPERATIONAL));
95 * Returns a test document.
109 * @return a test document
111 public ContainerNode createDocumentOne() {
112 return Builders.containerBuilder()
113 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME))
114 .withChild(createTestContainer())
118 private static ContainerNode createTestContainer() {
119 return Builders.containerBuilder()
120 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
121 .withChild(mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
122 .withChild(mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
123 .withChild(BAR_NODE).build())
128 void basicReadWrites() {
129 final var modificationTree = new InMemoryDataTreeModification(
130 new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
131 TreeNode.of(createDocumentOne(), Version.initial()), rootOper), rootOper);
132 final var originalBarNode = modificationTree.readNode(OUTER_LIST_2_PATH);
133 assertTrue(originalBarNode.isPresent());
134 assertSame(BAR_NODE, originalBarNode.orElseThrow());
136 // writes node to /outer-list/1/inner_list/two/value
137 modificationTree.write(TWO_TWO_VALUE_PATH, ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "test"));
139 // reads node to /outer-list/1/inner_list/two/value
140 // and checks if node is already present
141 final var barTwoCModified = modificationTree.readNode(TWO_TWO_VALUE_PATH);
142 assertTrue(barTwoCModified.isPresent());
143 assertEquals(ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "test"), barTwoCModified.orElseThrow());
145 // delete node to /outer-list/1/inner_list/two/value
146 modificationTree.delete(TWO_TWO_VALUE_PATH);
147 final var barTwoCAfterDelete = modificationTree.readNode(TWO_TWO_VALUE_PATH);
148 assertFalse(barTwoCAfterDelete.isPresent());
152 public DataTreeModification createEmptyModificationTree() {
154 * Creates empty Snapshot with associated schema context.
156 final var t = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
161 * Creates Mutable Data Tree based on provided snapshot and schema
165 return t.takeSnapshot().newModification();
169 void createFromEmptyState() {
171 final var modificationTree = createEmptyModificationTree();
172 // Writes empty container node to /test
173 modificationTree.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
175 // Writes empty list node to /test/outer-list
176 modificationTree.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
179 // Reads list node from /test/outer-list.
180 final var potentialOuterList = modificationTree.readNode(TestModel.OUTER_LIST_PATH);
181 assertFalse(potentialOuterList.isPresent());
183 // Reads container node from /test and verifies that it contains test node.
184 final var potentialTest = modificationTree.readNode(TestModel.TEST_PATH);
185 assertPresentAndType(potentialTest, ContainerNode.class);
189 void writeSubtreeReadChildren() {
190 final var modificationTree = createEmptyModificationTree();
191 modificationTree.write(TestModel.TEST_PATH, createTestContainer());
192 final var potential = modificationTree.readNode(TWO_TWO_PATH);
193 assertPresentAndType(potential, MapEntryNode.class);
197 void writeSubtreeDeleteChildren() {
198 final var modificationTree = createEmptyModificationTree();
199 modificationTree.write(TestModel.TEST_PATH, createTestContainer());
201 // We verify data are present
202 final var potentialBeforeDelete = modificationTree.readNode(TWO_TWO_PATH);
203 assertPresentAndType(potentialBeforeDelete, MapEntryNode.class);
205 modificationTree.delete(TWO_TWO_PATH);
206 final var potentialAfterDelete = modificationTree.readNode(TWO_TWO_PATH);
207 assertFalse(potentialAfterDelete.isPresent());
211 private static <T> T assertPresentAndType(final Optional<?> potential, final Class<T> type) {
212 assertNotNull(potential);
213 assertTrue(potential.isPresent());
214 assertTrue(type.isInstance(potential.orElseThrow()));
215 return type.cast(potential.orElseThrow());