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.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.NormalizedNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
21 import org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode;
22 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
23 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
24 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
25 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
26 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
27 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
28 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeSnapshot;
29 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
30 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
32 public class Bug2690Test extends AbstractTestModelTest {
33 private DataTree inMemoryDataTree;
36 public void prepare() {
37 inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
42 public void testWriteMerge1() throws DataValidationFailedException {
43 final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1);
44 final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2);
45 final SystemMapNode mapNode1 = ImmutableNodes.mapNodeBuilder()
46 .withNodeIdentifier(new NodeIdentifier(TestModel.OUTER_LIST_QNAME))
47 .withChild(fooEntryNode).build();
48 final SystemMapNode mapNode2 = ImmutableNodes.mapNodeBuilder()
49 .withNodeIdentifier(new NodeIdentifier(TestModel.OUTER_LIST_QNAME))
50 .withChild(barEntryNode).build();
52 final ContainerNode cont1 = Builders.containerBuilder()
53 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
54 .withChild(mapNode1).build();
56 final ContainerNode cont2 = Builders.containerBuilder()
57 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
58 .withChild(mapNode2).build();
60 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
61 modificationTree.write(TestModel.TEST_PATH, cont1);
62 modificationTree.merge(TestModel.TEST_PATH, cont2);
63 commit(modificationTree);
65 final DataTreeSnapshot snapshotAfterTx = inMemoryDataTree.takeSnapshot();
66 final DataTreeModification modificationAfterTx = snapshotAfterTx.newModification();
67 final Optional<NormalizedNode> readNode = modificationAfterTx.readNode(TestModel.OUTER_LIST_PATH);
68 assertTrue(readNode.isPresent());
69 assertEquals(2, ((NormalizedNodeContainer<?>)readNode.orElseThrow()).size());
73 public void testDeleteStructuralAndWriteChild() throws DataValidationFailedException {
74 final DataTreeModification modificationTree = setupTestDeleteStructuralAndWriteChild();
75 verifyTestDeleteStructuralAndWriteChild(modificationTree);
79 public void testDeleteStructuralAndWriteChildWithCommit() throws DataValidationFailedException {
80 final DataTreeModification modificationTree = setupTestDeleteStructuralAndWriteChild();
81 commit(modificationTree);
82 verifyTestDeleteStructuralAndWriteChild(inMemoryDataTree.takeSnapshot());
85 private DataTreeModification setupTestDeleteStructuralAndWriteChild() {
86 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
87 modificationTree.delete(TestModel.NON_PRESENCE_PATH);
88 modificationTree.write(TestModel.NAME_PATH, ImmutableNodes.leafNode(TestModel.NAME_QNAME, "abc"));
89 return modificationTree;
92 private static void verifyTestDeleteStructuralAndWriteChild(final DataTreeSnapshot snapshot) {
93 final Optional<NormalizedNode> readNode = snapshot.readNode(TestModel.NAME_PATH);
94 assertTrue(readNode.isPresent());
97 private void commit(final DataTreeModification modificationTree) throws DataValidationFailedException {
98 modificationTree.ready();
100 inMemoryDataTree.validate(modificationTree);
101 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
102 inMemoryDataTree.commit(prepare);