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