Merge "Make TCs to assert on actual values and remove toString asserts"
[openflowplugin.git] / openflowjava / 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.collect.Lists;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.net.InetAddress;
14 import java.net.UnknownHostException;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.TimeoutException;
18 import org.junit.Assert;
19 import org.junit.Test;
20 import org.mockito.Mock;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
23 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfiguration;
24 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfigurationImpl;
25 import org.opendaylight.openflowjava.protocol.impl.core.SwitchConnectionProviderImpl;
26 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.KeystoreType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.TransportProtocol;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Unit tests for SwitchConnectionProviderImpl.
34  *
35  * @author michal.polkorab
36  */
37 public class SwitchConnectionProviderImplTest {
38
39     @Mock SwitchConnectionHandler handler;
40
41     private static final int SWITCH_IDLE_TIMEOUT = 2000;
42     private static final int WAIT_TIMEOUT = 2000;
43     private static final int CHANNEL_OUTBOUND_QUEUE_SIZE = 1024;
44     private TlsConfiguration tlsConfiguration;
45     private SwitchConnectionProviderImpl provider;
46     private ConnectionConfigurationImpl config;
47
48     /**
49      * Creates new {@link SwitchConnectionProvider} instance for each test.
50      * @param protocol communication protocol
51      */
52     public void startUp(final TransportProtocol protocol) throws UnknownHostException {
53         MockitoAnnotations.initMocks(this);
54         config = null;
55         if (protocol != null) {
56             createConfig(protocol);
57         }
58         provider = new SwitchConnectionProviderImpl(config);
59     }
60
61     private void createConfig(final TransportProtocol protocol) throws UnknownHostException {
62         InetAddress startupAddress = InetAddress.getLocalHost();
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                     Lists.newArrayList("TLS_RSA_WITH_AES_128_CBC_SHA", "TLS_RSA_WITH_AES_128_CBC_SHA256")) ;
70         }
71         config = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration, SWITCH_IDLE_TIMEOUT, true,
72                 false, CHANNEL_OUTBOUND_QUEUE_SIZE);
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(config);
82         final 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() throws UnknownHostException {
95         startUp(null);
96         provider.setSwitchConnectionHandler(handler);
97         final 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() throws UnknownHostException {
110         startUp(TransportProtocol.TCP);
111         final ListenableFuture<Boolean> future = provider.startup();
112         try {
113             future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
114         } catch (InterruptedException | ExecutionException | TimeoutException e) {
115             Assert.assertEquals("Wrong state", "java.lang.IllegalStateException:"
116                     + " SwitchConnectionHandler is not set", e.getMessage());
117         }
118     }
119
120     /**
121      * Tests correct provider startup - over TCP.
122      */
123     @Test
124     public void testStartup4() throws UnknownHostException {
125         startUp(TransportProtocol.TCP);
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() throws UnknownHostException {
139         startUp(TransportProtocol.TLS);
140         provider.setSwitchConnectionHandler(handler);
141         try {
142             Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
143         } catch (InterruptedException | ExecutionException | TimeoutException e) {
144             Assert.fail();
145         }
146     }
147
148     /**
149      * Tests correct provider startup - over UDP.
150      */
151     @Test
152     public void testStartup6() throws UnknownHostException {
153         startUp(TransportProtocol.UDP);
154         provider.setSwitchConnectionHandler(handler);
155         try {
156             Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
157         } catch (InterruptedException | ExecutionException | TimeoutException e) {
158             Assert.fail();
159         }
160     }
161
162     /**
163      * Tests correct provider shutdown.
164      */
165     @Test
166     public void testShutdown() throws UnknownHostException {
167         startUp(TransportProtocol.TCP);
168         provider.setSwitchConnectionHandler(handler);
169         try {
170             Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
171             Assert.assertTrue("Failed to stop", provider.shutdown().get(5 * WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
172         } catch (InterruptedException | ExecutionException | TimeoutException e) {
173             LoggerFactory.getLogger(SwitchConnectionProviderImplTest.class).error("Unexpected error", e);
174         }
175     }
176
177 }