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