2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.tree.impl;
10 import static org.junit.jupiter.api.Assertions.assertEquals;
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;
24 final class StructuralApplyModificationTest extends AbstractTestModelTest {
25 private DataTree inMemoryDataTree;
29 inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION);
30 inMemoryDataTree.setEffectiveModelContext(SCHEMA_CONTEXT);
34 void testMapNodeParentAutoCreateDelete() throws DataValidationFailedException {
35 final var addListEntryModification = inMemoryDataTree.takeSnapshot().newModification();
38 final var rootContainerId = getNId(TestModel.TEST_QNAME);
39 addListEntryModification.write(YangInstanceIdentifier.of(rootContainerId),
40 ImmutableNodes.newContainerBuilder().withNodeIdentifier(rootContainerId).build());
42 final var outerListEntryId = NodeIdentifierWithPredicates.of(
43 TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1);
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);
52 addListEntryModification.ready();
53 inMemoryDataTree.validate(addListEntryModification);
54 inMemoryDataTree.commit(inMemoryDataTree.prepare(addListEntryModification));
56 // Check list parent auto created
57 assertNodeExistence(outerListParentPath, true);
60 final var deleteListEntryModification = inMemoryDataTree.takeSnapshot().newModification();
61 deleteListEntryModification.delete(outerListEntryPath);
62 deleteListEntryModification.ready();
63 inMemoryDataTree.validate(deleteListEntryModification);
64 inMemoryDataTree.commit(inMemoryDataTree.prepare(deleteListEntryModification));
66 // Check list parent auto deleted
67 assertNodeExistence(outerListParentPath, false);
71 void testMapNodeDirectEmptyWrite() {
72 final var addListEntryModification = inMemoryDataTree.takeSnapshot().newModification();
74 // Prepare root container
75 final var rootContainerId = getNId(TestModel.TEST_QNAME);
76 addListEntryModification.write(YangInstanceIdentifier.of(rootContainerId),
77 ImmutableNodes.newContainerBuilder().withNodeIdentifier(rootContainerId).build());
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))
85 // Check empty map node auto deleted
86 assertNodeExistence(outerListParentPath, false);
90 void testNonPresenceContainerDirectEmptyWrite() throws DataValidationFailedException {
91 final var addListEntryModification = inMemoryDataTree.takeSnapshot().newModification();
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)
99 addListEntryModification.ready();
100 inMemoryDataTree.validate(addListEntryModification);
101 inMemoryDataTree.commit(inMemoryDataTree.prepare(addListEntryModification));
103 // Check empty container auto deleted
104 assertNodeExistence(path, false);
108 void testNestedStrucutralNodes() throws DataValidationFailedException {
109 final var addListEntryModification = inMemoryDataTree.takeSnapshot().newModification();
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"));
114 addListEntryModification.write(path,
115 ImmutableNodes.newMapEntryBuilder()
116 .withNodeIdentifier(getNId(TestModel.A_LIST_QNAME, TestModel.A_NAME_QNAME, "1"))
119 addListEntryModification.ready();
120 inMemoryDataTree.validate(addListEntryModification);
121 inMemoryDataTree.commit(inMemoryDataTree.prepare(addListEntryModification));
123 // Check parent structure auto created
124 assertNodeExistence(path, true);
125 assertNodeExistence(TestModel.NON_PRESENCE_PATH, true);
126 assertNodeExistence(TestModel.DEEP_CHOICE_PATH, true);
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);
135 private static NodeIdentifier getNId(final QName qname) {
136 return YangInstanceIdentifier.NodeIdentifier.create(qname);
139 private static NodeIdentifierWithPredicates getNId(final QName qname, final QName key, final String val) {
140 return NodeIdentifierWithPredicates.of(qname, key, val);