Optimize IMDT tests
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / Bug2690Test.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12
13 import java.util.Optional;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
29 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
30 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
31
32 public class Bug2690Test extends AbstractTestModelTest {
33     private static final YangInstanceIdentifier NAME_PATH = YangInstanceIdentifier.of(TestModel.NON_PRESENCE_QNAME)
34             .node(TestModel.NAME_QNAME);
35
36     private DataTree inMemoryDataTree;
37
38     @Before
39     public void prepare() {
40         inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
41             SCHEMA_CONTEXT);
42     }
43
44     @Test
45     public void testWriteMerge1() throws DataValidationFailedException {
46         final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1);
47         final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2);
48         final MapNode mapNode1 = ImmutableNodes.mapNodeBuilder()
49                 .withNodeIdentifier(new NodeIdentifier(TestModel.OUTER_LIST_QNAME))
50                 .withChild(fooEntryNode).build();
51         final MapNode mapNode2 = ImmutableNodes.mapNodeBuilder()
52                 .withNodeIdentifier(new NodeIdentifier(TestModel.OUTER_LIST_QNAME))
53                 .withChild(barEntryNode).build();
54
55         final ContainerNode cont1 = Builders.containerBuilder()
56                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
57                 .withChild(mapNode1).build();
58
59         final ContainerNode cont2 = Builders.containerBuilder()
60                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
61                 .withChild(mapNode2).build();
62
63         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
64         modificationTree.write(TestModel.TEST_PATH, cont1);
65         modificationTree.merge(TestModel.TEST_PATH, cont2);
66         commit(modificationTree);
67
68         final DataTreeSnapshot snapshotAfterTx = inMemoryDataTree.takeSnapshot();
69         final DataTreeModification modificationAfterTx = snapshotAfterTx.newModification();
70         final Optional<NormalizedNode<?, ?>> readNode = modificationAfterTx.readNode(TestModel.OUTER_LIST_PATH);
71         assertTrue(readNode.isPresent());
72         assertEquals(2, ((NormalizedNodeContainer<?,?,?>)readNode.get()).getValue().size());
73     }
74
75     @Test
76     public void testDeleteStructuralAndWriteChild() throws DataValidationFailedException {
77         final DataTreeModification modificationTree = setupTestDeleteStructuralAndWriteChild();
78         verifyTestDeleteStructuralAndWriteChild(modificationTree);
79     }
80
81     @Test
82     public void testDeleteStructuralAndWriteChildWithCommit() throws DataValidationFailedException {
83         final DataTreeModification modificationTree = setupTestDeleteStructuralAndWriteChild();
84         commit(modificationTree);
85         verifyTestDeleteStructuralAndWriteChild(inMemoryDataTree.takeSnapshot());
86     }
87
88     private DataTreeModification setupTestDeleteStructuralAndWriteChild() {
89         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
90         modificationTree.delete(YangInstanceIdentifier.of(TestModel.NON_PRESENCE_QNAME));
91         modificationTree.write(NAME_PATH, Builders.leafBuilder()
92             .withNodeIdentifier(new NodeIdentifier(TestModel.NAME_QNAME)).withValue("abc").build());
93         return modificationTree;
94     }
95
96     private static void verifyTestDeleteStructuralAndWriteChild(final DataTreeSnapshot snapshot) {
97         final Optional<NormalizedNode<?, ?>> readNode = snapshot.readNode(NAME_PATH);
98         assertTrue(readNode.isPresent());
99     }
100
101     private void commit(final DataTreeModification modificationTree) throws DataValidationFailedException {
102         modificationTree.ready();
103
104         inMemoryDataTree.validate(modificationTree);
105         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
106         inMemoryDataTree.commit(prepare);
107     }
108 }