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