Mass replace CRLF->LF
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / connection / SwitchConnectionProviderImplTest.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
9 package org.opendaylight.openflowjava.protocol.impl.core.connection;
10
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.TimeUnit;
15 import java.util.concurrent.TimeoutException;
16
17 import org.junit.Assert;
18 import org.junit.Test;
19 import org.mockito.Mock;
20 import org.mockito.MockitoAnnotations;
21 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
22 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfiguration;
23 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfigurationImpl;
24 import org.opendaylight.openflowjava.protocol.impl.core.SwitchConnectionProviderImpl;
25 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.KeystoreType;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.TransportProtocol;
29
30 import com.google.common.util.concurrent.ListenableFuture;
31
32 /**
33  * @author michal.polkorab
34  *
35  */
36 public class SwitchConnectionProviderImplTest {
37
38     @Mock SwitchConnectionHandler handler;
39
40     private static final int SWITCH_IDLE_TIMEOUT = 2000;
41     private static final int WAIT_TIMEOUT = 2000;
42     private InetAddress startupAddress;
43     private TlsConfiguration tlsConfiguration;
44     private SwitchConnectionProviderImpl provider;
45     private ConnectionConfigurationImpl config;
46
47     /**
48      * Creates new {@link SwitchConnectionProvider} instance for each test
49      * @param protocol communication protocol
50      */
51     public void startUp(TransportProtocol protocol) {
52         MockitoAnnotations.initMocks(this);
53         config = null;
54         if (protocol != null) {
55             createConfig(protocol);
56         }
57         provider = new SwitchConnectionProviderImpl();
58     }
59
60     private void createConfig(TransportProtocol protocol) {
61         try {
62             startupAddress = InetAddress.getLocalHost();
63         } catch (UnknownHostException e) {
64             e.printStackTrace();
65         }
66         tlsConfiguration = null;
67         if (protocol.equals(TransportProtocol.TLS)) {
68             tlsConfiguration = new TlsConfigurationImpl(KeystoreType.JKS,
69                     "/selfSignedSwitch", PathType.CLASSPATH, KeystoreType.JKS,
70                     "/selfSignedController", PathType.CLASSPATH) ;
71         }
72         config = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration, SWITCH_IDLE_TIMEOUT);
73         config.setTransferProtocol(protocol);
74     }
75
76     /**
77      * Tests provider startup - without configuration and {@link SwitchConnectionHandler}
78      */
79     @Test
80     public void testStartup1() {
81         provider = new SwitchConnectionProviderImpl();
82         ListenableFuture<Boolean> future = provider.startup();
83         try {
84             future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
85         } catch (InterruptedException | ExecutionException | TimeoutException e) {
86             Assert.assertEquals("Wrong state", "java.lang.NullPointerException", e.getMessage());
87         }
88     }
89
90     /**
91      * Tests provider startup - without configuration
92      */
93     @Test
94     public void testStartup2() {
95         startUp(null);
96         provider.setSwitchConnectionHandler(handler);
97         ListenableFuture<Boolean> future = provider.startup();
98         try {
99             future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
100         } catch (InterruptedException | ExecutionException | TimeoutException e) {
101             Assert.assertEquals("Wrong state", "java.lang.NullPointerException", e.getMessage());
102         }
103     }
104
105     /**
106      * Tests provider startup - without {@link SwitchConnectionHandler}
107      */
108     @Test
109     public void testStartup3() {
110         startUp(TransportProtocol.TCP);
111         provider.setConfiguration(config);
112         ListenableFuture<Boolean> future = provider.startup();
113         try {
114             future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
115         } catch (InterruptedException | ExecutionException | TimeoutException e) {
116             Assert.assertEquals("Wrong state", "java.lang.IllegalStateException:"
117                     + " SwitchConnectionHandler is not set", e.getMessage());
118         }
119     }
120
121     /**
122      * Tests correct provider startup - over TCP
123      */
124     @Test
125     public void testStartup4() {
126         startUp(TransportProtocol.TCP);
127         provider.setConfiguration(config);
128         provider.setSwitchConnectionHandler(handler);
129         try {
130             Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
131         } catch (InterruptedException | ExecutionException | TimeoutException e) {
132             Assert.fail();
133         }
134     }
135
136     /**
137      * Tests correct provider startup - over TLS
138      */
139     @Test
140     public void testStartup5() {
141         startUp(TransportProtocol.TLS);
142         provider.setConfiguration(config);
143         provider.setSwitchConnectionHandler(handler);
144         try {
145             Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
146         } catch (InterruptedException | ExecutionException | TimeoutException e) {
147             Assert.fail();
148         }
149     }
150
151     /**
152      * Tests correct provider startup - over UDP
153      */
154     @Test
155     public void testStartup6() {
156         startUp(TransportProtocol.UDP);
157         provider.setConfiguration(config);
158         provider.setSwitchConnectionHandler(handler);
159         try {
160             Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
161         } catch (InterruptedException | ExecutionException | TimeoutException e) {
162             Assert.fail();
163         }
164     }
165
166     /**
167      * Tests correct provider shutdown
168      */
169     @Test
170     public void testShutdown() {
171         startUp(TransportProtocol.TCP);
172         provider.setConfiguration(config);
173         provider.setSwitchConnectionHandler(handler);
174         try {
175             Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
176             Assert.assertTrue("Failed to stop", provider.shutdown().get(5 * WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
177         } catch (InterruptedException | ExecutionException | TimeoutException e) {
178             e.printStackTrace();
179         }
180     }
181
182 }