2 * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
9 package org.opendaylight.openflowjava.protocol.impl.core.connection;
\r
11 import java.net.InetAddress;
\r
12 import java.net.UnknownHostException;
\r
13 import java.util.concurrent.ExecutionException;
\r
14 import java.util.concurrent.TimeUnit;
\r
15 import java.util.concurrent.TimeoutException;
\r
17 import org.junit.Assert;
\r
18 import org.junit.Test;
\r
19 import org.mockito.Mock;
\r
20 import org.mockito.MockitoAnnotations;
\r
21 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
\r
22 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfiguration;
\r
23 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfigurationImpl;
\r
24 import org.opendaylight.openflowjava.protocol.impl.core.SwitchConnectionProviderImpl;
\r
25 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.KeystoreType;
\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType;
\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.TransportProtocol;
\r
30 import com.google.common.util.concurrent.ListenableFuture;
\r
33 * @author michal.polkorab
\r
36 public class SwitchConnectionProviderImplTest {
\r
38 @Mock SwitchConnectionHandler handler;
\r
40 private static final int SWITCH_IDLE_TIMEOUT = 2000;
\r
41 private static final int WAIT_TIMEOUT = 2000;
\r
42 private InetAddress startupAddress;
\r
43 private TlsConfiguration tlsConfiguration;
\r
44 private SwitchConnectionProviderImpl provider;
\r
45 private ConnectionConfigurationImpl config;
\r
48 * Creates new {@link SwitchConnectionProvider} instance for each test
\r
49 * @param protocol communication protocol
\r
51 public void startUp(TransportProtocol protocol) {
\r
52 MockitoAnnotations.initMocks(this);
\r
54 if (protocol != null) {
\r
55 createConfig(protocol);
\r
57 provider = new SwitchConnectionProviderImpl();
\r
60 private void createConfig(TransportProtocol protocol) {
\r
62 startupAddress = InetAddress.getLocalHost();
\r
63 } catch (UnknownHostException e) {
\r
64 e.printStackTrace();
\r
66 tlsConfiguration = null;
\r
67 if (protocol.equals(TransportProtocol.TLS)) {
\r
68 tlsConfiguration = new TlsConfigurationImpl(KeystoreType.JKS,
\r
69 "/selfSignedSwitch", PathType.CLASSPATH, KeystoreType.JKS,
\r
70 "/selfSignedController", PathType.CLASSPATH) ;
\r
72 config = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration, SWITCH_IDLE_TIMEOUT);
\r
73 config.setTransferProtocol(protocol);
\r
77 * Tests provider startup - without configuration and {@link SwitchConnectionHandler}
\r
80 public void testStartup1() {
\r
81 provider = new SwitchConnectionProviderImpl();
\r
82 ListenableFuture<Boolean> future = provider.startup();
\r
84 future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
\r
85 } catch (InterruptedException | ExecutionException | TimeoutException e) {
\r
86 Assert.assertEquals("Wrong state", "java.lang.NullPointerException", e.getMessage());
\r
91 * Tests provider startup - without configuration
\r
94 public void testStartup2() {
\r
96 provider.setSwitchConnectionHandler(handler);
\r
97 ListenableFuture<Boolean> future = provider.startup();
\r
99 future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
\r
100 } catch (InterruptedException | ExecutionException | TimeoutException e) {
\r
101 Assert.assertEquals("Wrong state", "java.lang.NullPointerException", e.getMessage());
\r
106 * Tests provider startup - without {@link SwitchConnectionHandler}
\r
109 public void testStartup3() {
\r
110 startUp(TransportProtocol.TCP);
\r
111 provider.setConfiguration(config);
\r
112 ListenableFuture<Boolean> future = provider.startup();
\r
114 future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
\r
115 } catch (InterruptedException | ExecutionException | TimeoutException e) {
\r
116 Assert.assertEquals("Wrong state", "java.lang.IllegalStateException:"
\r
117 + " SwitchConnectionHandler is not set", e.getMessage());
\r
122 * Tests correct provider startup - over TCP
\r
125 public void testStartup4() {
\r
126 startUp(TransportProtocol.TCP);
\r
127 provider.setConfiguration(config);
\r
128 provider.setSwitchConnectionHandler(handler);
\r
130 Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
\r
131 } catch (InterruptedException | ExecutionException | TimeoutException e) {
\r
137 * Tests correct provider startup - over TLS
\r
140 public void testStartup5() {
\r
141 startUp(TransportProtocol.TLS);
\r
142 provider.setConfiguration(config);
\r
143 provider.setSwitchConnectionHandler(handler);
\r
145 Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
\r
146 } catch (InterruptedException | ExecutionException | TimeoutException e) {
\r
152 * Tests correct provider startup - over UDP
\r
155 public void testStartup6() {
\r
156 startUp(TransportProtocol.UDP);
\r
157 provider.setConfiguration(config);
\r
158 provider.setSwitchConnectionHandler(handler);
\r
160 Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
\r
161 } catch (InterruptedException | ExecutionException | TimeoutException e) {
\r
167 * Tests correct provider shutdown
\r
170 public void testShutdown() {
\r
171 startUp(TransportProtocol.TCP);
\r
172 provider.setConfiguration(config);
\r
173 provider.setSwitchConnectionHandler(handler);
\r
175 Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
\r
176 Assert.assertTrue("Failed to stop", provider.shutdown().get(5 * WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
\r
177 } catch (InterruptedException | ExecutionException | TimeoutException e) {
\r
178 e.printStackTrace();
\r