Unit tests for ofoverlay
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / statistics / SflowClientSettingsListenerTest.java
1 /*\r
2  * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 \r
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.statistics;\r
10 \r
11 import static org.mockito.Mockito.mock;\r
12 import static org.mockito.Mockito.spy;\r
13 import static org.mockito.Mockito.times;\r
14 import static org.mockito.Mockito.verify;\r
15 import static org.mockito.Mockito.when;\r
16 \r
17 import java.util.Set;\r
18 import java.util.concurrent.ScheduledExecutorService;\r
19 \r
20 import com.google.common.collect.ImmutableSet;\r
21 import org.junit.Before;\r
22 import org.junit.Test;\r
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;\r
24 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;\r
25 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;\r
26 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;\r
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;\r
28 import org.opendaylight.groupbasedpolicy.api.StatisticsManager;\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayConfig;\r
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.sflow.values.SflowClientSettings;\r
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;\r
32 \r
33 public class SflowClientSettingsListenerTest {\r
34 \r
35     private SflowClientSettingsListener listener;\r
36     private DataObjectModification<SflowClientSettings> rootNode;\r
37     private Set<DataTreeModification<SflowClientSettings>> changes;\r
38 \r
39     private InstanceIdentifier<SflowClientSettings> rootIdentifier;\r
40 \r
41     @SuppressWarnings("unchecked")\r
42     @Before\r
43     public void init() {\r
44         DataBroker dataProvider = mock(DataBroker.class);\r
45 \r
46         StatisticsManager ofStatisticsManager = mock(StatisticsManager.class);\r
47         ScheduledExecutorService executor = mock(ScheduledExecutorService.class);\r
48         listener = spy(new SflowClientSettingsListener(dataProvider, executor, ofStatisticsManager));\r
49 \r
50         SflowClientSettings sflowClientSettings = mock(SflowClientSettings.class);\r
51 \r
52         rootNode = mock(DataObjectModification.class);\r
53         rootIdentifier = InstanceIdentifier.builder(OfOverlayConfig.class).child(SflowClientSettings.class).build();\r
54         DataTreeIdentifier<SflowClientSettings> rootPath =\r
55                 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, rootIdentifier);\r
56 \r
57         DataTreeModification<SflowClientSettings> change = mock(DataTreeModification.class);\r
58 \r
59         when(change.getRootNode()).thenReturn(rootNode);\r
60         when(change.getRootPath()).thenReturn(rootPath);\r
61 \r
62         changes = ImmutableSet.of(change);\r
63 \r
64         when(rootNode.getDataBefore()).thenReturn(sflowClientSettings);\r
65         when(rootNode.getDataAfter()).thenReturn(sflowClientSettings);\r
66     }\r
67 \r
68     @Test\r
69     public void testOnWrite() {\r
70         when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.WRITE);\r
71 \r
72         listener.onDataTreeChanged(changes);\r
73 \r
74         verify(listener).onWrite(rootNode, rootIdentifier);\r
75     }\r
76 \r
77     @Test\r
78     public void testOnDelete() {\r
79         when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.DELETE);\r
80 \r
81         listener.onDataTreeChanged(changes);\r
82 \r
83         verify(listener).onDelete(rootNode, rootIdentifier);\r
84     }\r
85 \r
86     @Test\r
87     public void testOnSubtreeModified() {\r
88         when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.SUBTREE_MODIFIED);\r
89 \r
90         // first call will initialize uninitialized dependencies;\r
91         // second call will call #close on them\r
92         listener.onDataTreeChanged(changes);\r
93         listener.onDataTreeChanged(changes);\r
94         verify(listener, times(2)).onSubtreeModified(rootNode, rootIdentifier);\r
95     }\r
96 \r
97 }\r