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