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
9 package org.opendaylight.controller.cluster.datastore.utils;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Mockito.doThrow;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17 import static org.opendaylight.controller.md.cluster.datastore.model.CompositeModel.AUG_CONTAINER;
18 import static org.opendaylight.controller.md.cluster.datastore.model.CompositeModel.AUG_INNER_CONTAINER;
19 import static org.opendaylight.controller.md.cluster.datastore.model.CompositeModel.AUG_QNAME;
20 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.NAME_QNAME;
21 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.TEST_QNAME;
22 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.innerNode;
23 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.outerNode;
24 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.outerNodeEntry;
26 import com.google.common.reflect.Reflection;
27 import java.lang.reflect.InvocationTargetException;
28 import java.util.Optional;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.Mock;
32 import org.mockito.Mockito;
33 import org.mockito.MockitoAnnotations;
34 import org.opendaylight.controller.cluster.datastore.Shard;
35 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
36 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
37 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
38 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
39 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
40 import org.opendaylight.yangtools.yang.common.QName;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
44 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
46 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
47 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
48 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
49 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor;
50 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
51 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
52 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
53 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
54 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
55 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
56 import org.opendaylight.yangtools.yang.data.impl.schema.tree.SchemaValidationFailedException;
57 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
59 public class PruningDataTreeModificationTest {
60 static final SchemaContext SCHEMA_CONTEXT = SchemaContextHelper.select(SchemaContextHelper.CARS_YANG,
61 SchemaContextHelper.ODL_DATASTORE_TEST_YANG);
63 static final QName INVALID_TEST_QNAME = QName.create(TestModel.TEST_QNAME, "invalid");
64 static final YangInstanceIdentifier INVALID_TEST_PATH = YangInstanceIdentifier.of(INVALID_TEST_QNAME);
67 private DataTreeModification mockModification;
69 private DataTree dataTree;
70 private DataTreeModification realModification;
71 private DataTreeModification proxyModification;
72 private PruningDataTreeModification pruningDataTreeModification;
75 @SuppressWarnings("checkstyle:avoidHidingCauseException")
77 MockitoAnnotations.initMocks(this);
79 dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION,
82 realModification = dataTree.takeSnapshot().newModification();
83 proxyModification = Reflection.newProxy(DataTreeModification.class, (proxy, method, args) -> {
85 method.invoke(mockModification, args);
86 return method.invoke(realModification, args);
87 } catch (InvocationTargetException e) {
92 pruningDataTreeModification = new PruningDataTreeModification(proxyModification, dataTree, SCHEMA_CONTEXT);
96 public void testDelete() {
97 pruningDataTreeModification.delete(CarsModel.BASE_PATH);
99 verify(mockModification, times(1)).delete(CarsModel.BASE_PATH);
103 public void testDeleteOnException() {
104 YangInstanceIdentifier path = CarsModel.BASE_PATH;
105 doThrow(SchemaValidationFailedException.class).when(mockModification).delete(path);
107 pruningDataTreeModification.delete(path);
109 verify(mockModification, times(1)).delete(path);
114 public void testMerge() {
115 NormalizedNode<?, ?> normalizedNode = CarsModel.create();
116 YangInstanceIdentifier path = CarsModel.BASE_PATH;
117 pruningDataTreeModification.merge(path, normalizedNode);
119 verify(mockModification, times(1)).merge(path, normalizedNode);
123 public void testMergeWithInvalidNamespace() throws DataValidationFailedException {
124 NormalizedNode<?, ?> normalizedNode = PeopleModel.emptyContainer();
125 YangInstanceIdentifier path = PeopleModel.BASE_PATH;
127 pruningDataTreeModification.merge(path, normalizedNode);
129 verify(mockModification, times(1)).merge(path, normalizedNode);
131 DataTreeCandidate candidate = getCandidate();
132 assertEquals("getModificationType", ModificationType.UNMODIFIED, candidate.getRootNode().getModificationType());
136 public void testMergeWithInvalidChildNodeNames() throws DataValidationFailedException {
137 ContainerNode augContainer = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
138 new YangInstanceIdentifier.NodeIdentifier(AUG_CONTAINER)).withChild(
139 ImmutableNodes.containerNode(AUG_INNER_CONTAINER)).build();
141 DataContainerChild<?, ?> outerNode = outerNode(outerNodeEntry(1, innerNode("one", "two")));
142 ContainerNode normalizedNode = ImmutableContainerNodeBuilder.create()
143 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME)).withChild(outerNode)
144 .withChild(augContainer).withChild(ImmutableNodes.leafNode(AUG_QNAME, "aug")).build();
146 YangInstanceIdentifier path = TestModel.TEST_PATH;
148 pruningDataTreeModification.merge(path, normalizedNode);
150 dataTree.commit(getCandidate());
152 ContainerNode prunedNode = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
153 new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME)).withChild(outerNode).build();
155 Optional<NormalizedNode<?, ?>> actual = dataTree.takeSnapshot().readNode(path);
156 assertTrue("After pruning present", actual.isPresent());
157 assertEquals("After pruning", prunedNode, actual.get());
161 public void testMergeWithValidNamespaceAndInvalidNodeName() throws DataValidationFailedException {
162 NormalizedNode<?, ?> normalizedNode = ImmutableNodes.containerNode(INVALID_TEST_QNAME);
163 YangInstanceIdentifier path = INVALID_TEST_PATH;
165 pruningDataTreeModification.merge(path, normalizedNode);
167 verify(mockModification, times(1)).merge(path, normalizedNode);
169 DataTreeCandidate candidate = getCandidate();
170 assertEquals("getModificationType", ModificationType.UNMODIFIED, candidate.getRootNode().getModificationType());
174 public void testWrite() {
175 NormalizedNode<?, ?> normalizedNode = CarsModel.create();
176 YangInstanceIdentifier path = CarsModel.BASE_PATH;
177 pruningDataTreeModification.write(path, normalizedNode);
179 verify(mockModification, times(1)).write(path, normalizedNode);
183 public void testWriteRootNode() throws Exception {
184 final DataTree localDataTree = new InMemoryDataTreeFactory().create(
185 DataTreeConfiguration.DEFAULT_CONFIGURATION, SCHEMA_CONTEXT);
187 DataTreeModification mod = localDataTree.takeSnapshot().newModification();
188 mod.write(CarsModel.BASE_PATH, CarsModel.create());
190 localDataTree.validate(mod);
191 localDataTree.commit(localDataTree.prepare(mod));
193 NormalizedNode<?, ?> normalizedNode = dataTree.takeSnapshot().readNode(YangInstanceIdentifier.EMPTY).get();
194 pruningDataTreeModification.write(YangInstanceIdentifier.EMPTY, normalizedNode);
195 dataTree.commit(getCandidate());
197 Optional<NormalizedNode<?, ?>> actual = dataTree.takeSnapshot().readNode(YangInstanceIdentifier.EMPTY);
198 assertTrue("Root present", actual.isPresent());
199 assertEquals("Root node", normalizedNode, actual.get());
203 public void testWriteRootNodeWithInvalidChild() throws Exception {
204 final Shard mockShard = Mockito.mock(Shard.class);
206 ShardDataTree shardDataTree = new ShardDataTree(mockShard, SCHEMA_CONTEXT, TreeType.CONFIGURATION);
207 NormalizedNode<?, ?> root = shardDataTree.readNode(YangInstanceIdentifier.EMPTY).get();
209 NormalizedNode<?, ?> normalizedNode = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
210 new YangInstanceIdentifier.NodeIdentifier(root.getNodeType())).withChild(
211 ImmutableNodes.containerNode(AUG_CONTAINER)).build();
212 pruningDataTreeModification.write(YangInstanceIdentifier.EMPTY, normalizedNode);
213 dataTree.commit(getCandidate());
215 Optional<NormalizedNode<?, ?>> actual = dataTree.takeSnapshot().readNode(YangInstanceIdentifier.EMPTY);
216 assertEquals("Root present", true, actual.isPresent());
217 assertEquals("Root node", root, actual.get());
222 public void testWriteWithInvalidNamespace() throws DataValidationFailedException {
223 NormalizedNode<?, ?> normalizedNode = PeopleModel.emptyContainer();
224 YangInstanceIdentifier path = PeopleModel.BASE_PATH;
226 pruningDataTreeModification.write(path, normalizedNode);
228 verify(mockModification, times(1)).write(path, normalizedNode);
230 DataTreeCandidate candidate = getCandidate();
231 assertEquals("getModificationType", ModificationType.UNMODIFIED, candidate.getRootNode().getModificationType());
235 public void testWriteWithInvalidChildNodeNames() throws DataValidationFailedException {
236 ContainerNode augContainer = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
237 new YangInstanceIdentifier.NodeIdentifier(AUG_CONTAINER)).withChild(
238 ImmutableNodes.containerNode(AUG_INNER_CONTAINER)).build();
240 DataContainerChild<?, ?> outerNode = outerNode(outerNodeEntry(1, innerNode("one", "two")));
241 ContainerNode normalizedNode = ImmutableContainerNodeBuilder.create()
242 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME)).withChild(outerNode)
243 .withChild(augContainer).withChild(ImmutableNodes.leafNode(AUG_QNAME, "aug"))
244 .withChild(ImmutableNodes.leafNode(NAME_QNAME, "name")).build();
246 YangInstanceIdentifier path = TestModel.TEST_PATH;
248 pruningDataTreeModification.write(path, normalizedNode);
250 dataTree.commit(getCandidate());
252 ContainerNode prunedNode = ImmutableContainerNodeBuilder.create()
253 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME)).withChild(outerNode)
254 .withChild(ImmutableNodes.leafNode(NAME_QNAME, "name")).build();
256 Optional<NormalizedNode<?, ?>> actual = dataTree.takeSnapshot().readNode(path);
257 assertTrue("After pruning present", actual.isPresent());
258 assertEquals("After pruning", prunedNode, actual.get());
262 public void testReady() {
263 pruningDataTreeModification.ready();
265 verify(mockModification).ready();
269 public void testApplyToCursor() {
270 DataTreeModificationCursor dataTreeModificationCursor = mock(DataTreeModificationCursor.class);
271 pruningDataTreeModification.applyToCursor(dataTreeModificationCursor);
273 verify(mockModification).applyToCursor(dataTreeModificationCursor);
277 public void testReadNode() {
278 pruningDataTreeModification.readNode(CarsModel.BASE_PATH);
280 verify(mockModification).readNode(CarsModel.BASE_PATH);
284 public void testNewModification() {
285 realModification.ready();
286 DataTreeModification dataTreeModification = pruningDataTreeModification.newModification();
288 assertTrue("new modification not of type PruningDataTreeModification",
289 dataTreeModification instanceof PruningDataTreeModification);
292 private DataTreeCandidate getCandidate() throws DataValidationFailedException {
293 pruningDataTreeModification.ready();
294 DataTreeModification mod = pruningDataTreeModification.delegate();
295 mod = mod == proxyModification ? realModification : mod;
296 dataTree.validate(mod);
297 return dataTree.prepare(mod);