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.junit.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.MastershipChangeService;
24 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager;
25 import org.opendaylight.openflowplugin.api.openflow.mastership.ReconciliationFrameworkEvent;
26 import org.opendaylight.yangtools.concepts.Registration;
28 @RunWith(MockitoJUnitRunner.class)
29 public class MastershipChangeServiceManagerImplTest {
32 private MastershipChangeService service;
34 private MastershipChangeService secondService;
36 private DeviceInfo deviceInfo;
38 private MasterChecker masterChecker;
40 private ReconciliationFrameworkEvent event;
42 private ReconciliationFrameworkEvent secondEvent;
44 private final MastershipChangeServiceManager manager = new MastershipChangeServiceManagerImpl();
45 private Registration registration;
46 private Registration registrationRF;
49 public void setUp() throws Exception {
50 registration = manager.register(service);
51 registrationRF = manager.reconciliationFrameworkRegistration(event);
53 Mockito.when(event.onDeviceDisconnected(Mockito.any())).thenReturn(Futures.immediateFuture(null));
57 public void register() {
58 Assert.assertNotNull(registration);
62 public void registerTwice() {
63 Registration registration2 = manager.register(secondService);
64 Assert.assertNotNull(registration);
65 Assert.assertNotNull(registration2);
69 public void uregisterTwice() throws Exception {
70 try (var registration2 = manager.register(secondService)) {
71 Assert.assertTrue(((MastershipChangeServiceManagerImpl)manager).serviceGroupListSize() == 2);
73 Assert.assertTrue(((MastershipChangeServiceManagerImpl)manager).serviceGroupListSize() == 1);
75 Assert.assertTrue(((MastershipChangeServiceManagerImpl)manager).serviceGroupListSize() == 0);
79 public void reconciliationFrameworkRegistration() {
80 Assert.assertNotNull(registrationRF);
83 @Test(expected = MastershipChangeException.class)
84 public void reconciliationFrameworkRegistrationTwice() throws Exception {
85 manager.reconciliationFrameworkRegistration(secondEvent);
89 public void unregosteringRF() throws Exception {
90 registrationRF.close();
91 Registration registration1 = manager.reconciliationFrameworkRegistration(secondEvent);
92 Assert.assertNotNull(registration1);
96 public void becomeMaster() {
97 manager.becomeMaster(deviceInfo);
98 Mockito.verify(service).onBecomeOwner(deviceInfo);
99 manager.becomeSlaveOrDisconnect(deviceInfo);
100 Mockito.verify(service).onLoseOwnership(deviceInfo);
104 public void becomeMasterBeforeDS() {
105 manager.becomeMasterBeforeSubmittedDS(deviceInfo);
106 Mockito.verify(event).onDevicePrepared(deviceInfo);
110 public void isReconciliationFrameworkRegistered() throws Exception {
111 Assert.assertTrue(manager.isReconciliationFrameworkRegistered());
112 registrationRF.close();
113 Assert.assertFalse(manager.isReconciliationFrameworkRegistered());
117 public void evokeEventAfterRegistration() {
118 List<DeviceInfo> deviceInfos = new ArrayList<>();
119 deviceInfos.add(deviceInfo);
120 manager.setMasterChecker(masterChecker);
121 Mockito.when(masterChecker.isAnyDeviceMastered()).thenReturn(true);
122 Mockito.when(masterChecker.listOfMasteredDevices()).thenReturn(deviceInfos);
123 manager.register(secondService);
124 Mockito.verify(secondService).onBecomeOwner(deviceInfo);