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.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;
25 final class StructuralApplyModificationTest extends AbstractTestModelTest {
26 private DataTree inMemoryDataTree;
30 inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION);
31 inMemoryDataTree.setEffectiveModelContext(SCHEMA_CONTEXT);
35 void testMapNodeParentAutoCreateDelete() throws DataValidationFailedException {
36 final var addListEntryModification = inMemoryDataTree.takeSnapshot().newModification();
39 final var rootContainerId = getNId(TestModel.TEST_QNAME);
40 addListEntryModification.write(YangInstanceIdentifier.of(rootContainerId),
41 Builders.containerBuilder().withNodeIdentifier(rootContainerId).build());
43 final var outerListEntryId = NodeIdentifierWithPredicates.of(
44 TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1);
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);
53 addListEntryModification.ready();
54 inMemoryDataTree.validate(addListEntryModification);
55 inMemoryDataTree.commit(inMemoryDataTree.prepare(addListEntryModification));
57 // Check list parent auto created
58 assertNodeExistence(outerListParentPath, true);
61 final var deleteListEntryModification = inMemoryDataTree.takeSnapshot().newModification();
62 deleteListEntryModification.delete(outerListEntryPath);
63 deleteListEntryModification.ready();
64 inMemoryDataTree.validate(deleteListEntryModification);
65 inMemoryDataTree.commit(inMemoryDataTree.prepare(deleteListEntryModification));
67 // Check list parent auto deleted
68 assertNodeExistence(outerListParentPath, false);
72 void testMapNodeDirectEmptyWrite() {
73 final var addListEntryModification = inMemoryDataTree.takeSnapshot().newModification();
75 // Prepare root container
76 final var rootContainerId = getNId(TestModel.TEST_QNAME);
77 addListEntryModification.write(YangInstanceIdentifier.of(rootContainerId),
78 Builders.containerBuilder().withNodeIdentifier(rootContainerId).build());
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));
84 // Check empty map node auto deleted
85 assertNodeExistence(outerListParentPath, false);
89 void testNonPresenceContainerDirectEmptyWrite() throws DataValidationFailedException {
90 final var addListEntryModification = inMemoryDataTree.takeSnapshot().newModification();
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());
96 addListEntryModification.ready();
97 inMemoryDataTree.validate(addListEntryModification);
98 inMemoryDataTree.commit(inMemoryDataTree.prepare(addListEntryModification));
100 // Check empty container auto deleted
101 assertNodeExistence(path, false);
105 void testNestedStrucutralNodes() throws DataValidationFailedException {
106 final var addListEntryModification = inMemoryDataTree.takeSnapshot().newModification();
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"));
111 addListEntryModification.write(path,
112 Builders.mapEntryBuilder()
113 .withNodeIdentifier(getNId(TestModel.A_LIST_QNAME, TestModel.A_NAME_QNAME, "1"))
116 addListEntryModification.ready();
117 inMemoryDataTree.validate(addListEntryModification);
118 inMemoryDataTree.commit(inMemoryDataTree.prepare(addListEntryModification));
120 // Check parent structure auto created
121 assertNodeExistence(path, true);
122 assertNodeExistence(TestModel.NON_PRESENCE_PATH, true);
123 assertNodeExistence(TestModel.DEEP_CHOICE_PATH, true);
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);
132 private static NodeIdentifier getNId(final QName qname) {
133 return YangInstanceIdentifier.NodeIdentifier.create(qname);
136 private static NodeIdentifierWithPredicates getNId(final QName qname, final QName key, final String val) {
137 return NodeIdentifierWithPredicates.of(qname, key, val);