Remove TestableContext
[netconf.git] / netconf / callhome-protocol / src / test / java / org / opendaylight / netconf / callhome / protocol / CallHomeSessionContextTest.java
index d989ca1b09d4e4bee144defdc0b763cd72999660..1be02d6563ac3a1b7a47797d428b7856719989b9 100644 (file)
@@ -5,15 +5,17 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.netconf.callhome.protocol;
 
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyBoolean;
-import static org.mockito.Matchers.anyString;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 
@@ -24,25 +26,23 @@ import io.netty.channel.EventLoopGroup;
 import java.io.IOException;
 import java.net.InetSocketAddress;
 import java.security.PublicKey;
-import org.apache.sshd.client.channel.ChannelSubsystem;
-import org.apache.sshd.client.channel.ClientChannel;
-import org.apache.sshd.client.channel.ClientChannel.Streaming;
-import org.apache.sshd.client.future.OpenFuture;
-import org.apache.sshd.client.session.ClientSession;
-import org.apache.sshd.client.session.ClientSessionImpl;
-import org.apache.sshd.common.AttributeStore.AttributeKey;
-import org.apache.sshd.common.future.SshFutureListener;
-import org.apache.sshd.common.io.IoInputStream;
-import org.apache.sshd.common.io.IoOutputStream;
-import org.apache.sshd.common.io.IoReadFuture;
-import org.apache.sshd.common.io.IoSession;
-import org.apache.sshd.common.kex.KeyExchange;
-import org.apache.sshd.common.util.buffer.Buffer;
 import org.junit.Before;
 import org.junit.Test;
-import org.mockito.Mockito;
 import org.opendaylight.netconf.client.NetconfClientSessionListener;
 import org.opendaylight.netconf.client.NetconfClientSessionNegotiatorFactory;
+import org.opendaylight.netconf.shaded.sshd.client.channel.ChannelSubsystem;
+import org.opendaylight.netconf.shaded.sshd.client.channel.ClientChannel;
+import org.opendaylight.netconf.shaded.sshd.client.future.OpenFuture;
+import org.opendaylight.netconf.shaded.sshd.client.session.ClientSessionImpl;
+import org.opendaylight.netconf.shaded.sshd.common.AttributeRepository.AttributeKey;
+import org.opendaylight.netconf.shaded.sshd.common.channel.StreamingChannel;
+import org.opendaylight.netconf.shaded.sshd.common.future.SshFutureListener;
+import org.opendaylight.netconf.shaded.sshd.common.io.IoInputStream;
+import org.opendaylight.netconf.shaded.sshd.common.io.IoOutputStream;
+import org.opendaylight.netconf.shaded.sshd.common.io.IoReadFuture;
+import org.opendaylight.netconf.shaded.sshd.common.io.IoSession;
+import org.opendaylight.netconf.shaded.sshd.common.kex.KeyExchange;
+import org.opendaylight.netconf.shaded.sshd.common.util.buffer.Buffer;
 
 public class CallHomeSessionContextTest {
     private ClientSessionImpl mockSession;
@@ -70,48 +70,52 @@ public class CallHomeSessionContextTest {
 
         realFactory = new CallHomeSessionContext.Factory(mockNettyGroup, mockNegotiatior, subListener);
 
-        KeyExchange kexMock = Mockito.mock(KeyExchange.class);
-        Mockito.doReturn(kexMock).when(mockSession).getKex();
+        KeyExchange kexMock = mock(KeyExchange.class);
+        doReturn(kexMock).when(mockSession).getKex();
 
-        PublicKey keyMock = Mockito.mock(PublicKey.class);
-        Mockito.doReturn(keyMock).when(kexMock).getServerKey();
+        PublicKey keyMock = mock(PublicKey.class);
+        doReturn(keyMock).when(mockSession).getServerKey();
         IoReadFuture mockFuture = mock(IoReadFuture.class);
         IoInputStream mockIn = mock(IoInputStream.class);
-        Mockito.doReturn(mockFuture).when(mockIn).read(any(Buffer.class));
+        doReturn(mockFuture).when(mockIn).read(any(Buffer.class));
         IoOutputStream mockOut = mock(IoOutputStream.class);
 
-        Mockito.doReturn(mockIn).when(mockChannel).getAsyncOut();
-        Mockito.doReturn(mockOut).when(mockChannel).getAsyncIn();
+        doReturn(mockIn).when(mockChannel).getAsyncOut();
+        doReturn(mockOut).when(mockChannel).getAsyncIn();
 
-        Mockito.doReturn(true).when(mockAuth).isServerAllowed();
+        doReturn(true).when(mockAuth).isServerAllowed();
 
         IoSession ioSession = mock(IoSession.class);
-        Mockito.doReturn(ioSession).when(mockSession).getIoSession();
-        Mockito.doReturn(address).when(ioSession).getRemoteAddress();
-        Mockito.doReturn(null).when(mockSession).setAttribute(any(AttributeKey.class), any());
-        Mockito.doReturn(null).when(mockSession).getAttribute(any(AttributeKey.class));
-        Mockito.doReturn("testSession").when(mockSession).toString();
+        doReturn(ioSession).when(mockSession).getIoSession();
+        doReturn(address).when(ioSession).getRemoteAddress();
+        doReturn(null).when(mockSession).setAttribute(any(AttributeKey.class), any());
+        doReturn(null).when(mockSession).getAttribute(any(AttributeKey.class));
+        doReturn("testSession").when(mockSession).toString();
 
         doNothing().when(mockAuth).applyTo(mockSession);
-        Mockito.doReturn("test").when(mockAuth).getSessionName();
+        doReturn("test").when(mockAuth).getSessionName();
     }
 
     @Test
     public void theContextShouldBeSettableAndRetrievableAsASessionAttribute() {
-        // redo instance below because previous constructor happened too early to capture behavior
-        instance = realFactory.createIfNotExists(mockSession, mockAuth, address);
         // when
-        CallHomeSessionContext.getFrom(mockSession);
+        instance = realFactory.createIfNotExists(mockSession, mockAuth);
         // then
+        assertNotNull(instance);
         verify(mockSession, times(1)).setAttribute(CallHomeSessionContext.SESSION_KEY, instance);
+        verify(mockSession, times(0)).getAttribute(any());
+
+        // when
+        CallHomeSessionContext.getFrom(mockSession);
+        // then
         verify(mockSession, times(1)).getAttribute(CallHomeSessionContext.SESSION_KEY);
     }
 
     @Test
     public void anAuthorizeActionShouldApplyToTheBoundSession() throws IOException {
-        instance = realFactory.createIfNotExists(mockSession, mockAuth, address);
+        instance = realFactory.createIfNotExists(mockSession, mockAuth);
         // when
-        Mockito.doReturn(null).when(mockSession).auth();
+        doReturn(null).when(mockSession).auth();
         instance.authorize();
         // then
         verify(mockAuth, times(1)).applyTo(mockSession);
@@ -122,34 +126,18 @@ public class CallHomeSessionContextTest {
         // given
         OpenFuture mockFuture = mock(OpenFuture.class);
         ChannelSubsystem mockChannelSubsystem = mock(ChannelSubsystem.class);
-        Mockito.doReturn(mockFuture).when(mockChannelSubsystem).open();
-        Mockito.doReturn(mockChannelSubsystem).when(mockSession).createSubsystemChannel(anyString());
+        doReturn(mockFuture).when(mockChannelSubsystem).open();
+        doReturn(mockChannelSubsystem).when(mockSession).createSubsystemChannel(anyString());
 
-        Mockito.doReturn(null).when(mockFuture).addListener(any(SshFutureListener.class));
-        doNothing().when(mockChannelSubsystem).setStreaming(any(Streaming.class));
-        instance = realFactory.createIfNotExists(mockSession, mockAuth, address);
+        doReturn(null).when(mockFuture).addListener(any(SshFutureListener.class));
+        doNothing().when(mockChannelSubsystem).setStreaming(any(StreamingChannel.Streaming.class));
+        instance = realFactory.createIfNotExists(mockSession, mockAuth);
         // when
         instance.openNetconfChannel();
         // then
         verify(mockFuture, times(1)).addListener(any(SshFutureListener.class));
     }
 
-    static class TestableContext extends CallHomeSessionContext {
-        MinaSshNettyChannel minaMock;
-
-        TestableContext(final ClientSession sshSession, final CallHomeAuthorization authorization,
-                        final InetSocketAddress address, final CallHomeSessionContext.Factory factory,
-                        final MinaSshNettyChannel minaMock) {
-            super(sshSession, authorization, address, factory);
-            this.minaMock = minaMock;
-        }
-
-        @Override
-        protected MinaSshNettyChannel newMinaSshNettyChannel(final ClientChannel netconfChannel) {
-            return minaMock;
-        }
-    }
-
     @Test
     public void openingTheChannelSuccessfullyNotifyTheChannelListener() {
         // given
@@ -161,20 +149,21 @@ public class CallHomeSessionContextTest {
                 any(CallHomeChannelActivator.class));
 
         ChannelFuture mockChanFuture = mock(ChannelFuture.class);
-        Mockito.doReturn(mockChanFuture).when(mockNettyGroup).register(any(Channel.class));
+        doReturn(mockChanFuture).when(mockNettyGroup).register(any(Channel.class));
 
-        Mockito.doReturn(mockNettyGroup).when(mockFactory).getNettyGroup();
-        Mockito.doReturn(mockChannelInitializer).when(mockFactory)
+        doReturn(mockNettyGroup).when(mockFactory).getNettyGroup();
+        doReturn(mockChannelInitializer).when(mockFactory)
                 .getChannelInitializer(any(NetconfClientSessionListener.class));
-        Mockito.doReturn(mockListener).when(mockFactory).getChannelOpenListener();
+        doReturn(mockListener).when(mockFactory).getChannelOpenListener();
 
         ChannelPipeline mockPipeline = mock(ChannelPipeline.class);
-        Mockito.doReturn(mockPipeline).when(mockMinaChannel).pipeline();
+        doReturn(mockPipeline).when(mockMinaChannel).pipeline();
 
         OpenFuture mockFuture = mock(OpenFuture.class);
-        Mockito.doReturn(true).when(mockFuture).isOpened();
+        doReturn(true).when(mockFuture).isOpened();
 
-        instance = new TestableContext(mockSession, mockAuth, address, mockFactory, mockMinaChannel);
+        instance = spy(new CallHomeSessionContext(mockSession, mockAuth, mockFactory));
+        doReturn(mockMinaChannel).when(instance).newMinaSshNettyChannel(any());
         SshFutureListener<OpenFuture> listener = instance.newSshFutureListener(mockChannel);
         // when
         listener.operationComplete(mockFuture);
@@ -186,11 +175,10 @@ public class CallHomeSessionContextTest {
     @Test
     public void failureToOpenTheChannelShouldCauseTheSessionToClose() {
         // given
-        instance = realFactory.createIfNotExists(mockSession, mockAuth, address);
-
+        instance = realFactory.createIfNotExists(mockSession, mockAuth);
         OpenFuture mockFuture = mock(OpenFuture.class);
-        Mockito.doReturn(false).when(mockFuture).isOpened();
-        Mockito.doReturn(new RuntimeException("test")).when(mockFuture).getException();
+        doReturn(false).when(mockFuture).isOpened();
+        doReturn(new RuntimeException("test")).when(mockFuture).getException();
 
         doReturn(null).when(mockSession).close(anyBoolean());
 
@@ -201,4 +189,11 @@ public class CallHomeSessionContextTest {
         // You'll see an error message logged to the console - it is expected.
         verify(mockSession, times(1)).close(anyBoolean());
     }
+
+    @Test
+    public void theContextConstructorShouldNotModifySession() {
+        instance = new CallHomeSessionContext(mockSession, mockAuth, realFactory);
+        verify(mockSession, times(0)).setAttribute(any(), any());
+        assertNull(CallHomeSessionContext.getFrom(mockSession));
+    }
 }