Bug 5543 - JUnit tests update
[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
16 import com.google.common.util.concurrent.ListenableFuture;
17 import io.netty.util.HashedWheelTimer;
18 import io.netty.util.TimerTask;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.Mock;
23 import org.mockito.runners.MockitoJUnitRunner;
24 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
25 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
26 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
27 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
28 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ServiceChangeListener;
29 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
33
34 import java.util.concurrent.ConcurrentHashMap;
35 import java.util.concurrent.TimeUnit;
36
37 @RunWith(MockitoJUnitRunner.class)
38 public class LifecycleConductorImplTest {
39
40     private LifecycleConductorImpl lifecycleConductor;
41
42     @Mock
43     private MessageIntelligenceAgency messageIntelligenceAgency;
44     @Mock
45     private ServiceChangeListener serviceChangeListener;
46     @Mock
47     private ConcurrentHashMap<NodeId, ServiceChangeListener> serviceChangeListeners;
48     @Mock
49     private DeviceContext deviceContext;
50     @Mock
51     private DeviceManager deviceManager;
52     @Mock
53     private DeviceState deviceState;
54     @Mock
55     private ConnectionContext connectionContext;
56     @Mock
57     private FeaturesReply featuresReply;
58     @Mock
59     private TimerTask timerTask;
60     @Mock
61     private TimeUnit timeUnit;
62     @Mock
63     private HashedWheelTimer hashedWheelTimer;
64     @Mock
65     private ListenableFuture<Void> listenableFuture;
66
67     private NodeId nodeId = new NodeId("openflow-junit:1");
68     private OfpRole ofpRole = OfpRole.NOCHANGE;
69     private long delay = 42;
70
71     @Before
72     public void setUp() {
73         when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
74         when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
75
76         lifecycleConductor = new LifecycleConductorImpl(messageIntelligenceAgency);
77         lifecycleConductor.setSafelyDeviceManager(deviceManager);
78
79         when(connectionContext.getFeatures()).thenReturn(featuresReply);
80     }
81
82
83
84     @Test
85     public void addOneTimeListenerWhenServicesChangesDoneTest() {
86         lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, nodeId);
87         assertEquals(false,lifecycleConductor.isServiceChangeListenersEmpty());
88     }
89
90
91     /**
92      * If serviceChangeListeners is empty NOTHING should happen
93      */
94     @Test
95     public void notifyServiceChangeListenersTest1() {
96         lifecycleConductor.notifyServiceChangeListeners(nodeId,true);
97         when(serviceChangeListeners.size()).thenReturn(0);
98         verify(serviceChangeListeners,times(0)).remove(nodeId);
99     }
100
101     /**
102      * If serviceChangeListeners is NOT empty remove(nodeID) should be removed
103      */
104     @Test
105     public void notifyServiceChangeListenersTest2() {
106         lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, nodeId);
107         assertEquals(false,lifecycleConductor.isServiceChangeListenersEmpty());
108         lifecycleConductor.notifyServiceChangeListeners(nodeId,true);
109         assertEquals(true,lifecycleConductor.isServiceChangeListenersEmpty());
110     }
111
112
113     /**
114      * When success flag is set to FALSE nodeID connection should be closed
115      */
116     @Test
117     public void roleInitializationDoneTest1() {
118         lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, nodeId);
119         lifecycleConductor.roleInitializationDone(nodeId,false);
120         verify(deviceContext,times(1)).shutdownConnection();
121     }
122
123     /**
124      * When success flag is set to TRUE LOG should be printed
125      */
126     @Test
127     public void roleInitializationDoneTest2() {
128         lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, nodeId);
129         lifecycleConductor.roleInitializationDone(nodeId,true);
130         verify(deviceContext,times(0)).shutdownConnection();
131     }
132
133     /**
134      * When getDeviceContext returns null nothing should happen
135      */
136     @Test
137     public void roleChangeOnDeviceTest1() {
138         when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(null);
139         lifecycleConductor.roleChangeOnDevice(nodeId,true,ofpRole,false);
140         verify(deviceContext,times(0)).shutdownConnection();
141         lifecycleConductor.roleChangeOnDevice(nodeId,false,ofpRole,false);
142         verify(deviceContext,times(0)).shutdownConnection();
143     }
144
145     /**
146      * When success flag is set to FALSE connection should be closed
147      */
148     @Test
149     public void roleChangeOnDeviceTest2() {
150         when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
151         lifecycleConductor.roleChangeOnDevice(nodeId,false,ofpRole,false);
152         verify(deviceContext,times(1)).shutdownConnection();
153     }
154
155     /**
156      * When success flag is set to TRUE and initializationPahse flag is set to TRUE starting
157      * device should be skipped
158      */
159     @Test
160     public void roleChangeOnDeviceTest3() {
161         when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
162         lifecycleConductor.roleChangeOnDevice(nodeId,true,ofpRole,true);
163         verify(deviceContext,times(0)).shutdownConnection();
164     }
165
166     /**
167      * When OfpRole == BECOMEMASTER setRole(OfpRole.BECOMEMASTER) should be called
168      */
169     @Test
170     public void roleChangeOnDeviceTest4() {
171         when(deviceContext.getDeviceState()).thenReturn(deviceState);
172         when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
173         when(deviceContext.onClusterRoleChange(null, OfpRole.BECOMEMASTER)).thenReturn(listenableFuture);
174         lifecycleConductor.roleChangeOnDevice(nodeId,true,OfpRole.BECOMEMASTER,false);
175         verify(deviceState,times(1)).setRole(OfpRole.BECOMEMASTER);
176         verify(deviceState,times(0)).setRole(OfpRole.BECOMESLAVE);
177     }
178
179     /**
180      * When OfpRole != BECOMEMASTER setRole(OfpRole.ECOMESLAVE) should be called
181      */
182     @Test
183     public void roleChangeOnDeviceTest5() {
184         when(deviceContext.getDeviceState()).thenReturn(deviceState);
185         when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
186         when(deviceContext.onClusterRoleChange(null, OfpRole.BECOMESLAVE)).thenReturn(listenableFuture);
187         lifecycleConductor.roleChangeOnDevice(nodeId,true,OfpRole.BECOMESLAVE,false);
188         verify(deviceState,times(1)).setRole(OfpRole.BECOMESLAVE);
189         verify(deviceState,times(0)).setRole(OfpRole.BECOMEMASTER);
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 }