Merge "Remove device without master from DS."
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / lifecycle / ContextChainImplTest.java
1 /**
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.lifecycle;
9
10 import com.google.common.util.concurrent.Futures;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.junit.runner.RunWith;
14 import org.mockito.Mock;
15 import org.mockito.Mockito;
16 import org.mockito.runners.MockitoJUnitRunner;
17 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
18 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
19 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
20 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChain;
21 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainMastershipState;
22 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
23 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
24 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
25
26 @RunWith(MockitoJUnitRunner.class)
27 public class ContextChainImplTest {
28
29     @Mock
30     private StatisticsContext statisticsContext;
31     @Mock
32     private RpcContext rpcContext;
33     @Mock
34     private DeviceContext deviceContext;
35     @Mock
36     private LifecycleService lifecycleService;
37     @Mock
38     private DeviceInfo deviceInfo;
39     @Mock
40     private ConnectionContext connectionContext;
41
42     private ContextChain contextChain;
43
44     @Before
45     public void setUp() throws Exception {
46
47         Mockito.when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
48         Mockito.when(deviceContext.stopClusterServices())
49                 .thenReturn(Futures.immediateFuture(null));
50         Mockito.when(rpcContext.stopClusterServices()).thenReturn(Futures.immediateFuture(null));
51         Mockito.when(statisticsContext.stopClusterServices()).thenReturn(Futures.immediateFuture(null));
52         Mockito.when(statisticsContext.initialGatherDynamicData()).thenReturn(Futures.immediateFuture(null));
53         Mockito.when(connectionContext.getDeviceInfo()).thenReturn(deviceInfo);
54
55         contextChain = new ContextChainImpl(connectionContext);
56         contextChain.addContext(statisticsContext);
57         contextChain.addContext(rpcContext);
58         contextChain.addContext(deviceContext);
59         contextChain.addLifecycleService(lifecycleService);
60
61     }
62
63     @Test
64     public void stopChain() throws Exception {
65         contextChain.stopChain();
66         Mockito.verify(deviceContext).stopClusterServices();
67         Mockito.verify(rpcContext).stopClusterServices();
68         Mockito.verify(statisticsContext).stopClusterServices();
69     }
70
71     @Test
72     public void close() throws Exception {
73         contextChain.close();
74         Mockito.verify(statisticsContext).close();
75         Mockito.verify(deviceContext).close();
76         Mockito.verify(rpcContext).close();
77         Mockito.verify(lifecycleService).close();
78     }
79
80     @Test
81     public void connectionDropped() throws Exception {
82         contextChain.isMastered(ContextChainMastershipState.INITIAL_GATHERING);
83         contextChain.isMastered(ContextChainMastershipState.INITIAL_SUBMIT);
84         contextChain.isMastered(ContextChainMastershipState.MASTER_ON_DEVICE);
85         contextChain.isMastered(ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
86         contextChain.connectionDropped();
87         Mockito.verify(deviceContext).stopClusterServices();
88         Mockito.verify(rpcContext).stopClusterServices();
89         Mockito.verify(statisticsContext).stopClusterServices();
90     }
91
92     @Test
93     public void makeDeviceSlave() throws Exception {
94         contextChain.makeDeviceSlave();
95         Mockito.verify(lifecycleService).makeDeviceSlave(Mockito.any(DeviceContext.class));
96     }
97
98 }
99