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.assertTrue;
13 import org.junit.jupiter.api.BeforeEach;
14 import org.junit.jupiter.api.Test;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
17 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
18 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
19 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
20 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
21 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
22 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeSnapshot;
23 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
24 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
26 class Bug2690Test extends AbstractTestModelTest {
27 private DataTree inMemoryDataTree;
31 inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
36 void testWriteMerge1() throws DataValidationFailedException {
37 final var fooEntryNode = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1);
38 final var barEntryNode = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2);
39 final var mapNode1 = ImmutableNodes.mapNodeBuilder()
40 .withNodeIdentifier(new NodeIdentifier(TestModel.OUTER_LIST_QNAME))
41 .withChild(fooEntryNode).build();
42 final var mapNode2 = ImmutableNodes.mapNodeBuilder()
43 .withNodeIdentifier(new NodeIdentifier(TestModel.OUTER_LIST_QNAME))
44 .withChild(barEntryNode).build();
46 final var cont1 = Builders.containerBuilder()
47 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
48 .withChild(mapNode1).build();
50 final var cont2 = Builders.containerBuilder()
51 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
52 .withChild(mapNode2).build();
54 final var modificationTree = inMemoryDataTree.takeSnapshot().newModification();
55 modificationTree.write(TestModel.TEST_PATH, cont1);
56 modificationTree.merge(TestModel.TEST_PATH, cont2);
57 commit(modificationTree);
59 final var snapshotAfterTx = inMemoryDataTree.takeSnapshot();
60 final var modificationAfterTx = snapshotAfterTx.newModification();
61 final var readNode = modificationAfterTx.readNode(TestModel.OUTER_LIST_PATH);
62 assertTrue(readNode.isPresent());
63 assertEquals(2, ((NormalizedNodeContainer<?>)readNode.orElseThrow()).size());
67 void testDeleteStructuralAndWriteChild() throws DataValidationFailedException {
68 final var modificationTree = setupTestDeleteStructuralAndWriteChild();
69 verifyTestDeleteStructuralAndWriteChild(modificationTree);
73 void testDeleteStructuralAndWriteChildWithCommit() throws DataValidationFailedException {
74 final var modificationTree = setupTestDeleteStructuralAndWriteChild();
75 commit(modificationTree);
76 verifyTestDeleteStructuralAndWriteChild(inMemoryDataTree.takeSnapshot());
79 private DataTreeModification setupTestDeleteStructuralAndWriteChild() {
80 final var modificationTree = inMemoryDataTree.takeSnapshot().newModification();
81 modificationTree.delete(TestModel.NON_PRESENCE_PATH);
82 modificationTree.write(TestModel.NAME_PATH, ImmutableNodes.leafNode(TestModel.NAME_QNAME, "abc"));
83 return modificationTree;
86 private static void verifyTestDeleteStructuralAndWriteChild(final DataTreeSnapshot snapshot) {
87 final var readNode = snapshot.readNode(TestModel.NAME_PATH);
88 assertTrue(readNode.isPresent());
91 private void commit(final DataTreeModification modificationTree) throws DataValidationFailedException {
92 modificationTree.ready();
94 inMemoryDataTree.validate(modificationTree);
95 final var prepare = inMemoryDataTree.prepare(modificationTree);
96 inMemoryDataTree.commit(prepare);