9f6d61bd8873575303f5e0939b1b258d313061c9
[yangtools.git] / data / yang-data-tree-ri / src / test / java / org / opendaylight / yangtools / yang / data / tree / impl / StructuralApplyModificationTest.java
1 /*
2  * Copyright (c) 2016 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.tree.impl;
9
10 import static org.junit.Assert.assertEquals;
11
12 import java.util.Optional;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.opendaylight.yangtools.yang.common.QName;
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.YangInstanceIdentifier.NodeIdentifierWithPredicates;
19 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
22 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
23 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
24 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
25 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
26 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeSnapshot;
27 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
28 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
29
30 public final class StructuralApplyModificationTest extends AbstractTestModelTest {
31     private DataTree inMemoryDataTree;
32
33     @Before
34     public void setUp() {
35         inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION);
36         inMemoryDataTree.setEffectiveModelContext(SCHEMA_CONTEXT);
37     }
38
39     @Test
40     public void testMapNodeParentAutoCreateDelete() throws DataValidationFailedException {
41         final DataTreeModification addListEntryModification = inMemoryDataTree.takeSnapshot().newModification();
42
43         // Prepare root
44         final YangInstanceIdentifier.NodeIdentifier rootContainerId = getNId(TestModel.TEST_QNAME);
45         addListEntryModification.write(YangInstanceIdentifier.of(rootContainerId),
46             Builders.containerBuilder().withNodeIdentifier(rootContainerId).build());
47
48         final NodeIdentifierWithPredicates outerListEntryId = NodeIdentifierWithPredicates.of(
49             TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1);
50
51         // Write list entry (MapEntryNode) without creating list parent (MapNode)
52         final MapEntryNode outerListEntry = Builders.mapEntryBuilder().withNodeIdentifier(outerListEntryId).build();
53         final YangInstanceIdentifier outerListParentPath = YangInstanceIdentifier.of(getNId(TestModel.TEST_QNAME),
54             getNId(TestModel.OUTER_LIST_QNAME));
55         final YangInstanceIdentifier outerListEntryPath = outerListParentPath.node(outerListEntryId);
56         addListEntryModification.write(outerListEntryPath, outerListEntry);
57
58         addListEntryModification.ready();
59         inMemoryDataTree.validate(addListEntryModification);
60         inMemoryDataTree.commit(inMemoryDataTree.prepare(addListEntryModification));
61
62         // Check list parent auto created
63         assertNodeExistence(outerListParentPath, true);
64
65         // Now delete
66         final DataTreeModification deleteListEntryModification = inMemoryDataTree.takeSnapshot().newModification();
67         deleteListEntryModification.delete(outerListEntryPath);
68         deleteListEntryModification.ready();
69         inMemoryDataTree.validate(deleteListEntryModification);
70         inMemoryDataTree.commit(inMemoryDataTree.prepare(deleteListEntryModification));
71
72         // Check list parent auto deleted
73         assertNodeExistence(outerListParentPath, false);
74     }
75
76     @Test
77     public void testMapNodeDirectEmptyWrite() {
78         final DataTreeModification addListEntryModification = inMemoryDataTree.takeSnapshot().newModification();
79
80         // Prepare root container
81         final YangInstanceIdentifier.NodeIdentifier rootContainerId = getNId(TestModel.TEST_QNAME);
82         addListEntryModification.write(YangInstanceIdentifier.of(rootContainerId),
83             Builders.containerBuilder().withNodeIdentifier(rootContainerId).build());
84
85         final YangInstanceIdentifier outerListParentPath = YangInstanceIdentifier.of(getNId(TestModel.TEST_QNAME),
86             getNId(TestModel.OUTER_LIST_QNAME));
87         addListEntryModification.merge(outerListParentPath, ImmutableNodes.mapNode(TestModel.OUTER_LIST_QNAME));
88
89         // Check empty map node auto deleted
90         assertNodeExistence(outerListParentPath, false);
91     }
92
93     @Test
94     public void testNonPresenceContainerDirectEmptyWrite() throws DataValidationFailedException {
95         final DataTreeModification addListEntryModification = inMemoryDataTree.takeSnapshot().newModification();
96
97         final YangInstanceIdentifier.NodeIdentifier rootContainerId = getNId(TestModel.NON_PRESENCE_QNAME);
98         final YangInstanceIdentifier path = YangInstanceIdentifier.of(rootContainerId);
99         addListEntryModification.write(path, Builders.containerBuilder().withNodeIdentifier(rootContainerId).build());
100
101         addListEntryModification.ready();
102         inMemoryDataTree.validate(addListEntryModification);
103         inMemoryDataTree.commit(inMemoryDataTree.prepare(addListEntryModification));
104
105         // Check empty container auto deleted
106         assertNodeExistence(path, false);
107     }
108
109     @Test
110     public void testNestedStrucutralNodes() throws DataValidationFailedException {
111         final DataTreeModification addListEntryModification = inMemoryDataTree.takeSnapshot().newModification();
112
113         final YangInstanceIdentifier path = TestModel.DEEP_CHOICE_PATH.node(TestModel.A_LIST_QNAME)
114             .node(getNId(TestModel.A_LIST_QNAME, TestModel.A_NAME_QNAME, "1"));
115
116         addListEntryModification.write(path,
117             Builders.mapEntryBuilder()
118                 .withNodeIdentifier(getNId(TestModel.A_LIST_QNAME, TestModel.A_NAME_QNAME, "1"))
119                 .build());
120
121         addListEntryModification.ready();
122         inMemoryDataTree.validate(addListEntryModification);
123         inMemoryDataTree.commit(inMemoryDataTree.prepare(addListEntryModification));
124
125         // Check parent structure auto created
126         assertNodeExistence(path, true);
127         assertNodeExistence(TestModel.NON_PRESENCE_PATH, true);
128         assertNodeExistence(TestModel.DEEP_CHOICE_PATH, true);
129     }
130
131     private void assertNodeExistence(final YangInstanceIdentifier outerListParentPath, final boolean shouldBePresent) {
132         final DataTreeSnapshot snapshotAfterCommits = inMemoryDataTree.takeSnapshot();
133         final Optional<NormalizedNode> readNode = snapshotAfterCommits.readNode(outerListParentPath);
134         assertEquals(readNode.isPresent(), shouldBePresent);
135     }
136
137     private static NodeIdentifier getNId(final QName qname) {
138         return YangInstanceIdentifier.NodeIdentifier.create(qname);
139     }
140
141     private static NodeIdentifierWithPredicates getNId(final QName qname, final QName key, final String val) {
142         return NodeIdentifierWithPredicates.of(qname, key, val);
143     }
144 }