2 * Copyright (c) 2016 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 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.device.DeviceContext;
22 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
23 import org.opendaylight.openflowplugin.api.openflow.device.handlers.ClusterInitializationPhaseHandler;
24 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceRemovedHandler;
25 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
26 import org.opendaylight.openflowplugin.api.openflow.lifecycle.MastershipChangeListener;
27 import org.opendaylight.openflowplugin.impl.role.RoleChangeException;
29 @RunWith(MockitoJUnitRunner.class)
30 public class LifecycleServiceImplTest {
32 private static final String TEST_NODE = "test node";
33 private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER = ServiceGroupIdentifier.create(TEST_NODE);
36 private DeviceInfo deviceInfo;
38 private DeviceContext deviceContext;
40 private ClusterSingletonServiceProvider clusterSingletonServiceProvider;
42 private ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
44 private MastershipChangeListener mastershipChangeListener;
46 private ClusterInitializationPhaseHandler clusterInitializationPhaseHandler;
48 private DeviceRemovedHandler deviceRemovedHandler;
50 private LifecycleService lifecycleService;
54 Mockito.when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
55 Mockito.when(deviceContext.getServiceIdentifier()).thenReturn(SERVICE_GROUP_IDENTIFIER);
56 Mockito.when(clusterSingletonServiceProvider.registerClusterSingletonService(Mockito.any()))
57 .thenReturn(clusterSingletonServiceRegistration);
59 lifecycleService = new LifecycleServiceImpl(mastershipChangeListener);
60 lifecycleService.registerService(
61 clusterSingletonServiceProvider,
66 public void getIdentifier() throws Exception {
67 Assert.assertEquals(lifecycleService.getIdentifier(), SERVICE_GROUP_IDENTIFIER);
71 public void makeDeviceSlave() throws Exception {
72 Mockito.when(deviceContext.makeDeviceSlave())
73 .thenReturn(Futures.immediateFuture(null));
74 lifecycleService.makeDeviceSlave(deviceContext);
75 Mockito.verify(mastershipChangeListener).onSlaveRoleAcquired(Mockito.any(DeviceInfo.class));
79 public void makeDeviceSlaveFailure() throws Exception {
80 Mockito.when(deviceContext.makeDeviceSlave())
81 .thenReturn(Futures.immediateFailedFuture(new RoleChangeException(TEST_NODE)));
82 lifecycleService.makeDeviceSlave(deviceContext);
83 Mockito.verify(mastershipChangeListener).onSlaveRoleNotAcquired(Mockito.any(DeviceInfo.class));
87 public void instantiateServiceInstanceFail() throws Exception {
88 Mockito.when(deviceContext.onContextInstantiateService(Mockito.any()))
90 lifecycleService.instantiateServiceInstance();
91 Mockito.verify(mastershipChangeListener).onNotAbleToStartMastershipMandatory(Mockito.any(DeviceInfo.class), Mockito.anyString());
95 public void close() throws Exception {
96 lifecycleService.registerDeviceRemovedHandler(deviceRemovedHandler);
97 lifecycleService.close();
98 Mockito.verify(deviceRemovedHandler).onDeviceRemoved(Mockito.any(DeviceInfo.class));
102 public void closeTwoTimes() throws Exception {
103 lifecycleService.registerDeviceRemovedHandler(deviceRemovedHandler);
104 lifecycleService.close();
105 lifecycleService.close();
106 Mockito.verify(deviceRemovedHandler, Mockito.times(1))
107 .onDeviceRemoved(Mockito.any(DeviceInfo.class));
111 public void closeThreeTimes() throws Exception {
112 lifecycleService.registerDeviceRemovedHandler(deviceRemovedHandler);
113 lifecycleService.close();
114 lifecycleService.close();
115 Mockito.verify(deviceRemovedHandler, Mockito.times(1))
116 .onDeviceRemoved(Mockito.any(DeviceInfo.class));