2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netconf.client;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Mockito.doReturn;
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;
34 public class NetconfClientDispatcherImplTest {
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();
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));
47 ChannelPromise promise = Mockito.mock(ChannelPromise.class);
48 doReturn(promise).when(chf).addListener(any(GenericFutureListener.class));
49 doReturn(thr).when(chf).cause();
52 NetconfHelloMessageAdditionalHeader header =
53 new NetconfHelloMessageAdditionalHeader("a", "host", "port", "trans", "id");
54 NetconfClientSessionListener listener = new SimpleNetconfClientSessionListener();
55 InetSocketAddress address = InetSocketAddress.createUnresolved("host", 830);
56 ReconnectStrategyFactory reconnectStrategyFactory = Mockito.mock(ReconnectStrategyFactory.class);
57 AuthenticationHandler handler = Mockito.mock(AuthenticationHandler.class);
58 ReconnectStrategy reconnect = Mockito.mock(ReconnectStrategy.class);
60 doReturn(5).when(reconnect).getConnectTimeout();
61 doReturn("").when(reconnect).toString();
62 doReturn("").when(handler).toString();
63 doReturn("").when(reconnectStrategyFactory).toString();
64 doReturn(reconnect).when(reconnectStrategyFactory).createReconnectStrategy();
66 NetconfReconnectingClientConfiguration cfg = NetconfReconnectingClientConfigurationBuilder.create()
67 .withProtocol(NetconfClientConfiguration.NetconfClientProtocol.SSH)
69 .withConnectionTimeoutMillis(timeout)
70 .withReconnectStrategy(reconnect)
71 .withAdditionalHeader(header)
72 .withSessionListener(listener)
73 .withConnectStrategyFactory(reconnectStrategyFactory)
74 .withAuthHandler(handler).build();
76 NetconfReconnectingClientConfiguration cfg2 = NetconfReconnectingClientConfigurationBuilder.create()
77 .withProtocol(NetconfClientConfiguration.NetconfClientProtocol.TCP)
79 .withConnectionTimeoutMillis(timeout)
80 .withReconnectStrategy(reconnect)
81 .withAdditionalHeader(header)
82 .withSessionListener(listener)
83 .withConnectStrategyFactory(reconnectStrategyFactory)
84 .withAuthHandler(handler).build();
86 NetconfClientDispatcherImpl dispatcher = new NetconfClientDispatcherImpl(bossGroup, workerGroup, timer);
87 Future<NetconfClientSession> sshSession = dispatcher.createClient(cfg);
88 Future<NetconfClientSession> tcpSession = dispatcher.createClient(cfg2);
90 Future<Void> sshReconn = dispatcher.createReconnectingClient(cfg);
91 final Future<Void> tcpReconn = dispatcher.createReconnectingClient(cfg2);
93 assertNotNull(sshSession);
94 assertNotNull(tcpSession);
95 assertNotNull(sshReconn);
96 assertNotNull(tcpReconn);
98 SslHandlerFactory sslHandlerFactory = Mockito.mock(SslHandlerFactory.class);
99 NetconfReconnectingClientConfiguration cfg3 = NetconfReconnectingClientConfigurationBuilder.create()
100 .withProtocol(NetconfClientConfiguration.NetconfClientProtocol.TLS)
101 .withAddress(address)
102 .withConnectionTimeoutMillis(timeout)
103 .withReconnectStrategy(reconnect)
104 .withAdditionalHeader(header)
105 .withSessionListener(listener)
106 .withConnectStrategyFactory(reconnectStrategyFactory)
107 .withSslHandlerFactory(sslHandlerFactory).build();
109 Future<NetconfClientSession> tlsSession = dispatcher.createClient(cfg3);
110 Future<Void> tlsReconn = dispatcher.createReconnectingClient(cfg3);
112 assertNotNull(tlsSession);
113 assertNotNull(tlsReconn);