BUG-1422 Introduce Call-Home functionality for the NETCONF Topology.
[netconf.git] / netconf / callhome-protocol / src / test / java / org / opendaylight / netconf / callhome / protocol / MinaSshNettyChannelTest.java
1 /*
2  * Copyright (c) 2016 Brocade Communication Systems and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.callhome.protocol;
10
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;
16
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.future.SshFutureListener;
27 import org.apache.sshd.common.io.IoInputStream;
28 import org.apache.sshd.common.io.IoOutputStream;
29 import org.apache.sshd.common.io.IoReadFuture;
30 import org.apache.sshd.common.io.IoWriteFuture;
31 import org.apache.sshd.common.util.Buffer;
32 import org.junit.Before;
33 import org.junit.Ignore;
34 import org.junit.Test;
35 import org.mockito.Mockito;
36
37
38 public class MinaSshNettyChannelTest {
39     CallHomeSessionContext mockContext;
40     ClientSession mockSession;
41     ClientChannel mockChannel;
42     MinaSshNettyChannel instance;
43
44     @Before
45     public void setup() {
46         IoReadFuture mockFuture = mock(IoReadFuture.class);
47         IoInputStream mockIn = mock(IoInputStream.class);
48         Mockito.doReturn(mockFuture).when(mockIn).read(any(Buffer.class));
49         IoOutputStream mockOut = mock(IoOutputStream.class);
50         mockContext = mock(CallHomeSessionContext.class);
51         mockSession = mock(ClientSession.class);
52         mockChannel = mock(ClientChannel.class);
53         Mockito.doReturn(mockIn).when(mockChannel).getAsyncOut();
54         Mockito.doReturn(mockOut).when(mockChannel).getAsyncIn();
55
56         IoWriteFuture mockWrFuture = mock(IoWriteFuture.class);
57         Mockito.doReturn(false).when(mockOut).isClosed();
58         Mockito.doReturn(false).when(mockOut).isClosing();
59         Mockito.doReturn(mockWrFuture).when(mockOut).write(any(Buffer.class));
60         Mockito.doReturn(null).when(mockWrFuture).addListener(any());
61
62         Mockito.doReturn(mockFuture).when(mockFuture).addListener(Mockito.any());
63
64         instance = new MinaSshNettyChannel(mockContext, mockSession, mockChannel);
65     }
66
67     @Test
68     public void ourChannelHandlerShouldBeFirstInThePipeline() {
69         // given
70         ChannelHandler firstHandler = instance.pipeline().first();
71         String firstName = firstHandler.getClass().getName();
72         // expect
73         assertTrue(firstName.contains("callhome"));
74     }
75
76     @Test
77     public void ourChannelHandlerShouldForwardWrites() throws Exception {
78         ChannelHandler mockHandler = mock(ChannelHandler.class);
79         ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
80         Mockito.doReturn(mockHandler).when(ctx).handler();
81         ChannelPromise promise = mock(ChannelPromise.class);
82
83         ByteBufAllocator mockAlloc = mock(ByteBufAllocator.class);
84         ByteBuf bytes = new EmptyByteBuf(mockAlloc);
85
86         // we would really like to just verify that the async handler write() was
87         // called but it is a final class, so no mocking. instead we set up the
88         // mock channel to have no async input, which will cause a failure later
89         // on the write promise that we use as a cheap way to tell that write()
90         // got called. ick.
91
92         Mockito.doReturn(null).when(mockChannel).getAsyncIn();
93         Mockito.doReturn(null).when(promise).setFailure(any(Throwable.class));
94
95         // Need to reconstruct instance to pick up null async in above
96         instance = new MinaSshNettyChannel(mockContext, mockSession, mockChannel);
97
98         // when
99         ChannelOutboundHandlerAdapter outadapter = (ChannelOutboundHandlerAdapter) instance.pipeline().first();
100         outadapter.write(ctx, bytes, promise);
101
102         // then
103         verify(promise, times(1)).setFailure(any(Throwable.class));
104     }
105 }