Remove netconf from commons/opendaylight pom
[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 static org.junit.Assert.assertNotNull;
12 import static org.mockito.Matchers.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.controller.netconf.client.conf.NetconfClientConfiguration;
27 import org.opendaylight.controller.netconf.client.conf.NetconfReconnectingClientConfiguration;
28 import org.opendaylight.controller.netconf.client.conf.NetconfReconnectingClientConfigurationBuilder;
29 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
30 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
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
51         Long timeout = 200L;
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);
58
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();
64
65         NetconfReconnectingClientConfiguration cfg = NetconfReconnectingClientConfigurationBuilder.create().
66                 withProtocol(NetconfClientConfiguration.NetconfClientProtocol.SSH).
67                 withAddress(address).
68                 withConnectionTimeoutMillis(timeout).
69                 withReconnectStrategy(reconnect).
70                 withAdditionalHeader(header).
71                 withSessionListener(listener).
72                 withConnectStrategyFactory(reconnectStrategyFactory).
73                 withAuthHandler(handler).build();
74
75         NetconfReconnectingClientConfiguration cfg2 = NetconfReconnectingClientConfigurationBuilder.create().
76                 withProtocol(NetconfClientConfiguration.NetconfClientProtocol.TCP).
77                 withAddress(address).
78                 withConnectionTimeoutMillis(timeout).
79                 withReconnectStrategy(reconnect).
80                 withAdditionalHeader(header).
81                 withSessionListener(listener).
82                 withConnectStrategyFactory(reconnectStrategyFactory).
83                 withAuthHandler(handler).build();
84
85         NetconfClientDispatcherImpl dispatcher = new NetconfClientDispatcherImpl(bossGroup, workerGroup, timer);
86         Future<NetconfClientSession> sshSession = dispatcher.createClient(cfg);
87         Future<NetconfClientSession> tcpSession = dispatcher.createClient(cfg2);
88
89         Future<Void> sshReconn = dispatcher.createReconnectingClient(cfg);
90         Future<Void> tcpReconn = dispatcher.createReconnectingClient(cfg2);
91
92         assertNotNull(sshSession);
93         assertNotNull(tcpSession);
94         assertNotNull(sshReconn);
95         assertNotNull(tcpReconn);
96
97     }
98 }