Bump odlparent to 5.0.0
[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.ArgumentMatchers.any;
12 import static org.mockito.ArgumentMatchers.anyBoolean;
13 import static org.mockito.ArgumentMatchers.anyString;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
19
20 import io.netty.channel.Channel;
21 import io.netty.channel.ChannelFuture;
22 import io.netty.channel.ChannelPipeline;
23 import io.netty.channel.EventLoopGroup;
24 import java.io.IOException;
25 import java.net.InetSocketAddress;
26 import java.security.PublicKey;
27 import org.apache.sshd.client.channel.ChannelSubsystem;
28 import org.apache.sshd.client.channel.ClientChannel;
29 import org.apache.sshd.client.channel.ClientChannel.Streaming;
30 import org.apache.sshd.client.future.OpenFuture;
31 import org.apache.sshd.client.session.ClientSession;
32 import org.apache.sshd.client.session.ClientSessionImpl;
33 import org.apache.sshd.common.AttributeRepository.AttributeKey;
34 import org.apache.sshd.common.future.SshFutureListener;
35 import org.apache.sshd.common.io.IoInputStream;
36 import org.apache.sshd.common.io.IoOutputStream;
37 import org.apache.sshd.common.io.IoReadFuture;
38 import org.apache.sshd.common.io.IoSession;
39 import org.apache.sshd.common.kex.KeyExchange;
40 import org.apache.sshd.common.util.buffer.Buffer;
41 import org.junit.Before;
42 import org.junit.Ignore;
43 import org.junit.Test;
44 import org.mockito.Mockito;
45 import org.opendaylight.netconf.client.NetconfClientSessionListener;
46 import org.opendaylight.netconf.client.NetconfClientSessionNegotiatorFactory;
47
48 public class CallHomeSessionContextTest {
49     private ClientSessionImpl mockSession;
50     private CallHomeAuthorization mockAuth;
51     private ClientChannel mockChannel;
52     private InetSocketAddress address;
53
54     private ReverseSshChannelInitializer mockChannelInitializer;
55     private CallHomeNetconfSubsystemListener subListener;
56     private EventLoopGroup mockNettyGroup;
57     private CallHomeSessionContext.Factory realFactory;
58     private CallHomeSessionContext instance;
59     private NetconfClientSessionNegotiatorFactory mockNegotiatior;
60
61     @Before
62     public void setup() {
63         mockSession = mock(ClientSessionImpl.class);
64         mockAuth = mock(CallHomeAuthorization.class);
65         mockChannel = mock(ClientChannel.class);
66         address = mock(InetSocketAddress.class);
67
68         mockNegotiatior = mock(NetconfClientSessionNegotiatorFactory.class);
69         subListener = mock(CallHomeNetconfSubsystemListener.class);
70         mockNettyGroup = mock(EventLoopGroup.class);
71
72         realFactory = new CallHomeSessionContext.Factory(mockNettyGroup, mockNegotiatior, subListener);
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         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 mockChannelSubsystem = mock(ChannelSubsystem.class);
126         Mockito.doReturn(mockFuture).when(mockChannelSubsystem).open();
127         Mockito.doReturn(mockChannelSubsystem).when(mockSession).createSubsystemChannel(anyString());
128
129         Mockito.doReturn(null).when(mockFuture).addListener(any(SshFutureListener.class));
130         doNothing().when(mockChannelSubsystem).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         TestableContext(final ClientSession sshSession, final CallHomeAuthorization authorization,
142                         final InetSocketAddress address, final CallHomeSessionContext.Factory factory,
143                         final MinaSshNettyChannel minaMock) {
144             super(sshSession, authorization, address, factory);
145             this.minaMock = minaMock;
146         }
147
148         @Override
149         protected MinaSshNettyChannel newMinaSshNettyChannel(final ClientChannel netconfChannel) {
150             return minaMock;
151         }
152     }
153
154     @Test
155     public void openingTheChannelSuccessfullyNotifyTheChannelListener() {
156         // given
157         MinaSshNettyChannel mockMinaChannel = mock(MinaSshNettyChannel.class);
158         CallHomeSessionContext.Factory mockFactory = mock(CallHomeSessionContext.Factory.class);
159
160         CallHomeNetconfSubsystemListener mockListener = mock(CallHomeNetconfSubsystemListener.class);
161         doNothing().when(mockListener).onNetconfSubsystemOpened(any(CallHomeProtocolSessionContext.class),
162                 any(CallHomeChannelActivator.class));
163
164         ChannelFuture mockChanFuture = mock(ChannelFuture.class);
165         Mockito.doReturn(mockChanFuture).when(mockNettyGroup).register(any(Channel.class));
166
167         Mockito.doReturn(mockNettyGroup).when(mockFactory).getNettyGroup();
168         Mockito.doReturn(mockChannelInitializer).when(mockFactory)
169                 .getChannelInitializer(any(NetconfClientSessionListener.class));
170         Mockito.doReturn(mockListener).when(mockFactory).getChannelOpenListener();
171
172         ChannelPipeline mockPipeline = mock(ChannelPipeline.class);
173         Mockito.doReturn(mockPipeline).when(mockMinaChannel).pipeline();
174
175         OpenFuture mockFuture = mock(OpenFuture.class);
176         Mockito.doReturn(true).when(mockFuture).isOpened();
177
178         instance = new TestableContext(mockSession, mockAuth, address, mockFactory, mockMinaChannel);
179         SshFutureListener<OpenFuture> listener = instance.newSshFutureListener(mockChannel);
180         // when
181         listener.operationComplete(mockFuture);
182         // then
183         verify(mockListener, times(1)).onNetconfSubsystemOpened(any(CallHomeProtocolSessionContext.class),
184                 any(CallHomeChannelActivator.class));
185     }
186
187     @Test
188     @Ignore
189     public void failureToOpenTheChannelShouldCauseTheSessionToClose() {
190         // given
191         instance = realFactory.createIfNotExists(mockSession, mockAuth, address);
192
193         OpenFuture mockFuture = mock(OpenFuture.class);
194         Mockito.doReturn(false).when(mockFuture).isOpened();
195         Mockito.doReturn(new RuntimeException("test")).when(mockFuture).getException();
196
197         doReturn(null).when(mockSession).close(anyBoolean());
198
199         // when
200         SshFutureListener<OpenFuture> listener = instance.newSshFutureListener(mockChannel);
201         listener.operationComplete(mockFuture);
202         // then
203         // You'll see an error message logged to the console - it is expected.
204         verify(mockSession, times(1)).close(anyBoolean());
205     }
206 }