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