Integrate MRI projects for Neon
[netconf.git] / netconf / netconf-client / src / test / java / org / opendaylight / netconf / client / NetconfClientDispatcherImplTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. 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.client;
10
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.doReturn;
14
15 import io.netty.channel.Channel;
16 import io.netty.channel.ChannelFuture;
17 import io.netty.channel.ChannelPromise;
18 import io.netty.channel.EventLoopGroup;
19 import io.netty.util.HashedWheelTimer;
20 import io.netty.util.Timer;
21 import io.netty.util.concurrent.GenericFutureListener;
22 import java.net.InetSocketAddress;
23 import java.util.concurrent.Future;
24 import org.junit.Test;
25 import org.mockito.Mockito;
26 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
27 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration;
28 import org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration;
29 import org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfigurationBuilder;
30 import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
31 import org.opendaylight.protocol.framework.ReconnectStrategy;
32 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
33
34 public class NetconfClientDispatcherImplTest {
35     @Test
36     public void testNetconfClientDispatcherImpl() throws Exception {
37         EventLoopGroup bossGroup = Mockito.mock(EventLoopGroup.class);
38         EventLoopGroup workerGroup = Mockito.mock(EventLoopGroup.class);
39         Timer timer = new HashedWheelTimer();
40
41         ChannelFuture chf = Mockito.mock(ChannelFuture.class);
42         Channel ch = Mockito.mock(Channel.class);
43         doReturn(ch).when(chf).channel();
44         Throwable thr = Mockito.mock(Throwable.class);
45         doReturn(chf).when(workerGroup).register(any(Channel.class));
46
47         ChannelPromise promise = Mockito.mock(ChannelPromise.class);
48         doReturn(promise).when(chf).addListener(any(GenericFutureListener.class));
49         doReturn(thr).when(chf).cause();
50         doReturn(true).when(chf).isDone();
51         doReturn(false).when(chf).isSuccess();
52
53         Long timeout = 200L;
54         NetconfHelloMessageAdditionalHeader header =
55                 new NetconfHelloMessageAdditionalHeader("a", "host", "port", "trans", "id");
56         NetconfClientSessionListener listener = new SimpleNetconfClientSessionListener();
57         InetSocketAddress address = InetSocketAddress.createUnresolved("host", 830);
58         ReconnectStrategyFactory reconnectStrategyFactory = Mockito.mock(ReconnectStrategyFactory.class);
59         AuthenticationHandler handler = Mockito.mock(AuthenticationHandler.class);
60         ReconnectStrategy reconnect = Mockito.mock(ReconnectStrategy.class);
61
62         doReturn(5).when(reconnect).getConnectTimeout();
63         doReturn("").when(reconnect).toString();
64         doReturn("").when(handler).toString();
65         doReturn("").when(reconnectStrategyFactory).toString();
66         doReturn(reconnect).when(reconnectStrategyFactory).createReconnectStrategy();
67
68         NetconfReconnectingClientConfiguration cfg = NetconfReconnectingClientConfigurationBuilder.create()
69                 .withProtocol(NetconfClientConfiguration.NetconfClientProtocol.SSH)
70                 .withAddress(address)
71                 .withConnectionTimeoutMillis(timeout)
72                 .withReconnectStrategy(reconnect)
73                 .withAdditionalHeader(header)
74                 .withSessionListener(listener)
75                 .withConnectStrategyFactory(reconnectStrategyFactory)
76                 .withAuthHandler(handler).build();
77
78         NetconfReconnectingClientConfiguration cfg2 = NetconfReconnectingClientConfigurationBuilder.create()
79                 .withProtocol(NetconfClientConfiguration.NetconfClientProtocol.TCP)
80                 .withAddress(address)
81                 .withConnectionTimeoutMillis(timeout)
82                 .withReconnectStrategy(reconnect)
83                 .withAdditionalHeader(header)
84                 .withSessionListener(listener)
85                 .withConnectStrategyFactory(reconnectStrategyFactory)
86                 .withAuthHandler(handler).build();
87
88         NetconfClientDispatcherImpl dispatcher = new NetconfClientDispatcherImpl(bossGroup, workerGroup, timer);
89         Future<NetconfClientSession> sshSession = dispatcher.createClient(cfg);
90         Future<NetconfClientSession> tcpSession = dispatcher.createClient(cfg2);
91
92         Future<Void> sshReconn = dispatcher.createReconnectingClient(cfg);
93         final Future<Void> tcpReconn = dispatcher.createReconnectingClient(cfg2);
94
95         assertNotNull(sshSession);
96         assertNotNull(tcpSession);
97         assertNotNull(sshReconn);
98         assertNotNull(tcpReconn);
99
100         SslHandlerFactory sslHandlerFactory = Mockito.mock(SslHandlerFactory.class);
101         NetconfReconnectingClientConfiguration cfg3 = NetconfReconnectingClientConfigurationBuilder.create()
102                 .withProtocol(NetconfClientConfiguration.NetconfClientProtocol.TLS)
103                 .withAddress(address)
104                 .withConnectionTimeoutMillis(timeout)
105                 .withReconnectStrategy(reconnect)
106                 .withAdditionalHeader(header)
107                 .withSessionListener(listener)
108                 .withConnectStrategyFactory(reconnectStrategyFactory)
109                 .withSslHandlerFactory(sslHandlerFactory).build();
110
111         Future<NetconfClientSession> tlsSession = dispatcher.createClient(cfg3);
112         Future<Void> tlsReconn = dispatcher.createReconnectingClient(cfg3);
113
114         assertNotNull(tlsSession);
115         assertNotNull(tlsReconn);
116     }
117 }