BUG-1422 Introduce Call-Home functionality for the NETCONF Topology.
[netconf.git] / netconf / callhome-protocol / src / test / java / org / opendaylight / netconf / callhome / protocol / CallHomeSessionContextTest.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.mockito.Matchers.any;
12 import static org.mockito.Matchers.anyBoolean;
13 import static org.mockito.Matchers.anyString;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17
18 import io.netty.channel.Channel;
19 import io.netty.channel.ChannelFuture;
20 import io.netty.channel.ChannelPipeline;
21 import io.netty.channel.EventLoopGroup;
22 import java.io.IOException;
23 import java.net.InetSocketAddress;
24 import java.security.PublicKey;
25 import org.apache.sshd.ClientChannel;
26 import org.apache.sshd.ClientChannel.Streaming;
27 import org.apache.sshd.ClientSession;
28 import org.apache.sshd.client.channel.ChannelSubsystem;
29 import org.apache.sshd.client.future.OpenFuture;
30 import org.apache.sshd.client.session.ClientSessionImpl;
31 import org.apache.sshd.common.KeyExchange;
32 import org.apache.sshd.common.Session.AttributeKey;
33 import org.apache.sshd.common.future.SshFutureListener;
34 import org.apache.sshd.common.io.IoInputStream;
35 import org.apache.sshd.common.io.IoOutputStream;
36 import org.apache.sshd.common.io.IoReadFuture;
37 import org.apache.sshd.common.io.IoSession;
38 import org.apache.sshd.common.util.Buffer;
39 import org.junit.Before;
40 import org.junit.Ignore;
41 import org.junit.Test;
42 import org.mockito.Mockito;
43 import org.opendaylight.netconf.client.NetconfClientSessionListener;
44 import org.opendaylight.netconf.client.NetconfClientSessionNegotiatorFactory;
45
46 public class CallHomeSessionContextTest {
47     private ClientSessionImpl mockSession;
48     private CallHomeAuthorization mockAuth;
49     private ClientChannel mockChannel;
50     private InetSocketAddress address;
51
52     private ReverseSshChannelInitializer mockChannelInitializer;
53     private CallHomeNetconfSubsystemListener subListener;
54     private EventLoopGroup mockNettyGroup;
55     private CallHomeSessionContext.Factory realFactory;
56     private CallHomeSessionContext instance;
57     private NetconfClientSessionNegotiatorFactory mockNegotiatior;
58
59     @Before
60     public void setup() {
61         mockSession = mock(ClientSessionImpl.class);
62         mockAuth = mock(CallHomeAuthorization.class);
63         mockChannel = mock(ClientChannel.class);
64         address = mock(InetSocketAddress.class);
65
66         mockNegotiatior = mock(NetconfClientSessionNegotiatorFactory.class);
67         subListener = mock(CallHomeNetconfSubsystemListener.class);
68         mockNettyGroup = mock(EventLoopGroup.class);
69
70         realFactory = new CallHomeSessionContext.Factory(mockNettyGroup, mockNegotiatior, subListener);
71
72
73
74         KeyExchange kexMock = Mockito.mock(KeyExchange.class);
75         Mockito.doReturn(kexMock).when(mockSession).getKex();
76
77         PublicKey keyMock = Mockito.mock(PublicKey.class);
78         Mockito.doReturn(keyMock).when(kexMock).getServerKey();
79         IoReadFuture mockFuture = mock(IoReadFuture.class);
80         IoInputStream mockIn = mock(IoInputStream.class);
81         Mockito.doReturn(mockFuture).when(mockIn).read(any(Buffer.class));
82         IoOutputStream mockOut = mock(IoOutputStream.class);
83
84         Mockito.doReturn(mockIn).when(mockChannel).getAsyncOut();
85         Mockito.doReturn(mockOut).when(mockChannel).getAsyncIn();
86
87         Mockito.doReturn(true).when(mockAuth).isServerAllowed();
88
89         IoSession ioSession = mock(IoSession.class);
90         Mockito.doReturn(ioSession).when(mockSession).getIoSession();
91         Mockito.doReturn(address).when(ioSession).getRemoteAddress();
92         Mockito.doReturn(null).when(mockSession).setAttribute(any(AttributeKey.class), any());
93         Mockito.doReturn(null).when(mockSession).getAttribute(any(AttributeKey.class));
94         Mockito.doReturn("testSession").when(mockSession).toString();
95
96         Mockito.doNothing().when(mockAuth).applyTo(mockSession);
97         Mockito.doReturn("test").when(mockAuth).getSessionName();
98     }
99
100     @Test
101     public void theContextShouldBeSettableAndRetrievableAsASessionAttribute() {
102         // redo instance below because previous constructor happened too early to capture behavior
103         instance = realFactory.createIfNotExists(mockSession, mockAuth, address);
104         // when
105         CallHomeSessionContext.getFrom(mockSession);
106         // then
107         verify(mockSession, times(1)).setAttribute(CallHomeSessionContext.SESSION_KEY, instance);
108         verify(mockSession, times(1)).getAttribute(CallHomeSessionContext.SESSION_KEY);
109     }
110
111     @Test
112     public void anAuthorizeActionShouldApplyToTheBoundSession() throws IOException {
113         instance = realFactory.createIfNotExists(mockSession, mockAuth, address);
114         // when
115         Mockito.doReturn(null).when(mockSession).auth();
116         instance.authorize();
117         // then
118         verify(mockAuth, times(1)).applyTo(mockSession);
119     }
120
121     @Test
122     public void creatingAChannelSuccessfullyShouldResultInAnAttachedListener() throws IOException {
123         // given
124         OpenFuture mockFuture = mock(OpenFuture.class);
125         ChannelSubsystem mockChannel = mock(ChannelSubsystem.class);
126         Mockito.doReturn(mockFuture).when(mockChannel).open();
127         Mockito.doReturn(mockChannel).when(mockSession).createSubsystemChannel(anyString());
128
129         Mockito.doReturn(null).when(mockFuture).addListener(any(SshFutureListener.class));
130         Mockito.doNothing().when(mockChannel).setStreaming(any(Streaming.class));
131         instance = realFactory.createIfNotExists(mockSession, mockAuth, address);
132         // when
133         instance.openNetconfChannel();
134         // then
135         verify(mockFuture, times(1)).addListener(any(SshFutureListener.class));
136     }
137
138     static class TestableContext extends CallHomeSessionContext {
139         MinaSshNettyChannel minaMock;
140
141         public TestableContext(ClientSession sshSession, CallHomeAuthorization authorization, InetSocketAddress address,
142                 CallHomeSessionContext.Factory factory, MinaSshNettyChannel minaMock) {
143             super(sshSession, authorization, address, factory);
144             this.minaMock = minaMock;
145         }
146
147         @Override
148         protected MinaSshNettyChannel newMinaSshNettyChannel(ClientChannel netconfChannel) {
149             return minaMock;
150         }
151     }
152
153     @Ignore
154     @Test
155     public void openingTheChannelSuccessfullyShouldFireActiveChannel() {
156         // given
157         MinaSshNettyChannel mockMinaChannel = mock(MinaSshNettyChannel.class);
158         CallHomeSessionContext.Factory mockFactory = mock(CallHomeSessionContext.Factory.class);
159
160         ChannelFuture mockChanFuture = mock(ChannelFuture.class);
161         Mockito.doReturn(mockChanFuture).when(mockNettyGroup).register(any(Channel.class));
162
163         Mockito.doReturn(mockNettyGroup).when(mockFactory).getNettyGroup();
164         Mockito.doReturn(mockChannelInitializer).when(mockFactory)
165                 .getChannelInitializer(any(NetconfClientSessionListener.class));
166
167         ChannelPipeline mockPipeline = mock(ChannelPipeline.class);
168         Mockito.doReturn(mockPipeline).when(mockMinaChannel).pipeline();
169
170         OpenFuture mockFuture = mock(OpenFuture.class);
171         Mockito.doReturn(true).when(mockFuture).isOpened();
172
173         instance = new TestableContext(mockSession, mockAuth, address, mockFactory, mockMinaChannel);
174         SshFutureListener<OpenFuture> listener = instance.newSshFutureListener(mockChannel);
175         // when
176         listener.operationComplete(mockFuture);
177         // then
178         verify(mockPipeline, times(1)).fireChannelActive();
179     }
180
181     @Test
182     @Ignore
183     public void failureToOpenTheChannelShouldCauseTheSessionToClose() {
184         // given
185         SshFutureListener<OpenFuture> listener = instance.newSshFutureListener(mockChannel);
186         OpenFuture mockFuture = mock(OpenFuture.class);
187         Mockito.doReturn(false).when(mockFuture).isOpened();
188         Mockito.doReturn(new RuntimeException("test")).when(mockFuture).getException();
189         // when
190         listener.operationComplete(mockFuture);
191         // then
192         verify(mockSession, times(1)).close(anyBoolean());
193     }
194 }