Merge "Bug 6366 - of-switch-config-pusher - DTCL instead of DTL"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / role / RoleContextImplTest.java
index ca0ffb6d193c0ae9a57b681a602d66f8f887df87..75a233ed4f4ad65173a8212d9d006e80afe42915 100644 (file)
@@ -8,52 +8,57 @@
 package org.opendaylight.openflowplugin.impl.role;
 
 
+import com.google.common.util.concurrent.Futures;
+import io.netty.util.HashedWheelTimer;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.Mockito;
-import org.mockito.invocation.InvocationOnMock;
 import org.mockito.runners.MockitoJUnitRunner;
-import org.mockito.stubbing.Answer;
 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
-import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
-import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
-import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
+import org.opendaylight.openflowplugin.api.OFConstants;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
-import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
+import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
 import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInput;
 
 @RunWith(MockitoJUnitRunner.class)
 public class RoleContextImplTest {
 
-    private static final Logger LOG = LoggerFactory.getLogger(RoleContextImpl.class);
-
     @Mock
-    private LifecycleConductor conductor;
+    HashedWheelTimer hashedWheelTimer;
     @Mock
     private DeviceInfo deviceInfo;
     @Mock
     private RoleManager roleManager;
+    @Mock
+    private LifecycleService lifecycleService;
+    @Mock
+    private SalRoleService salRoleService;
 
     private final NodeId nodeId = NodeId.getDefaultInstance("openflow:1");
     private RoleContext roleContext;
+    private RoleContextImpl roleContextSpy;
 
     @Before
     public void setup() throws CandidateAlreadyRegisteredException {
-        roleContext = new RoleContextImpl(deviceInfo, conductor, roleManager);
+        roleContext = new RoleContextImpl(deviceInfo, hashedWheelTimer, roleManager);
+        roleContext.setSalRoleService(salRoleService);
         Mockito.when(deviceInfo.getNodeId()).thenReturn(nodeId);
+        Mockito.when(salRoleService.setRole(Mockito.<SetRoleInput>any())).thenReturn(Futures.immediateFuture(null));
+        roleContextSpy = Mockito.spy((RoleContextImpl) roleContext);
     }
 
     @Test
     public void testCreateRequestContext() throws Exception {
         roleContext.createRequestContext();
-        Mockito.verify(conductor).reserveXidForDeviceMessage(deviceInfo);
+        Mockito.verify(deviceInfo).reserveXidForDeviceMessage();
     }
 
     @Test(expected = NullPointerException.class)
@@ -65,4 +70,45 @@ public class RoleContextImplTest {
     public void testGetNodeId() throws Exception {
         Assert.assertTrue(roleContext.getDeviceInfo().getNodeId().equals(nodeId));
     }
+
+    @Test
+    public void startupClusterServices() throws Exception {
+        Mockito.when(deviceInfo.getVersion()).thenReturn(null);
+        roleContextSpy.startupClusterServices();
+        Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMEMASTER);
+    }
+
+    @Test
+    public void startupClusterServicesVersion10() throws Exception {
+        Mockito.when(deviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_0);
+        roleContextSpy.startupClusterServices();
+        Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMEMASTER);
+    }
+
+    @Test
+    public void startupClusterServicesVersion13() throws Exception {
+        Mockito.when(deviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
+        roleContextSpy.startupClusterServices();
+        Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMEMASTER);
+    }
+
+    @Test
+    public void stopClusterServicesNotDisconnected() throws Exception {
+        roleContextSpy.stopClusterServices(false);
+        Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMESLAVE);
+        Mockito.verify(roleManager, Mockito.never()).removeDeviceFromOperationalDS(Mockito.<DeviceInfo>any(), Mockito.anyInt());
+    }
+
+    @Test
+    public void stopClusterServicesDisconnected() throws Exception {
+        roleContextSpy.stopClusterServices(true);
+        Mockito.verify(roleManager, Mockito.atLeastOnce()).removeDeviceFromOperationalDS(Mockito.<DeviceInfo>any(), Mockito.anyInt());
+    }
+
+    @Test
+    public void makeDeviceSlave() throws Exception {
+        roleContextSpy.makeDeviceSlave();
+        Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMESLAVE);
+    }
+
 }