62a2b33eb9c81299c6f289af2ca7d1bb89beebbc
[netconf.git] / protocol / 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.ReconnectFuture;
31 import org.opendaylight.netconf.nettyutil.ReconnectStrategy;
32 import org.opendaylight.netconf.nettyutil.ReconnectStrategyFactory;
33 import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
34
35 public class NetconfClientDispatcherImplTest {
36     @Test
37     public void testNetconfClientDispatcherImpl() throws Exception {
38         EventLoopGroup bossGroup = Mockito.mock(EventLoopGroup.class);
39         EventLoopGroup workerGroup = Mockito.mock(EventLoopGroup.class);
40         Timer timer = new HashedWheelTimer();
41
42         ChannelFuture chf = Mockito.mock(ChannelFuture.class);
43         Channel ch = Mockito.mock(Channel.class);
44         doReturn(ch).when(chf).channel();
45         Throwable thr = Mockito.mock(Throwable.class);
46         doReturn(chf).when(workerGroup).register(any(Channel.class));
47
48         ChannelPromise promise = Mockito.mock(ChannelPromise.class);
49         doReturn(promise).when(chf).addListener(any(GenericFutureListener.class));
50         doReturn(thr).when(chf).cause();
51         doReturn(true).when(chf).isDone();
52         doReturn(false).when(chf).isSuccess();
53
54         Long timeout = 200L;
55         NetconfHelloMessageAdditionalHeader header =
56                 new NetconfHelloMessageAdditionalHeader("a", "host", "port", "trans", "id");
57         NetconfClientSessionListener listener = new SimpleNetconfClientSessionListener();
58         InetSocketAddress address = InetSocketAddress.createUnresolved("host", 830);
59         ReconnectStrategyFactory reconnectStrategyFactory = Mockito.mock(ReconnectStrategyFactory.class);
60         AuthenticationHandler handler = Mockito.mock(AuthenticationHandler.class);
61         ReconnectStrategy reconnect = Mockito.mock(ReconnectStrategy.class);
62
63         doReturn(5).when(reconnect).getConnectTimeout();
64         doReturn("").when(reconnect).toString();
65         doReturn("").when(handler).toString();
66         doReturn("").when(reconnectStrategyFactory).toString();
67         doReturn(reconnect).when(reconnectStrategyFactory).createReconnectStrategy();
68
69         NetconfReconnectingClientConfiguration cfg = NetconfReconnectingClientConfigurationBuilder.create()
70                 .withProtocol(NetconfClientConfiguration.NetconfClientProtocol.SSH)
71                 .withAddress(address)
72                 .withConnectionTimeoutMillis(timeout)
73                 .withReconnectStrategy(reconnect)
74                 .withAdditionalHeader(header)
75                 .withSessionListener(listener)
76                 .withConnectStrategyFactory(reconnectStrategyFactory)
77                 .withAuthHandler(handler).build();
78
79         NetconfReconnectingClientConfiguration cfg2 = NetconfReconnectingClientConfigurationBuilder.create()
80                 .withProtocol(NetconfClientConfiguration.NetconfClientProtocol.TCP)
81                 .withAddress(address)
82                 .withConnectionTimeoutMillis(timeout)
83                 .withReconnectStrategy(reconnect)
84                 .withAdditionalHeader(header)
85                 .withSessionListener(listener)
86                 .withConnectStrategyFactory(reconnectStrategyFactory)
87                 .withAuthHandler(handler).build();
88
89         NetconfClientDispatcherImpl dispatcher = new NetconfClientDispatcherImpl(bossGroup, workerGroup, timer);
90         Future<NetconfClientSession> sshSession = dispatcher.createClient(cfg);
91         Future<NetconfClientSession> tcpSession = dispatcher.createClient(cfg2);
92
93         ReconnectFuture sshReconn = dispatcher.createReconnectingClient(cfg);
94         final ReconnectFuture tcpReconn = dispatcher.createReconnectingClient(cfg2);
95
96         assertNotNull(sshSession);
97         assertNotNull(tcpSession);
98         assertNotNull(sshReconn);
99         assertNotNull(tcpReconn);
100
101         SslHandlerFactory sslHandlerFactory = Mockito.mock(SslHandlerFactory.class);
102         NetconfReconnectingClientConfiguration cfg3 = NetconfReconnectingClientConfigurationBuilder.create()
103                 .withProtocol(NetconfClientConfiguration.NetconfClientProtocol.TLS)
104                 .withAddress(address)
105                 .withConnectionTimeoutMillis(timeout)
106                 .withReconnectStrategy(reconnect)
107                 .withAdditionalHeader(header)
108                 .withSessionListener(listener)
109                 .withConnectStrategyFactory(reconnectStrategyFactory)
110                 .withSslHandlerFactory(sslHandlerFactory).build();
111
112         Future<NetconfClientSession> tlsSession = dispatcher.createClient(cfg3);
113         ReconnectFuture tlsReconn = dispatcher.createReconnectingClient(cfg3);
114
115         assertNotNull(tlsSession);
116         assertNotNull(tlsReconn);
117     }
118 }