Modernize SwitchConnectionProviderImplTest 71/100471/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 7 Apr 2022 12:47:51 +0000 (14:47 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 7 Apr 2022 12:47:51 +0000 (14:47 +0200)
There is a ton of things wrong here, modernize the test. We will revisit
it soon for JDK17 compatibility.

Change-Id: I741fd6bf8fed7499b4c06055c085fc1abcadea23
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
openflowjava/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/connection/SwitchConnectionProviderImplTest.java

index fee78164daae887f0450475eeebc2db27b0f46e5..8303d3e1d49bc469781f20a9cb7486d0e99079d6 100644 (file)
@@ -5,17 +5,20 @@
  * 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.impl.core.connection;
 
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
+
 import com.google.common.util.concurrent.ListenableFuture;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.util.List;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
-import org.junit.Assert;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
@@ -29,7 +32,6 @@ import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionPro
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.KeystoreType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.TransportProtocol;
-import org.slf4j.LoggerFactory;
 
 /**
  * Unit tests for SwitchConnectionProviderImpl.
@@ -84,11 +86,11 @@ public class SwitchConnectionProviderImplTest {
     public void testStartup1() throws UnknownHostException {
         startUp(null);
         final ListenableFuture<Boolean> future = provider.startup();
-        try {
-            future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
-        } catch (InterruptedException | ExecutionException | TimeoutException e) {
-            Assert.assertEquals("Wrong state", "java.lang.NullPointerException", e.getMessage());
-        }
+
+        final var cause = assertThrows(ExecutionException.class, () -> future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS))
+            .getCause();
+        assertThat(cause, instanceOf(NullPointerException.class));
+        assertEquals(null, cause.getMessage());
     }
 
     /**
@@ -99,11 +101,11 @@ public class SwitchConnectionProviderImplTest {
         startUp(null);
         provider.setSwitchConnectionHandler(handler);
         final ListenableFuture<Boolean> future = provider.startup();
-        try {
-            future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
-        } catch (InterruptedException | ExecutionException | TimeoutException e) {
-            Assert.assertEquals("Wrong state", "java.lang.NullPointerException", e.getMessage());
-        }
+
+        final var cause = assertThrows(ExecutionException.class, () -> future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS))
+            .getCause();
+        assertThat(cause, instanceOf(NullPointerException.class));
+        assertEquals(null, cause.getMessage());
     }
 
     /**
@@ -113,69 +115,55 @@ public class SwitchConnectionProviderImplTest {
     public void testStartup3() throws UnknownHostException {
         startUp(TransportProtocol.TCP);
         final ListenableFuture<Boolean> future = provider.startup();
-        try {
-            future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
-        } catch (InterruptedException | ExecutionException | TimeoutException e) {
-            Assert.assertEquals("Wrong state", "java.lang.IllegalStateException:"
-                    + " SwitchConnectionHandler is not set", e.getMessage());
-        }
+
+        final var cause = assertThrows(ExecutionException.class, () -> future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS))
+            .getCause();
+        assertThat(cause, instanceOf(IllegalStateException.class));
+        assertEquals("SwitchConnectionHandler is not set", cause.getMessage());
     }
 
     /**
      * Tests correct provider startup - over TCP.
      */
     @Test
-    public void testStartup4() throws UnknownHostException {
+    public void testStartup4() throws Exception {
         startUp(TransportProtocol.TCP);
         provider.setSwitchConnectionHandler(handler);
-        try {
-            Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
-        } catch (InterruptedException | ExecutionException | TimeoutException e) {
-            Assert.fail();
-        }
+
+        assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
     }
 
     /**
      * Tests correct provider startup - over TLS.
      */
     @Test
-    public void testStartup5() throws UnknownHostException {
+    public void testStartup5() throws Exception {
         startUp(TransportProtocol.TLS);
         provider.setSwitchConnectionHandler(handler);
-        try {
-            Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
-        } catch (InterruptedException | ExecutionException | TimeoutException e) {
-            Assert.fail();
-        }
+
+        assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
     }
 
     /**
      * Tests correct provider startup - over UDP.
      */
     @Test
-    public void testStartup6() throws UnknownHostException {
+    public void testStartup6() throws Exception {
         startUp(TransportProtocol.UDP);
         provider.setSwitchConnectionHandler(handler);
-        try {
-            Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
-        } catch (InterruptedException | ExecutionException | TimeoutException e) {
-            Assert.fail();
-        }
+
+        assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
     }
 
     /**
      * Tests correct provider shutdown.
      */
     @Test
-    public void testShutdown() throws UnknownHostException {
+    public void testShutdown() throws Exception {
         startUp(TransportProtocol.TCP);
         provider.setSwitchConnectionHandler(handler);
-        try {
-            Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
-            Assert.assertTrue("Failed to stop", provider.shutdown().get(5 * WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
-        } catch (InterruptedException | ExecutionException | TimeoutException e) {
-            LoggerFactory.getLogger(SwitchConnectionProviderImplTest.class).error("Unexpected error", e);
-        }
-    }
 
+        assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
+        assertTrue("Failed to stop", provider.shutdown().get(5 * WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
+    }
 }