2e0eaf43840bcb4321c1c2914c9f3f9e1d6ca84b
[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.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;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import static org.junit.Assert.*;
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 FutureCallback<ResultState> resultStateFutureCallback;
39     @Mock
40     private MasterChecker masterChecker;
41     @Mock
42     private ReconciliationFrameworkEvent event;
43     @Mock
44     private ReconciliationFrameworkEvent secondEvent;
45
46     final private MastershipChangeServiceManager manager = new MastershipChangeServiceManagerImpl();
47     private MastershipChangeRegistration registration;
48     private ReconciliationFrameworkRegistration registrationRF;
49
50     @Before
51     public void setUp() throws Exception {
52         registration = manager.register(service);
53         registrationRF = manager.reconciliationFrameworkRegistration(event);
54     }
55
56     @Test
57     public void register() throws Exception {
58         Assert.assertNotNull(registration);
59     }
60
61     @Test
62     public void registerTwice() throws Exception {
63         MastershipChangeRegistration registration2;
64         registration2 = manager.register(secondService);
65         Assert.assertNotNull(registration);
66         Assert.assertNotNull(registration2);
67     }
68
69     @Test
70     public void uregisterTwice() throws Exception {
71         MastershipChangeRegistration registration2;
72         registration2 = manager.register(secondService);
73         Assert.assertTrue(((MastershipChangeServiceManagerImpl)manager).serviceGroupListSize() == 2);
74         registration.close();
75         Assert.assertTrue(((MastershipChangeServiceManagerImpl)manager).serviceGroupListSize() == 1);
76         registration2.close();
77         Assert.assertTrue(((MastershipChangeServiceManagerImpl)manager).serviceGroupListSize() == 0);
78     }
79
80     @Test
81     public void reconciliationFrameworkRegistration() throws Exception {
82         Assert.assertNotNull(registrationRF);
83     }
84
85     @Test(expected = MastershipChangeException.class)
86     public void reconciliationFrameworkRegistrationTwice() throws Exception {
87         manager.reconciliationFrameworkRegistration(secondEvent);
88     }
89
90     @Test
91     public void unregosteringRF() throws Exception {
92         registrationRF.close();
93         ReconciliationFrameworkRegistration registration1;
94         registration1 = manager.reconciliationFrameworkRegistration(secondEvent);
95         Assert.assertNotNull(registration1);
96     }
97
98     @Test
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);
104     }
105
106     @Test
107     public void becomeMasterBeforeDS() throws Exception {
108         manager.becomeMasterBeforeSubmittedDS(deviceInfo, resultStateFutureCallback);
109         Mockito.verify(event).onDevicePrepared(deviceInfo, resultStateFutureCallback);
110     }
111
112     @Test
113     public void isReconciliationFrameworkRegistered() throws Exception {
114         Assert.assertTrue(manager.isReconciliationFrameworkRegistered());
115         registrationRF.close();
116         Assert.assertFalse(manager.isReconciliationFrameworkRegistered());
117     }
118
119     @Test
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);
128     }
129
130 }