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.FutureCallback;
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.openflowplugin.api.openflow.device.DeviceInfo;
19 import org.opendaylight.openflowplugin.api.openflow.lifecycle.MasterChecker;
20 import org.opendaylight.openflowplugin.api.openflow.mastership.*;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.rf.state.rev170713.ResultState;
23 import java.util.ArrayList;
24 import java.util.List;
26 import static org.junit.Assert.*;
28 @RunWith(MockitoJUnitRunner.class)
29 public class MastershipChangeServiceManagerImplTest {
32 private MastershipChangeService service;
34 private MastershipChangeService secondService;
36 private DeviceInfo deviceInfo;
38 private FutureCallback<ResultState> resultStateFutureCallback;
40 private MasterChecker masterChecker;
42 private ReconciliationFrameworkEvent event;
44 private ReconciliationFrameworkEvent secondEvent;
46 final private MastershipChangeServiceManager manager = new MastershipChangeServiceManagerImpl();
47 private MastershipChangeRegistration registration;
48 private ReconciliationFrameworkRegistration registrationRF;
51 public void setUp() throws Exception {
52 registration = manager.register(service);
53 registrationRF = manager.reconciliationFrameworkRegistration(event);
57 public void register() throws Exception {
58 Assert.assertNotNull(registration);
62 public void registerTwice() throws Exception {
63 MastershipChangeRegistration registration2;
64 registration2 = manager.register(secondService);
65 Assert.assertNotNull(registration);
66 Assert.assertNotNull(registration2);
70 public void uregisterTwice() throws Exception {
71 MastershipChangeRegistration registration2;
72 registration2 = manager.register(secondService);
73 Assert.assertTrue(((MastershipChangeServiceManagerImpl)manager).serviceGroupListSize() == 2);
75 Assert.assertTrue(((MastershipChangeServiceManagerImpl)manager).serviceGroupListSize() == 1);
76 registration2.close();
77 Assert.assertTrue(((MastershipChangeServiceManagerImpl)manager).serviceGroupListSize() == 0);
81 public void reconciliationFrameworkRegistration() throws Exception {
82 Assert.assertNotNull(registrationRF);
85 @Test(expected = MastershipChangeException.class)
86 public void reconciliationFrameworkRegistrationTwice() throws Exception {
87 manager.reconciliationFrameworkRegistration(secondEvent);
91 public void unregosteringRF() throws Exception {
92 registrationRF.close();
93 ReconciliationFrameworkRegistration registration1;
94 registration1 = manager.reconciliationFrameworkRegistration(secondEvent);
95 Assert.assertNotNull(registration1);
99 public void becomeMaster() throws Exception {
100 manager.becomeMaster(deviceInfo);
101 Mockito.verify(service).onBecomeOwner(deviceInfo);
102 manager.becomeSlaveOrDisconnect(deviceInfo);
103 Mockito.verify(service).onLoseOwnership(deviceInfo);
107 public void becomeMasterBeforeDS() throws Exception {
108 manager.becomeMasterBeforeSubmittedDS(deviceInfo, resultStateFutureCallback);
109 Mockito.verify(event).onDevicePrepared(deviceInfo, resultStateFutureCallback);
113 public void isReconciliationFrameworkRegistered() throws Exception {
114 Assert.assertTrue(manager.isReconciliationFrameworkRegistered());
115 registrationRF.close();
116 Assert.assertFalse(manager.isReconciliationFrameworkRegistered());
120 public void evokeEventAfterRegistration() throws Exception {
121 List<DeviceInfo> deviceInfos = new ArrayList<>();
122 deviceInfos.add(deviceInfo);
123 manager.setMasterChecker(masterChecker);
124 Mockito.when(masterChecker.isAnyDeviceMastered()).thenReturn(true);
125 Mockito.when(masterChecker.listOfMasteredDevices()).thenReturn(deviceInfos);
126 manager.register(secondService);
127 Mockito.verify(secondService).onBecomeOwner(deviceInfo);