X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=openflowjava%2Fopenflow-protocol-impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fopenflowjava%2Fprotocol%2Fimpl%2Fcore%2Fconnection%2FSwitchConnectionProviderImplTest.java;h=f5fbe8bf36b15c1c2339a81e9cc3fe101ba3825e;hb=10ad385a19ce8d8ceed884b4ef9b8ff97cf17295;hp=4ebdd969838c5c1b763240a206ecbe01a51ce367;hpb=5aba6a0e908e9eca681da43d6db187f668b1e03b;p=openflowplugin.git diff --git a/openflowjava/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/connection/SwitchConnectionProviderImplTest.java b/openflowjava/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/connection/SwitchConnectionProviderImplTest.java index 4ebdd96983..f5fbe8bf36 100644 --- a/openflowjava/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/connection/SwitchConnectionProviderImplTest.java +++ b/openflowjava/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/connection/SwitchConnectionProviderImplTest.java @@ -5,21 +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.impl.core.connection; -import com.google.common.collect.Lists; -import com.google.common.util.concurrent.ListenableFuture; +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 static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; + 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.Before; import org.junit.Test; +import org.junit.runner.RunWith; import org.mockito.Mock; -import org.mockito.MockitoAnnotations; +import org.mockito.junit.MockitoJUnitRunner; import org.opendaylight.infrautils.diagstatus.DiagStatusService; +import org.opendaylight.infrautils.diagstatus.ServiceRegistration; import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler; import org.opendaylight.openflowjava.protocol.api.connection.TlsConfiguration; import org.opendaylight.openflowjava.protocol.api.connection.TlsConfigurationImpl; @@ -28,17 +36,17 @@ 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. * * @author michal.polkorab */ +@RunWith(MockitoJUnitRunner.class) public class SwitchConnectionProviderImplTest { @Mock SwitchConnectionHandler handler; - @Mock DiagStatusService diagStatusService; + @Mock DiagStatusService diagStatus; private static final int SWITCH_IDLE_TIMEOUT = 2000; private static final int WAIT_TIMEOUT = 2000; @@ -47,17 +55,21 @@ public class SwitchConnectionProviderImplTest { private SwitchConnectionProviderImpl provider; private ConnectionConfigurationImpl config; + @Before + public void before() { + doReturn(mock(ServiceRegistration.class)).when(diagStatus).register(any()); + } + /** * Creates new {@link SwitchConnectionProvider} instance for each test. * @param protocol communication protocol */ public void startUp(final TransportProtocol protocol) throws UnknownHostException { - MockitoAnnotations.initMocks(this); config = null; if (protocol != null) { createConfig(protocol); } - provider = new SwitchConnectionProviderImpl(config, diagStatusService); + provider = new SwitchConnectionProviderImpl(diagStatus, config); } private void createConfig(final TransportProtocol protocol) throws UnknownHostException { @@ -68,7 +80,7 @@ public class SwitchConnectionProviderImplTest { tlsConfiguration = new TlsConfigurationImpl(KeystoreType.JKS, "/selfSignedSwitch", PathType.CLASSPATH, KeystoreType.JKS, "/selfSignedController", PathType.CLASSPATH, - Lists.newArrayList("TLS_RSA_WITH_AES_128_CBC_SHA", "TLS_RSA_WITH_AES_128_CBC_SHA256")) ; + List.of("TLS_RSA_WITH_AES_128_CBC_SHA", "TLS_RSA_WITH_AES_128_CBC_SHA256")); } config = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration, SWITCH_IDLE_TIMEOUT, true, false, CHANNEL_OUTBOUND_QUEUE_SIZE); @@ -81,12 +93,12 @@ public class SwitchConnectionProviderImplTest { @Test public void testStartup1() throws UnknownHostException { startUp(null); - final ListenableFuture 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 future = provider.startup(); + + final var cause = assertThrows(ExecutionException.class, () -> future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS)) + .getCause(); + assertThat(cause, instanceOf(IllegalStateException.class)); + assertEquals("Connection not configured", cause.getMessage()); } /** @@ -96,12 +108,12 @@ public class SwitchConnectionProviderImplTest { public void testStartup2() throws UnknownHostException { startUp(null); provider.setSwitchConnectionHandler(handler); - final ListenableFuture 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 future = provider.startup(); + + final var cause = assertThrows(ExecutionException.class, () -> future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS)) + .getCause(); + assertThat(cause, instanceOf(IllegalStateException.class)); + assertEquals("Connection not configured", cause.getMessage()); } /** @@ -110,70 +122,56 @@ public class SwitchConnectionProviderImplTest { @Test public void testStartup3() throws UnknownHostException { startUp(TransportProtocol.TCP); - final ListenableFuture 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 future = provider.startup(); + + 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(); - } + + 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(); - } + + 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(); - } + + 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); - } - } + provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS); + assertTrue("Failed to stop", provider.shutdown().get(5 * WAIT_TIMEOUT, TimeUnit.MILLISECONDS)); + } }