BUG-2340 Fix improper cleanup of resources in netconf ssh handler
[controller.git] / opendaylight / netconf / netconf-netty-util / src / test / java / org / opendaylight / controller / netconf / nettyutil / handler / ssh / client / AsyncSshHandlerTest.java
index 73f2287c8bce6151b9dd33f8fa0bc7ed8d39ac40..dfca1b80c41afd3d855216babc3a3ad6e8d45db4 100644 (file)
@@ -8,7 +8,6 @@
 
 package org.opendaylight.controller.netconf.nettyutil.handler.ssh.client;
 
-import static org.junit.Assert.fail;
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyBoolean;
 import static org.mockito.Matchers.anyObject;
@@ -18,9 +17,9 @@ import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyNoMoreInteractions;
 import static org.mockito.Mockito.verifyZeroInteractions;
 
 import com.google.common.util.concurrent.FutureCallback;
@@ -30,8 +29,10 @@ import com.google.common.util.concurrent.SettableFuture;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 import io.netty.channel.Channel;
+import io.netty.channel.ChannelFuture;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.channel.ChannelPromise;
+import io.netty.channel.DefaultChannelPromise;
 import java.io.IOException;
 import java.net.SocketAddress;
 import org.apache.sshd.ClientChannel;
@@ -143,6 +144,7 @@ public class AsyncSshHandlerTest {
         doReturn(ctx).when(ctx).fireChannelActive();
         doReturn(ctx).when(ctx).fireChannelInactive();
         doReturn(ctx).when(ctx).fireChannelRead(anyObject());
+        doReturn(mock(ChannelFuture.class)).when(ctx).disconnect(any(ChannelPromise.class));
         doReturn(getMockedPromise()).when(ctx).newPromise();
     }
 
@@ -179,7 +181,6 @@ public class AsyncSshHandlerTest {
         verify(subsystemChannel).setStreaming(ClientChannel.Streaming.Async);
 
         verify(promise).setSuccess();
-        verifyNoMoreInteractions(promise);
         verify(ctx).fireChannelActive();
     }
 
@@ -533,14 +534,8 @@ public class AsyncSshHandlerTest {
 
         verify(subsystemChannel).setStreaming(ClientChannel.Streaming.Async);
 
-        try {
-            sshChannelOpenListener.operationComplete(getFailedOpenFuture());
-            fail("Exception expected");
-        } catch (final Exception e) {
-            verify(promise).setFailure(any(Throwable.class));
-            verifyNoMoreInteractions(promise);
-            // TODO should ctx.channelInactive be called if we throw exception ?
-        }
+        sshChannelOpenListener.operationComplete(getFailedOpenFuture());
+        verify(promise).setFailure(any(Throwable.class));
     }
 
     @Test
@@ -555,14 +550,8 @@ public class AsyncSshHandlerTest {
 
         final AuthFuture authFuture = getFailedAuthFuture();
 
-        try {
-            sshAuthListener.operationComplete(authFuture);
-            fail("Exception expected");
-        } catch (final Exception e) {
-            verify(promise).setFailure(any(Throwable.class));
-            verifyNoMoreInteractions(promise);
-            // TODO should ctx.channelInactive be called ?
-        }
+        sshAuthListener.operationComplete(authFuture);
+        verify(promise).setFailure(any(Throwable.class));
     }
 
     private AuthFuture getFailedAuthFuture() {
@@ -584,14 +573,8 @@ public class AsyncSshHandlerTest {
         asyncSshHandler.connect(ctx, remoteAddress, localAddress, promise);
 
         final ConnectFuture connectFuture = getFailedConnectFuture();
-        try {
-            sshConnectListener.operationComplete(connectFuture);
-            fail("Exception expected");
-        } catch (final Exception e) {
-            verify(promise).setFailure(any(Throwable.class));
-            verifyNoMoreInteractions(promise);
-            // TODO should ctx.channelInactive be called ?
-        }
+        sshConnectListener.operationComplete(connectFuture);
+        verify(promise).setFailure(any(Throwable.class));
     }
 
     private ConnectFuture getFailedConnectFuture() {
@@ -602,10 +585,7 @@ public class AsyncSshHandlerTest {
     }
 
     private ChannelPromise getMockedPromise() {
-        final ChannelPromise promise = mock(ChannelPromise.class);
-        doReturn(promise).when(promise).setSuccess();
-        doReturn(promise).when(promise).setFailure(any(Throwable.class));
-        return promise;
+        return spy(new DefaultChannelPromise(channel));
     }
 
     private static abstract class SuccessFutureListener<T extends SshFuture<T>> implements FutureCallback<SshFutureListener<T>> {