Make private methods static
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / cluster / datastore / node / utils / transformer / NormalizedNodePrunerTest.java
1 /*
2  * Copyright (c) 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.node.utils.transformer;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.never;
18 import static org.mockito.Mockito.verify;
19 import com.google.common.collect.ImmutableMap;
20 import com.google.common.collect.ImmutableSet;
21 import java.io.IOException;
22 import java.util.concurrent.atomic.AtomicInteger;
23 import javax.xml.transform.dom.DOMSource;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.ArgumentCaptor;
28 import org.mockito.Mock;
29 import org.mockito.MockitoAnnotations;
30 import org.opendaylight.controller.cluster.datastore.node.utils.NormalizedNodeNavigator;
31 import org.opendaylight.controller.cluster.datastore.node.utils.NormalizedNodeVisitor;
32 import org.opendaylight.controller.cluster.datastore.util.TestModel;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
38 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
39 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
40 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
41 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
43 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
44 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
45 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
46 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
47
48 public class NormalizedNodePrunerTest {
49
50     private NormalizedNodePruner prunerFullSchema;
51
52     private NormalizedNodePruner prunerNoAugSchema;
53
54     @Mock
55     private NormalizedNodeBuilderWrapper normalizedNodeBuilderWrapper;
56
57     @Mock
58     private NormalizedNodeContainerBuilder normalizedNodeContainerBuilder;
59
60     @Mock
61     private NormalizedNode<?,?> normalizedNode;
62
63     @Before
64     public void setUp(){
65         MockitoAnnotations.initMocks(this);
66         prunerFullSchema = new NormalizedNodePruner(TestModel.createTestContext());
67         prunerNoAugSchema = new NormalizedNodePruner(TestModel.createTestContextWithoutAugmentationSchema());
68         doReturn(normalizedNodeContainerBuilder).when(normalizedNodeBuilderWrapper).builder();
69         doReturn(TestModel.BOOLEAN_LEAF_QNAME).when(normalizedNodeBuilderWrapper).nodeType();
70         doReturn(normalizedNode).when(normalizedNodeContainerBuilder).build();
71         doReturn(new YangInstanceIdentifier.NodeIdentifier(TestModel.BOOLEAN_LEAF_QNAME)).when(normalizedNodeBuilderWrapper).identifier();
72     }
73
74     @Test
75     public void testNodesNotPrunedWhenSchemaPresent() throws IOException {
76         NormalizedNodePruner pruner = prunerFullSchema;
77
78         NormalizedNodeWriter normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(pruner);
79
80         NormalizedNode<?, ?> expected = createTestContainer();
81
82         normalizedNodeWriter.write(expected);
83
84         NormalizedNode<?, ?> actual = pruner.normalizedNode();
85
86         assertEquals(expected, actual);
87
88     }
89
90     @Test(expected = IllegalStateException.class)
91     public void testReusePruner() throws IOException {
92         NormalizedNodePruner pruner = prunerFullSchema;
93
94         NormalizedNodeWriter normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(pruner);
95
96         NormalizedNode<?, ?> expected = createTestContainer();
97
98         normalizedNodeWriter.write(expected);
99
100         NormalizedNode<?, ?> actual = pruner.normalizedNode();
101
102         assertEquals(expected, actual);
103
104         NormalizedNodeWriter.forStreamWriter(pruner).write(expected);
105
106     }
107
108
109     @Test
110     public void testNodesPrunedWhenAugmentationSchemaNotPresent() throws IOException {
111         NormalizedNodePruner pruner = prunerNoAugSchema;
112
113         NormalizedNodeWriter normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(pruner);
114
115         NormalizedNode<?, ?> expected = createTestContainer();
116
117         normalizedNodeWriter.write(expected);
118
119         NormalizedNode<?, ?> actual = pruner.normalizedNode();
120
121         Assert.assertNotEquals(expected, actual);
122
123         // Asserting true here instead of checking actual value because I don't want this assertion to be fragile
124         assertTrue(countNodes(expected, "store:aug") > 0);
125
126         // All nodes from the augmentation module are gone from the resulting node
127         assertEquals(0, countNodes(actual, "store:aug"));
128     }
129
130     @Test
131     public void testNodesPrunedWhenTestSchemaNotPresent() throws IOException {
132         NormalizedNodePruner pruner = new NormalizedNodePruner(TestModel.createTestContextWithoutTestSchema());
133
134         NormalizedNodeWriter normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(pruner);
135
136         NormalizedNode<?, ?> expected = createTestContainer();
137
138         normalizedNodeWriter.write(expected);
139
140         NormalizedNode<?, ?> actual = pruner.normalizedNode();
141
142         // Since top level schema is missing null is returned
143         assertNull(actual);
144
145         // Asserting true here instead of checking actual value because I don't want this assertion to be fragile
146         assertTrue(countNodes(expected, "urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test") > 0);
147
148     }
149
150
151     private static int countNodes(NormalizedNode<?,?> normalizedNode, final String namespaceFilter){
152         if(normalizedNode == null){
153             return 0;
154         }
155         final AtomicInteger count = new AtomicInteger();
156         new NormalizedNodeNavigator(new NormalizedNodeVisitor() {
157
158             @Override
159             public void visitNode(int level, String parentPath, NormalizedNode<?, ?> normalizedNode) {
160                 if(!(normalizedNode.getIdentifier() instanceof YangInstanceIdentifier.AugmentationIdentifier)) {
161                     if (normalizedNode.getIdentifier().getNodeType().getNamespace().toString().contains(namespaceFilter)) {
162                         count.incrementAndGet();
163                     }
164                 }
165             }
166         }).navigate(YangInstanceIdentifier.builder().build().toString(), normalizedNode);
167
168         return count.get();
169     }
170
171     @Test(expected = IllegalStateException.class)
172     public void testLeafNodeHasNoParent() throws IOException {
173         prunerFullSchema.leafNode(new YangInstanceIdentifier.NodeIdentifier(TestModel.BOOLEAN_LEAF_QNAME), mock(Object.class));
174     }
175
176     @Test
177     public void testLeafNodeHasParent() throws IOException {
178         prunerFullSchema.stack().push(normalizedNodeBuilderWrapper);
179         Object o = mock(Object.class);
180         prunerFullSchema.leafNode(new YangInstanceIdentifier.NodeIdentifier(TestModel.BOOLEAN_LEAF_QNAME), o);
181
182         ArgumentCaptor<NormalizedNode> captor = ArgumentCaptor.forClass(NormalizedNode.class);
183
184         verify(normalizedNodeContainerBuilder).addChild(captor.capture());
185
186         NormalizedNode<?, ?> value = captor.getValue();
187         assertEquals(normalizedNodeBuilderWrapper.identifier().getNodeType(), value.getNodeType());
188         assertEquals(normalizedNodeBuilderWrapper.identifier(), value.getIdentifier());
189         assertEquals(o, value.getValue());
190
191     }
192
193     @Test
194     public void testLeafNodeSchemaMissing() throws IOException {
195         prunerNoAugSchema.stack().push(normalizedNodeBuilderWrapper);
196         prunerNoAugSchema.leafNode(new YangInstanceIdentifier.NodeIdentifier(TestModel.AUG_CONT_QNAME), mock(Object.class));
197         verify(normalizedNodeContainerBuilder, never()).addChild(any(NormalizedNode.class));
198     }
199
200     @Test(expected = IllegalStateException.class)
201     public void testLeafSetEntryNodeHasNoParent() throws IOException {
202         prunerFullSchema.leafSetEntryNode(mock(Object.class));
203     }
204
205     @Test
206     public void testLeafSetEntryNodeHasParent() throws IOException {
207         prunerFullSchema.stack().push(normalizedNodeBuilderWrapper);
208         Object o = mock(Object.class);
209         YangInstanceIdentifier.PathArgument nodeIdentifier
210                 = new YangInstanceIdentifier.NodeWithValue(normalizedNodeBuilderWrapper.identifier().getNodeType(), o);
211         prunerFullSchema.leafSetEntryNode(o);
212
213         ArgumentCaptor<NormalizedNode> captor = ArgumentCaptor.forClass(NormalizedNode.class);
214
215         verify(normalizedNodeContainerBuilder).addChild(captor.capture());
216
217         NormalizedNode<?, ?> value = captor.getValue();
218         assertEquals(nodeIdentifier.getNodeType(), value.getNodeType());
219         assertEquals(nodeIdentifier, value.getIdentifier());
220         assertEquals(o, value.getValue());
221
222     }
223
224     @Test
225     public void testLeafSetEntryNodeSchemaMissing() throws IOException {
226         doReturn(new YangInstanceIdentifier.NodeIdentifier(TestModel.AUG_CONT_QNAME)).when(normalizedNodeBuilderWrapper).identifier();
227
228         prunerNoAugSchema.stack().push(normalizedNodeBuilderWrapper);
229         prunerNoAugSchema.leafSetEntryNode(new YangInstanceIdentifier.NodeIdentifier(TestModel.AUG_CONT_QNAME));
230
231         verify(normalizedNodeContainerBuilder, never()).addChild(any(NormalizedNode.class));
232     }
233
234     @Test(expected = IllegalStateException.class)
235     public void testAnyXMLNodeHasNoParent() throws IOException {
236         prunerFullSchema.anyxmlNode(new YangInstanceIdentifier.NodeIdentifier(TestModel.BOOLEAN_LEAF_QNAME), mock(Object.class));
237     }
238
239     @Test
240     public void testAnyXMLNodeHasParent() throws IOException {
241         prunerFullSchema.stack().push(normalizedNodeBuilderWrapper);
242         YangInstanceIdentifier.NodeIdentifier nodeIdentifier = new YangInstanceIdentifier.NodeIdentifier(TestModel.BOOLEAN_LEAF_QNAME);
243         DOMSource o = mock(DOMSource.class);
244         prunerFullSchema.anyxmlNode(nodeIdentifier, o);
245
246         ArgumentCaptor<NormalizedNode> captor = ArgumentCaptor.forClass(NormalizedNode.class);
247
248         verify(normalizedNodeContainerBuilder).addChild(captor.capture());
249
250         NormalizedNode<?, ?> value = captor.getValue();
251         assertEquals(nodeIdentifier.getNodeType(), value.getNodeType());
252         assertEquals(nodeIdentifier, value.getIdentifier());
253         assertEquals(o, value.getValue());
254     }
255
256     @Test
257     public void testAnyXmlNodeSchemaMissing() throws IOException {
258         prunerNoAugSchema.stack().push(normalizedNodeBuilderWrapper);
259         prunerNoAugSchema.anyxmlNode(new YangInstanceIdentifier.NodeIdentifier(TestModel.AUG_CONT_QNAME), mock(DOMSource.class));
260
261         verify(normalizedNodeContainerBuilder, never()).addChild(any(NormalizedNode.class));
262
263
264     }
265
266
267     @Test
268     public void testLeafSetPushesBuilderToStack() throws IOException {
269         prunerFullSchema.startLeafSet(new YangInstanceIdentifier.NodeIdentifier(TestModel.BOOLEAN_LEAF_QNAME), 10);
270
271         assertEquals(1, prunerFullSchema.stack().size());
272         assertTrue(prunerFullSchema.stack().peek().toString(), prunerFullSchema.stack().peek() instanceof NormalizedNodeBuilderWrapper);
273         assertTrue(prunerFullSchema.stack().peek().builder().toString(), prunerFullSchema.stack().peek().builder() instanceof ListNodeBuilder);
274     }
275
276     @Test
277     public void testStartContainerNodePushesBuilderToStack() throws IOException {
278         prunerFullSchema.startContainerNode(new YangInstanceIdentifier.NodeIdentifier(TestModel.BOOLEAN_LEAF_QNAME), 10);
279
280         assertEquals(1, prunerFullSchema.stack().size());
281         assertTrue(prunerFullSchema.stack().peek().toString(), prunerFullSchema.stack().peek() instanceof NormalizedNodeBuilderWrapper);
282         assertTrue(prunerFullSchema.stack().peek().builder().toString(), prunerFullSchema.stack().peek().builder() instanceof DataContainerNodeAttrBuilder);
283     }
284
285     @Test
286     public void testStartUnkeyedListPushesBuilderToStack() throws IOException {
287         prunerFullSchema.startUnkeyedList(new YangInstanceIdentifier.NodeIdentifier(TestModel.BOOLEAN_LEAF_QNAME), 10);
288
289         assertEquals(1, prunerFullSchema.stack().size());
290         assertTrue(prunerFullSchema.stack().peek().toString(), prunerFullSchema.stack().peek() instanceof NormalizedNodeBuilderWrapper);
291         assertTrue(prunerFullSchema.stack().peek().builder().toString(), prunerFullSchema.stack().peek().builder() instanceof CollectionNodeBuilder);
292     }
293
294     @Test
295     public void testStartUnkeyedListItemPushesBuilderToStack() throws IOException {
296         prunerFullSchema.startUnkeyedListItem(new YangInstanceIdentifier.NodeIdentifier(TestModel.BOOLEAN_LEAF_QNAME), 10);
297
298         assertEquals(1, prunerFullSchema.stack().size());
299         assertTrue(prunerFullSchema.stack().peek().toString(), prunerFullSchema.stack().peek() instanceof NormalizedNodeBuilderWrapper);
300         assertTrue(prunerFullSchema.stack().peek().builder().toString(), prunerFullSchema.stack().peek().builder() instanceof DataContainerNodeAttrBuilder);
301     }
302
303     @Test
304     public void testStartMapNodePushesBuilderToStack() throws IOException {
305         prunerFullSchema.startMapNode(new YangInstanceIdentifier.NodeIdentifier(TestModel.BOOLEAN_LEAF_QNAME), 10);
306
307         assertEquals(1, prunerFullSchema.stack().size());
308         assertTrue(prunerFullSchema.stack().peek().toString(), prunerFullSchema.stack().peek() instanceof NormalizedNodeBuilderWrapper);
309         assertTrue(prunerFullSchema.stack().peek().builder().toString(), prunerFullSchema.stack().peek().builder() instanceof CollectionNodeBuilder);
310     }
311
312     @Test
313     public void testStartMapEntryNodePushesBuilderToStack() throws IOException {
314         prunerFullSchema.startMapEntryNode(
315                 new YangInstanceIdentifier.NodeIdentifierWithPredicates(TestModel.BOOLEAN_LEAF_QNAME,
316                         ImmutableMap.<QName, Object>of(TestModel.BOOLEAN_LEAF_QNAME, "value")), 10);
317
318         assertEquals(1, prunerFullSchema.stack().size());
319         assertTrue(prunerFullSchema.stack().peek().toString(), prunerFullSchema.stack().peek() instanceof NormalizedNodeBuilderWrapper);
320         assertTrue(prunerFullSchema.stack().peek().builder().toString(), prunerFullSchema.stack().peek().builder() instanceof DataContainerNodeAttrBuilder);
321     }
322
323     @Test
324     public void testStartOrderedMapNodePushesBuilderToStack() throws IOException {
325         prunerFullSchema.startOrderedMapNode(new YangInstanceIdentifier.NodeIdentifier(TestModel.BOOLEAN_LEAF_QNAME), 10);
326
327         assertEquals(1, prunerFullSchema.stack().size());
328         assertTrue(prunerFullSchema.stack().peek().toString(), prunerFullSchema.stack().peek() instanceof NormalizedNodeBuilderWrapper);
329         assertTrue(prunerFullSchema.stack().peek().builder().toString(), prunerFullSchema.stack().peek().builder() instanceof CollectionNodeBuilder);
330     }
331
332     @Test
333     public void testStartChoiceNodePushesBuilderToStack() throws IOException {
334         prunerFullSchema.startChoiceNode(new YangInstanceIdentifier.NodeIdentifier(TestModel.BOOLEAN_LEAF_QNAME), 10);
335
336         assertEquals(1, prunerFullSchema.stack().size());
337         assertTrue(prunerFullSchema.stack().peek().toString(), prunerFullSchema.stack().peek() instanceof NormalizedNodeBuilderWrapper);
338         assertTrue(prunerFullSchema.stack().peek().builder().toString(), prunerFullSchema.stack().peek().builder() instanceof DataContainerNodeBuilder);
339     }
340
341     @Test
342     public void testStartAugmentationPushesBuilderToStack() throws IOException {
343         prunerFullSchema.startAugmentationNode(new YangInstanceIdentifier.AugmentationIdentifier(ImmutableSet.of(TestModel.AUG_CONT_QNAME)));
344
345         assertEquals(1, prunerFullSchema.stack().size());
346         assertTrue(prunerFullSchema.stack().peek().toString(), prunerFullSchema.stack().peek() instanceof NormalizedNodeBuilderWrapper);
347         assertTrue(prunerFullSchema.stack().peek().builder().toString(), prunerFullSchema.stack().peek().builder() instanceof DataContainerNodeBuilder);
348     }
349
350     @Test(expected = IllegalStateException.class)
351     public void testEndNodeWhenNoBuildersOnStack() throws IOException {
352         prunerFullSchema.endNode();
353     }
354
355     @Test
356     public void testEndNodeWhenOneBuildersOnStack() throws IOException {
357         prunerFullSchema.stack().push(normalizedNodeBuilderWrapper);
358         prunerFullSchema.endNode();
359         assertEquals(normalizedNode, prunerFullSchema.normalizedNode());
360     }
361
362     @Test
363     public void testEndNodeSchemaMissing() throws IOException {
364         doReturn(new YangInstanceIdentifier.NodeIdentifier(TestModel.AUG_CONT_QNAME)).when(normalizedNodeBuilderWrapper).identifier();
365
366         prunerNoAugSchema.stack().push(normalizedNodeBuilderWrapper);
367         prunerNoAugSchema.endNode();
368
369         assertEquals(null, prunerNoAugSchema.normalizedNode());
370     }
371
372     @Test
373     public void testEndNodeWhenMoreThanOneBuilderOnStack() throws IOException {
374         // A little lazy in adding the "parent" builder
375         prunerFullSchema.stack().push(normalizedNodeBuilderWrapper);
376         prunerFullSchema.stack().push(normalizedNodeBuilderWrapper);
377         prunerFullSchema.endNode();
378         assertEquals(null, prunerFullSchema.normalizedNode());
379
380         verify(normalizedNodeContainerBuilder).addChild(any(NormalizedNode.class));
381     }
382
383     private static NormalizedNode<?, ?> createTestContainer() {
384         byte[] bytes1 = {1,2,3};
385         LeafSetEntryNode<Object> entry1 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
386                 new YangInstanceIdentifier.NodeWithValue(TestModel.BINARY_LEAF_LIST_QNAME, bytes1)).
387                 withValue(bytes1).build();
388
389         byte[] bytes2 = {};
390         LeafSetEntryNode<Object> entry2 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
391                 new YangInstanceIdentifier.NodeWithValue(TestModel.BINARY_LEAF_LIST_QNAME, bytes2)).
392                 withValue(bytes2).build();
393
394         LeafSetEntryNode<Object> entry3 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
395                 new YangInstanceIdentifier.NodeWithValue(TestModel.BINARY_LEAF_LIST_QNAME, null)).
396                 withValue(null).build();
397
398
399         return TestModel.createBaseTestContainerBuilder().
400                 withChild(ImmutableLeafSetNodeBuilder.create().withNodeIdentifier(
401                         new YangInstanceIdentifier.NodeIdentifier(TestModel.BINARY_LEAF_LIST_QNAME)).
402                         withChild(entry1).withChild(entry2).withChild(entry3).build()).
403                 withChild(ImmutableNodes.leafNode(TestModel.SOME_BINARY_DATA_QNAME, new byte[]{1, 2, 3, 4})).
404                 withChild(Builders.orderedMapBuilder().
405                         withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.ORDERED_LIST_QNAME)).
406                         withChild(ImmutableNodes.mapEntry(TestModel.ORDERED_LIST_ENTRY_QNAME,
407                                 TestModel.ID_QNAME, 11)).build()).
408                 build();
409     }
410 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.