48e558c957eac99d54d5194fe9cdb84043717eeb
[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.junit.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.MastershipChangeService;
24 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager;
25 import org.opendaylight.openflowplugin.api.openflow.mastership.ReconciliationFrameworkEvent;
26 import org.opendaylight.yangtools.concepts.Registration;
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     private final MastershipChangeServiceManager manager = new MastershipChangeServiceManagerImpl();
45     private Registration registration;
46     private Registration registrationRF;
47
48     @Before
49     public void setUp() throws Exception {
50         registration = manager.register(service);
51         registrationRF = manager.reconciliationFrameworkRegistration(event);
52
53         Mockito.when(event.onDeviceDisconnected(Mockito.any())).thenReturn(Futures.immediateFuture(null));
54     }
55
56     @Test
57     public void register() {
58         Assert.assertNotNull(registration);
59     }
60
61     @Test
62     public void registerTwice() {
63         Registration registration2 = manager.register(secondService);
64         Assert.assertNotNull(registration);
65         Assert.assertNotNull(registration2);
66     }
67
68     @Test
69     public void uregisterTwice() throws Exception {
70         try (var registration2 = manager.register(secondService)) {
71             Assert.assertTrue(((MastershipChangeServiceManagerImpl)manager).serviceGroupListSize() == 2);
72             registration.close();
73             Assert.assertTrue(((MastershipChangeServiceManagerImpl)manager).serviceGroupListSize() == 1);
74         }
75         Assert.assertTrue(((MastershipChangeServiceManagerImpl)manager).serviceGroupListSize() == 0);
76     }
77
78     @Test
79     public void reconciliationFrameworkRegistration() {
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         Registration registration1 = manager.reconciliationFrameworkRegistration(secondEvent);
92         Assert.assertNotNull(registration1);
93     }
94
95     @Test
96     public void becomeMaster() {
97         manager.becomeMaster(deviceInfo);
98         Mockito.verify(service).onBecomeOwner(deviceInfo);
99         manager.becomeSlaveOrDisconnect(deviceInfo);
100         Mockito.verify(service).onLoseOwnership(deviceInfo);
101     }
102
103     @Test
104     public void becomeMasterBeforeDS() {
105         manager.becomeMasterBeforeSubmittedDS(deviceInfo);
106         Mockito.verify(event).onDevicePrepared(deviceInfo);
107     }
108
109     @Test
110     public void isReconciliationFrameworkRegistered() throws Exception {
111         Assert.assertTrue(manager.isReconciliationFrameworkRegistered());
112         registrationRF.close();
113         Assert.assertFalse(manager.isReconciliationFrameworkRegistered());
114     }
115
116     @Test
117     public void evokeEventAfterRegistration() {
118         List<DeviceInfo> deviceInfos = new ArrayList<>();
119         deviceInfos.add(deviceInfo);
120         manager.setMasterChecker(masterChecker);
121         Mockito.when(masterChecker.isAnyDeviceMastered()).thenReturn(true);
122         Mockito.when(masterChecker.listOfMasteredDevices()).thenReturn(deviceInfos);
123         manager.register(secondService);
124         Mockito.verify(secondService).onBecomeOwner(deviceInfo);
125     }
126
127 }