X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=netconf%2Fnetconf-netty-util%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Fnettyutil%2Fhandler%2Fssh%2Fclient%2FAsyncSshHandlerTest.java;h=4023ece6dbd67311262e16961aeb4b70c3ad89a2;hb=edf3b06cb4e53a91f3756663dfbea9a1eb9154d1;hp=cc5d1d35919a88b7c2baf72ebe5435f4f1a0a39c;hpb=3911a32461cf5817787c807cad676d4164f0cbd4;p=netconf.git diff --git a/netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandlerTest.java b/netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandlerTest.java index cc5d1d3591..4023ece6db 100644 --- a/netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandlerTest.java +++ b/netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandlerTest.java @@ -21,9 +21,11 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyZeroInteractions; + import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.SettableFuture; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; @@ -32,6 +34,7 @@ import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPromise; import io.netty.channel.DefaultChannelPromise; +import io.netty.channel.EventLoop; import java.io.IOException; import java.net.SocketAddress; import org.apache.sshd.client.SshClient; @@ -57,8 +60,6 @@ import org.junit.Test; import org.mockito.Matchers; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler; public class AsyncSshHandlerTest { @@ -75,6 +76,8 @@ public class AsyncSshHandlerTest { private SocketAddress remoteAddress; @Mock private SocketAddress localAddress; + @Mock + private EventLoop eventLoop; private AsyncSshHandler asyncSshHandler; @@ -90,6 +93,7 @@ public class AsyncSshHandlerTest { stubAuth(); stubSshClient(); stubChannel(); + stubEventLoop(); stubCtx(); stubRemoteAddress(); @@ -116,7 +120,7 @@ public class AsyncSshHandlerTest { public void onSuccess(final SshFutureListener result) { sshAuthListener = result; } - }); + }, MoreExecutors.directExecutor()); doReturn(authFuture).when(authHandler).authenticate(any(ClientSession.class)); } @@ -124,12 +128,9 @@ public class AsyncSshHandlerTest { private static > ListenableFuture> stubAddListener(final T future) { final SettableFuture> listenerSettableFuture = SettableFuture.create(); - doAnswer(new Answer() { - @Override - public Object answer(final InvocationOnMock invocation) throws Throwable { - listenerSettableFuture.set((SshFutureListener) invocation.getArguments()[0]); - return null; - } + doAnswer(invocation -> { + listenerSettableFuture.set((SshFutureListener) invocation.getArguments()[0]); + return null; }).when(future).addListener(any(SshFutureListener.class)); return listenerSettableFuture; @@ -152,6 +153,11 @@ public class AsyncSshHandlerTest { doReturn("channel").when(channel).toString(); } + private void stubEventLoop() { + doReturn(eventLoop).when(channel).eventLoop(); + doReturn(Boolean.TRUE).when(eventLoop).inEventLoop(); + } + private void stubSshClient() throws IOException { doNothing().when(sshClient).start(); final ConnectFuture connectFuture = mock(ConnectFuture.class); @@ -160,7 +166,7 @@ public class AsyncSshHandlerTest { public void onSuccess(final SshFutureListener result) { sshConnectListener = result; } - }); + }, MoreExecutors.directExecutor()); doReturn(connectFuture).when(sshClient).connect("usr", remoteAddress); } @@ -212,12 +218,13 @@ public class AsyncSshHandlerTest { @Override public void onSuccess(final SshFutureListener result) { doReturn(new IllegalStateException()).when(mockedReadFuture).getException(); - doReturn(mockedReadFuture).when(mockedReadFuture).removeListener(Matchers.>any()); + doReturn(mockedReadFuture).when(mockedReadFuture) + .removeListener(Matchers.any()); doReturn(true).when(asyncOut).isClosing(); doReturn(true).when(asyncOut).isClosed(); result.operationComplete(mockedReadFuture); } - }); + }, MoreExecutors.directExecutor()); final IoOutputStream asyncIn = getMockedIoOutputStream(); final ChannelSubsystem subsystemChannel = getMockedSubsystemChannel(asyncOut, asyncIn); @@ -242,10 +249,11 @@ public class AsyncSshHandlerTest { @Override public void onSuccess(final SshFutureListener result) { doReturn(new IllegalStateException()).when(mockedReadFuture).getException(); - doReturn(mockedReadFuture).when(mockedReadFuture).removeListener(Matchers.>any()); + doReturn(mockedReadFuture).when(mockedReadFuture) + .removeListener(Matchers.any()); result.operationComplete(mockedReadFuture); } - }); + }, MoreExecutors.directExecutor()); final IoOutputStream asyncIn = getMockedIoOutputStream(); final ChannelSubsystem subsystemChannel = getMockedSubsystemChannel(asyncOut, asyncIn); @@ -297,7 +305,7 @@ public class AsyncSshHandlerTest { doReturn(true).when(asyncIn).isClosed(); result.operationComplete(ioWriteFuture); } - }); + }, MoreExecutors.directExecutor()); final ChannelSubsystem subsystemChannel = getMockedSubsystemChannel(asyncOut, asyncIn); final ClientSession sshSession = getMockedSshSession(subsystemChannel); @@ -331,11 +339,14 @@ public class AsyncSshHandlerTest { final ChannelPromise firstWritePromise = getMockedPromise(); - // intercept listener for first write, so we can invoke successful write later thus simulate pending of the first write - final ListenableFuture> firstWriteListenerFuture = stubAddListener(ioWriteFuture); + // intercept listener for first write, + // so we can invoke successful write later thus simulate pending of the first write + final ListenableFuture> firstWriteListenerFuture = + stubAddListener(ioWriteFuture); asyncSshHandler.write(ctx, Unpooled.copiedBuffer(new byte[]{0,1,2,3,4,5}), firstWritePromise); final SshFutureListener firstWriteListener = firstWriteListenerFuture.get(); - // intercept second listener, this is the listener for pending write for the pending write to know when pending state ended + // intercept second listener, + // this is the listener for pending write for the pending write to know when pending state ended final ListenableFuture> pendingListener = stubAddListener(ioWriteFuture); final ChannelPromise secondWritePromise = getMockedPromise(); @@ -377,8 +388,10 @@ public class AsyncSshHandlerTest { final ChannelPromise firstWritePromise = getMockedPromise(); - // intercept listener for first write, so we can invoke successful write later thus simulate pending of the first write - final ListenableFuture> firstWriteListenerFuture = stubAddListener(ioWriteFuture); + // intercept listener for first write, + // so we can invoke successful write later thus simulate pending of the first write + final ListenableFuture> firstWriteListenerFuture = + stubAddListener(ioWriteFuture); asyncSshHandler.write(ctx, Unpooled.copiedBuffer(new byte[]{0,1,2,3,4,5}), firstWritePromise); final ChannelPromise secondWritePromise = getMockedPromise(); @@ -447,7 +460,7 @@ public class AsyncSshHandlerTest { doReturn(true).when(closeFuture).isClosed(); result.operationComplete(closeFuture); } - }); + }, MoreExecutors.directExecutor()); doReturn(closeFuture).when(sshSession).close(false); doReturn(subsystemChannel).when(sshSession).createSubsystemChannel(anyString()); @@ -455,7 +468,8 @@ public class AsyncSshHandlerTest { return sshSession; } - private ChannelSubsystem getMockedSubsystemChannel(final IoInputStream asyncOut, final IoOutputStream asyncIn) throws IOException { + private ChannelSubsystem getMockedSubsystemChannel(final IoInputStream asyncOut, + final IoOutputStream asyncIn) throws IOException { final ChannelSubsystem subsystemChannel = mock(ChannelSubsystem.class); doReturn("subsystemChannel").when(subsystemChannel).toString(); @@ -467,7 +481,7 @@ public class AsyncSshHandlerTest { public void onSuccess(final SshFutureListener result) { sshChannelOpenListener = result; } - }); + }, MoreExecutors.directExecutor()); doReturn(asyncOut).when(subsystemChannel).getAsyncOut(); @@ -479,7 +493,7 @@ public class AsyncSshHandlerTest { private static IoOutputStream getMockedIoOutputStream() { final IoOutputStream mock = mock(IoOutputStream.class); final IoWriteFuture ioWriteFuture = mock(IoWriteFuture.class); - doReturn(ioWriteFuture).when(ioWriteFuture).addListener(Matchers.>any()); + doReturn(ioWriteFuture).when(ioWriteFuture).addListener(Matchers.any()); doReturn(true).when(ioWriteFuture).isWritten(); Futures.addCallback(stubAddListener(ioWriteFuture), new SuccessFutureListener() { @@ -487,7 +501,7 @@ public class AsyncSshHandlerTest { public void onSuccess(final SshFutureListener result) { result.operationComplete(ioWriteFuture); } - }); + }, MoreExecutors.directExecutor()); doReturn(ioWriteFuture).when(mock).write(any(Buffer.class)); doReturn(false).when(mock).isClosed(); @@ -499,10 +513,10 @@ public class AsyncSshHandlerTest { final IoInputStream mock = mock(IoInputStream.class); final IoReadFuture ioReadFuture = mock(IoReadFuture.class); doReturn(null).when(ioReadFuture).getException(); - doReturn(ioReadFuture).when(ioReadFuture).removeListener(Matchers.>any()); + doReturn(ioReadFuture).when(ioReadFuture).removeListener(Matchers.any()); doReturn(5).when(ioReadFuture).getRead(); doReturn(new ByteArrayBuffer(new byte[]{0, 1, 2, 3, 4})).when(ioReadFuture).getBuffer(); - doReturn(ioReadFuture).when(ioReadFuture).addListener(Matchers.>any()); + doReturn(ioReadFuture).when(ioReadFuture).addListener(Matchers.any()); // Always success for read Futures.addCallback(stubAddListener(ioReadFuture), new SuccessFutureListener() { @@ -510,7 +524,7 @@ public class AsyncSshHandlerTest { public void onSuccess(final SshFutureListener result) { result.operationComplete(ioReadFuture); } - }); + }, MoreExecutors.directExecutor()); doReturn(ioReadFuture).when(mock).read(any(Buffer.class)); doReturn(false).when(mock).isClosed(); @@ -588,14 +602,15 @@ public class AsyncSshHandlerTest { return spy(new DefaultChannelPromise(channel)); } - private static abstract class SuccessFutureListener> implements FutureCallback> { + private abstract static class SuccessFutureListener> + implements FutureCallback> { @Override - public abstract void onSuccess(final SshFutureListener result); + public abstract void onSuccess(SshFutureListener result); @Override - public void onFailure(final Throwable t) { - throw new RuntimeException(t); + public void onFailure(final Throwable throwable) { + throw new RuntimeException(throwable); } } }