Merge "BUG-4117: add support of Old Notif. for Statistics"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / role / RoleManagerImplTest.java
index bba0245d9a75e345d8aba960745ea93dc9c3ed42..3febe5466ccd088dd1f7727ba3abffa370f0536e 100644 (file)
@@ -36,6 +36,7 @@ import org.opendaylight.openflowplugin.api.OFConstants;
 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
@@ -43,13 +44,14 @@ import org.opendaylight.openflowplugin.api.openflow.lifecycle.RoleChangeListener
 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
 
-/**
- * Created by Jozef Bacigal
- * Date: 19.4.2016.
- * Time: 13:08
- */
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.atLeastOnce;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+
 @RunWith(MockitoJUnitRunner.class)
 public class RoleManagerImplTest {
 
@@ -92,6 +94,12 @@ public class RoleManagerImplTest {
     @Mock
     LifecycleConductor conductor;
 
+    @Mock
+    DeviceState deviceState;
+
+    @Mock
+    GetFeaturesOutput featuresOutput;
+
     private RoleManagerImpl roleManager;
     private RoleManagerImpl roleManagerSpy;
     private RoleContext roleContextSpy;
@@ -101,15 +109,18 @@ public class RoleManagerImplTest {
     private final EntityOwnershipChange masterTxEntity = new EntityOwnershipChange(RoleManagerImpl.makeTxEntity(nodeId), false, true, true);
     private final EntityOwnershipChange slaveEntity = new EntityOwnershipChange(RoleManagerImpl.makeEntity(nodeId), true, false, true);
     private final EntityOwnershipChange slaveTxEntityLast = new EntityOwnershipChange(RoleManagerImpl.makeTxEntity(nodeId), true, false, false);
+    private final EntityOwnershipChange masterEntityNotOwner = new EntityOwnershipChange(RoleManagerImpl.makeEntity(nodeId), true, false, true);
 
     private InOrder inOrder;
 
     @Before
     public void setUp() throws Exception {
         CheckedFuture<Void, TransactionCommitFailedException> future = Futures.immediateCheckedFuture(null);
+        Mockito.when(deviceState.getFeatures()).thenReturn(featuresOutput);
         Mockito.when(entityOwnershipService.registerListener(Mockito.anyString(), Mockito.any(EntityOwnershipListener.class))).thenReturn(entityOwnershipListenerRegistration);
         Mockito.when(entityOwnershipService.registerCandidate(Mockito.any(Entity.class))).thenReturn(entityOwnershipCandidateRegistration);
         Mockito.when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
+        Mockito.when(deviceContext.getDeviceState()).thenReturn(deviceState);
         Mockito.when(connectionContext.getFeatures()).thenReturn(featuresReply);
         Mockito.when(connectionContext.getNodeId()).thenReturn(nodeId);
         Mockito.when(connectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
@@ -125,7 +136,6 @@ public class RoleManagerImplTest {
         roleManager.setDeviceTerminationPhaseHandler(deviceTerminationPhaseHandler);
         Mockito.when(conductor.getDeviceContext(Mockito.<NodeId>any())).thenReturn(deviceContext);
         roleManagerSpy = Mockito.spy(roleManager);
-        Mockito.doNothing().when(roleManagerSpy).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
         roleManagerSpy.onDeviceContextLevelUp(nodeId);
         roleContextSpy = Mockito.spy(roleManager.getRoleContext(nodeId));
         inOrder = Mockito.inOrder(entityOwnershipListenerRegistration, roleManagerSpy, roleContextSpy);
@@ -177,6 +187,7 @@ public class RoleManagerImplTest {
 
     @Test
     public void testOwnershipChanged2() throws Exception {
+        Mockito.doNothing().when(roleManagerSpy).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
         roleManagerSpy.ownershipChanged(masterEntity);
         roleManagerSpy.ownershipChanged(masterTxEntity);
         inOrder.verify(roleManagerSpy, Mockito.calls(1)).changeOwnershipForTxEntity(Mockito.<EntityOwnershipChange>any(),Mockito.<RoleContext>any());
@@ -218,6 +229,23 @@ public class RoleManagerImplTest {
         inOrder.verify(roleManagerSpy, Mockito.calls(1)).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
     }
 
+    @Test
+    public void testChangeOwnershipForTxEntity3() throws Exception {
+        Mockito.when(roleContextSpy.isTxCandidateRegistered()).thenReturn(false);
+        roleManagerSpy.changeOwnershipForTxEntity(slaveTxEntityLast, roleContextSpy);
+        verify(roleContextSpy).close();
+        verify(roleContextSpy).getNodeId();
+        verify(conductor).closeConnection(nodeId);
+    }
+
+    @Test
+    public void testChangeOwnershipForTxEntity4() throws Exception {
+        Mockito.when(roleContextSpy.isTxCandidateRegistered()).thenReturn(true);
+        roleManagerSpy.changeOwnershipForTxEntity(masterEntityNotOwner, roleContextSpy);
+        verify(roleContextSpy).close();
+        verify(conductor).closeConnection(nodeId);
+    }
+
     @Test
     public void testAddListener() throws Exception {
         roleManager.addRoleChangeListener((new RoleChangeListener() {
@@ -239,5 +267,26 @@ public class RoleManagerImplTest {
         roleManager.notifyListenersRoleChangeOnDevice(nodeId, true, OfpRole.BECOMEMASTER, false);
     }
 
+    @Test
+    public void testMakeDeviceRoleChange() throws Exception{
+        roleManagerSpy.makeDeviceRoleChange(OfpRole.BECOMEMASTER, roleContextSpy, true);
+        verify(roleManagerSpy, atLeastOnce()).sendRoleChangeToDevice(Mockito.<OfpRole>any(), Mockito.<RoleContext>any());
+        verify(roleManagerSpy, atLeastOnce()).notifyListenersRoleChangeOnDevice(Mockito.<NodeId>any(), eq(true), Mockito.<OfpRole>any(), eq(true));
+    }
 
+    @Test
+    public void testServicesChangeDone() throws Exception {
+        final NodeId nodeId2 = NodeId.getDefaultInstance("openflow:2");
+        roleManagerSpy.setRoleContext(nodeId2, roleContextSpy);
+        roleManagerSpy.servicesChangeDone(nodeId2, true);
+        verify(roleContextSpy).unregisterCandidate(Mockito.<Entity>any());
+    }
+
+    @Test
+    public void testServicesChangeDoneContextIsNull() throws Exception {
+        final NodeId nodeId2 = NodeId.getDefaultInstance("openflow:2");
+        roleManagerSpy.setRoleContext(nodeId, roleContextSpy);
+        roleManagerSpy.servicesChangeDone(nodeId2, true);
+        verify(roleContextSpy, never()).unregisterCandidate(Mockito.<Entity>any());
+    }
 }
\ No newline at end of file