2 * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowjava.protocol.impl.core.connection;
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;
31 * @author michal.polkorab
34 public class SwitchConnectionProviderImplTest {
36 @Mock SwitchConnectionHandler handler;
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;
46 * Creates new {@link SwitchConnectionProvider} instance for each test
47 * @param protocol communication protocol
49 public void startUp(final TransportProtocol protocol) {
50 MockitoAnnotations.initMocks(this);
52 if (protocol != null) {
53 createConfig(protocol);
55 provider = new SwitchConnectionProviderImpl();
58 private void createConfig(final TransportProtocol protocol) {
60 startupAddress = InetAddress.getLocalHost();
61 } catch (final UnknownHostException e) {
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) ;
70 config = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration, SWITCH_IDLE_TIMEOUT, true);
71 config.setTransferProtocol(protocol);
75 * Tests provider startup - without configuration and {@link SwitchConnectionHandler}
78 public void testStartup1() {
79 provider = new SwitchConnectionProviderImpl();
80 final ListenableFuture<Boolean> future = provider.startup();
82 future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
83 } catch (InterruptedException | ExecutionException | TimeoutException e) {
84 Assert.assertEquals("Wrong state", "java.lang.NullPointerException", e.getMessage());
89 * Tests provider startup - without configuration
92 public void testStartup2() {
94 provider.setSwitchConnectionHandler(handler);
95 final ListenableFuture<Boolean> future = provider.startup();
97 future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
98 } catch (InterruptedException | ExecutionException | TimeoutException e) {
99 Assert.assertEquals("Wrong state", "java.lang.NullPointerException", e.getMessage());
104 * Tests provider startup - without {@link SwitchConnectionHandler}
107 public void testStartup3() {
108 startUp(TransportProtocol.TCP);
109 provider.setConfiguration(config);
110 final ListenableFuture<Boolean> future = provider.startup();
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());
120 * Tests correct provider startup - over TCP
123 public void testStartup4() {
124 startUp(TransportProtocol.TCP);
125 provider.setConfiguration(config);
126 provider.setSwitchConnectionHandler(handler);
128 Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
129 } catch (InterruptedException | ExecutionException | TimeoutException e) {
135 * Tests correct provider startup - over TLS
138 public void testStartup5() {
139 startUp(TransportProtocol.TLS);
140 provider.setConfiguration(config);
141 provider.setSwitchConnectionHandler(handler);
143 Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
144 } catch (InterruptedException | ExecutionException | TimeoutException e) {
150 * Tests correct provider startup - over UDP
153 public void testStartup6() {
154 startUp(TransportProtocol.UDP);
155 provider.setConfiguration(config);
156 provider.setSwitchConnectionHandler(handler);
158 Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
159 } catch (InterruptedException | ExecutionException | TimeoutException e) {
165 * Tests correct provider shutdown
168 public void testShutdown() {
169 startUp(TransportProtocol.TCP);
170 provider.setConfiguration(config);
171 provider.setSwitchConnectionHandler(handler);
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) {