Change return type of events
[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 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;
27
28 @RunWith(MockitoJUnitRunner.class)
29 public class MastershipChangeServiceManagerImplTest {
30
31     @Mock
32     private MastershipChangeService service;
33     @Mock
34     private MastershipChangeService secondService;
35     @Mock
36     private DeviceInfo deviceInfo;
37     @Mock
38     private MasterChecker masterChecker;
39     @Mock
40     private ReconciliationFrameworkEvent event;
41     @Mock
42     private ReconciliationFrameworkEvent secondEvent;
43
44     final private MastershipChangeServiceManager manager = new MastershipChangeServiceManagerImpl();
45     private MastershipChangeRegistration registration;
46     private ReconciliationFrameworkRegistration registrationRF;
47
48     @Before
49     public void setUp() throws Exception {
50         registration = manager.register(service);
51         registrationRF = manager.reconciliationFrameworkRegistration(event);
52     }
53
54     @Test
55     public void register() throws Exception {
56         Assert.assertNotNull(registration);
57     }
58
59     @Test
60     public void registerTwice() throws Exception {
61         MastershipChangeRegistration registration2;
62         registration2 = manager.register(secondService);
63         Assert.assertNotNull(registration);
64         Assert.assertNotNull(registration2);
65     }
66
67     @Test
68     public void uregisterTwice() throws Exception {
69         MastershipChangeRegistration registration2;
70         registration2 = manager.register(secondService);
71         Assert.assertTrue(((MastershipChangeServiceManagerImpl)manager).serviceGroupListSize() == 2);
72         registration.close();
73         Assert.assertTrue(((MastershipChangeServiceManagerImpl)manager).serviceGroupListSize() == 1);
74         registration2.close();
75         Assert.assertTrue(((MastershipChangeServiceManagerImpl)manager).serviceGroupListSize() == 0);
76     }
77
78     @Test
79     public void reconciliationFrameworkRegistration() throws Exception {
80         Assert.assertNotNull(registrationRF);
81     }
82
83     @Test(expected = MastershipChangeException.class)
84     public void reconciliationFrameworkRegistrationTwice() throws Exception {
85         manager.reconciliationFrameworkRegistration(secondEvent);
86     }
87
88     @Test
89     public void unregosteringRF() throws Exception {
90         registrationRF.close();
91         ReconciliationFrameworkRegistration registration1;
92         registration1 = manager.reconciliationFrameworkRegistration(secondEvent);
93         Assert.assertNotNull(registration1);
94     }
95
96     @Test
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);
102     }
103
104     @Test
105     public void becomeMasterBeforeDS() throws Exception {
106         manager.becomeMasterBeforeSubmittedDS(deviceInfo);
107         Mockito.verify(event).onDevicePrepared(deviceInfo);
108     }
109
110     @Test
111     public void isReconciliationFrameworkRegistered() throws Exception {
112         Assert.assertTrue(manager.isReconciliationFrameworkRegistered());
113         registrationRF.close();
114         Assert.assertFalse(manager.isReconciliationFrameworkRegistered());
115     }
116
117     @Test
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);
126     }
127
128 }