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