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(connectionContext.getDeviceInfo()).thenReturn(deviceInfo);
66 Mockito.when(clusterSingletonServiceProvider.registerClusterSingletonService(Mockito.any()))
67 .thenReturn(clusterSingletonServiceRegistration);
69 contextChain = new ContextChainImpl(contextChainMastershipWatcher, connectionContext,
70 MoreExecutors.newDirectExecutorService());
71 contextChain.addContext(statisticsContext);
72 contextChain.addContext(rpcContext);
73 contextChain.addContext(deviceContext);
74 contextChain.registerServices(clusterSingletonServiceProvider);
78 public void closeServiceInstance() throws Exception {
79 contextChain.closeServiceInstance();
80 Mockito.verify(deviceContext).closeServiceInstance();
81 Mockito.verify(rpcContext).closeServiceInstance();
82 Mockito.verify(statisticsContext).closeServiceInstance();
86 public void close() throws Exception {
87 contextChain.registerDeviceRemovedHandler(deviceRemovedHandler);
89 Mockito.verify(statisticsContext).close();
90 Mockito.verify(deviceContext).close();
91 Mockito.verify(rpcContext).close();
92 Mockito.verify(deviceRemovedHandler).onDeviceRemoved(Mockito.any(DeviceInfo.class));
96 public void closeTwoTimes() throws Exception {
97 contextChain.registerDeviceRemovedHandler(deviceRemovedHandler);
100 Mockito.verify(deviceRemovedHandler, Mockito.times(1))
101 .onDeviceRemoved(Mockito.any(DeviceInfo.class));
105 public void closeThreeTimes() throws Exception {
106 contextChain.registerDeviceRemovedHandler(deviceRemovedHandler);
107 contextChain.close();
108 contextChain.close();
109 Mockito.verify(deviceRemovedHandler, Mockito.times(1))
110 .onDeviceRemoved(Mockito.any(DeviceInfo.class));
114 public void getIdentifier() throws Exception {
115 Assert.assertEquals(contextChain.getIdentifier(), SERVICE_GROUP_IDENTIFIER);
119 public void instantiateServiceInstanceFail() throws Exception {
120 Mockito.doThrow(new IllegalStateException()).when(deviceContext).instantiateServiceInstance();
121 contextChain.instantiateServiceInstance();
122 Mockito.verify(contextChainMastershipWatcher)
123 .onNotAbleToStartMastershipMandatory(Mockito.any(DeviceInfo.class), Mockito.anyString());