Remove redundant exception declarations
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / connection / UdpHandlerTest.java
1 /*
2  * Copyright (c) 2014 Pantheon Technologies s.r.o. 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.openflowjava.protocol.impl.core.connection;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.net.InetAddress;
12 import java.util.concurrent.ExecutionException;
13 import java.util.concurrent.TimeUnit;
14 import java.util.concurrent.TimeoutException;
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.Mock;
19 import org.mockito.MockitoAnnotations;
20 import org.opendaylight.openflowjava.protocol.impl.core.UdpChannelInitializer;
21 import org.opendaylight.openflowjava.protocol.impl.core.UdpHandler;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Unit tests for UdpHandler.
27  *
28  * @author madamjak
29  */
30 public class UdpHandlerTest {
31
32     private static final Logger LOG = LoggerFactory.getLogger(UdpHandlerTest.class);
33
34     @Mock
35     private UdpChannelInitializer udpChannelInitializerMock;
36     private UdpHandler udpHandler;
37
38     /**
39      * Mock init.
40      */
41     @Before
42     public void startUp() {
43         MockitoAnnotations.initMocks(this);
44     }
45
46     /**
47      * Test to create UdpHandler with empty address and zero port.
48      */
49     @Test
50     public void testWithEmptyAddress() throws Exception {
51         udpHandler = new UdpHandler(null, 0, () -> { });
52         udpHandler.setChannelInitializer(udpChannelInitializerMock);
53         Assert.assertTrue("Wrong - start server", startupServer(false));
54         try {
55             Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500, TimeUnit.MILLISECONDS));
56         } catch (TimeoutException e) {
57             Assert.fail("Wrong - getIsOnlineFuture timed out");
58         }
59         Assert.assertFalse("Wrong - port has been set to zero", udpHandler.getPort() == 0);
60         shutdownServer();
61     }
62
63     /**
64      * Test to create UdpHandler with empty address and zero port on Epoll native transport.
65      */
66     @Test
67     public void testWithEmptyAddressOnEpoll() throws Exception {
68         udpHandler = new UdpHandler(null, 0, () -> { });
69         udpHandler.setChannelInitializer(udpChannelInitializerMock);
70         Assert.assertTrue("Wrong - start server", startupServer(true));
71         try {
72             Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS));
73         } catch (TimeoutException e) {
74             Assert.fail("Wrong - getIsOnlineFuture timed out");
75         }
76         Assert.assertFalse("Wrong - port has been set to zero", udpHandler.getPort() == 0);
77         shutdownServer();
78     }
79
80     /**
81      * Test to create UdpHandler with fill address and given port.
82      */
83     @Test
84     public void testWithAddressAndPort() throws Exception {
85         int port = 9874;
86         udpHandler = new UdpHandler(InetAddress.getLocalHost(), port, () -> { });
87         udpHandler.setChannelInitializer(udpChannelInitializerMock);
88         Assert.assertTrue("Wrong - start server", startupServer(false));
89         try {
90             Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS));
91         } catch (TimeoutException e) {
92             Assert.fail("Wrong - getIsOnlineFuture timed out");
93         }
94         Assert.assertEquals("Wrong - bad port number has been set", port, udpHandler.getPort());
95         shutdownServer();
96     }
97
98     /**
99      * Test to create UdpHandler with fill address and given port on Epoll native transport.
100      */
101     @Test
102     public void testWithAddressAndPortOnEpoll() throws Exception {
103         int port = 9874;
104         udpHandler = new UdpHandler(InetAddress.getLocalHost(), port, () -> { });
105         udpHandler.setChannelInitializer(udpChannelInitializerMock);
106         Assert.assertTrue("Wrong - start server", startupServer(true));
107         try {
108             Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS));
109         } catch (TimeoutException e) {
110             Assert.fail("Wrong - getIsOnlineFuture timed out");
111         }
112         Assert.assertEquals("Wrong - bad port number has been set", port, udpHandler.getPort());
113         shutdownServer();
114     }
115
116     private Boolean startupServer(final boolean isEpollEnabled)
117             throws InterruptedException, ExecutionException {
118         ListenableFuture<Boolean> online = udpHandler.getIsOnlineFuture();
119         /**
120          * Test EPoll based native transport if isEpollEnabled is true.
121          * Else use Nio based transport.
122          */
123         udpHandler.initiateEventLoopGroups(null, isEpollEnabled);
124         new Thread(udpHandler).start();
125
126         try {
127             online.get(10, TimeUnit.SECONDS);
128         } catch (TimeoutException e) {
129             LOG.warn("Timeout while waiting for UDP handler to start", e);
130         }
131
132         return online.isDone();
133     }
134
135     private void shutdownServer() throws InterruptedException, ExecutionException, TimeoutException {
136         ListenableFuture<Boolean> shutdownRet = udpHandler.shutdown() ;
137         final Boolean shutdownSucceeded = shutdownRet.get(10, TimeUnit.SECONDS);
138         Assert.assertTrue("Wrong - shutdown failed", shutdownSucceeded);
139     }
140 }