Fix unused import warnings
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / LifecycleConductorImplTest.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 package org.opendaylight.openflowplugin.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12 import static org.mockito.Mockito.times;
13 import static org.mockito.Mockito.verify;
14 import static org.mockito.Mockito.when;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import io.netty.util.HashedWheelTimer;
17 import io.netty.util.TimerTask;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.TimeUnit;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.Mock;
24 import org.mockito.runners.MockitoJUnitRunner;
25 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
26 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
27 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
28 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
29 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ServiceChangeListener;
30 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
31 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
35
36 @RunWith(MockitoJUnitRunner.class)
37 public class LifecycleConductorImplTest {
38
39     private LifecycleConductorImpl lifecycleConductor;
40
41     @Mock
42     private MessageIntelligenceAgency messageIntelligenceAgency;
43     @Mock
44     private ServiceChangeListener serviceChangeListener;
45     @Mock
46     private ConcurrentHashMap<NodeId, ServiceChangeListener> serviceChangeListeners;
47     @Mock
48     private DeviceContext deviceContext;
49     @Mock
50     private DeviceManager deviceManager;
51     @Mock
52     private DeviceState deviceState;
53     @Mock
54     private ConnectionContext connectionContext;
55     @Mock
56     private FeaturesReply featuresReply;
57     @Mock
58     private TimerTask timerTask;
59     @Mock
60     private TimeUnit timeUnit;
61     @Mock
62     private HashedWheelTimer hashedWheelTimer;
63     @Mock
64     private ListenableFuture<Void> listenableFuture;
65     @Mock
66     private StatisticsManager statisticsManager;
67
68     private NodeId nodeId = new NodeId("openflow-junit:1");
69     private OfpRole ofpRole = OfpRole.NOCHANGE;
70     private long delay = 42;
71
72     @Before
73     public void setUp() {
74         when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
75         when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
76
77         lifecycleConductor = new LifecycleConductorImpl(messageIntelligenceAgency);
78         lifecycleConductor.setSafelyDeviceManager(deviceManager);
79         lifecycleConductor.setSafelyStatisticsManager(statisticsManager);
80
81         when(connectionContext.getFeatures()).thenReturn(featuresReply);
82     }
83
84
85
86     @Test
87     public void addOneTimeListenerWhenServicesChangesDoneTest() {
88         lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, nodeId);
89         assertEquals(false,lifecycleConductor.isServiceChangeListenersEmpty());
90     }
91
92
93     /**
94      * If serviceChangeListeners is empty NOTHING should happen
95      */
96     @Test
97     public void notifyServiceChangeListenersTest1() {
98         lifecycleConductor.notifyServiceChangeListeners(nodeId,true);
99         when(serviceChangeListeners.size()).thenReturn(0);
100         verify(serviceChangeListeners,times(0)).remove(nodeId);
101     }
102
103     /**
104      * If serviceChangeListeners is NOT empty remove(nodeID) should be removed
105      */
106     @Test
107     public void notifyServiceChangeListenersTest2() {
108         lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, nodeId);
109         assertEquals(false,lifecycleConductor.isServiceChangeListenersEmpty());
110         lifecycleConductor.notifyServiceChangeListeners(nodeId,true);
111         assertEquals(true,lifecycleConductor.isServiceChangeListenersEmpty());
112     }
113
114
115     /**
116      * When success flag is set to FALSE nodeID connection should be closed
117      */
118     @Test
119     public void roleInitializationDoneTest1() {
120         lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, nodeId);
121         lifecycleConductor.roleInitializationDone(nodeId,false);
122         verify(deviceContext,times(1)).shutdownConnection();
123     }
124
125     /**
126      * When success flag is set to TRUE LOG should be printed
127      */
128     @Test
129     public void roleInitializationDoneTest2() {
130         lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, nodeId);
131         lifecycleConductor.roleInitializationDone(nodeId,true);
132         verify(deviceContext,times(0)).shutdownConnection();
133     }
134
135     /**
136      * When getDeviceContext returns null nothing should happen
137      */
138     @Test
139     public void roleChangeOnDeviceTest1() {
140         when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(null);
141         lifecycleConductor.roleChangeOnDevice(nodeId,true,ofpRole,false);
142         verify(deviceContext,times(0)).shutdownConnection();
143         lifecycleConductor.roleChangeOnDevice(nodeId,false,ofpRole,false);
144         verify(deviceContext,times(0)).shutdownConnection();
145     }
146
147     /**
148      * When success flag is set to FALSE connection should be closed
149      */
150     @Test
151     public void roleChangeOnDeviceTest2() {
152         when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
153         lifecycleConductor.roleChangeOnDevice(nodeId,false,ofpRole,false);
154         verify(deviceContext,times(1)).shutdownConnection();
155     }
156
157     /**
158      * When success flag is set to TRUE and initializationPahse flag is set to TRUE starting
159      * device should be skipped
160      */
161     @Test
162     public void roleChangeOnDeviceTest3() {
163         when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
164         lifecycleConductor.roleChangeOnDevice(nodeId,true,ofpRole,true);
165         verify(deviceContext,times(0)).shutdownConnection();
166     }
167
168     /**
169      * When OfpRole == BECOMEMASTER setRole(OfpRole.BECOMEMASTER) should be called
170      */
171     @Test
172     public void roleChangeOnDeviceTest4() {
173         when(deviceContext.getDeviceState()).thenReturn(deviceState);
174         when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
175         when(deviceContext.onClusterRoleChange(null, OfpRole.BECOMEMASTER)).thenReturn(listenableFuture);
176         lifecycleConductor.roleChangeOnDevice(nodeId,true,OfpRole.BECOMEMASTER,false);
177         verify(statisticsManager).startScheduling(nodeId);
178     }
179
180     /**
181      * When OfpRole != BECOMEMASTER setRole(OfpRole.ECOMESLAVE) should be called
182      */
183     @Test
184     public void roleChangeOnDeviceTest5() {
185         when(deviceContext.getDeviceState()).thenReturn(deviceState);
186         when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
187         when(deviceContext.onClusterRoleChange(null, OfpRole.BECOMESLAVE)).thenReturn(listenableFuture);
188         lifecycleConductor.roleChangeOnDevice(nodeId,true,OfpRole.BECOMESLAVE,false);
189         verify(statisticsManager).stopScheduling(nodeId);
190     }
191
192     /**
193      * If getDeviceContext returns null nothing should happen
194      */
195     @Test
196     public void gainVersionSafelyTest1() {
197         when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(null);
198         assertNull(lifecycleConductor.gainVersionSafely(nodeId));
199     }
200
201     /**
202      * If getDeviceContext returns deviceContext getPrimaryConnectionContext() should be called
203      */
204     @Test
205     public void gainVersionSafelyTest2() {
206         when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
207         lifecycleConductor.gainVersionSafely(nodeId);
208         verify(deviceContext,times(1)).getPrimaryConnectionContext();
209     }
210
211     /**
212      * If getDeviceContext return null then null should be returned
213      */
214     @Test
215     public void gainConnectionStateSafelyTest1() {
216         when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(null);
217         assertNull(lifecycleConductor.gainConnectionStateSafely(nodeId));
218     }
219
220     /**
221      * If getDeviceContext return deviceContext then getPrimaryConnectionContext should be called
222      */
223     @Test
224     public void gainConnectionStateSafelyTest2() {
225         when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
226         lifecycleConductor.gainConnectionStateSafely(nodeId);
227         verify(deviceContext,times(1)).getPrimaryConnectionContext();
228     }
229
230     /**
231      * If getDeviceContext returns null then null should be returned
232      */
233     @Test
234     public void reserveXidForDeviceMessageTest1() {
235         when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(null);
236         assertNull(lifecycleConductor.reserveXidForDeviceMessage(nodeId));
237     }
238
239     /**
240      * If getDeviceContext returns deviceContext reserveXidForDeviceMessage() should be called
241      */
242     @Test
243     public void reserveXidForDeviceMessageTest2() {
244         when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
245         lifecycleConductor.reserveXidForDeviceMessage(nodeId);
246         verify(deviceContext,times(1)).reserveXidForDeviceMessage();
247     }
248
249     /**
250      * When succes flag is set to FALSE connection should be closed
251      */
252     @Test
253     public void deviceStartInitializationDoneTest() {
254         lifecycleConductor.deviceStartInitializationDone(nodeId, false);
255         verify(deviceContext,times(1)).shutdownConnection();
256     }
257
258     /**
259      * When succes flag is set to FALSE connection should be closed
260      */
261     @Test
262     public void deviceInitializationDoneTest() {
263         lifecycleConductor.deviceInitializationDone(nodeId, false);
264         verify(deviceContext,times(1)).shutdownConnection();
265     }
266 }