More OFP production code migration
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / connection / listener / SystemNotificationsListenerImplTest.java
index 62eb39a37017d461764fa25f561977861bb0f760..effbf13bf8fa860c6b4237c4cbda84b1af711e59 100644 (file)
@@ -8,6 +8,8 @@
 
 package org.opendaylight.openflowplugin.impl.connection.listener;
 
+import static org.mockito.ArgumentMatchers.any;
+
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.SettableFuture;
@@ -18,12 +20,13 @@ import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
-import org.mockito.Matchers;
 import org.mockito.Mock;
 import org.mockito.Mockito;
-import org.mockito.runners.MockitoJUnitRunner;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.opendaylight.mdsal.binding.api.NotificationPublishService;
 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
+import org.opendaylight.openflowplugin.api.openflow.connection.DeviceConnectionStatusProvider;
 import org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl;
 import org.opendaylight.openflowplugin.impl.util.ThreadPoolLoggingExecutor;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
@@ -37,6 +40,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.S
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEventBuilder;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
+import org.opendaylight.yangtools.yang.common.Uint32;
+import org.opendaylight.yangtools.yang.common.Uint8;
 
 /**
  * Testing basic bahavior of {@link SystemNotificationsListenerImpl}.
@@ -51,6 +56,10 @@ public class SystemNotificationsListenerImplTest {
     private ConnectionAdapter connectionAdapter;
     @Mock
     private FeaturesReply features;
+    @Mock
+    private DeviceConnectionStatusProvider deviceConnectionStatusProvider;
+    @Mock
+    private NotificationPublishService notificationPublishService;
 
     private ConnectionContext connectionContext;
     private ConnectionContextImpl connectionContextGolem;
@@ -64,26 +73,23 @@ public class SystemNotificationsListenerImplTest {
 
     @Before
     public void setUp() {
-        connectionContextGolem = new ConnectionContextImpl(connectionAdapter);
+        connectionContextGolem = new ConnectionContextImpl(connectionAdapter, deviceConnectionStatusProvider);
         connectionContextGolem.changeStateToWorking();
         connectionContextGolem.setNodeId(NODE_ID);
+        connectionContextGolem.setFeatures(features);
         connectionContext = Mockito.spy(connectionContextGolem);
 
         Mockito.when(connectionAdapter.getRemoteAddress()).thenReturn(
                 InetSocketAddress.createUnresolved("unit-odl.example.org", 4242));
 
-        Mockito.when(features.getAuxiliaryId()).thenReturn((short) 0);
-
-        Mockito.when(connectionContext.getConnectionAdapter()).thenReturn(connectionAdapter);
-        Mockito.when(connectionContext.getFeatures()).thenReturn(features);
-
-        systemNotificationsListener =
-                new SystemNotificationsListenerImpl(connectionContext, ECHO_REPLY_TIMEOUT, threadPool);
+        Mockito.when(features.getAuxiliaryId()).thenReturn(Uint8.ZERO);
 
+        systemNotificationsListener = new SystemNotificationsListenerImpl(connectionContext, ECHO_REPLY_TIMEOUT,
+                threadPool, notificationPublishService);
     }
 
     @After
-    public void tearDown() throws Exception {
+    public void tearDown() {
         Mockito.verifyNoMoreInteractions(connectionContext);
     }
 
@@ -91,9 +97,7 @@ public class SystemNotificationsListenerImplTest {
      * Successful scenario - connection is on and closes without errors.
      */
     @Test
-    public void testOnDisconnectEvent1() throws Exception {
-        Mockito.when(connectionAdapter.isAlive()).thenReturn(true);
-        Mockito.when(connectionAdapter.disconnect()).thenReturn(Futures.immediateFuture(Boolean.TRUE));
+    public void testOnDisconnectEvent1() {
 
         DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
         systemNotificationsListener.onDisconnectEvent(disconnectNotification);
@@ -108,9 +112,7 @@ public class SystemNotificationsListenerImplTest {
      * Broken scenario - connection is on but fails to close.
      */
     @Test
-    public void testOnDisconnectEvent2() throws Exception {
-        Mockito.when(connectionAdapter.isAlive()).thenReturn(true);
-        Mockito.when(connectionAdapter.disconnect()).thenReturn(Futures.immediateFuture(Boolean.FALSE));
+    public void testOnDisconnectEvent2() {
 
         DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
         systemNotificationsListener.onDisconnectEvent(disconnectNotification);
@@ -125,13 +127,9 @@ public class SystemNotificationsListenerImplTest {
      * Successful scenario - connection is already down.
      */
     @Test
-    public void testOnDisconnectEvent3() throws Exception {
+    public void testOnDisconnectEvent3() {
         connectionContextGolem.changeStateToTimeouting();
 
-        Mockito.when(connectionAdapter.isAlive()).thenReturn(true);
-        Mockito.when(connectionAdapter.disconnect())
-                .thenReturn(Futures.<Boolean>immediateFailedFuture(new Exception("unit exception")));
-
         DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
         systemNotificationsListener.onDisconnectEvent(disconnectNotification);
 
@@ -145,9 +143,8 @@ public class SystemNotificationsListenerImplTest {
      * Broken scenario - connection is on but throws error on close.
      */
     @Test
-    public void testOnDisconnectEvent4() throws Exception {
+    public void testOnDisconnectEvent4() {
         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);
@@ -164,9 +161,9 @@ public class SystemNotificationsListenerImplTest {
     @Test
     public void testOnSwitchIdleEvent1() throws Exception {
         final ListenableFuture<RpcResult<EchoOutput>> echoReply =
-                Futures.immediateFuture(RpcResultBuilder.success(new EchoOutputBuilder().setXid(0L).build()).build());
+                RpcResultBuilder.success(new EchoOutputBuilder().setXid(Uint32.ZERO).build()).buildFuture();
 
-        Mockito.when(connectionAdapter.echo(Matchers.any(EchoInput.class))).thenReturn(echoReply);
+        Mockito.when(connectionAdapter.echo(any(EchoInput.class))).thenReturn(echoReply);
 
         SwitchIdleEvent notification = new SwitchIdleEventBuilder().setInfo("wake up, device sleeps").build();
         systemNotificationsListener.onSwitchIdleEvent(notification);
@@ -175,7 +172,7 @@ public class SystemNotificationsListenerImplTest {
         Thread.sleep(SAFE_TIMEOUT);
 
         verifyCommonInvocations();
-        Mockito.verify(connectionAdapter, Mockito.timeout(SAFE_TIMEOUT)).echo(Matchers.any(EchoInput.class));
+        Mockito.verify(connectionAdapter, Mockito.timeout(SAFE_TIMEOUT)).echo(any(EchoInput.class));
         Mockito.verify(connectionAdapter, Mockito.never()).disconnect();
         Mockito.verify(connectionContext).changeStateToTimeouting();
         Mockito.verify(connectionContext).changeStateToWorking();
@@ -187,10 +184,10 @@ public class SystemNotificationsListenerImplTest {
     @Test
     public void testOnSwitchIdleEvent2() throws Exception {
         final SettableFuture<RpcResult<EchoOutput>> echoReply = SettableFuture.create();
-        Mockito.when(connectionAdapter.echo(Matchers.any(EchoInput.class))).thenReturn(echoReply);
+        Mockito.when(connectionAdapter.echo(any(EchoInput.class))).thenReturn(echoReply);
         Mockito.when(connectionAdapter.isAlive()).thenReturn(true);
         Mockito.when(connectionAdapter.disconnect())
-                .thenReturn(Futures.<Boolean>immediateFailedFuture(new Exception("unit exception")));
+                .thenReturn(Futures.immediateFailedFuture(new Exception("unit exception")));
 
         SwitchIdleEvent notification = new SwitchIdleEventBuilder().setInfo("wake up, device sleeps").build();
         systemNotificationsListener.onSwitchIdleEvent(notification);
@@ -198,7 +195,7 @@ public class SystemNotificationsListenerImplTest {
         Thread.sleep(SystemNotificationsListenerImpl.MAX_ECHO_REPLY_TIMEOUT + SAFE_TIMEOUT);
 
         verifyCommonInvocations();
-        Mockito.verify(connectionAdapter, Mockito.timeout(SAFE_TIMEOUT)).echo(Matchers.any(EchoInput.class));
+        Mockito.verify(connectionAdapter, Mockito.timeout(SAFE_TIMEOUT)).echo(any(EchoInput.class));
         Mockito.verify(connectionAdapter).disconnect();
         Mockito.verify(connectionContext).changeStateToTimeouting();
         Mockito.verify(connectionContext).closeConnection(true);
@@ -208,11 +205,11 @@ public class SystemNotificationsListenerImplTest {
 
     private void verifyCommonInvocations() {
         verifyCommonInvocationsSubSet();
+        Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).atLeastOnce()).getFeatures();
         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();
     }
 }