Merge "BUG-4117: preparation for FlowCapableNode notification impl."
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / connection / listener / SystemNotificationsListenerImplTest.java
index 118d6dfa1e41ae3fb47080eef579c16850a2aee6..a361d61ac9ca60eeda8a1d0ea438a5ec88f60382 100644 (file)
@@ -8,30 +8,20 @@
 
 package org.opendaylight.openflowplugin.impl.connection.listener;
 
-import static org.junit.Assert.*;
-
-import org.opendaylight.openflowplugin.impl.connection.HandshakeContextImpl;
-
-import java.util.concurrent.ArrayBlockingQueue;
-import java.util.concurrent.TimeUnit;
-import org.opendaylight.openflowplugin.openflow.md.core.ThreadPoolLoggingExecutor;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.SettableFuture;
 import java.net.InetSocketAddress;
-import java.util.List;
 import org.junit.After;
-import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
-import org.mockito.ArgumentCaptor;
-import org.mockito.Captor;
 import org.mockito.Matchers;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.runners.MockitoJUnitRunner;
 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
 import org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutputBuilder;
@@ -50,35 +40,33 @@ import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 public class SystemNotificationsListenerImplTest {
 
     public static final int SAFE_TIMEOUT = 1000;
+    private final static int ECHO_REPLY_TIMEOUT = 2000;
     @Mock
     private org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter connectionAdapter;
     @Mock
     private FeaturesReply features;
     private ConnectionContext connectionContext;
-    @Captor
-    private ArgumentCaptor<ConnectionContext.CONNECTION_STATE> connectionStateArgumentCaptor;
 
     private SystemNotificationsListenerImpl systemNotificationsListener;
     private ConnectionContextImpl connectionContextGolem;
+    private static final NodeId nodeId = new NodeId("OFP:TEST");
 
     @Before
     public void setUp() {
         connectionContextGolem = new ConnectionContextImpl(connectionAdapter);
-        connectionContextGolem.setConnectionState(ConnectionContext.CONNECTION_STATE.WORKING);
+        connectionContextGolem.changeStateToWorking();
+        connectionContextGolem.setNodeId(nodeId);
+        connectionContext = Mockito.spy(connectionContextGolem);
 
         Mockito.when(connectionAdapter.getRemoteAddress()).thenReturn(
                 InetSocketAddress.createUnresolved("unit-odl.example.org", 4242));
-        connectionContext = Mockito.spy(connectionContextGolem);
+
         Mockito.when(features.getAuxiliaryId()).thenReturn((short) 0);
 
         Mockito.when(connectionContext.getConnectionAdapter()).thenReturn(connectionAdapter);
         Mockito.when(connectionContext.getFeatures()).thenReturn(features);
 
-        ThreadPoolLoggingExecutor threadPoolLoggingExecutor = new ThreadPoolLoggingExecutor(2000, 2000, 0L, TimeUnit.MILLISECONDS,
-                new ArrayBlockingQueue<Runnable>(20), "OFHandshake-test identifier");
-
-        systemNotificationsListener = new SystemNotificationsListenerImpl(connectionContext,
-               new HandshakeContextImpl(threadPoolLoggingExecutor, null));
+        systemNotificationsListener = new SystemNotificationsListenerImpl(connectionContext, ECHO_REPLY_TIMEOUT);
     }
 
     @After
@@ -99,11 +87,10 @@ public class SystemNotificationsListenerImplTest {
         DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
         systemNotificationsListener.onDisconnectEvent(disconnectNotification);
 
-        verifyCommonInvocations();
-        Mockito.verify(connectionAdapter).disconnect();
-        Mockito.verify(connectionContext).setConnectionState(ConnectionContext.CONNECTION_STATE.RIP);
-        Mockito.verify(connectionContext).propagateClosingConnection();
-        assertTrue(systemNotificationsListener.handshakeContext.getHandshakePool().isTerminated());
+        verifyCommonInvocationsSubSet();
+        Mockito.verify(connectionContext).onConnectionClosed();
+        Mockito.verify(connectionContext).getConnectionAdapter();
+        Mockito.verify(connectionContext).getNodeId();
     }
 
     /**
@@ -119,11 +106,10 @@ public class SystemNotificationsListenerImplTest {
         DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
         systemNotificationsListener.onDisconnectEvent(disconnectNotification);
 
-        verifyCommonInvocations();
-        Mockito.verify(connectionAdapter).disconnect();
-        Mockito.verify(connectionContext).setConnectionState(ConnectionContext.CONNECTION_STATE.RIP);
-        Mockito.verify(connectionContext).propagateClosingConnection();
-        assertTrue(systemNotificationsListener.handshakeContext.getHandshakePool().isTerminated());
+        verifyCommonInvocationsSubSet();
+        Mockito.verify(connectionContext).onConnectionClosed();
+        Mockito.verify(connectionContext).getConnectionAdapter();
+        Mockito.verify(connectionContext).getNodeId();
     }
 
     /**
@@ -133,7 +119,7 @@ public class SystemNotificationsListenerImplTest {
      */
     @Test
     public void testOnDisconnectEvent3() throws Exception {
-        connectionContextGolem.setConnectionState(ConnectionContext.CONNECTION_STATE.TIMEOUTING);
+        connectionContextGolem.changeStateToTimeouting();
 
         Mockito.when(connectionAdapter.isAlive()).thenReturn(true);
         Mockito.when(connectionAdapter.disconnect()).thenReturn(Futures.<Boolean>immediateFailedFuture(new Exception("unit exception")));
@@ -141,11 +127,10 @@ public class SystemNotificationsListenerImplTest {
         DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
         systemNotificationsListener.onDisconnectEvent(disconnectNotification);
 
-        verifyCommonInvocations();
-        Mockito.verify(connectionAdapter).disconnect();
-        Mockito.verify(connectionContext).setConnectionState(ConnectionContext.CONNECTION_STATE.RIP);
-        Mockito.verify(connectionContext).propagateClosingConnection();
-        assertTrue(systemNotificationsListener.handshakeContext.getHandshakePool().isTerminated());
+        verifyCommonInvocationsSubSet();
+        Mockito.verify(connectionContext).onConnectionClosed();
+        Mockito.verify(connectionContext).getConnectionAdapter();
+        Mockito.verify(connectionContext).getNodeId();
     }
 
     /**
@@ -155,18 +140,16 @@ public class SystemNotificationsListenerImplTest {
      */
     @Test
     public void testOnDisconnectEvent4() throws Exception {
-        connectionContextGolem.setConnectionState(ConnectionContext.CONNECTION_STATE.RIP);
-
+        Mockito.when(connectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.RIP);
         Mockito.when(connectionAdapter.isAlive()).thenReturn(false);
 
         DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
         systemNotificationsListener.onDisconnectEvent(disconnectNotification);
 
-        verifyCommonInvocations();
-        Mockito.verify(connectionAdapter, Mockito.never()).disconnect();
-        Mockito.verify(connectionContext).setConnectionState(ConnectionContext.CONNECTION_STATE.RIP);
-        Mockito.verify(connectionContext).propagateClosingConnection();
-        assertTrue(systemNotificationsListener.handshakeContext.getHandshakePool().isTerminated());
+        verifyCommonInvocationsSubSet();
+        Mockito.verify(connectionContext).onConnectionClosed();
+        Mockito.verify(connectionContext).getConnectionAdapter();
+        Mockito.verify(connectionContext).getNodeId();
     }
 
     /**
@@ -187,18 +170,11 @@ public class SystemNotificationsListenerImplTest {
         EchoOutput echoReplyVal = new EchoOutputBuilder().build();
         echoReply.set(RpcResultBuilder.success(echoReplyVal).build());
 
-        Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).times(2))
-                .setConnectionState(connectionStateArgumentCaptor.capture());
-        List<ConnectionContext.CONNECTION_STATE> allStates = connectionStateArgumentCaptor.getAllValues();
-        Assert.assertEquals(2, allStates.size());
-        Assert.assertEquals(ConnectionContext.CONNECTION_STATE.TIMEOUTING, allStates.get(0));
-        Assert.assertEquals(ConnectionContext.CONNECTION_STATE.WORKING, allStates.get(1));
-
         verifyCommonInvocations();
         Mockito.verify(connectionAdapter, Mockito.timeout(SAFE_TIMEOUT)).echo(Matchers.any(EchoInput.class));
-        Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT)).setConnectionState(ConnectionContext.CONNECTION_STATE.WORKING);
         Mockito.verify(connectionAdapter, Mockito.never()).disconnect();
-        assertFalse(systemNotificationsListener.handshakeContext.getHandshakePool().isTerminated());
+        Mockito.verify(connectionContext).changeStateToTimeouting();
+        Mockito.verify(connectionContext).changeStateToWorking();
     }
 
     /**
@@ -220,21 +196,20 @@ public class SystemNotificationsListenerImplTest {
 
         verifyCommonInvocations();
         Mockito.verify(connectionAdapter, Mockito.timeout(SAFE_TIMEOUT)).echo(Matchers.any(EchoInput.class));
-        Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).times(2))
-                .setConnectionState(connectionStateArgumentCaptor.capture());
-        List<ConnectionContext.CONNECTION_STATE> allStates = connectionStateArgumentCaptor.getAllValues();
-        Assert.assertEquals(2, allStates.size());
-        Assert.assertEquals(ConnectionContext.CONNECTION_STATE.TIMEOUTING, allStates.get(0));
-        Assert.assertEquals(ConnectionContext.CONNECTION_STATE.RIP, allStates.get(1));
-
         Mockito.verify(connectionAdapter).disconnect();
-        Mockito.verify(connectionContext).propagateClosingConnection();
-        assertTrue(systemNotificationsListener.handshakeContext.getHandshakePool().isTerminated());
+        Mockito.verify(connectionContext).changeStateToTimeouting();
+        Mockito.verify(connectionContext).closeConnection(true);
+        Mockito.verify(connectionContext).getNodeId();
+
     }
 
     private void verifyCommonInvocations() {
+        verifyCommonInvocationsSubSet();
+        Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).atLeastOnce()).getConnectionAdapter();
+    }
+
+    private void verifyCommonInvocationsSubSet() {
         Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).atLeastOnce()).getConnectionState();
         Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).atLeastOnce()).getFeatures();
-        Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).atLeastOnce()).getConnectionAdapter();
     }
 }
\ No newline at end of file