Remove redundant exception declarations
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / mastership / MastershipChangeServiceManagerImplTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.impl.mastership;
9
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;
28
29 @RunWith(MockitoJUnitRunner.class)
30 public class MastershipChangeServiceManagerImplTest {
31
32     @Mock
33     private MastershipChangeService service;
34     @Mock
35     private MastershipChangeService secondService;
36     @Mock
37     private DeviceInfo deviceInfo;
38     @Mock
39     private MasterChecker masterChecker;
40     @Mock
41     private ReconciliationFrameworkEvent event;
42     @Mock
43     private ReconciliationFrameworkEvent secondEvent;
44
45     private final MastershipChangeServiceManager manager = new MastershipChangeServiceManagerImpl();
46     private MastershipChangeRegistration registration;
47     private ReconciliationFrameworkRegistration registrationRF;
48
49     @Before
50     public void setUp() throws Exception {
51         registration = manager.register(service);
52         registrationRF = manager.reconciliationFrameworkRegistration(event);
53
54         Mockito.when(event.onDeviceDisconnected(Mockito.any())).thenReturn(Futures.immediateFuture(null));
55     }
56
57     @Test
58     public void register() {
59         Assert.assertNotNull(registration);
60     }
61
62     @Test
63     public void registerTwice() {
64         MastershipChangeRegistration registration2;
65         registration2 = manager.register(secondService);
66         Assert.assertNotNull(registration);
67         Assert.assertNotNull(registration2);
68     }
69
70     @Test
71     public void uregisterTwice() throws Exception {
72         MastershipChangeRegistration registration2;
73         registration2 = manager.register(secondService);
74         Assert.assertTrue(((MastershipChangeServiceManagerImpl)manager).serviceGroupListSize() == 2);
75         registration.close();
76         Assert.assertTrue(((MastershipChangeServiceManagerImpl)manager).serviceGroupListSize() == 1);
77         registration2.close();
78         Assert.assertTrue(((MastershipChangeServiceManagerImpl)manager).serviceGroupListSize() == 0);
79     }
80
81     @Test
82     public void reconciliationFrameworkRegistration() {
83         Assert.assertNotNull(registrationRF);
84     }
85
86     @Test(expected = MastershipChangeException.class)
87     public void reconciliationFrameworkRegistrationTwice() throws Exception {
88         manager.reconciliationFrameworkRegistration(secondEvent);
89     }
90
91     @Test
92     public void unregosteringRF() throws Exception {
93         registrationRF.close();
94         ReconciliationFrameworkRegistration registration1;
95         registration1 = manager.reconciliationFrameworkRegistration(secondEvent);
96         Assert.assertNotNull(registration1);
97     }
98
99     @Test
100     public void becomeMaster() {
101         manager.becomeMaster(deviceInfo);
102         Mockito.verify(service).onBecomeOwner(deviceInfo);
103         manager.becomeSlaveOrDisconnect(deviceInfo);
104         Mockito.verify(service).onLoseOwnership(deviceInfo);
105     }
106
107     @Test
108     public void becomeMasterBeforeDS() {
109         manager.becomeMasterBeforeSubmittedDS(deviceInfo);
110         Mockito.verify(event).onDevicePrepared(deviceInfo);
111     }
112
113     @Test
114     public void isReconciliationFrameworkRegistered() throws Exception {
115         Assert.assertTrue(manager.isReconciliationFrameworkRegistered());
116         registrationRF.close();
117         Assert.assertFalse(manager.isReconciliationFrameworkRegistered());
118     }
119
120     @Test
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);
129     }
130
131 }