Change getIsOnlineFuture() return type
[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 package org.opendaylight.openflowjava.protocol.impl.core.connection;
9
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertThrows;
14 import static org.junit.Assert.assertTrue;
15 import static org.mockito.ArgumentMatchers.any;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18
19 import java.net.InetAddress;
20 import java.net.UnknownHostException;
21 import java.util.List;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.TimeUnit;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mock;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.opendaylight.infrautils.diagstatus.DiagStatusService;
30 import org.opendaylight.infrautils.diagstatus.ServiceRegistration;
31 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
32 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfiguration;
33 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfigurationImpl;
34 import org.opendaylight.openflowjava.protocol.impl.core.SwitchConnectionProviderImpl;
35 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.KeystoreType;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.TransportProtocol;
39
40 /**
41  * Unit tests for SwitchConnectionProviderImpl.
42  *
43  * @author michal.polkorab
44  */
45 @RunWith(MockitoJUnitRunner.class)
46 public class SwitchConnectionProviderImplTest {
47
48     @Mock SwitchConnectionHandler handler;
49     @Mock DiagStatusService diagStatus;
50
51     private static final int SWITCH_IDLE_TIMEOUT = 2000;
52     private static final int WAIT_TIMEOUT = 2000;
53     private static final int CHANNEL_OUTBOUND_QUEUE_SIZE = 1024;
54     private TlsConfiguration tlsConfiguration;
55     private SwitchConnectionProviderImpl provider;
56     private ConnectionConfigurationImpl config;
57
58     @Before
59     public void before() {
60         doReturn(mock(ServiceRegistration.class)).when(diagStatus).register(any());
61     }
62
63     /**
64      * Creates new {@link SwitchConnectionProvider} instance for each test.
65      * @param protocol communication protocol
66      */
67     public void startUp(final TransportProtocol protocol) throws UnknownHostException {
68         config = null;
69         if (protocol != null) {
70             createConfig(protocol);
71         }
72         provider = new SwitchConnectionProviderImpl(diagStatus, config);
73     }
74
75     private void createConfig(final TransportProtocol protocol) throws UnknownHostException {
76         InetAddress startupAddress = InetAddress.getLocalHost();
77
78         tlsConfiguration = null;
79         if (protocol.equals(TransportProtocol.TLS)) {
80             tlsConfiguration = new TlsConfigurationImpl(KeystoreType.JKS,
81                     "/selfSignedSwitch", PathType.CLASSPATH, KeystoreType.JKS,
82                     "/selfSignedController", PathType.CLASSPATH,
83                     List.of("TLS_RSA_WITH_AES_128_CBC_SHA", "TLS_RSA_WITH_AES_128_CBC_SHA256"));
84         }
85         config = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration, SWITCH_IDLE_TIMEOUT, true,
86                 false, CHANNEL_OUTBOUND_QUEUE_SIZE);
87         config.setTransferProtocol(protocol);
88     }
89
90     /**
91      * Tests provider startup - without configuration and {@link SwitchConnectionHandler}.
92      */
93     @Test
94     public void testStartup1() throws UnknownHostException {
95         startUp(null);
96         final var future = provider.startup();
97
98         final var cause = assertThrows(ExecutionException.class, () -> future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS))
99             .getCause();
100         assertThat(cause, instanceOf(IllegalStateException.class));
101         assertEquals("Connection not configured", cause.getMessage());
102     }
103
104     /**
105      * Tests provider startup - without configuration.
106      */
107     @Test
108     public void testStartup2() throws UnknownHostException {
109         startUp(null);
110         provider.setSwitchConnectionHandler(handler);
111         final var future = provider.startup();
112
113         final var cause = assertThrows(ExecutionException.class, () -> future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS))
114             .getCause();
115         assertThat(cause, instanceOf(IllegalStateException.class));
116         assertEquals("Connection not configured", cause.getMessage());
117     }
118
119     /**
120      * Tests provider startup - without {@link SwitchConnectionHandler}.
121      */
122     @Test
123     public void testStartup3() throws UnknownHostException {
124         startUp(TransportProtocol.TCP);
125         final var future = provider.startup();
126
127         final var cause = assertThrows(ExecutionException.class, () -> future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS))
128             .getCause();
129         assertThat(cause, instanceOf(IllegalStateException.class));
130         assertEquals("SwitchConnectionHandler is not set", cause.getMessage());
131     }
132
133     /**
134      * Tests correct provider startup - over TCP.
135      */
136     @Test
137     public void testStartup4() throws Exception {
138         startUp(TransportProtocol.TCP);
139         provider.setSwitchConnectionHandler(handler);
140
141         provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
142     }
143
144     /**
145      * Tests correct provider startup - over TLS.
146      */
147     @Test
148     public void testStartup5() throws Exception {
149         startUp(TransportProtocol.TLS);
150         provider.setSwitchConnectionHandler(handler);
151
152         provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
153     }
154
155     /**
156      * Tests correct provider startup - over UDP.
157      */
158     @Test
159     public void testStartup6() throws Exception {
160         startUp(TransportProtocol.UDP);
161         provider.setSwitchConnectionHandler(handler);
162
163         provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
164     }
165
166     /**
167      * Tests correct provider shutdown.
168      */
169     @Test
170     public void testShutdown() throws Exception {
171         startUp(TransportProtocol.TCP);
172         provider.setSwitchConnectionHandler(handler);
173
174         provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
175         assertTrue("Failed to stop", provider.shutdown().get(5 * WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
176     }
177 }