Bug 8535: Fix IPv6 OXMHeader Mask issue
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / lifecycle / LifecycleServiceImplTest.java
1 /**
2  * Copyright (c) 2016 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.lifecycle;
9
10 import com.google.common.util.concurrent.Futures;
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.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
19 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
20 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
21 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
22 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
23 import org.opendaylight.openflowplugin.api.openflow.device.handlers.ClusterInitializationPhaseHandler;
24 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceRemovedHandler;
25 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
26 import org.opendaylight.openflowplugin.api.openflow.lifecycle.MastershipChangeListener;
27 import org.opendaylight.openflowplugin.impl.role.RoleChangeException;
28
29 @RunWith(MockitoJUnitRunner.class)
30 public class LifecycleServiceImplTest {
31
32     private static final String TEST_NODE = "test node";
33     private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER = ServiceGroupIdentifier.create(TEST_NODE);
34
35     @Mock
36     private DeviceInfo deviceInfo;
37     @Mock
38     private DeviceContext deviceContext;
39     @Mock
40     private ClusterSingletonServiceProvider clusterSingletonServiceProvider;
41     @Mock
42     private ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
43     @Mock
44     private MastershipChangeListener mastershipChangeListener;
45     @Mock
46     private ClusterInitializationPhaseHandler clusterInitializationPhaseHandler;
47     @Mock
48     private DeviceRemovedHandler deviceRemovedHandler;
49
50     private LifecycleService lifecycleService;
51
52     @Before
53     public void setUp() {
54         Mockito.when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
55         Mockito.when(deviceContext.getServiceIdentifier()).thenReturn(SERVICE_GROUP_IDENTIFIER);
56         Mockito.when(clusterSingletonServiceProvider.registerClusterSingletonService(Mockito.any()))
57                 .thenReturn(clusterSingletonServiceRegistration);
58
59         lifecycleService = new LifecycleServiceImpl(mastershipChangeListener);
60         lifecycleService.registerService(
61                 clusterSingletonServiceProvider,
62                 deviceContext);
63     }
64
65     @Test
66     public void getIdentifier() throws Exception {
67         Assert.assertEquals(lifecycleService.getIdentifier(), SERVICE_GROUP_IDENTIFIER);
68     }
69
70     @Test
71     public void makeDeviceSlave() throws Exception {
72         Mockito.when(deviceContext.makeDeviceSlave())
73                 .thenReturn(Futures.immediateFuture(null));
74         lifecycleService.makeDeviceSlave(deviceContext);
75         Mockito.verify(mastershipChangeListener).onSlaveRoleAcquired(Mockito.any(DeviceInfo.class));
76     }
77
78     @Test
79     public void makeDeviceSlaveFailure() throws Exception {
80         Mockito.when(deviceContext.makeDeviceSlave())
81                 .thenReturn(Futures.immediateFailedFuture(new RoleChangeException(TEST_NODE)));
82         lifecycleService.makeDeviceSlave(deviceContext);
83         Mockito.verify(mastershipChangeListener).onSlaveRoleNotAcquired(Mockito.any(DeviceInfo.class));
84     }
85
86     @Test
87     public void instantiateServiceInstanceFail() throws Exception {
88         Mockito.when(deviceContext.onContextInstantiateService(Mockito.any()))
89                 .thenReturn(false);
90         lifecycleService.instantiateServiceInstance();
91         Mockito.verify(mastershipChangeListener).onNotAbleToStartMastership(Mockito.any(DeviceInfo.class), Mockito.anyString());
92     }
93
94     @Test
95     public void close() throws Exception {
96         lifecycleService.registerDeviceRemovedHandler(deviceRemovedHandler);
97         lifecycleService.close();
98         Mockito.verify(deviceRemovedHandler).onDeviceRemoved(Mockito.any(DeviceInfo.class));
99     }
100
101     @Test
102     public void closeTwoTimes() throws Exception {
103         lifecycleService.registerDeviceRemovedHandler(deviceRemovedHandler);
104         lifecycleService.close();
105         lifecycleService.close();
106         Mockito.verify(deviceRemovedHandler, Mockito.times(1))
107                 .onDeviceRemoved(Mockito.any(DeviceInfo.class));
108     }
109
110     @Test
111     public void closeThreeTimes() throws Exception {
112         lifecycleService.registerDeviceRemovedHandler(deviceRemovedHandler);
113         lifecycleService.close();
114         lifecycleService.close();
115         Mockito.verify(deviceRemovedHandler, Mockito.times(1))
116                 .onDeviceRemoved(Mockito.any(DeviceInfo.class));
117     }
118
119 }