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.impl.schema.tree;
10 import static org.junit.Assert.assertEquals;
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;
29 public final class StructuralApplyModificationTest extends AbstractTestModelTest {
30 private DataTree inMemoryDataTree;
34 inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION);
35 inMemoryDataTree.setSchemaContext(SCHEMA_CONTEXT);
39 public void testMapNodeParentAutoCreateDelete() throws DataValidationFailedException {
40 final DataTreeModification addListEntryModification = inMemoryDataTree.takeSnapshot().newModification();
43 final YangInstanceIdentifier.NodeIdentifier rootContainerId = getNId(TestModel.TEST_QNAME);
44 addListEntryModification.write(YangInstanceIdentifier.create(rootContainerId),
45 Builders.containerBuilder().withNodeIdentifier(rootContainerId).build());
47 final NodeIdentifierWithPredicates outerListEntryId = NodeIdentifierWithPredicates.of(
48 TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1);
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);
57 addListEntryModification.ready();
58 inMemoryDataTree.validate(addListEntryModification);
59 inMemoryDataTree.commit(inMemoryDataTree.prepare(addListEntryModification));
61 // Check list parent auto created
62 assertNodeExistence(outerListParentPath, true);
65 final DataTreeModification deleteListEntryModification = inMemoryDataTree.takeSnapshot().newModification();
66 deleteListEntryModification.delete(outerListEntryPath);
67 deleteListEntryModification.ready();
68 inMemoryDataTree.validate(deleteListEntryModification);
69 inMemoryDataTree.commit(inMemoryDataTree.prepare(deleteListEntryModification));
71 // Check list parent auto deleted
72 assertNodeExistence(outerListParentPath, false);
76 public void testMapNodeDirectEmptyWrite() {
77 final DataTreeModification addListEntryModification = inMemoryDataTree.takeSnapshot().newModification();
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());
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));
88 // Check empty map node auto deleted
89 assertNodeExistence(outerListParentPath, false);
93 public void testNonPresenceContainerDirectEmptyWrite() throws DataValidationFailedException {
94 final DataTreeModification addListEntryModification = inMemoryDataTree.takeSnapshot().newModification();
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());
100 addListEntryModification.ready();
101 inMemoryDataTree.validate(addListEntryModification);
102 inMemoryDataTree.commit(inMemoryDataTree.prepare(addListEntryModification));
104 // Check empty container auto deleted
105 assertNodeExistence(path, false);
109 public void testNestedStrucutralNodes() throws DataValidationFailedException {
110 final DataTreeModification addListEntryModification = inMemoryDataTree.takeSnapshot().newModification();
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"));
115 addListEntryModification.write(path,
116 Builders.mapEntryBuilder()
117 .withNodeIdentifier(getNId(TestModel.A_LIST_QNAME, TestModel.A_NAME_QNAME, "1"))
120 addListEntryModification.ready();
121 inMemoryDataTree.validate(addListEntryModification);
122 inMemoryDataTree.commit(inMemoryDataTree.prepare(addListEntryModification));
124 // Check parent structure auto created
125 assertNodeExistence(path, true);
126 assertNodeExistence(TestModel.NON_PRESENCE_PATH, true);
127 assertNodeExistence(TestModel.DEEP_CHOICE_PATH, true);
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);
136 private static NodeIdentifier getNId(final QName qname) {
137 return YangInstanceIdentifier.NodeIdentifier.create(qname);
140 private static NodeIdentifierWithPredicates getNId(final QName qname, final QName key, final String val) {
141 return NodeIdentifierWithPredicates.of(qname, key, val);