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.assertTrue;
13 import java.util.Optional;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
28 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
29 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
31 public class Bug2690Test extends AbstractTestModelTest {
32 private DataTree inMemoryDataTree;
35 public void prepare() {
36 inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
41 public void testWriteMerge1() throws DataValidationFailedException {
42 final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1);
43 final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2);
44 final MapNode mapNode1 = ImmutableNodes.mapNodeBuilder()
45 .withNodeIdentifier(new NodeIdentifier(TestModel.OUTER_LIST_QNAME))
46 .withChild(fooEntryNode).build();
47 final MapNode mapNode2 = ImmutableNodes.mapNodeBuilder()
48 .withNodeIdentifier(new NodeIdentifier(TestModel.OUTER_LIST_QNAME))
49 .withChild(barEntryNode).build();
51 final ContainerNode cont1 = Builders.containerBuilder()
52 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
53 .withChild(mapNode1).build();
55 final ContainerNode cont2 = Builders.containerBuilder()
56 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
57 .withChild(mapNode2).build();
59 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
60 modificationTree.write(TestModel.TEST_PATH, cont1);
61 modificationTree.merge(TestModel.TEST_PATH, cont2);
62 commit(modificationTree);
64 final DataTreeSnapshot snapshotAfterTx = inMemoryDataTree.takeSnapshot();
65 final DataTreeModification modificationAfterTx = snapshotAfterTx.newModification();
66 final Optional<NormalizedNode<?, ?>> readNode = modificationAfterTx.readNode(TestModel.OUTER_LIST_PATH);
67 assertTrue(readNode.isPresent());
68 assertEquals(2, ((NormalizedNodeContainer<?,?,?>)readNode.get()).size());
72 public void testDeleteStructuralAndWriteChild() throws DataValidationFailedException {
73 final DataTreeModification modificationTree = setupTestDeleteStructuralAndWriteChild();
74 verifyTestDeleteStructuralAndWriteChild(modificationTree);
78 public void testDeleteStructuralAndWriteChildWithCommit() throws DataValidationFailedException {
79 final DataTreeModification modificationTree = setupTestDeleteStructuralAndWriteChild();
80 commit(modificationTree);
81 verifyTestDeleteStructuralAndWriteChild(inMemoryDataTree.takeSnapshot());
84 private DataTreeModification setupTestDeleteStructuralAndWriteChild() {
85 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
86 modificationTree.delete(TestModel.NON_PRESENCE_PATH);
87 modificationTree.write(TestModel.NAME_PATH, ImmutableNodes.leafNode(TestModel.NAME_QNAME, "abc"));
88 return modificationTree;
91 private static void verifyTestDeleteStructuralAndWriteChild(final DataTreeSnapshot snapshot) {
92 final Optional<NormalizedNode<?, ?>> readNode = snapshot.readNode(TestModel.NAME_PATH);
93 assertTrue(readNode.isPresent());
96 private void commit(final DataTreeModification modificationTree) throws DataValidationFailedException {
97 modificationTree.ready();
99 inMemoryDataTree.validate(modificationTree);
100 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
101 inMemoryDataTree.commit(prepare);