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