Add NormalizedNodeContainer.size()
[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.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;
30
31 public class Bug2690Test extends AbstractTestModelTest {
32     private DataTree inMemoryDataTree;
33
34     @Before
35     public void prepare() {
36         inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
37             SCHEMA_CONTEXT);
38     }
39
40     @Test
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();
50
51         final ContainerNode cont1 = Builders.containerBuilder()
52                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
53                 .withChild(mapNode1).build();
54
55         final ContainerNode cont2 = Builders.containerBuilder()
56                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
57                 .withChild(mapNode2).build();
58
59         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
60         modificationTree.write(TestModel.TEST_PATH, cont1);
61         modificationTree.merge(TestModel.TEST_PATH, cont2);
62         commit(modificationTree);
63
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());
69     }
70
71     @Test
72     public void testDeleteStructuralAndWriteChild() throws DataValidationFailedException {
73         final DataTreeModification modificationTree = setupTestDeleteStructuralAndWriteChild();
74         verifyTestDeleteStructuralAndWriteChild(modificationTree);
75     }
76
77     @Test
78     public void testDeleteStructuralAndWriteChildWithCommit() throws DataValidationFailedException {
79         final DataTreeModification modificationTree = setupTestDeleteStructuralAndWriteChild();
80         commit(modificationTree);
81         verifyTestDeleteStructuralAndWriteChild(inMemoryDataTree.takeSnapshot());
82     }
83
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;
89     }
90
91     private static void verifyTestDeleteStructuralAndWriteChild(final DataTreeSnapshot snapshot) {
92         final Optional<NormalizedNode<?, ?>> readNode = snapshot.readNode(TestModel.NAME_PATH);
93         assertTrue(readNode.isPresent());
94     }
95
96     private void commit(final DataTreeModification modificationTree) throws DataValidationFailedException {
97         modificationTree.ready();
98
99         inMemoryDataTree.validate(modificationTree);
100         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
101         inMemoryDataTree.commit(prepare);
102     }
103 }