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.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.spi.connection.SwitchConnectionProvider;
\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.KeystoreType;
\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType;
\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.TransportProtocol;
\r
29 import com.google.common.util.concurrent.ListenableFuture;
\r
32 * @author michal.polkorab
\r
35 public class SwitchConnectionProviderImplTest {
\r
37 @Mock SwitchConnectionHandler handler;
\r
39 private static final int SWITCH_IDLE_TIMEOUT = 2000;
\r
40 private static final int WAIT_TIMEOUT = 2000;
\r
41 private InetAddress startupAddress;
\r
42 private TlsConfiguration tlsConfiguration;
\r
43 private SwitchConnectionProviderImpl provider;
\r
44 private ConnectionConfigurationImpl config;
\r
47 * Creates new {@link SwitchConnectionProvider} instance for each test
\r
48 * @param protocol communication protocol
\r
50 public void startUp(TransportProtocol protocol) {
\r
51 MockitoAnnotations.initMocks(this);
\r
53 if (protocol != null) {
\r
54 createConfig(protocol);
\r
56 provider = new SwitchConnectionProviderImpl();
\r
59 private void createConfig(TransportProtocol protocol) {
\r
61 startupAddress = InetAddress.getLocalHost();
\r
62 } catch (UnknownHostException e) {
\r
63 e.printStackTrace();
\r
65 tlsConfiguration = null;
\r
66 if (protocol.equals(TransportProtocol.TLS)) {
\r
67 tlsConfiguration = new TlsConfigurationImpl(KeystoreType.JKS,
\r
68 "/selfSignedSwitch", PathType.CLASSPATH, KeystoreType.JKS,
\r
69 "/selfSignedController", PathType.CLASSPATH) ;
\r
71 config = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration, SWITCH_IDLE_TIMEOUT);
\r
72 config.setTransferProtocol(protocol);
\r
76 * Tests provider startup - without configuration and {@link SwitchConnectionHandler}
\r
79 public void testStartup1() {
\r
80 provider = new SwitchConnectionProviderImpl();
\r
81 ListenableFuture<Boolean> future = provider.startup();
\r
83 future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
\r
84 } catch (InterruptedException | ExecutionException | TimeoutException e) {
\r
85 Assert.assertEquals("Wrong state", "java.lang.NullPointerException", e.getMessage());
\r
90 * Tests provider startup - without configuration
\r
93 public void testStartup2() {
\r
95 provider.setSwitchConnectionHandler(handler);
\r
96 ListenableFuture<Boolean> future = provider.startup();
\r
98 future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
\r
99 } catch (InterruptedException | ExecutionException | TimeoutException e) {
\r
100 Assert.assertEquals("Wrong state", "java.lang.NullPointerException", e.getMessage());
\r
105 * Tests provider startup - without {@link SwitchConnectionHandler}
\r
108 public void testStartup3() {
\r
109 startUp(TransportProtocol.TCP);
\r
110 provider.setConfiguration(config);
\r
111 ListenableFuture<Boolean> future = provider.startup();
\r
113 future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
\r
114 } catch (InterruptedException | ExecutionException | TimeoutException e) {
\r
115 Assert.assertEquals("Wrong state", "java.lang.IllegalStateException:"
\r
116 + " SwitchConnectionHandler is not set", e.getMessage());
\r
121 * Tests correct provider startup - over TCP
\r
124 public void testStartup4() {
\r
125 startUp(TransportProtocol.TCP);
\r
126 provider.setConfiguration(config);
\r
127 provider.setSwitchConnectionHandler(handler);
\r
129 Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
\r
130 } catch (InterruptedException | ExecutionException | TimeoutException e) {
\r
136 * Tests correct provider startup - over TLS
\r
139 public void testStartup5() {
\r
140 startUp(TransportProtocol.TLS);
\r
141 provider.setConfiguration(config);
\r
142 provider.setSwitchConnectionHandler(handler);
\r
144 Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
\r
145 } catch (InterruptedException | ExecutionException | TimeoutException e) {
\r
151 * Tests correct provider startup - over UDP
\r
154 public void testStartup6() {
\r
155 startUp(TransportProtocol.UDP);
\r
156 provider.setConfiguration(config);
\r
157 provider.setSwitchConnectionHandler(handler);
\r
159 Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
\r
160 } catch (InterruptedException | ExecutionException | TimeoutException e) {
\r
166 * Tests correct provider shutdown
\r
169 public void testShutdown() {
\r
170 startUp(TransportProtocol.TCP);
\r
171 provider.setConfiguration(config);
\r
172 provider.setSwitchConnectionHandler(handler);
\r
174 Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
\r
175 Assert.assertTrue("Failed to stop", provider.shutdown().get(5 * WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
\r
176 } catch (InterruptedException | ExecutionException | TimeoutException e) {
\r
177 e.printStackTrace();
\r