Simplify error checking in writeWithPendingDetection()
[netconf.git] / netconf / netconf-netty-util / src / test / java / org / opendaylight / netconf / nettyutil / handler / ssh / client / AsyncSshHandlerTest.java
index 31462cb897a1ef18fc57fd3fca6a1225f7da01dd..e504c679ba5b3d0e04eba433f193f89208b1a91b 100644 (file)
@@ -32,6 +32,7 @@ import io.netty.channel.ChannelFuture;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.channel.ChannelPromise;
 import io.netty.channel.DefaultChannelPromise;
+import io.netty.util.concurrent.EventExecutor;
 import java.io.IOException;
 import java.net.SocketAddress;
 import java.util.concurrent.TimeUnit;
@@ -76,6 +77,8 @@ public class AsyncSshHandlerTest {
     private SocketAddress localAddress;
     @Mock
     private ChannelConfig channelConfig;
+    @Mock
+    private EventExecutor executor;
 
     private AsyncSshHandler asyncSshHandler;
 
@@ -136,6 +139,11 @@ public class AsyncSshHandlerTest {
         doReturn(ctx).when(ctx).fireChannelInactive();
         doReturn(mock(ChannelFuture.class)).when(ctx).disconnect(any(ChannelPromise.class));
         doReturn(getMockedPromise()).when(ctx).newPromise();
+        doReturn(executor).when(ctx).executor();
+        doAnswer(invocation -> {
+            invocation.getArgument(0, Runnable.class).run();
+            return null;
+        }).when(executor).execute(any());
     }
 
     private void stubChannel() {
@@ -205,12 +213,11 @@ public class AsyncSshHandlerTest {
         final IoInputStream asyncOut = getMockedIoInputStream();
         final IoOutputStream asyncIn = getMockedIoOutputStream();
 
-        final IoWriteFuture ioWriteFuture = asyncIn.writePacket(new ByteArrayBuffer());
+        final IoWriteFuture ioWriteFuture = asyncIn.writeBuffer(new ByteArrayBuffer());
 
         Futures.addCallback(stubAddListener(ioWriteFuture), new SuccessFutureListener<IoWriteFuture>() {
             @Override
             public void onSuccess(final SshFutureListener<IoWriteFuture> result) {
-                doReturn(false).when(ioWriteFuture).isWritten();
                 doReturn(new IllegalStateException()).when(ioWriteFuture).getException();
                 result.operationComplete(ioWriteFuture);
             }
@@ -236,7 +243,7 @@ public class AsyncSshHandlerTest {
 
         final IoInputStream asyncOut = getMockedIoInputStream();
         final IoOutputStream asyncIn = getMockedIoOutputStream();
-        final IoWriteFuture ioWriteFuture = asyncIn.writePacket(new ByteArrayBuffer());
+        final IoWriteFuture ioWriteFuture = asyncIn.writeBuffer(new ByteArrayBuffer());
 
         final NettyAwareChannelSubsystem subsystemChannel = getMockedSubsystemChannel(asyncOut, asyncIn);
         final ClientSession sshSession = getMockedSshSession(subsystemChannel);
@@ -261,7 +268,7 @@ public class AsyncSshHandlerTest {
         final ChannelPromise secondWritePromise = getMockedPromise();
         asyncSshHandler.write(ctx, Unpooled.copiedBuffer(new byte[]{0, 1, 2, 3, 4, 5}), secondWritePromise);
 
-        doReturn(ioWriteFuture).when(asyncIn).writePacket(any(Buffer.class));
+        doReturn(ioWriteFuture).when(asyncIn).writeBuffer(any(Buffer.class));
 
         verifyNoMoreInteractions(firstWritePromise, secondWritePromise);
 
@@ -283,7 +290,7 @@ public class AsyncSshHandlerTest {
 
         final IoInputStream asyncOut = getMockedIoInputStream();
         final IoOutputStream asyncIn = getMockedIoOutputStream();
-        final IoWriteFuture ioWriteFuture = asyncIn.writePacket(new ByteArrayBuffer());
+        final IoWriteFuture ioWriteFuture = asyncIn.writeBuffer(new ByteArrayBuffer());
 
         final NettyAwareChannelSubsystem subsystemChannel = getMockedSubsystemChannel(asyncOut, asyncIn);
         final ClientSession sshSession = getMockedSshSession(subsystemChannel);
@@ -303,7 +310,7 @@ public class AsyncSshHandlerTest {
 
         final ChannelPromise secondWritePromise = getMockedPromise();
         // now make write throw pending exception
-        doThrow(WritePendingException.class).when(asyncIn).writePacket(any(Buffer.class));
+        doThrow(WritePendingException.class).when(asyncIn).writeBuffer(any(Buffer.class));
         for (int i = 0; i < 1001; i++) {
             asyncSshHandler.write(ctx, Unpooled.copiedBuffer(new byte[]{0, 1, 2, 3, 4, 5}), secondWritePromise);
         }
@@ -334,20 +341,20 @@ public class AsyncSshHandlerTest {
     }
 
     private static OpenFuture getSuccessOpenFuture() {
-        final OpenFuture failedOpenFuture = mock(OpenFuture.class);
-        doReturn(true).when(failedOpenFuture).isOpened();
-        return failedOpenFuture;
+        final OpenFuture openFuture = mock(OpenFuture.class);
+        doReturn(null).when(openFuture).getException();
+        return openFuture;
     }
 
     private static AuthFuture getSuccessAuthFuture() {
         final AuthFuture authFuture = mock(AuthFuture.class);
-        doReturn(true).when(authFuture).isSuccess();
+        doReturn(null).when(authFuture).getException();
         return authFuture;
     }
 
     private static ConnectFuture getSuccessConnectFuture(final ClientSession sshSession) {
         final ConnectFuture connectFuture = mock(ConnectFuture.class);
-        doReturn(true).when(connectFuture).isConnected();
+        doReturn(null).when(connectFuture).getException();
 
         doReturn(sshSession).when(connectFuture).getSession();
         return connectFuture;
@@ -399,7 +406,7 @@ public class AsyncSshHandlerTest {
     private static IoOutputStream getMockedIoOutputStream() throws IOException {
         final IoOutputStream mock = mock(IoOutputStream.class);
         final IoWriteFuture ioWriteFuture = mock(IoWriteFuture.class);
-        doReturn(true).when(ioWriteFuture).isWritten();
+        doReturn(null).when(ioWriteFuture).getException();
 
         Futures.addCallback(stubAddListener(ioWriteFuture), new SuccessFutureListener<IoWriteFuture>() {
             @Override
@@ -408,7 +415,7 @@ public class AsyncSshHandlerTest {
             }
         }, MoreExecutors.directExecutor());
 
-        doReturn(ioWriteFuture).when(mock).writePacket(any(Buffer.class));
+        doReturn(ioWriteFuture).when(mock).writeBuffer(any(Buffer.class));
         doReturn(false).when(mock).isClosed();
         doReturn(false).when(mock).isClosing();
         return mock;
@@ -467,16 +474,14 @@ public class AsyncSshHandlerTest {
 
     private static AuthFuture getFailedAuthFuture() {
         final AuthFuture authFuture = mock(AuthFuture.class);
-        doReturn(false).when(authFuture).isSuccess();
         doReturn(new IllegalStateException()).when(authFuture).getException();
         return authFuture;
     }
 
     private static OpenFuture getFailedOpenFuture() {
-        final OpenFuture authFuture = mock(OpenFuture.class);
-        doReturn(false).when(authFuture).isOpened();
-        doReturn(new IllegalStateException()).when(authFuture).getException();
-        return authFuture;
+        final OpenFuture openFuture = mock(OpenFuture.class);
+        doReturn(new IllegalStateException()).when(openFuture).getException();
+        return openFuture;
     }
 
     @Test
@@ -490,7 +495,6 @@ public class AsyncSshHandlerTest {
 
     private static ConnectFuture getFailedConnectFuture() {
         final ConnectFuture connectFuture = mock(ConnectFuture.class);
-        doReturn(false).when(connectFuture).isConnected();
         doReturn(new IllegalStateException()).when(connectFuture).getException();
         return connectFuture;
     }