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.mastership;
10 import com.google.common.util.concurrent.Futures;
11 import java.util.ArrayList;
12 import java.util.List;
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.Mock;
18 import org.mockito.Mockito;
19 import org.mockito.runners.MockitoJUnitRunner;
20 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
21 import org.opendaylight.openflowplugin.api.openflow.lifecycle.MasterChecker;
22 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeException;
23 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeRegistration;
24 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeService;
25 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager;
26 import org.opendaylight.openflowplugin.api.openflow.mastership.ReconciliationFrameworkEvent;
27 import org.opendaylight.openflowplugin.api.openflow.mastership.ReconciliationFrameworkRegistration;
29 @RunWith(MockitoJUnitRunner.class)
30 public class MastershipChangeServiceManagerImplTest {
33 private MastershipChangeService service;
35 private MastershipChangeService secondService;
37 private DeviceInfo deviceInfo;
39 private MasterChecker masterChecker;
41 private ReconciliationFrameworkEvent event;
43 private ReconciliationFrameworkEvent secondEvent;
45 private final MastershipChangeServiceManager manager = new MastershipChangeServiceManagerImpl();
46 private MastershipChangeRegistration registration;
47 private ReconciliationFrameworkRegistration registrationRF;
50 public void setUp() throws Exception {
51 registration = manager.register(service);
52 registrationRF = manager.reconciliationFrameworkRegistration(event);
54 Mockito.when(event.onDeviceDisconnected(Mockito.any())).thenReturn(Futures.immediateFuture(null));
58 public void register() {
59 Assert.assertNotNull(registration);
63 public void registerTwice() {
64 MastershipChangeRegistration registration2;
65 registration2 = manager.register(secondService);
66 Assert.assertNotNull(registration);
67 Assert.assertNotNull(registration2);
71 public void uregisterTwice() throws Exception {
72 MastershipChangeRegistration registration2;
73 registration2 = manager.register(secondService);
74 Assert.assertTrue(((MastershipChangeServiceManagerImpl)manager).serviceGroupListSize() == 2);
76 Assert.assertTrue(((MastershipChangeServiceManagerImpl)manager).serviceGroupListSize() == 1);
77 registration2.close();
78 Assert.assertTrue(((MastershipChangeServiceManagerImpl)manager).serviceGroupListSize() == 0);
82 public void reconciliationFrameworkRegistration() {
83 Assert.assertNotNull(registrationRF);
86 @Test(expected = MastershipChangeException.class)
87 public void reconciliationFrameworkRegistrationTwice() throws Exception {
88 manager.reconciliationFrameworkRegistration(secondEvent);
92 public void unregosteringRF() throws Exception {
93 registrationRF.close();
94 ReconciliationFrameworkRegistration registration1;
95 registration1 = manager.reconciliationFrameworkRegistration(secondEvent);
96 Assert.assertNotNull(registration1);
100 public void becomeMaster() {
101 manager.becomeMaster(deviceInfo);
102 Mockito.verify(service).onBecomeOwner(deviceInfo);
103 manager.becomeSlaveOrDisconnect(deviceInfo);
104 Mockito.verify(service).onLoseOwnership(deviceInfo);
108 public void becomeMasterBeforeDS() {
109 manager.becomeMasterBeforeSubmittedDS(deviceInfo);
110 Mockito.verify(event).onDevicePrepared(deviceInfo);
114 public void isReconciliationFrameworkRegistered() throws Exception {
115 Assert.assertTrue(manager.isReconciliationFrameworkRegistered());
116 registrationRF.close();
117 Assert.assertFalse(manager.isReconciliationFrameworkRegistered());
121 public void evokeEventAfterRegistration() {
122 List<DeviceInfo> deviceInfos = new ArrayList<>();
123 deviceInfos.add(deviceInfo);
124 manager.setMasterChecker(masterChecker);
125 Mockito.when(masterChecker.isAnyDeviceMastered()).thenReturn(true);
126 Mockito.when(masterChecker.listOfMasteredDevices()).thenReturn(deviceInfos);
127 manager.register(secondService);
128 Mockito.verify(secondService).onBecomeOwner(deviceInfo);