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