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.nettyutil.handler.ssh.authentication.AuthenticationHandler;
27 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
28 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration;
29 import org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration;
30 import org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfigurationBuilder;
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 = new NetconfHelloMessageAdditionalHeader("a", "host", "port", "trans", "id");
53 NetconfClientSessionListener listener = new SimpleNetconfClientSessionListener();
54 InetSocketAddress address = InetSocketAddress.createUnresolved("host", 830);
55 ReconnectStrategyFactory reconnectStrategyFactory = Mockito.mock(ReconnectStrategyFactory.class);
56 AuthenticationHandler handler = Mockito.mock(AuthenticationHandler.class);
57 ReconnectStrategy reconnect = Mockito.mock(ReconnectStrategy.class);
59 doReturn(5).when(reconnect).getConnectTimeout();
60 doReturn("").when(reconnect).toString();
61 doReturn("").when(handler).toString();
62 doReturn("").when(reconnectStrategyFactory).toString();
63 doReturn(reconnect).when(reconnectStrategyFactory).createReconnectStrategy();
65 NetconfReconnectingClientConfiguration cfg = NetconfReconnectingClientConfigurationBuilder.create().
66 withProtocol(NetconfClientConfiguration.NetconfClientProtocol.SSH).
68 withConnectionTimeoutMillis(timeout).
69 withReconnectStrategy(reconnect).
70 withAdditionalHeader(header).
71 withSessionListener(listener).
72 withConnectStrategyFactory(reconnectStrategyFactory).
73 withAuthHandler(handler).build();
75 NetconfReconnectingClientConfiguration cfg2 = NetconfReconnectingClientConfigurationBuilder.create().
76 withProtocol(NetconfClientConfiguration.NetconfClientProtocol.TCP).
78 withConnectionTimeoutMillis(timeout).
79 withReconnectStrategy(reconnect).
80 withAdditionalHeader(header).
81 withSessionListener(listener).
82 withConnectStrategyFactory(reconnectStrategyFactory).
83 withAuthHandler(handler).build();
85 NetconfClientDispatcherImpl dispatcher = new NetconfClientDispatcherImpl(bossGroup, workerGroup, timer);
86 Future<NetconfClientSession> sshSession = dispatcher.createClient(cfg);
87 Future<NetconfClientSession> tcpSession = dispatcher.createClient(cfg2);
89 Future<Void> sshReconn = dispatcher.createReconnectingClient(cfg);
90 Future<Void> tcpReconn = dispatcher.createReconnectingClient(cfg2);
92 assertNotNull(sshSession);
93 assertNotNull(tcpSession);
94 assertNotNull(sshReconn);
95 assertNotNull(tcpReconn);