2 * Copyright (c) 2014, 2015 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.controller.cluster.datastore.utils;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Mockito.doThrow;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
16 import static org.opendaylight.controller.md.cluster.datastore.model.CompositeModel.AUG_CONTAINER;
17 import static org.opendaylight.controller.md.cluster.datastore.model.CompositeModel.AUG_INNER_CONTAINER;
18 import static org.opendaylight.controller.md.cluster.datastore.model.CompositeModel.AUG_QNAME;
19 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.NAME_QNAME;
20 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.TEST_QNAME;
21 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.innerNode;
22 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.outerNode;
23 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.outerNodeEntry;
25 import com.google.common.reflect.Reflection;
26 import java.lang.reflect.InvocationTargetException;
27 import java.util.Optional;
28 import org.junit.Before;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mock;
33 import org.mockito.Mockito;
34 import org.mockito.junit.MockitoJUnitRunner;
35 import org.opendaylight.controller.cluster.datastore.Shard;
36 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
37 import org.opendaylight.controller.cluster.datastore.node.utils.transformer.ReusableNormalizedNodePruner;
38 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
39 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
40 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
41 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
42 import org.opendaylight.yangtools.yang.common.QName;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
46 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
48 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
49 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
50 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
51 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor;
52 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
53 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
54 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
55 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
56 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
57 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
58 import org.opendaylight.yangtools.yang.data.impl.schema.tree.SchemaValidationFailedException;
59 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
60 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
62 @RunWith(MockitoJUnitRunner.class)
63 public class PruningDataTreeModificationTest {
64 static final QName INVALID_TEST_QNAME = QName.create(TestModel.TEST_QNAME, "invalid");
65 static final YangInstanceIdentifier INVALID_TEST_PATH = YangInstanceIdentifier.of(INVALID_TEST_QNAME);
67 private static EffectiveModelContext SCHEMA_CONTEXT;
68 private static DataSchemaContextTree CONTEXT_TREE;
71 private DataTreeModification mockModification;
73 private DataTree dataTree;
74 private DataTreeModification realModification;
75 private DataTreeModification proxyModification;
76 private PruningDataTreeModification pruningDataTreeModification;
79 public static void beforeClass() {
80 SCHEMA_CONTEXT = SchemaContextHelper.select(SchemaContextHelper.CARS_YANG,
81 SchemaContextHelper.ODL_DATASTORE_TEST_YANG);
82 CONTEXT_TREE = DataSchemaContextTree.from(SCHEMA_CONTEXT);
86 @SuppressWarnings("checkstyle:avoidHidingCauseException")
88 dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION,
91 realModification = dataTree.takeSnapshot().newModification();
92 proxyModification = Reflection.newProxy(DataTreeModification.class, (proxy, method, args) -> {
94 method.invoke(mockModification, args);
95 return method.invoke(realModification, args);
96 } catch (InvocationTargetException e) {
101 pruningDataTreeModification = new PruningDataTreeModification.Reactive(proxyModification, dataTree,
102 // Cannot reuse with parallel tests
103 ReusableNormalizedNodePruner.forDataSchemaContext(CONTEXT_TREE));
107 public void testDelete() {
108 pruningDataTreeModification.delete(CarsModel.BASE_PATH);
110 verify(mockModification, times(1)).delete(CarsModel.BASE_PATH);
114 public void testDeleteOnException() {
115 YangInstanceIdentifier path = CarsModel.BASE_PATH;
116 doThrow(SchemaValidationFailedException.class).when(mockModification).delete(path);
118 pruningDataTreeModification.delete(path);
120 verify(mockModification, times(1)).delete(path);
125 public void testMerge() {
126 NormalizedNode normalizedNode = CarsModel.create();
127 YangInstanceIdentifier path = CarsModel.BASE_PATH;
128 pruningDataTreeModification.merge(path, normalizedNode);
130 verify(mockModification, times(1)).merge(path, normalizedNode);
134 public void testMergeWithInvalidNamespace() throws DataValidationFailedException {
135 NormalizedNode normalizedNode = PeopleModel.emptyContainer();
136 YangInstanceIdentifier path = PeopleModel.BASE_PATH;
138 pruningDataTreeModification.merge(path, normalizedNode);
140 verify(mockModification, times(1)).merge(path, normalizedNode);
142 DataTreeCandidate candidate = getCandidate();
143 assertEquals("getModificationType", ModificationType.UNMODIFIED, candidate.getRootNode().getModificationType());
147 public void testMergeWithInvalidChildNodeNames() throws DataValidationFailedException {
148 ContainerNode augContainer = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
149 new YangInstanceIdentifier.NodeIdentifier(AUG_CONTAINER)).withChild(
150 ImmutableNodes.containerNode(AUG_INNER_CONTAINER)).build();
152 DataContainerChild outerNode = outerNode(outerNodeEntry(1, innerNode("one", "two")));
153 ContainerNode normalizedNode = ImmutableContainerNodeBuilder.create()
154 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME)).withChild(outerNode)
155 .withChild(augContainer).withChild(ImmutableNodes.leafNode(AUG_QNAME, "aug")).build();
157 YangInstanceIdentifier path = TestModel.TEST_PATH;
159 pruningDataTreeModification.merge(path, normalizedNode);
161 dataTree.commit(getCandidate());
163 ContainerNode prunedNode = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
164 new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME)).withChild(outerNode).build();
166 Optional<NormalizedNode> actual = dataTree.takeSnapshot().readNode(path);
167 assertTrue("After pruning present", actual.isPresent());
168 assertEquals("After pruning", prunedNode, actual.get());
172 public void testMergeWithValidNamespaceAndInvalidNodeName() throws DataValidationFailedException {
173 NormalizedNode normalizedNode = ImmutableNodes.containerNode(INVALID_TEST_QNAME);
174 YangInstanceIdentifier path = INVALID_TEST_PATH;
176 pruningDataTreeModification.merge(path, normalizedNode);
178 verify(mockModification, times(1)).merge(path, normalizedNode);
180 DataTreeCandidate candidate = getCandidate();
181 assertEquals("getModificationType", ModificationType.UNMODIFIED, candidate.getRootNode().getModificationType());
185 public void testWrite() {
186 NormalizedNode normalizedNode = CarsModel.create();
187 YangInstanceIdentifier path = CarsModel.BASE_PATH;
188 pruningDataTreeModification.write(path, normalizedNode);
190 verify(mockModification, times(1)).write(path, normalizedNode);
194 public void testWriteRootNode() throws Exception {
195 final DataTree localDataTree = new InMemoryDataTreeFactory().create(
196 DataTreeConfiguration.DEFAULT_CONFIGURATION, SCHEMA_CONTEXT);
198 DataTreeModification mod = localDataTree.takeSnapshot().newModification();
199 mod.write(CarsModel.BASE_PATH, CarsModel.create());
201 localDataTree.validate(mod);
202 localDataTree.commit(localDataTree.prepare(mod));
204 NormalizedNode normalizedNode = dataTree.takeSnapshot().readNode(YangInstanceIdentifier.empty()).get();
205 pruningDataTreeModification.write(YangInstanceIdentifier.empty(), normalizedNode);
206 dataTree.commit(getCandidate());
208 Optional<NormalizedNode> actual = dataTree.takeSnapshot().readNode(YangInstanceIdentifier.empty());
209 assertTrue("Root present", actual.isPresent());
210 assertEquals("Root node", normalizedNode, actual.get());
214 public void testWriteRootNodeWithInvalidChild() throws Exception {
215 final Shard mockShard = Mockito.mock(Shard.class);
217 ShardDataTree shardDataTree = new ShardDataTree(mockShard, SCHEMA_CONTEXT, TreeType.CONFIGURATION);
218 NormalizedNode root = shardDataTree.readNode(YangInstanceIdentifier.empty()).get();
220 NormalizedNode normalizedNode = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
221 new YangInstanceIdentifier.NodeIdentifier(root.getIdentifier().getNodeType())).withChild(
222 ImmutableNodes.containerNode(AUG_CONTAINER)).build();
223 pruningDataTreeModification.write(YangInstanceIdentifier.empty(), normalizedNode);
224 dataTree.commit(getCandidate());
226 Optional<NormalizedNode> actual = dataTree.takeSnapshot().readNode(YangInstanceIdentifier.empty());
227 assertEquals("Root present", true, actual.isPresent());
228 assertEquals("Root node", root, actual.get());
233 public void testWriteWithInvalidNamespace() throws DataValidationFailedException {
234 NormalizedNode normalizedNode = PeopleModel.emptyContainer();
235 YangInstanceIdentifier path = PeopleModel.BASE_PATH;
237 pruningDataTreeModification.write(path, normalizedNode);
239 verify(mockModification, times(1)).write(path, normalizedNode);
241 DataTreeCandidate candidate = getCandidate();
242 assertEquals("getModificationType", ModificationType.UNMODIFIED, candidate.getRootNode().getModificationType());
246 public void testWriteWithInvalidChildNodeNames() throws DataValidationFailedException {
247 ContainerNode augContainer = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
248 new YangInstanceIdentifier.NodeIdentifier(AUG_CONTAINER)).withChild(
249 ImmutableNodes.containerNode(AUG_INNER_CONTAINER)).build();
251 DataContainerChild outerNode = outerNode(outerNodeEntry(1, innerNode("one", "two")));
252 ContainerNode normalizedNode = ImmutableContainerNodeBuilder.create()
253 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME)).withChild(outerNode)
254 .withChild(augContainer).withChild(ImmutableNodes.leafNode(AUG_QNAME, "aug"))
255 .withChild(ImmutableNodes.leafNode(NAME_QNAME, "name")).build();
257 YangInstanceIdentifier path = TestModel.TEST_PATH;
259 pruningDataTreeModification.write(path, normalizedNode);
261 dataTree.commit(getCandidate());
263 ContainerNode prunedNode = ImmutableContainerNodeBuilder.create()
264 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME)).withChild(outerNode)
265 .withChild(ImmutableNodes.leafNode(NAME_QNAME, "name")).build();
267 Optional<NormalizedNode> actual = dataTree.takeSnapshot().readNode(path);
268 assertTrue("After pruning present", actual.isPresent());
269 assertEquals("After pruning", prunedNode, actual.get());
273 public void testReady() {
274 pruningDataTreeModification.ready();
276 verify(mockModification).ready();
280 public void testApplyToCursor() {
281 DataTreeModificationCursor dataTreeModificationCursor = mock(DataTreeModificationCursor.class);
282 pruningDataTreeModification.applyToCursor(dataTreeModificationCursor);
284 verify(mockModification).applyToCursor(dataTreeModificationCursor);
288 public void testReadNode() {
289 pruningDataTreeModification.readNode(CarsModel.BASE_PATH);
291 verify(mockModification).readNode(CarsModel.BASE_PATH);
295 public void testNewModification() {
296 realModification.ready();
297 DataTreeModification dataTreeModification = pruningDataTreeModification.newModification();
299 assertTrue("new modification not of type PruningDataTreeModification",
300 dataTreeModification instanceof PruningDataTreeModification);
303 private DataTreeCandidate getCandidate() throws DataValidationFailedException {
304 pruningDataTreeModification.ready();
305 DataTreeModification mod = pruningDataTreeModification.delegate();
306 mod = mod == proxyModification ? realModification : mod;
307 dataTree.validate(mod);
308 return dataTree.prepare(mod);