Add unit test - fully covered SwitchConnectionProviderImpl, ConnectionAdapterImpl
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / 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.connection;
9
10 import java.io.IOException;
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
23 import com.google.common.util.concurrent.ListenableFuture;
24
25 /**
26  * @author madamjak
27  *
28  */
29 public class UdpHandlerTest {
30     @Mock UdpChannelInitializer udpChannelInitializerMock;
31     UdpHandler udpHandler;
32     /**
33      * Mock init
34      */
35     @Before
36     public void startUp() {
37         MockitoAnnotations.initMocks(this);
38     }
39
40     /**
41      * Test to create UdpHandler with empty address and zero port
42      * @throws InterruptedException
43      * @throws ExecutionException
44      * @throws IOException
45      */
46     @Test
47     public void testWithEmptyAddress() throws InterruptedException, ExecutionException, IOException {
48         udpHandler = new UdpHandler(null, 0);
49         udpHandler.setChannelInitializer(udpChannelInitializerMock);
50         Assert.assertTrue("Wrong - start server", startupServer());
51         try {
52             Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS).booleanValue());
53         } catch (TimeoutException e) {
54             Assert.fail("Wrong - getIsOnlineFuture timed out");
55         }
56         Assert.assertFalse("Wrong - port has been set to zero", udpHandler.getPort() == 0);
57         shutdownServer();
58     }
59
60     /**
61      * Test to create UdpHandler with fill address and given port
62      * @throws InterruptedException
63      * @throws ExecutionException
64      * @throws IOException
65      */
66     @Test
67     public void testWithAddressAndPort() throws InterruptedException, ExecutionException, IOException{
68         int port = 9874;
69         udpHandler = new UdpHandler(InetAddress.getLocalHost(), port);
70         udpHandler.setChannelInitializer(udpChannelInitializerMock);
71         Assert.assertTrue("Wrong - start server", startupServer());
72         try {
73             Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS).booleanValue());
74         } catch (TimeoutException e) {
75             Assert.fail("Wrong - getIsOnlineFuture timed out");
76         }
77         Assert.assertEquals("Wrong - bad port number has been set", port, udpHandler.getPort());
78         shutdownServer();
79     }
80
81     private Boolean startupServer() throws InterruptedException, IOException, ExecutionException {
82         ListenableFuture<Boolean> online = udpHandler.getIsOnlineFuture();
83
84             (new Thread(udpHandler)).start();
85             int retry = 0;
86             while (online.isDone() != true && retry++ < 20) {
87                 Thread.sleep(100);
88             }
89         return online.isDone() ;
90     }
91
92     private void shutdownServer() throws InterruptedException, ExecutionException {
93         ListenableFuture<Boolean> shutdownRet = udpHandler.shutdown() ;
94         while ( shutdownRet.isDone() != true )
95             Thread.sleep(100) ;
96         Assert.assertTrue("Wrong - shutdown failed", shutdownRet.get());
97     }
98 }