2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.impl.lifecycle;
10 import com.google.common.util.concurrent.Futures;
11 import com.google.common.util.concurrent.MoreExecutors;
12 import org.junit.Assert;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.junit.runner.RunWith;
16 import org.mockito.Mock;
17 import org.mockito.Mockito;
18 import org.mockito.runners.MockitoJUnitRunner;
19 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
20 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
21 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
22 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
23 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
24 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
25 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceRemovedHandler;
26 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChain;
27 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainMastershipWatcher;
28 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
29 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
31 @RunWith(MockitoJUnitRunner.class)
32 public class ContextChainImplTest {
34 private static final String TEST_NODE = "test node";
35 private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER = ServiceGroupIdentifier.create(TEST_NODE);
38 private StatisticsContext statisticsContext;
40 private RpcContext rpcContext;
42 private DeviceContext deviceContext;
44 private DeviceInfo deviceInfo;
46 private ConnectionContext connectionContext;
48 private ClusterSingletonServiceProvider clusterSingletonServiceProvider;
50 private ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
52 private ContextChainMastershipWatcher contextChainMastershipWatcher;
54 private DeviceRemovedHandler deviceRemovedHandler;
56 private ContextChain contextChain;
59 public void setUp() throws Exception {
60 Mockito.when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
61 Mockito.when(deviceInfo.getServiceIdentifier()).thenReturn(SERVICE_GROUP_IDENTIFIER);
62 Mockito.when(deviceContext.closeServiceInstance()).thenReturn(Futures.immediateFuture(null));
63 Mockito.when(rpcContext.closeServiceInstance()).thenReturn(Futures.immediateFuture(null));
64 Mockito.when(statisticsContext.closeServiceInstance()).thenReturn(Futures.immediateFuture(null));
65 Mockito.when(statisticsContext.gatherDynamicData()).thenReturn(Futures.immediateFuture(null));
66 Mockito.when(connectionContext.getDeviceInfo()).thenReturn(deviceInfo);
67 Mockito.when(connectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
68 Mockito.when(clusterSingletonServiceProvider.registerClusterSingletonService(Mockito.any()))
69 .thenReturn(clusterSingletonServiceRegistration);
71 contextChain = new ContextChainImpl(contextChainMastershipWatcher, connectionContext,
72 MoreExecutors.newDirectExecutorService());
73 contextChain.addContext(statisticsContext);
74 contextChain.addContext(rpcContext);
75 contextChain.addContext(deviceContext);
76 contextChain.registerServices(clusterSingletonServiceProvider);
80 public void closeServiceInstance() throws Exception {
81 contextChain.closeServiceInstance();
82 Mockito.verify(deviceContext).closeServiceInstance();
83 Mockito.verify(rpcContext).closeServiceInstance();
84 Mockito.verify(statisticsContext).closeServiceInstance();
88 public void close() throws Exception {
89 contextChain.registerDeviceRemovedHandler(deviceRemovedHandler);
91 Mockito.verify(statisticsContext).close();
92 Mockito.verify(deviceContext).close();
93 Mockito.verify(rpcContext).close();
94 Mockito.verify(deviceRemovedHandler).onDeviceRemoved(Mockito.any(DeviceInfo.class));
98 public void closeTwoTimes() throws Exception {
99 contextChain.registerDeviceRemovedHandler(deviceRemovedHandler);
100 contextChain.close();
101 contextChain.close();
102 Mockito.verify(deviceRemovedHandler, Mockito.times(1))
103 .onDeviceRemoved(Mockito.any(DeviceInfo.class));
107 public void closeThreeTimes() throws Exception {
108 contextChain.registerDeviceRemovedHandler(deviceRemovedHandler);
109 contextChain.close();
110 contextChain.close();
111 Mockito.verify(deviceRemovedHandler, Mockito.times(1))
112 .onDeviceRemoved(Mockito.any(DeviceInfo.class));
116 public void getIdentifier() throws Exception {
117 Assert.assertEquals(contextChain.getIdentifier(), SERVICE_GROUP_IDENTIFIER);
121 public void instantiateServiceInstanceFail() throws Exception {
122 Mockito.doThrow(new IllegalStateException()).when(deviceContext).instantiateServiceInstance();
123 contextChain.instantiateServiceInstance();
124 Mockito.verify(contextChainMastershipWatcher).onNotAbleToStartMastershipMandatory(Mockito.any(DeviceInfo.class), Mockito.anyString());