Merge "BUG 1853 : Clustered Data Store causes Out of Memory"
[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.NetconfClientConfigurationBuilder;
25 import org.opendaylight.controller.netconf.client.conf.NetconfReconnectingClientConfiguration;
26 import org.opendaylight.controller.netconf.client.conf.NetconfReconnectingClientConfigurationBuilder;
27 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
28 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
29 import org.opendaylight.protocol.framework.ReconnectStrategy;
30 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
31
32 import java.net.InetSocketAddress;
33 import java.util.concurrent.Future;
34
35 import static org.junit.Assert.assertEquals;
36 import static org.junit.Assert.assertNotNull;
37 import static org.mockito.Matchers.any;
38 import static org.mockito.Mockito.doReturn;
39
40 public class NetconfClientDispatcherImplTest {
41     @Test
42     public void testNetconfClientDispatcherImpl() throws Exception {
43         EventLoopGroup bossGroup = Mockito.mock(EventLoopGroup.class);
44         EventLoopGroup workerGroup = Mockito.mock(EventLoopGroup.class);
45         Timer timer = new HashedWheelTimer();
46
47         ChannelFuture chf = Mockito.mock(ChannelFuture.class);
48         Channel ch = Mockito.mock(Channel.class);
49         doReturn(ch).when(chf).channel();
50         Throwable thr = Mockito.mock(Throwable.class);
51         doReturn(chf).when(workerGroup).register(any(Channel.class));
52
53         ChannelPromise promise = Mockito.mock(ChannelPromise.class);
54         doReturn(promise).when(chf).addListener(any(GenericFutureListener.class));
55         doReturn(thr).when(chf).cause();
56
57         Long timeout = 200L;
58         NetconfHelloMessageAdditionalHeader header = new NetconfHelloMessageAdditionalHeader("a", "host", "port", "trans", "id");
59         NetconfClientSessionListener listener = new SimpleNetconfClientSessionListener();
60         InetSocketAddress address = InetSocketAddress.createUnresolved("host", 830);
61         ReconnectStrategyFactory reconnectStrategyFactory = Mockito.mock(ReconnectStrategyFactory.class);
62         AuthenticationHandler handler = Mockito.mock(AuthenticationHandler.class);
63         ReconnectStrategy reconnect = Mockito.mock(ReconnectStrategy.class);
64
65         doReturn(5).when(reconnect).getConnectTimeout();
66         doReturn("").when(reconnect).toString();
67         doReturn("").when(handler).toString();
68         doReturn("").when(reconnectStrategyFactory).toString();
69         doReturn(reconnect).when(reconnectStrategyFactory).createReconnectStrategy();
70
71         NetconfReconnectingClientConfiguration cfg = NetconfReconnectingClientConfigurationBuilder.create().
72                 withProtocol(NetconfClientConfiguration.NetconfClientProtocol.SSH).
73                 withAddress(address).
74                 withConnectionTimeoutMillis(timeout).
75                 withReconnectStrategy(reconnect).
76                 withAdditionalHeader(header).
77                 withSessionListener(listener).
78                 withConnectStrategyFactory(reconnectStrategyFactory).
79                 withAuthHandler(handler).build();
80
81         NetconfReconnectingClientConfiguration cfg2 = NetconfReconnectingClientConfigurationBuilder.create().
82                 withProtocol(NetconfClientConfiguration.NetconfClientProtocol.TCP).
83                 withAddress(address).
84                 withConnectionTimeoutMillis(timeout).
85                 withReconnectStrategy(reconnect).
86                 withAdditionalHeader(header).
87                 withSessionListener(listener).
88                 withConnectStrategyFactory(reconnectStrategyFactory).
89                 withAuthHandler(handler).build();
90
91         NetconfClientDispatcherImpl dispatcher = new NetconfClientDispatcherImpl(bossGroup, workerGroup, timer);
92         Future<NetconfClientSession> sshSession = dispatcher.createClient(cfg);
93         Future<NetconfClientSession> tcpSession = dispatcher.createClient(cfg2);
94
95         Future<Void> sshReconn = dispatcher.createReconnectingClient(cfg);
96         Future<Void> tcpReconn = dispatcher.createReconnectingClient(cfg2);
97
98         assertNotNull(sshSession);
99         assertNotNull(tcpSession);
100         assertNotNull(sshReconn);
101         assertNotNull(tcpReconn);
102
103     }
104 }