Bug 6110: Fixed bugs in statistics manager due to race condition.
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / OpenFlowPluginProviderImplTest.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.openflowplugin.impl;
10
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.eq;
13 import static org.mockito.Mockito.verify;
14 import static org.mockito.Mockito.when;
15
16 import com.google.common.collect.Lists;
17 import com.google.common.util.concurrent.Futures;
18 import org.junit.After;
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.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
26 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
27 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
28 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
29 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
30 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
31 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
32 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.StatisticsManagerControlService;
34
35 @RunWith(MockitoJUnitRunner.class)
36 public class OpenFlowPluginProviderImplTest {
37
38     @Mock
39     DataBroker dataBroker;
40
41     @Mock
42     RpcProviderRegistry rpcProviderRegistry;
43
44     @Mock
45     NotificationService notificationService;
46
47     @Mock
48     WriteTransaction writeTransaction;
49
50     @Mock
51     EntityOwnershipService entityOwnershipService;
52
53     @Mock
54     EntityOwnershipListenerRegistration entityOwnershipListenerRegistration;
55
56     @Mock
57     BindingAwareBroker.RpcRegistration<StatisticsManagerControlService> controlServiceRegistration;
58
59     @Mock
60     SwitchConnectionProvider switchConnectionProvider;
61
62     @Mock
63     ClusterSingletonServiceProvider clusterSingletonServiceProvider;
64
65     private static final long RPC_REQUESTS_QUOTA = 500;
66     private static final long GLOBAL_NOTIFICATION_QUOTA = 131072;
67     private static final int THREAD_POOL_MIN_THREADS = 1;
68     private static final int THREAD_POOL_MAX_THREADS = 32000;
69     private static final long THREAD_POOL_TIMEOUT = 60;
70
71     private OpenFlowPluginProviderImpl provider;
72
73     @Before
74     public void setUp() throws Exception {
75         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
76         when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
77         when(entityOwnershipService.registerListener(any(), any())).thenReturn(entityOwnershipListenerRegistration);
78         when(rpcProviderRegistry.addRpcImplementation(eq(StatisticsManagerControlService.class), any())).thenReturn(controlServiceRegistration);
79         when(switchConnectionProvider.startup()).thenReturn(Futures.immediateCheckedFuture(null));
80
81         provider = new OpenFlowPluginProviderImpl(
82                 RPC_REQUESTS_QUOTA,
83                 GLOBAL_NOTIFICATION_QUOTA,
84                 THREAD_POOL_MIN_THREADS,
85                 THREAD_POOL_MAX_THREADS,
86                 THREAD_POOL_TIMEOUT);
87
88         provider.setDataBroker(dataBroker);
89         provider.setRpcProviderRegistry(rpcProviderRegistry);
90         provider.setNotificationProviderService(notificationService);
91         provider.setSwitchConnectionProviders(Lists.newArrayList(switchConnectionProvider));
92         provider.setClusteringSingletonServicesProvider(clusterSingletonServiceProvider);
93     }
94
95     @After
96     public void tearDown() throws Exception {
97
98     }
99
100     @Test
101     public void testInitializeAndClose() throws Exception {
102         provider.initialize();
103         verify(switchConnectionProvider).startup();
104     }
105 }