Merge "Bug 5596 Singleton API test"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / role / RoleContextImplTest.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.role;
9
10
11 import com.google.common.util.concurrent.Futures;
12 import io.netty.util.HashedWheelTimer;
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.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
21 import org.opendaylight.openflowplugin.api.OFConstants;
22 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
23 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
24 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
25 import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInput;
30
31 @RunWith(MockitoJUnitRunner.class)
32 public class RoleContextImplTest {
33
34     @Mock
35     HashedWheelTimer hashedWheelTimer;
36     @Mock
37     private DeviceInfo deviceInfo;
38     @Mock
39     private RoleManager roleManager;
40     @Mock
41     private LifecycleService lifecycleService;
42     @Mock
43     private SalRoleService salRoleService;
44
45     private final NodeId nodeId = NodeId.getDefaultInstance("openflow:1");
46     private RoleContext roleContext;
47     private RoleContextImpl roleContextSpy;
48
49     @Before
50     public void setup() throws CandidateAlreadyRegisteredException {
51         roleContext = new RoleContextImpl(deviceInfo, hashedWheelTimer, roleManager);
52         roleContext.setSalRoleService(salRoleService);
53         Mockito.when(deviceInfo.getNodeId()).thenReturn(nodeId);
54         Mockito.when(salRoleService.setRole(Mockito.<SetRoleInput>any())).thenReturn(Futures.immediateFuture(null));
55         roleContextSpy = Mockito.spy((RoleContextImpl) roleContext);
56     }
57
58     @Test
59     public void testCreateRequestContext() throws Exception {
60         roleContext.createRequestContext();
61         Mockito.verify(deviceInfo).reserveXidForDeviceMessage();
62     }
63
64     @Test(expected = NullPointerException.class)
65     public void testSetSalRoleService() throws Exception {
66         roleContext.setSalRoleService(null);
67     }
68
69     @Test
70     public void testGetNodeId() throws Exception {
71         Assert.assertTrue(roleContext.getDeviceInfo().getNodeId().equals(nodeId));
72     }
73
74     @Test
75     public void startupClusterServices() throws Exception {
76         Mockito.when(deviceInfo.getVersion()).thenReturn(null);
77         roleContextSpy.startupClusterServices();
78         Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMEMASTER);
79     }
80
81     @Test
82     public void startupClusterServicesVersion10() throws Exception {
83         Mockito.when(deviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_0);
84         roleContextSpy.startupClusterServices();
85         Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMEMASTER);
86     }
87
88     @Test
89     public void startupClusterServicesVersion13() throws Exception {
90         Mockito.when(deviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
91         roleContextSpy.startupClusterServices();
92         Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMEMASTER);
93     }
94
95     @Test
96     public void stopClusterServicesNotDisconnected() throws Exception {
97         roleContextSpy.stopClusterServices(false);
98         Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMESLAVE);
99         Mockito.verify(roleManager, Mockito.never()).removeDeviceFromOperationalDS(Mockito.<DeviceInfo>any(), Mockito.anyInt());
100     }
101
102     @Test
103     public void stopClusterServicesDisconnected() throws Exception {
104         roleContextSpy.stopClusterServices(true);
105         Mockito.verify(roleManager, Mockito.atLeastOnce()).removeDeviceFromOperationalDS(Mockito.<DeviceInfo>any(), Mockito.anyInt());
106     }
107
108     @Test
109     public void makeDeviceSlave() throws Exception {
110         roleContextSpy.makeDeviceSlave();
111         Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMESLAVE);
112     }
113
114 }