Bump odlparent to 4.0.10
[netconf.git] / netconf / callhome-protocol / src / test / java / org / opendaylight / netconf / callhome / protocol / CallHomeSessionContextTest.java
index 3f2f468fc5c618b2d5f520991664d8c862f6e0ae..fc70a06b363d3d83e51b5bb4ccb1a97da6f01457 100644 (file)
@@ -5,12 +5,13 @@
  * 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.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.times;
 import static org.mockito.Mockito.verify;
@@ -22,20 +23,20 @@ import io.netty.channel.EventLoopGroup;
 import java.io.IOException;
 import java.net.InetSocketAddress;
 import java.security.PublicKey;
-import org.apache.sshd.ClientChannel;
-import org.apache.sshd.ClientChannel.Streaming;
-import org.apache.sshd.ClientSession;
 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.KeyExchange;
-import org.apache.sshd.common.Session.AttributeKey;
+import org.apache.sshd.common.AttributeRepository.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.util.Buffer;
+import org.apache.sshd.common.kex.KeyExchange;
+import org.apache.sshd.common.util.buffer.Buffer;
 import org.junit.Before;
 import org.junit.Ignore;
 import org.junit.Test;
@@ -69,8 +70,6 @@ public class CallHomeSessionContextTest {
 
         realFactory = new CallHomeSessionContext.Factory(mockNettyGroup, mockNegotiatior, subListener);
 
-
-
         KeyExchange kexMock = Mockito.mock(KeyExchange.class);
         Mockito.doReturn(kexMock).when(mockSession).getKex();
 
@@ -93,7 +92,7 @@ public class CallHomeSessionContextTest {
         Mockito.doReturn(null).when(mockSession).getAttribute(any(AttributeKey.class));
         Mockito.doReturn("testSession").when(mockSession).toString();
 
-        Mockito.doNothing().when(mockAuth).applyTo(mockSession);
+        doNothing().when(mockAuth).applyTo(mockSession);
         Mockito.doReturn("test").when(mockAuth).getSessionName();
     }
 
@@ -122,12 +121,12 @@ public class CallHomeSessionContextTest {
     public void creatingAChannelSuccessfullyShouldResultInAnAttachedListener() throws IOException {
         // given
         OpenFuture mockFuture = mock(OpenFuture.class);
-        ChannelSubsystem mockChannel = mock(ChannelSubsystem.class);
-        Mockito.doReturn(mockFuture).when(mockChannel).open();
-        Mockito.doReturn(mockChannel).when(mockSession).createSubsystemChannel(anyString());
+        ChannelSubsystem mockChannelSubsystem = mock(ChannelSubsystem.class);
+        Mockito.doReturn(mockFuture).when(mockChannelSubsystem).open();
+        Mockito.doReturn(mockChannelSubsystem).when(mockSession).createSubsystemChannel(anyString());
 
         Mockito.doReturn(null).when(mockFuture).addListener(any(SshFutureListener.class));
-        Mockito.doNothing().when(mockChannel).setStreaming(any(Streaming.class));
+        doNothing().when(mockChannelSubsystem).setStreaming(any(Streaming.class));
         instance = realFactory.createIfNotExists(mockSession, mockAuth, address);
         // when
         instance.openNetconfChannel();
@@ -138,31 +137,36 @@ public class CallHomeSessionContextTest {
     static class TestableContext extends CallHomeSessionContext {
         MinaSshNettyChannel minaMock;
 
-        public TestableContext(ClientSession sshSession, CallHomeAuthorization authorization, InetSocketAddress address,
-                CallHomeSessionContext.Factory factory, 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(ClientChannel netconfChannel) {
+        protected MinaSshNettyChannel newMinaSshNettyChannel(final ClientChannel netconfChannel) {
             return minaMock;
         }
     }
 
-    @Ignore
     @Test
-    public void openingTheChannelSuccessfullyShouldFireActiveChannel() {
+    public void openingTheChannelSuccessfullyNotifyTheChannelListener() {
         // given
         MinaSshNettyChannel mockMinaChannel = mock(MinaSshNettyChannel.class);
         CallHomeSessionContext.Factory mockFactory = mock(CallHomeSessionContext.Factory.class);
 
+        CallHomeNetconfSubsystemListener mockListener = mock(CallHomeNetconfSubsystemListener.class);
+        doNothing().when(mockListener).onNetconfSubsystemOpened(any(CallHomeProtocolSessionContext.class),
+                any(CallHomeChannelActivator.class));
+
         ChannelFuture mockChanFuture = mock(ChannelFuture.class);
         Mockito.doReturn(mockChanFuture).when(mockNettyGroup).register(any(Channel.class));
 
         Mockito.doReturn(mockNettyGroup).when(mockFactory).getNettyGroup();
         Mockito.doReturn(mockChannelInitializer).when(mockFactory)
                 .getChannelInitializer(any(NetconfClientSessionListener.class));
+        Mockito.doReturn(mockListener).when(mockFactory).getChannelOpenListener();
 
         ChannelPipeline mockPipeline = mock(ChannelPipeline.class);
         Mockito.doReturn(mockPipeline).when(mockMinaChannel).pipeline();
@@ -175,20 +179,27 @@ public class CallHomeSessionContextTest {
         // when
         listener.operationComplete(mockFuture);
         // then
-        verify(mockPipeline, times(1)).fireChannelActive();
+        verify(mockListener, times(1)).onNetconfSubsystemOpened(any(CallHomeProtocolSessionContext.class),
+                any(CallHomeChannelActivator.class));
     }
 
     @Test
     @Ignore
     public void failureToOpenTheChannelShouldCauseTheSessionToClose() {
         // given
-        SshFutureListener<OpenFuture> listener = instance.newSshFutureListener(mockChannel);
+        instance = realFactory.createIfNotExists(mockSession, mockAuth, address);
+
         OpenFuture mockFuture = mock(OpenFuture.class);
         Mockito.doReturn(false).when(mockFuture).isOpened();
         Mockito.doReturn(new RuntimeException("test")).when(mockFuture).getException();
+
+        doReturn(null).when(mockSession).close(anyBoolean());
+
         // when
+        SshFutureListener<OpenFuture> listener = instance.newSshFutureListener(mockChannel);
         listener.operationComplete(mockFuture);
         // then
+        // You'll see an error message logged to the console - it is expected.
         verify(mockSession, times(1)).close(anyBoolean());
     }
 }