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