2 * Copyright (c) 2016 Brocade Communication Systems and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.netconf.callhome.protocol;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
17 import io.netty.buffer.ByteBuf;
18 import io.netty.buffer.ByteBufAllocator;
19 import io.netty.buffer.EmptyByteBuf;
20 import io.netty.channel.ChannelHandler;
21 import io.netty.channel.ChannelHandlerContext;
22 import io.netty.channel.ChannelOutboundHandlerAdapter;
23 import io.netty.channel.ChannelPromise;
24 import org.apache.sshd.ClientChannel;
25 import org.apache.sshd.ClientSession;
26 import org.apache.sshd.common.io.IoInputStream;
27 import org.apache.sshd.common.io.IoOutputStream;
28 import org.apache.sshd.common.io.IoReadFuture;
29 import org.apache.sshd.common.io.IoWriteFuture;
30 import org.apache.sshd.common.util.Buffer;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.Mockito;
36 public class MinaSshNettyChannelTest {
37 private CallHomeSessionContext mockContext;
38 private ClientSession mockSession;
39 private ClientChannel mockChannel;
40 private MinaSshNettyChannel instance;
44 IoReadFuture mockFuture = mock(IoReadFuture.class);
45 IoInputStream mockIn = mock(IoInputStream.class);
46 Mockito.doReturn(mockFuture).when(mockIn).read(any(Buffer.class));
47 IoOutputStream mockOut = mock(IoOutputStream.class);
48 mockContext = mock(CallHomeSessionContext.class);
49 mockSession = mock(ClientSession.class);
50 mockChannel = mock(ClientChannel.class);
51 Mockito.doReturn(mockIn).when(mockChannel).getAsyncOut();
52 Mockito.doReturn(mockOut).when(mockChannel).getAsyncIn();
54 IoWriteFuture mockWrFuture = mock(IoWriteFuture.class);
55 Mockito.doReturn(false).when(mockOut).isClosed();
56 Mockito.doReturn(false).when(mockOut).isClosing();
57 Mockito.doReturn(mockWrFuture).when(mockOut).write(any(Buffer.class));
58 Mockito.doReturn(null).when(mockWrFuture).addListener(any());
60 Mockito.doReturn(mockFuture).when(mockFuture).addListener(Mockito.any());
62 instance = new MinaSshNettyChannel(mockContext, mockSession, mockChannel);
66 public void ourChannelHandlerShouldBeFirstInThePipeline() {
68 ChannelHandler firstHandler = instance.pipeline().first();
69 String firstName = firstHandler.getClass().getName();
71 assertTrue(firstName.contains("callhome"));
75 public void ourChannelHandlerShouldForwardWrites() throws Exception {
76 ChannelHandler mockHandler = mock(ChannelHandler.class);
77 ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
78 Mockito.doReturn(mockHandler).when(ctx).handler();
79 ChannelPromise promise = mock(ChannelPromise.class);
81 ByteBufAllocator mockAlloc = mock(ByteBufAllocator.class);
82 ByteBuf bytes = new EmptyByteBuf(mockAlloc);
84 // we would really like to just verify that the async handler write() was
85 // called but it is a final class, so no mocking. instead we set up the
86 // mock channel to have no async input, which will cause a failure later
87 // on the write promise that we use as a cheap way to tell that write()
90 Mockito.doReturn(null).when(mockChannel).getAsyncIn();
91 Mockito.doReturn(null).when(promise).setFailure(any(Throwable.class));
93 // Need to reconstruct instance to pick up null async in above
94 instance = new MinaSshNettyChannel(mockContext, mockSession, mockChannel);
97 ChannelOutboundHandlerAdapter outadapter = (ChannelOutboundHandlerAdapter) instance.pipeline().first();
98 outadapter.write(ctx, bytes, promise);
101 verify(promise, times(1)).setFailure(any(Throwable.class));