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