Fix unit test CS warnings in sal-distributed-datastore
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / utils / PruningDataTreeModificationTest.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.datastore.utils;
10
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
26 import com.google.common.base.Optional;
27 import com.google.common.reflect.Reflection;
28 import java.lang.reflect.InvocationTargetException;
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.DataTreeCandidateTip;
47 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
48 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor;
49 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
50 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
51 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
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;
58
59 public class PruningDataTreeModificationTest {
60     static final SchemaContext SCHEMA_CONTEXT = SchemaContextHelper.select(SchemaContextHelper.CARS_YANG,
61             SchemaContextHelper.ODL_DATASTORE_TEST_YANG);
62
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);
65
66     @Mock
67     private DataTreeModification mockModification;
68
69     private TipProducingDataTree dataTree;
70     private DataTreeModification realModification;
71     private DataTreeModification proxyModification;
72     private PruningDataTreeModification pruningDataTreeModification;
73
74     @Before
75     public void setUp() {
76         MockitoAnnotations.initMocks(this);
77
78         dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION);
79         dataTree.setSchemaContext(SCHEMA_CONTEXT);
80
81         realModification = dataTree.takeSnapshot().newModification();
82         proxyModification = Reflection.newProxy(DataTreeModification.class, (proxy, method, args) -> {
83             try {
84                 method.invoke(mockModification, args);
85                 return method.invoke(realModification, args);
86             } catch (InvocationTargetException e) {
87                 throw e.getCause();
88             }
89         });
90
91         pruningDataTreeModification = new PruningDataTreeModification(proxyModification, dataTree, SCHEMA_CONTEXT);
92     }
93
94     @Test
95     public void testDelete() {
96         pruningDataTreeModification.delete(CarsModel.BASE_PATH);
97
98         verify(mockModification, times(1)).delete(CarsModel.BASE_PATH);
99     }
100
101     @Test
102     public void testDeleteOnException() {
103         YangInstanceIdentifier path = CarsModel.BASE_PATH;
104         doThrow(SchemaValidationFailedException.class).when(mockModification).delete(path);
105
106         pruningDataTreeModification.delete(path);
107
108         verify(mockModification, times(1)).delete(path);
109     }
110
111
112     @Test
113     public void testMerge() {
114         NormalizedNode<?, ?> normalizedNode = CarsModel.create();
115         YangInstanceIdentifier path = CarsModel.BASE_PATH;
116         pruningDataTreeModification.merge(path, normalizedNode);
117
118         verify(mockModification, times(1)).merge(path, normalizedNode);
119     }
120
121     @Test
122     public void testMergeWithInvalidNamespace() throws DataValidationFailedException {
123         NormalizedNode<?, ?> normalizedNode = PeopleModel.emptyContainer();
124         YangInstanceIdentifier path = PeopleModel.BASE_PATH;
125
126         pruningDataTreeModification.merge(path, normalizedNode);
127
128         verify(mockModification, times(1)).merge(path, normalizedNode);
129
130         DataTreeCandidateTip candidate = getCandidate();
131         assertEquals("getModificationType", ModificationType.UNMODIFIED, candidate.getRootNode().getModificationType());
132     }
133
134     @Test
135     public void testMergeWithInvalidChildNodeNames() throws DataValidationFailedException {
136         ContainerNode augContainer = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
137                 new YangInstanceIdentifier.NodeIdentifier(AUG_CONTAINER)).withChild(
138                         ImmutableNodes.containerNode(AUG_INNER_CONTAINER)).build();
139
140         DataContainerChild<?, ?> outerNode = outerNode(outerNodeEntry(1, innerNode("one", "two")));
141         ContainerNode normalizedNode = ImmutableContainerNodeBuilder.create()
142                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME)).withChild(outerNode)
143                 .withChild(augContainer).withChild(ImmutableNodes.leafNode(AUG_QNAME, "aug")).build();
144
145         YangInstanceIdentifier path = TestModel.TEST_PATH;
146
147         pruningDataTreeModification.merge(path, normalizedNode);
148
149         dataTree.commit(getCandidate());
150
151         ContainerNode prunedNode = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
152                 new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME)).withChild(outerNode).build();
153
154         Optional<NormalizedNode<?, ?>> actual = dataTree.takeSnapshot().readNode(path);
155         assertEquals("After pruning present", true, actual.isPresent());
156         assertEquals("After pruning", prunedNode, actual.get());
157     }
158
159     @Test
160     public void testMergeWithValidNamespaceAndInvalidNodeName() throws DataValidationFailedException {
161         NormalizedNode<?, ?> normalizedNode = ImmutableNodes.containerNode(INVALID_TEST_QNAME);
162         YangInstanceIdentifier path = INVALID_TEST_PATH;
163
164         pruningDataTreeModification.merge(path, normalizedNode);
165
166         verify(mockModification, times(1)).merge(path, normalizedNode);
167
168         DataTreeCandidateTip candidate = getCandidate();
169         assertEquals("getModificationType", ModificationType.UNMODIFIED, candidate.getRootNode().getModificationType());
170     }
171
172     @Test
173     public void testWrite() {
174         NormalizedNode<?, ?> normalizedNode = CarsModel.create();
175         YangInstanceIdentifier path = CarsModel.BASE_PATH;
176         pruningDataTreeModification.write(path, normalizedNode);
177
178         verify(mockModification, times(1)).write(path, normalizedNode);
179     }
180
181     @Test
182     public void testWriteRootNode() throws Exception {
183         final DataTree localDataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION);
184         localDataTree.setSchemaContext(SCHEMA_CONTEXT);
185
186         DataTreeModification mod = localDataTree.takeSnapshot().newModification();
187         mod.write(CarsModel.BASE_PATH, CarsModel.create());
188         mod.ready();
189         localDataTree.validate(mod);
190         localDataTree.commit(localDataTree.prepare(mod));
191
192         NormalizedNode<?, ?> normalizedNode = dataTree.takeSnapshot().readNode(YangInstanceIdentifier.EMPTY).get();
193         pruningDataTreeModification.write(YangInstanceIdentifier.EMPTY, normalizedNode);
194         dataTree.commit(getCandidate());
195
196         Optional<NormalizedNode<?, ?>> actual = dataTree.takeSnapshot().readNode(YangInstanceIdentifier.EMPTY);
197         assertEquals("Root present", true, actual.isPresent());
198         assertEquals("Root node", normalizedNode, actual.get());
199     }
200
201     @Test
202     public void testWriteRootNodeWithInvalidChild() throws Exception {
203         final Shard mockShard = Mockito.mock(Shard.class);
204
205         ShardDataTree shardDataTree = new ShardDataTree(mockShard, SCHEMA_CONTEXT, TreeType.CONFIGURATION);
206         NormalizedNode<?, ?> root = shardDataTree.readNode(YangInstanceIdentifier.EMPTY).get();
207
208         NormalizedNode<?, ?> normalizedNode = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
209                 new YangInstanceIdentifier.NodeIdentifier(root.getNodeType())).withChild(
210                         ImmutableNodes.containerNode(AUG_CONTAINER)).build();
211         pruningDataTreeModification.write(YangInstanceIdentifier.EMPTY, normalizedNode);
212         dataTree.commit(getCandidate());
213
214         Optional<NormalizedNode<?, ?>> actual = dataTree.takeSnapshot().readNode(YangInstanceIdentifier.EMPTY);
215         assertEquals("Root present", true, actual.isPresent());
216         assertEquals("Root node", root, actual.get());
217
218     }
219
220     @Test
221     public void testWriteWithInvalidNamespace() throws DataValidationFailedException {
222         NormalizedNode<?, ?> normalizedNode = PeopleModel.emptyContainer();
223         YangInstanceIdentifier path = PeopleModel.BASE_PATH;
224
225         pruningDataTreeModification.write(path, normalizedNode);
226
227         verify(mockModification, times(1)).write(path, normalizedNode);
228
229         DataTreeCandidateTip candidate = getCandidate();
230         assertEquals("getModificationType", ModificationType.UNMODIFIED, candidate.getRootNode().getModificationType());
231     }
232
233     @Test
234     public void testWriteWithInvalidChildNodeNames() throws DataValidationFailedException {
235         ContainerNode augContainer = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
236                 new YangInstanceIdentifier.NodeIdentifier(AUG_CONTAINER)).withChild(
237                         ImmutableNodes.containerNode(AUG_INNER_CONTAINER)).build();
238
239         DataContainerChild<?, ?> outerNode = outerNode(outerNodeEntry(1, innerNode("one", "two")));
240         ContainerNode normalizedNode = ImmutableContainerNodeBuilder.create()
241                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME)).withChild(outerNode)
242                 .withChild(augContainer).withChild(ImmutableNodes.leafNode(AUG_QNAME, "aug"))
243                 .withChild(ImmutableNodes.leafNode(NAME_QNAME, "name")).build();
244
245         YangInstanceIdentifier path = TestModel.TEST_PATH;
246
247         pruningDataTreeModification.write(path, normalizedNode);
248
249         dataTree.commit(getCandidate());
250
251         ContainerNode prunedNode = ImmutableContainerNodeBuilder.create()
252                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME)).withChild(outerNode)
253                 .withChild(ImmutableNodes.leafNode(NAME_QNAME, "name")).build();
254
255         Optional<NormalizedNode<?, ?>> actual = dataTree.takeSnapshot().readNode(path);
256         assertEquals("After pruning present", true, actual.isPresent());
257         assertEquals("After pruning", prunedNode, actual.get());
258     }
259
260     @Test
261     public void testReady() {
262         pruningDataTreeModification.ready();
263
264         verify(mockModification).ready();
265     }
266
267     @Test
268     public void testApplyToCursor() {
269         DataTreeModificationCursor dataTreeModificationCursor = mock(DataTreeModificationCursor.class);
270         pruningDataTreeModification.applyToCursor(dataTreeModificationCursor);
271
272         verify(mockModification).applyToCursor(dataTreeModificationCursor);
273     }
274
275     @Test
276     public void testReadNode() {
277         pruningDataTreeModification.readNode(CarsModel.BASE_PATH);
278
279         verify(mockModification).readNode(CarsModel.BASE_PATH);
280     }
281
282     @Test
283     public void testNewModification() {
284         realModification.ready();
285         DataTreeModification dataTreeModification = pruningDataTreeModification.newModification();
286
287         assertTrue("new modification not of type PruningDataTreeModification",
288                 dataTreeModification instanceof PruningDataTreeModification);
289     }
290
291     private DataTreeCandidateTip getCandidate() throws DataValidationFailedException {
292         pruningDataTreeModification.ready();
293         DataTreeModification mod = pruningDataTreeModification.delegate();
294         mod = mod == proxyModification ? realModification : mod;
295         dataTree.validate(mod);
296         DataTreeCandidateTip candidate = dataTree.prepare(mod);
297         return candidate;
298     }
299 }