dfbb972e8c8f90b5e6f3285fea974c6924ff4640
[controller.git] / opendaylight / netconf / netconf-client / src / test / java / org / opendaylight / controller / 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.controller.netconf.client;
10
11 import io.netty.channel.Channel;
12 import io.netty.channel.ChannelFuture;
13 import io.netty.channel.ChannelPromise;
14 import io.netty.channel.EventLoopGroup;
15 import io.netty.util.HashedWheelTimer;
16 import io.netty.util.Timer;
17 import io.netty.util.concurrent.GenericFutureListener;
18 import org.junit.Test;
19 import org.mockito.Mockito;
20 import org.opendaylight.controller.netconf.client.NetconfClientDispatcherImpl;
21 import org.opendaylight.controller.netconf.client.NetconfClientSessionListener;
22 import org.opendaylight.controller.netconf.client.SimpleNetconfClientSessionListener;
23 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfiguration;
24 import org.opendaylight.controller.netconf.client.conf.NetconfReconnectingClientConfiguration;
25 import org.opendaylight.controller.netconf.client.conf.NetconfReconnectingClientConfigurationBuilder;
26 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
27 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
28 import org.opendaylight.protocol.framework.ReconnectStrategy;
29 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
30
31 import java.net.InetSocketAddress;
32 import java.util.concurrent.Future;
33
34 import static org.junit.Assert.assertNotNull;
35 import static org.mockito.Matchers.any;
36 import static org.mockito.Mockito.doReturn;
37
38 public class NetconfClientDispatcherImplTest {
39     @Test
40     public void testNetconfClientDispatcherImpl() throws Exception {
41         EventLoopGroup bossGroup = Mockito.mock(EventLoopGroup.class);
42         EventLoopGroup workerGroup = Mockito.mock(EventLoopGroup.class);
43         Timer timer = new HashedWheelTimer();
44
45         ChannelFuture chf = Mockito.mock(ChannelFuture.class);
46         Channel ch = Mockito.mock(Channel.class);
47         doReturn(ch).when(chf).channel();
48         Throwable thr = Mockito.mock(Throwable.class);
49         doReturn(chf).when(workerGroup).register(any(Channel.class));
50
51         ChannelPromise promise = Mockito.mock(ChannelPromise.class);
52         doReturn(promise).when(chf).addListener(any(GenericFutureListener.class));
53         doReturn(thr).when(chf).cause();
54
55         Long timeout = 200L;
56         NetconfHelloMessageAdditionalHeader header = 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         Future<Void> sshReconn = dispatcher.createReconnectingClient(cfg);
94         Future<Void> tcpReconn = dispatcher.createReconnectingClient(cfg2);
95
96         assertNotNull(sshSession);
97         assertNotNull(tcpSession);
98         assertNotNull(sshReconn);
99         assertNotNull(tcpReconn);
100
101     }
102 }