Merge "Bug 6110: Fixed bugs in statistics manager due to race condition." into stable...
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / lifecycle / LifecycleServiceImplTest.java
1 /**
2  * Copyright (c) 2016 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.Assert;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 import org.mockito.Mock;
16 import org.mockito.Mockito;
17 import org.mockito.runners.MockitoJUnitRunner;
18 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
19 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
20 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
21 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
22 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
23 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
24 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
25 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
26 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
27 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
28 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
29 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
30
31 @RunWith(MockitoJUnitRunner.class)
32 public class LifecycleServiceImplTest {
33
34     private static final String TEST_NODE = "test node";
35     private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER = ServiceGroupIdentifier.create(TEST_NODE);
36
37     @Mock
38     private DeviceInfo deviceInfo;
39     @Mock
40     private DeviceContext deviceContext;
41     @Mock
42     private RpcContext rpcContext;
43     @Mock
44     private StatisticsContext statContext;
45     @Mock
46     private ConnectionContext connectionContext;
47     @Mock
48     private DeviceFlowRegistry deviceFlowRegistry;
49     @Mock
50     private ClusterSingletonServiceProvider clusterSingletonServiceProvider;
51     @Mock
52     private ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
53     @Mock
54     EntityOwnershipListenerRegistration entityOwnershipListenerRegistration;
55
56     private LifecycleService lifecycleService;
57
58     @Before
59     public void setUp() {
60         Mockito.when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
61         Mockito.when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
62         Mockito.when(deviceContext.getDeviceFlowRegistry()).thenReturn(deviceFlowRegistry);
63         Mockito.when(deviceContext.getServiceIdentifier()).thenReturn(SERVICE_GROUP_IDENTIFIER);
64         Mockito.when(deviceFlowRegistry.fill()).thenReturn(Futures.immediateFuture(null));
65         Mockito.when(connectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
66         Mockito.when(deviceInfo.getLOGValue()).thenReturn(TEST_NODE);
67         Mockito.when(clusterSingletonServiceProvider.registerClusterSingletonService(Mockito.any()))
68                 .thenReturn(clusterSingletonServiceRegistration);
69
70         Mockito.when(deviceContext.stopClusterServices()).thenReturn(Futures.immediateFuture(null));
71         Mockito.when(statContext.stopClusterServices()).thenReturn(Futures.immediateFuture(null));
72         Mockito.when(rpcContext.stopClusterServices()).thenReturn(Futures.immediateFuture(null));
73
74         lifecycleService = new LifecycleServiceImpl();
75         lifecycleService.setDeviceContext(deviceContext);
76         lifecycleService.setRpcContext(rpcContext);
77         lifecycleService.setStatContext(statContext);
78         lifecycleService.registerService(clusterSingletonServiceProvider);
79     }
80
81     @Test
82     public void instantiateServiceInstance() throws Exception {
83         lifecycleService.instantiateServiceInstance();
84         Mockito.verify(deviceContext).setLifecycleInitializationPhaseHandler(Mockito.any());
85         Mockito.verify(statContext).setLifecycleInitializationPhaseHandler(Mockito.any());
86         Mockito.verify(statContext).setInitialSubmitHandler(Mockito.any());
87         Mockito.verify(rpcContext).setLifecycleInitializationPhaseHandler(Mockito.any());
88     }
89
90     @Test
91     public void closeServiceInstance() throws Exception {
92         lifecycleService.closeServiceInstance().get();
93         Mockito.verify(statContext).stopClusterServices();
94         Mockito.verify(deviceContext).stopClusterServices();
95         Mockito.verify(rpcContext).stopClusterServices();
96     }
97
98     @Test
99     public void getIdentifier() throws Exception {
100         Assert.assertEquals(lifecycleService.getIdentifier(), SERVICE_GROUP_IDENTIFIER);
101     }
102
103     @Test
104     public void closeConnection() throws Exception {
105         lifecycleService.closeConnection();
106         Mockito.verify(deviceContext).shutdownConnection();
107     }
108
109 }