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
8 package org.opendaylight.netconf.callhome.protocol;
10 import static org.junit.Assert.assertTrue;
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.times;
14 import static org.mockito.Mockito.verify;
16 import io.netty.buffer.ByteBuf;
17 import io.netty.buffer.ByteBufAllocator;
18 import io.netty.buffer.EmptyByteBuf;
19 import io.netty.channel.ChannelHandler;
20 import io.netty.channel.ChannelHandlerContext;
21 import io.netty.channel.ChannelOutboundHandlerAdapter;
22 import io.netty.channel.ChannelPromise;
23 import java.io.IOException;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Mockito;
27 import org.opendaylight.netconf.shaded.sshd.client.channel.ClientChannel;
28 import org.opendaylight.netconf.shaded.sshd.client.session.ClientSession;
29 import org.opendaylight.netconf.shaded.sshd.common.io.IoInputStream;
30 import org.opendaylight.netconf.shaded.sshd.common.io.IoOutputStream;
31 import org.opendaylight.netconf.shaded.sshd.common.io.IoReadFuture;
32 import org.opendaylight.netconf.shaded.sshd.common.io.IoWriteFuture;
33 import org.opendaylight.netconf.shaded.sshd.common.util.buffer.Buffer;
35 public class MinaSshNettyChannelTest {
36 private CallHomeSessionContext mockContext;
37 private ClientSession mockSession;
38 private ClientChannel mockChannel;
39 private MinaSshNettyChannel instance;
42 public void setup() throws IOException {
43 IoReadFuture mockFuture = mock(IoReadFuture.class);
44 IoInputStream mockIn = mock(IoInputStream.class);
45 Mockito.doReturn(mockFuture).when(mockIn).read(any(Buffer.class));
46 mockContext = mock(CallHomeSessionContext.class);
47 mockSession = mock(ClientSession.class);
48 mockChannel = mock(ClientChannel.class);
49 Mockito.doReturn(mockIn).when(mockChannel).getAsyncOut();
51 IoOutputStream mockOut = mock(IoOutputStream.class);
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).writePacket(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 // we would really like to just verify that the async handler write() was
82 // called but it is a final class, so no mocking. instead we set up the
83 // mock channel to have no async input, which will cause a failure later
84 // on the write promise that we use as a cheap way to tell that write()
87 Mockito.doReturn(null).when(mockChannel).getAsyncIn();
88 Mockito.doReturn(null).when(promise).setFailure(any(Throwable.class));
90 // Need to reconstruct instance to pick up null async in above
91 instance = new MinaSshNettyChannel(mockContext, mockSession, mockChannel);
94 ChannelOutboundHandlerAdapter outadapter = (ChannelOutboundHandlerAdapter) instance.pipeline().first();
95 ByteBufAllocator mockAlloc = mock(ByteBufAllocator.class);
96 ByteBuf bytes = new EmptyByteBuf(mockAlloc);
97 outadapter.write(ctx, bytes, promise);
100 verify(promise, times(1)).setFailure(any(Throwable.class));