2 * Copyright (c) 2014 Brocade Communications 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.openflowjava.protocol.impl.core;
11 import static org.junit.Assert.assertEquals;
12 import io.netty.channel.ChannelHandlerContext;
14 import java.io.IOException;
15 import java.net.BindException;
16 import java.net.InetAddress;
17 import java.net.InetSocketAddress;
18 import java.net.Socket;
19 import java.util.concurrent.ExecutionException;
21 import org.junit.Assert;
22 import org.junit.Test;
23 import org.mockito.Mock;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
26 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializationFactory;
27 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializationFactory;
29 import com.google.common.util.concurrent.ListenableFuture;
35 public class TcpHandlerTest {
37 private InetAddress serverAddress = InetAddress.getLoopbackAddress() ;
38 @Mock ChannelHandlerContext mockChHndlrCtx ;
39 @Mock TcpChannelInitializer mockChannelInitializer;
40 @Mock SwitchConnectionHandler mockSwitchConnHndler ;
41 @Mock SerializationFactory mockSerializationFactory ;
42 @Mock DeserializationFactory mockDeserializationFactory ;
44 TcpHandler tcpHandler ;
49 public TcpHandlerTest() {
50 MockitoAnnotations.initMocks(this);
54 * Test run with null address set
56 * @throws InterruptedException
57 * @throws ExecutionException
60 public void testRunWithNullAddress() throws IOException, InterruptedException, ExecutionException {
62 tcpHandler = new TcpHandler(null, 0);
63 tcpHandler.setChannelInitializer(mockChannelInitializer);
65 assertEquals("failed to start server", true, startupServer()) ;
66 assertEquals("failed to connect client", true, clientConnection(tcpHandler.getPort())) ;
71 * Test run with address set
73 * @throws InterruptedException
74 * @throws ExecutionException
77 public void testRunWithAddress() throws IOException, InterruptedException, ExecutionException {
79 tcpHandler = new TcpHandler(serverAddress, 0);
80 tcpHandler.setChannelInitializer(mockChannelInitializer);
82 assertEquals("failed to start server", true, startupServer()) ;
83 assertEquals("failed to connect client", true, clientConnection(tcpHandler.getPort())) ;
88 * Test run with encryption
89 * @throws InterruptedException
91 * @throws ExecutionException
94 public void testRunWithEncryption () throws InterruptedException, IOException, ExecutionException {
95 int serverPort = 28001;
96 tcpHandler = new TcpHandler(serverAddress, serverPort);
97 tcpHandler.setChannelInitializer(mockChannelInitializer);
99 assertEquals( "failed to start server", true, startupServer()) ;
100 assertEquals( "wrong connection count", 0, tcpHandler.getNumberOfConnections() );
101 assertEquals( "wrong port", serverPort, tcpHandler.getPort() );
102 assertEquals( "wrong address", serverAddress.getHostAddress(), tcpHandler.getAddress()) ;
104 assertEquals("failed to connect client", true, clientConnection(tcpHandler.getPort())) ;
110 * Test run on already used port
111 * @throws IOException
114 public void testSocketAlreadyInUse() throws IOException {
115 int serverPort = 28001;
116 Socket firstBinder = new Socket();
117 boolean exceptionThrown = false;
119 firstBinder.bind(new InetSocketAddress(serverAddress, serverPort));
120 } catch (Exception e) {
121 Assert.fail("Test precondition failed - not able to bind socket to port " + serverPort);
124 tcpHandler = new TcpHandler(serverAddress, serverPort);
125 tcpHandler.setChannelInitializer(mockChannelInitializer);
127 } catch (Exception e) {
128 if (e instanceof BindException) {
129 exceptionThrown = true;
133 Assert.assertTrue("Expected BindException has not been thrown", exceptionThrown == true);
137 * Trigger the server shutdown and wait 2 seconds for completion
139 private void shutdownServer() throws InterruptedException, ExecutionException {
140 ListenableFuture<Boolean> shutdownRet = tcpHandler.shutdown() ;
141 while ( shutdownRet.isDone() != true )
143 assertEquals("shutdown failed", true, shutdownRet.get());
147 * @throws InterruptedException
148 * @throws IOException
149 * @throws ExecutionException
151 private Boolean startupServer() throws InterruptedException, IOException, ExecutionException {
152 ListenableFuture<Boolean> online = tcpHandler.getIsOnlineFuture();
154 (new Thread(tcpHandler)).start();
156 while (online.isDone() != true && retry++ < 20) {
159 return online.isDone() ;
162 * @throws IOException
164 private static Boolean clientConnection(int port) throws IOException {
165 // Connect, and disconnect
166 Socket socket = new Socket(InetAddress.getLoopbackAddress(), port );
167 Boolean result = socket.isConnected();