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;
25 import com.google.common.base.Optional;
26 import com.google.common.reflect.Reflection;
27 import java.lang.reflect.InvocationHandler;
28 import java.lang.reflect.InvocationTargetException;
29 import java.lang.reflect.Method;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
35 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
36 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
37 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
38 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
39 import org.opendaylight.yangtools.yang.common.QName;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
43 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
45 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
46 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor;
47 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
48 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
49 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
50 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
51 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
52 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
53 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
54 import org.opendaylight.yangtools.yang.data.impl.schema.tree.SchemaValidationFailedException;
55 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
57 public class PruningDataTreeModificationTest {
58 static final SchemaContext SCHEMA_CONTEXT = SchemaContextHelper.select(SchemaContextHelper.CARS_YANG,
59 SchemaContextHelper.ODL_DATASTORE_TEST_YANG);
61 static final QName INVALID_TEST_QNAME = QName.create(TestModel.TEST_QNAME, "invalid");
62 static final YangInstanceIdentifier INVALID_TEST_PATH = YangInstanceIdentifier.of(INVALID_TEST_QNAME);
65 private DataTreeModification mockModification;
67 private TipProducingDataTree dataTree;
68 private DataTreeModification realModification;
69 private DataTreeModification proxyModification;
70 private PruningDataTreeModification pruningDataTreeModification;
74 MockitoAnnotations.initMocks(this);
76 dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION);
77 dataTree.setSchemaContext(SCHEMA_CONTEXT);
79 realModification = dataTree.takeSnapshot().newModification();
80 proxyModification = Reflection.newProxy(DataTreeModification.class, new InvocationHandler() {
82 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
84 method.invoke(mockModification, args);
85 return method.invoke(realModification, args);
86 } 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 DataTreeCandidateTip 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().withNodeIdentifier(
143 new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME)).withChild(outerNode).withChild(augContainer).
144 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 assertEquals("After pruning present", true, 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 DataTreeCandidateTip 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 ShardDataTree shardDataTree = new ShardDataTree(SCHEMA_CONTEXT, TreeType.CONFIGURATION);
185 DataTreeModification mod = shardDataTree.newModification();
187 mod.write(CarsModel.BASE_PATH, CarsModel.create());
188 shardDataTree.commit(mod);
190 NormalizedNode<?, ?> normalizedNode = shardDataTree.readNode(YangInstanceIdentifier.EMPTY).get();
191 pruningDataTreeModification.write(YangInstanceIdentifier.EMPTY, normalizedNode);
192 dataTree.commit(getCandidate());
194 Optional<NormalizedNode<?, ?>> actual = dataTree.takeSnapshot().readNode(YangInstanceIdentifier.EMPTY);
195 assertEquals("Root present", true, actual.isPresent());
196 assertEquals("Root node", normalizedNode, actual.get());
201 public void testWriteRootNodeWithInvalidChild() throws Exception{
202 ShardDataTree shardDataTree = new ShardDataTree(SCHEMA_CONTEXT, TreeType.CONFIGURATION);
203 NormalizedNode<?, ?> root = shardDataTree.readNode(YangInstanceIdentifier.EMPTY).get();
205 NormalizedNode<?, ?> normalizedNode = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
206 new YangInstanceIdentifier.NodeIdentifier(root.getNodeType())).withChild(
207 ImmutableNodes.containerNode(AUG_CONTAINER)).build();
208 pruningDataTreeModification.write(YangInstanceIdentifier.EMPTY, normalizedNode);
209 dataTree.commit(getCandidate());
211 Optional<NormalizedNode<?, ?>> actual = dataTree.takeSnapshot().readNode(YangInstanceIdentifier.EMPTY);
212 assertEquals("Root present", true, actual.isPresent());
213 assertEquals("Root node", root, actual.get());
218 public void testWriteWithInvalidNamespace() throws DataValidationFailedException{
219 NormalizedNode<?, ?> normalizedNode = PeopleModel.emptyContainer();
220 YangInstanceIdentifier path = PeopleModel.BASE_PATH;
222 pruningDataTreeModification.write(path, normalizedNode);
224 verify(mockModification, times(1)).write(path, normalizedNode);
226 DataTreeCandidateTip candidate = getCandidate();
227 assertEquals("getModificationType", ModificationType.UNMODIFIED, candidate.getRootNode().getModificationType());
231 public void testWriteWithInvalidChildNodeNames() throws DataValidationFailedException{
232 ContainerNode augContainer = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
233 new YangInstanceIdentifier.NodeIdentifier(AUG_CONTAINER)).withChild(
234 ImmutableNodes.containerNode(AUG_INNER_CONTAINER)).build();
236 DataContainerChild<?, ?> outerNode = outerNode(outerNodeEntry(1, innerNode("one", "two")));
237 ContainerNode normalizedNode = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
238 new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME)).withChild(outerNode).withChild(augContainer).
239 withChild(ImmutableNodes.leafNode(AUG_QNAME, "aug")).
240 withChild(ImmutableNodes.leafNode(NAME_QNAME, "name")).build();
242 YangInstanceIdentifier path = TestModel.TEST_PATH;
244 pruningDataTreeModification.write(path, normalizedNode);
246 dataTree.commit(getCandidate());
248 ContainerNode prunedNode = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
249 new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME)).withChild(outerNode).
250 withChild(ImmutableNodes.leafNode(NAME_QNAME, "name")).build();
252 Optional<NormalizedNode<?, ?>> actual = dataTree.takeSnapshot().readNode(path);
253 assertEquals("After pruning present", true, actual.isPresent());
254 assertEquals("After pruning", prunedNode, actual.get());
258 public void testReady(){
259 pruningDataTreeModification.ready();
261 verify(mockModification).ready();
265 public void testApplyToCursor(){
266 DataTreeModificationCursor dataTreeModificationCursor = mock(DataTreeModificationCursor.class);
267 pruningDataTreeModification.applyToCursor(dataTreeModificationCursor);
269 verify(mockModification).applyToCursor(dataTreeModificationCursor);
273 public void testReadNode(){
274 pruningDataTreeModification.readNode(CarsModel.BASE_PATH);
276 verify(mockModification).readNode(CarsModel.BASE_PATH);
280 public void testNewModification(){
281 realModification.ready();
282 DataTreeModification dataTreeModification = pruningDataTreeModification.newModification();
284 assertTrue("new modification not of type PruningDataTreeModification", dataTreeModification instanceof PruningDataTreeModification);
287 private DataTreeCandidateTip getCandidate() throws DataValidationFailedException {
288 pruningDataTreeModification.ready();
289 DataTreeModification mod = pruningDataTreeModification.getResultingModification();
290 mod = mod == proxyModification ? realModification : mod;
291 dataTree.validate(mod);
292 DataTreeCandidateTip candidate = dataTree.prepare(mod);