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