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 java.util.ArrayList;
11 import java.util.List;
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.openflowplugin.api.openflow.device.DeviceInfo;
20 import org.opendaylight.openflowplugin.api.openflow.lifecycle.MasterChecker;
21 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeException;
22 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeRegistration;
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.openflowplugin.api.openflow.mastership.ReconciliationFrameworkRegistration;
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 final private MastershipChangeServiceManager manager = new MastershipChangeServiceManagerImpl();
45 private MastershipChangeRegistration registration;
46 private ReconciliationFrameworkRegistration registrationRF;
49 public void setUp() throws Exception {
50 registration = manager.register(service);
51 registrationRF = manager.reconciliationFrameworkRegistration(event);
55 public void register() throws Exception {
56 Assert.assertNotNull(registration);
60 public void registerTwice() throws Exception {
61 MastershipChangeRegistration registration2;
62 registration2 = manager.register(secondService);
63 Assert.assertNotNull(registration);
64 Assert.assertNotNull(registration2);
68 public void uregisterTwice() throws Exception {
69 MastershipChangeRegistration registration2;
70 registration2 = manager.register(secondService);
71 Assert.assertTrue(((MastershipChangeServiceManagerImpl)manager).serviceGroupListSize() == 2);
73 Assert.assertTrue(((MastershipChangeServiceManagerImpl)manager).serviceGroupListSize() == 1);
74 registration2.close();
75 Assert.assertTrue(((MastershipChangeServiceManagerImpl)manager).serviceGroupListSize() == 0);
79 public void reconciliationFrameworkRegistration() throws Exception {
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 ReconciliationFrameworkRegistration registration1;
92 registration1 = manager.reconciliationFrameworkRegistration(secondEvent);
93 Assert.assertNotNull(registration1);
97 public void becomeMaster() throws Exception {
98 manager.becomeMaster(deviceInfo);
99 Mockito.verify(service).onBecomeOwner(deviceInfo);
100 manager.becomeSlaveOrDisconnect(deviceInfo);
101 Mockito.verify(service).onLoseOwnership(deviceInfo);
105 public void becomeMasterBeforeDS() throws Exception {
106 manager.becomeMasterBeforeSubmittedDS(deviceInfo);
107 Mockito.verify(event).onDevicePrepared(deviceInfo);
111 public void isReconciliationFrameworkRegistered() throws Exception {
112 Assert.assertTrue(manager.isReconciliationFrameworkRegistered());
113 registrationRF.close();
114 Assert.assertFalse(manager.isReconciliationFrameworkRegistered());
118 public void evokeEventAfterRegistration() throws Exception {
119 List<DeviceInfo> deviceInfos = new ArrayList<>();
120 deviceInfos.add(deviceInfo);
121 manager.setMasterChecker(masterChecker);
122 Mockito.when(masterChecker.isAnyDeviceMastered()).thenReturn(true);
123 Mockito.when(masterChecker.listOfMasteredDevices()).thenReturn(deviceInfos);
124 manager.register(secondService);
125 Mockito.verify(secondService).onBecomeOwner(deviceInfo);