BUG-58: refactor to take advantage of netty
[bgpcep.git] / framework / src / test / java / org / opendaylight / protocol / framework / ServerTest.java
1 /*
2  * Copyright (c) 2013 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 package org.opendaylight.protocol.framework;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
13 import io.netty.channel.Channel;
14 import io.netty.channel.ChannelFuture;
15 import io.netty.util.concurrent.DefaultPromise;
16 import io.netty.util.concurrent.GlobalEventExecutor;
17 import io.netty.util.concurrent.Promise;
18
19 import java.io.IOException;
20 import java.net.ConnectException;
21 import java.net.InetSocketAddress;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.TimeUnit;
24
25 import org.junit.After;
26 import org.junit.Test;
27
28 public class ServerTest {
29         public static final int PORT = 18080;
30
31         DispatcherImpl clientDispatcher, dispatcher;
32
33         final SimpleSessionListener pce = new SimpleSessionListener();
34
35         SimpleSession session = null;
36
37         ChannelFuture server = null;
38
39         public final InetSocketAddress serverAddress = new InetSocketAddress("127.0.0.5", PORT);
40
41         @Test
42         public void testConnectionEstablished() throws Exception {
43                 this.dispatcher = new DispatcherImpl();
44
45                 final Promise<Boolean> p = new DefaultPromise<>(GlobalEventExecutor.INSTANCE);
46
47                 this.server = this.dispatcher.createServer(this.serverAddress,
48                                 new SessionListenerFactory<SimpleSessionListener>() {
49                         @Override
50                         public SimpleSessionListener getSessionListener() {
51                                 return new SimpleSessionListener();
52                         }
53                 }, new SessionNegotiatorFactory<SimpleMessage, SimpleSession, SimpleSessionListener>() {
54
55                         @Override
56                         public SessionNegotiator<SimpleSession> getSessionNegotiator(final SessionListenerFactory<SimpleSessionListener> factory,
57                                         final Channel channel, final Promise<SimpleSession> promise) {
58                                 p.setSuccess(true);
59                                 return new SimpleSessionNegotiator(promise, channel);
60                         }
61                 }, new MessageFactory());
62
63                 server.get();
64
65                 this.clientDispatcher = new DispatcherImpl();
66
67                 this.session = this.clientDispatcher.createClient(serverAddress,
68                                 new SimpleSessionListener(), new SessionNegotiatorFactory<SimpleMessage, SimpleSession, SimpleSessionListener>() {
69                         @Override
70                         public SessionNegotiator<SimpleSession> getSessionNegotiator(final SessionListenerFactory<SimpleSessionListener> factory,
71                                         final Channel channel, final Promise<SimpleSession> promise) {
72                                 return new SimpleSessionNegotiator(promise, channel);
73                         }
74                 }, new MessageFactory(), new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000)).get();
75
76                 assertEquals(true, p.get(3, TimeUnit.SECONDS));
77         }
78
79         public void testConnectionFailed() throws IOException, InterruptedException {
80                 this.dispatcher = new DispatcherImpl();
81                 this.clientDispatcher = new DispatcherImpl();
82                 final SimpleSessionListener listener = new SimpleSessionListener();
83
84                 try {
85                         this.clientDispatcher.createClient(serverAddress, listener,
86                                         new SessionNegotiatorFactory<SimpleMessage, SimpleSession, SimpleSessionListener>() {
87                                 @Override
88                                 public SessionNegotiator<SimpleSession> getSessionNegotiator(final SessionListenerFactory<SimpleSessionListener> factory,
89                                                 final Channel channel, final Promise<SimpleSession> promise) {
90                                         // TODO Auto-generated method stub
91                                         return null;
92                                 }
93                         }, new MessageFactory(), new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000)).get();
94
95                         fail("Connection succeeded unexpectedly");
96                 } catch (ExecutionException e) {
97                         assertTrue(listener.failed);
98                         assertTrue(e.getCause() instanceof ConnectException);
99                 }
100         }
101
102         @After
103         public void tearDown() throws IOException {
104                 server.channel().close();
105                 this.dispatcher.close();
106                 this.clientDispatcher.close();
107                 try {
108                         Thread.sleep(100);
109                 } catch (final InterruptedException e) {
110                         throw new RuntimeException(e);
111                 }
112         }
113 }