Bump upstreams
[openflowplugin.git] / openflowjava / openflow-protocol-it / src / test / java / org / opendaylight / openflowjava / protocol / it / integration / IntegrationTest.java
index 77e747a96279f793715e110399c08d9e07969e65..fc78e954dbdd38e9d97b7686a1955a9a14e6bb1d 100644 (file)
@@ -5,17 +5,29 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.openflowjava.protocol.it.integration;
 
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+
 import java.net.InetAddress;
 import java.util.ArrayList;
 import java.util.Deque;
 import java.util.List;
 import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
 import org.junit.After;
 import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentMatchers;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.opendaylight.infrautils.diagstatus.DiagStatusService;
+import org.opendaylight.infrautils.diagstatus.ServiceRegistration;
 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfiguration;
 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfigurationImpl;
 import org.opendaylight.openflowjava.protocol.impl.clients.ClientEvent;
@@ -40,9 +52,12 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
+ * End-to-end integration test.
+ *
  * @author michal.polkorab
  * @author timotej.kubas
  */
+@RunWith(MockitoJUnitRunner.class)
 public class IntegrationTest {
 
     private static final Logger LOGGER = LoggerFactory
@@ -50,22 +65,29 @@ public class IntegrationTest {
 
     private static int port;
     private TlsConfiguration tlsConfiguration;
+    private static final int CHANNEL_OUTBOUND_QUEUE_SIZE = 1024;
     private static final int SWITCH_IDLE_TIMEOUT = 2000;
     private static final long CONNECTION_TIMEOUT = 2000;
     private InetAddress startupAddress;
     private MockPlugin mockPlugin;
     private SwitchConnectionProviderImpl switchConnectionProvider;
     private ConnectionConfigurationImpl connConfig;
+    @Mock
+    private ExecutorService executorService;
 
-    private Thread t;
+    private Thread thread;
+
+    private enum ClientType {
+        SIMPLE,
+        LISTENING
+    }
 
-    private enum ClientType {SIMPLE, LISTENING}
-    /**
-     * @param protocol communication protocol to be used during test
-     * @throws Exception
-     */
     public void setUp(final TransportProtocol protocol) throws Exception {
         LOGGER.debug("\n starting test -------------------------------");
+        doAnswer(invocation -> {
+            invocation.getArgument(0, Runnable.class).run();
+            return null;
+        }).when(executorService).execute(ArgumentMatchers.any());
 
         final String currentDir = System.getProperty("user.dir");
         LOGGER.debug("Current dir using System: {}", currentDir);
@@ -75,15 +97,18 @@ public class IntegrationTest {
             tlsConfiguration = new TlsConfigurationImpl(KeystoreType.JKS,
                     "/selfSignedSwitch", PathType.CLASSPATH, KeystoreType.JKS,
                     "/selfSignedController", PathType.CLASSPATH,
-                    new ArrayList<String>());
+                    List.of());
         }
-        connConfig = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration, SWITCH_IDLE_TIMEOUT, true);
+        connConfig = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration,
+                SWITCH_IDLE_TIMEOUT, true, false, CHANNEL_OUTBOUND_QUEUE_SIZE);
         connConfig.setTransferProtocol(protocol);
-        mockPlugin = new MockPlugin();
+        mockPlugin = new MockPlugin(executorService);
 
-        switchConnectionProvider = new SwitchConnectionProviderImpl();
+        final var diagStatusService = mock(DiagStatusService.class);
+        doReturn(mock(ServiceRegistration.class)).when(diagStatusService).register(any());
+
+        switchConnectionProvider = new SwitchConnectionProviderImpl(diagStatusService, connConfig);
         switchConnectionProvider.setSwitchConnectionHandler(mockPlugin);
-        switchConnectionProvider.setConfiguration(connConfig);
         switchConnectionProvider.startup().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
         if (protocol.equals(TransportProtocol.TCP) || protocol.equals(TransportProtocol.TLS)) {
             final TcpHandler tcpHandler = (TcpHandler) switchConnectionProvider.getServerFacade();
@@ -94,18 +119,14 @@ public class IntegrationTest {
         }
     }
 
-    /**
-     * @throws Exception
-     */
     @After
-    public void tearDown() throws Exception {
+    public void tearDown() {
         switchConnectionProvider.close();
         LOGGER.debug("\n ending test -------------------------------");
     }
 
     /**
-     * Library integration and communication test with handshake
-     * @throws Exception
+     * Library integration and communication test with handshake.
      */
     @Test
     public void testHandshake() throws Exception {
@@ -113,7 +134,8 @@ public class IntegrationTest {
         final int amountOfCLients = 1;
         final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
         final ScenarioHandler handler = new ScenarioHandler(scenario);
-        final List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.TCP, ClientType.SIMPLE);
+        final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
+                TransportProtocol.TCP, ClientType.SIMPLE);
         final OFClient firstClient = clients.get(0);
         firstClient.getScenarioDone().get();
         Thread.sleep(1000);
@@ -122,8 +144,7 @@ public class IntegrationTest {
     }
 
     /**
-     * Library integration and secured communication test with handshake
-     * @throws Exception
+     * Library integration and secured communication test with handshake.
      */
     @Test
     public void testTlsHandshake() throws Exception {
@@ -131,7 +152,8 @@ public class IntegrationTest {
         final int amountOfCLients = 1;
         final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
         final ScenarioHandler handler = new ScenarioHandler(scenario);
-        final List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.TLS, ClientType.SIMPLE);
+        final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
+                TransportProtocol.TLS, ClientType.SIMPLE);
         final OFClient firstClient = clients.get(0);
         firstClient.getScenarioDone().get();
         Thread.sleep(1000);
@@ -140,8 +162,7 @@ public class IntegrationTest {
     }
 
     /**
-     * Library integration and communication test with handshake + echo exchange
-     * @throws Exception
+     * Library integration and communication test with handshake + echo exchange.
      */
     @Test
     public void testHandshakeAndEcho() throws Exception {
@@ -153,7 +174,8 @@ public class IntegrationTest {
         scenario.addFirst(new SleepEvent(1000));
         scenario.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
         final ScenarioHandler handler = new ScenarioHandler(scenario);
-        final List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.TCP, ClientType.SIMPLE);
+        final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
+                TransportProtocol.TCP, ClientType.SIMPLE);
         final OFClient firstClient = clients.get(0);
         firstClient.getScenarioDone().get();
 
@@ -161,8 +183,7 @@ public class IntegrationTest {
     }
 
     /**
-     * Library integration and secured communication test with handshake + echo exchange
-     * @throws Exception
+     * Library integration and secured communication test with handshake + echo exchange.
      */
     @Test
     public void testTlsHandshakeAndEcho() throws Exception {
@@ -174,7 +195,8 @@ public class IntegrationTest {
         scenario.addFirst(new SleepEvent(1000));
         scenario.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
         final ScenarioHandler handler = new ScenarioHandler(scenario);
-        final List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.TLS, ClientType.SIMPLE);
+        final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
+                TransportProtocol.TLS, ClientType.SIMPLE);
         final OFClient firstClient = clients.get(0);
         firstClient.getScenarioDone().get();
 
@@ -182,8 +204,7 @@ public class IntegrationTest {
     }
 
     /**
-     * Library udp integration and communication test with handshake + echo exchange
-     * @throws Exception
+     * Library udp integration and communication test with handshake + echo exchange.
      */
     @Test
     public void testUdpHandshakeAndEcho() throws Exception {
@@ -195,7 +216,8 @@ public class IntegrationTest {
         scenario.addFirst(new SleepEvent(1000));
         scenario.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
         final ScenarioHandler handler = new ScenarioHandler(scenario);
-        final List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.UDP, ClientType.SIMPLE);
+        final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
+                TransportProtocol.UDP, ClientType.SIMPLE);
         final OFClient firstClient = clients.get(0);
         firstClient.getScenarioDone().get();
 
@@ -203,8 +225,7 @@ public class IntegrationTest {
     }
 
     /**
-     * Library integration and communication test (with virtual machine)
-     * @throws Exception
+     * Library integration and communication test (with virtual machine).
      */
     //@Test
     public void testCommunicationWithVM() throws Exception {
@@ -212,13 +233,16 @@ public class IntegrationTest {
     }
 
     /**
-     * @param amountOfCLients
+     * Creates and start a client.
+     *
+     * @param amountOfCLients number of clients
      * @param protocol true if encrypted connection should be used
      * @return new clients up and running
      * @throws ExecutionException if some client could not start
      */
     private List<OFClient> createAndStartClient(final int amountOfCLients, final ScenarioHandler scenarioHandler,
-            final TransportProtocol protocol, final ClientType clientType) throws ExecutionException {
+            final TransportProtocol protocol, final ClientType clientType)
+                    throws ExecutionException, InterruptedException, TimeoutException {
         final List<OFClient> clientsHorde = new ArrayList<>();
         for (int i = 0; i < amountOfCLients; i++) {
             LOGGER.debug("startup address in createclient: {}", startupAddress.getHostAddress());
@@ -245,23 +269,15 @@ public class IntegrationTest {
             sc.setScenarioHandler(scenarioHandler);
             clientsHorde.add(sc);
             //sc.run();
-            t = new Thread(sc);
-            t.start();
+            thread = new Thread(sc);
+            thread.start();
         }
         for (final OFClient sc : clientsHorde) {
-            try {
-                sc.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
-            } catch (final Exception e) {
-                LOGGER.error("createAndStartClient: Something borked ... ", e.getMessage(), e);
-                throw new ExecutionException(e);
-            }
+            sc.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
         }
         return clientsHorde;
     }
 
-    /**
-     * @throws Exception
-     */
     @Test
     public void testInitiateConnection() throws Exception {
         setUp(TransportProtocol.TCP);