From e3261ca20db3ad074c2c548c1a29f3e004e79f22 Mon Sep 17 00:00:00 2001 From: Marian Adamjak Date: Fri, 17 Oct 2014 14:26:41 +0200 Subject: [PATCH] Add unit test - fully covered SwitchConnectionProviderImpl, ConnectionAdapterImpl Change-Id: I038649bd02d95ab8b37888f3a3c22daeaac99caa Signed-off-by: Marian Adamjak --- openflow-protocol-api/pom.xml | 4 + .../ExperimenterIdSerializerKey.java | 3 +- .../ExperimenterActionSerializerKeyTest.java | 14 +- .../ConnectionAdapterImp02lTest.java | 206 +++++++++++++++ .../connection/ConnectionAdapterImplTest.java | 8 +- .../SwitchConnectionProviderImpl02Test.java | 238 ++++++++++++++++++ .../SwitchConnectionProviderImplTest.java | 1 + .../impl/connection/UdpHandlerTest.java | 98 ++++++++ .../EchoInputMessageFactoryTest.java | 6 +- .../EchoReplyInputMessageFactoryTest.java | 3 +- 10 files changed, 562 insertions(+), 19 deletions(-) create mode 100644 openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/connection/ConnectionAdapterImp02lTest.java create mode 100644 openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/connection/SwitchConnectionProviderImpl02Test.java create mode 100644 openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/connection/UdpHandlerTest.java diff --git a/openflow-protocol-api/pom.xml b/openflow-protocol-api/pom.xml index 5d5bfb1e..6b580791 100644 --- a/openflow-protocol-api/pom.xml +++ b/openflow-protocol-api/pom.xml @@ -129,5 +129,9 @@ junit test + + org.mockito + mockito-all + diff --git a/openflow-protocol-api/src/main/java/org/opendaylight/openflowjava/protocol/api/keys/experimenter/ExperimenterIdSerializerKey.java b/openflow-protocol-api/src/main/java/org/opendaylight/openflowjava/protocol/api/keys/experimenter/ExperimenterIdSerializerKey.java index 1e9325e9..47d3608a 100644 --- a/openflow-protocol-api/src/main/java/org/opendaylight/openflowjava/protocol/api/keys/experimenter/ExperimenterIdSerializerKey.java +++ b/openflow-protocol-api/src/main/java/org/opendaylight/openflowjava/protocol/api/keys/experimenter/ExperimenterIdSerializerKey.java @@ -15,7 +15,8 @@ import org.opendaylight.yangtools.yang.binding.DataObject; * @author michal.polkorab * @param class of object to be serialized */ -public class ExperimenterIdSerializerKey extends MessageTypeKey { +public class ExperimenterIdSerializerKey extends MessageTypeKey + implements ExperimenterSerializerKey { private Long experimenterId; diff --git a/openflow-protocol-api/src/test/java/org/opendaylight/openflowjava/protocol/api/keys/experimenter/ExperimenterActionSerializerKeyTest.java b/openflow-protocol-api/src/test/java/org/opendaylight/openflowjava/protocol/api/keys/experimenter/ExperimenterActionSerializerKeyTest.java index 657a34c3..761698d3 100644 --- a/openflow-protocol-api/src/test/java/org/opendaylight/openflowjava/protocol/api/keys/experimenter/ExperimenterActionSerializerKeyTest.java +++ b/openflow-protocol-api/src/test/java/org/opendaylight/openflowjava/protocol/api/keys/experimenter/ExperimenterActionSerializerKeyTest.java @@ -19,6 +19,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev1 */ public class ExperimenterActionSerializerKeyTest { + /** * Test ExperimenterActionSerializerKey equals and hashCode */ @@ -49,24 +50,17 @@ public class ExperimenterActionSerializerKeyTest { @Test public void testEquals() { - ExperimenterActionSerializerKey key1; ExperimenterActionSerializerKey key2; - key1 = new ExperimenterActionSerializerKey(EncodeConstants.OF10_VERSION_ID, 42L, null); - Assert.assertTrue("Wrong equal to identical object.", key1.equals(key1)); - Assert.assertFalse("Wrong equal to different class.", key1.equals(new Object())); - key2 = new ExperimenterActionSerializerKey(EncodeConstants.OF10_VERSION_ID, 42L, TestSubType2.class); Assert.assertFalse("Wrong equal by actionSubType.", key1.equals(key2)); - key1 = new ExperimenterActionSerializerKey(EncodeConstants.OF10_VERSION_ID, 42L, TestSubType.class); Assert.assertFalse("Wrong equal by actionSubType.", key1.equals(key2)); - } - - + + private static class TestSubType extends ExperimenterActionSubType { // empty class - only used in test for comparation } @@ -74,4 +68,6 @@ public class ExperimenterActionSerializerKeyTest { private static class TestSubType2 extends ExperimenterActionSubType { // empty class - only used in test for comparation } + + } \ No newline at end of file diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/connection/ConnectionAdapterImp02lTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/connection/ConnectionAdapterImp02lTest.java new file mode 100644 index 00000000..e9645cf8 --- /dev/null +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/connection/ConnectionAdapterImp02lTest.java @@ -0,0 +1,206 @@ +/* + * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * 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.connection; + +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelOutboundHandlerAdapter; +import io.netty.channel.ChannelPromise; +import io.netty.channel.embedded.EmbeddedChannel; +import java.net.InetSocketAddress; +import java.util.concurrent.TimeUnit; +import junit.framework.Assert; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.TableModInput; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.RemovalListener; +import com.google.common.cache.RemovalNotification; + +/** + * @author madamjak + * @author michal.polkorab + */ +public class ConnectionAdapterImp02lTest { + private static final int RPC_RESPONSE_EXPIRATION = 1; + private static final RemovalListener> REMOVAL_LISTENER = + new RemovalListener>() { + @Override + public void onRemoval( + final RemovalNotification> notification) { + notification.getValue().discard(); + } + }; + + @Mock EchoInput echoInput; + @Mock BarrierInput barrierInput; + @Mock EchoReplyInput echoReplyInput; + @Mock ExperimenterInput experimenterInput; + @Mock FlowModInput flowModInput; + @Mock GetConfigInput getConfigInput; + @Mock GetFeaturesInput getFeaturesInput; + @Mock GetQueueConfigInput getQueueConfigInput; + @Mock GroupModInput groupModInput; + @Mock HelloInput helloInput; + @Mock MeterModInput meterModInput; + @Mock PacketOutInput packetOutInput; + @Mock MultipartRequestInput multipartRequestInput; + @Mock PortModInput portModInput; + @Mock RoleRequestInput roleRequestInput; + @Mock SetConfigInput setConfigInput; + @Mock TableModInput tableModInput; + @Mock GetAsyncInput getAsyncInput; + @Mock SetAsyncInput setAsyncInput; + private ConnectionAdapterImpl adapter; + private Cache> cache; + private OfHeader responseOfCall; + /** + * Initialize mocks + */ + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + } + /** + * Disconnect adapter + */ + @After + public void tierDown(){ + if (adapter != null && adapter.isAlive()) { + adapter.disconnect(); + } + } + /** + * Test Rpc Calls + */ + @Test + public void testRcp() { + EmbeddedChannel embChannel = new EmbeddedChannel(new EmbededChannelHandler()); + adapter = new ConnectionAdapterImpl(embChannel,InetSocketAddress.createUnresolved("localhost", 9876)); + cache = CacheBuilder.newBuilder().concurrencyLevel(1).expireAfterWrite(RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES) + .removalListener(REMOVAL_LISTENER).build(); + adapter.setResponseCache(cache); + // -- barrier + adapter.barrier(barrierInput); + embChannel.runPendingTasks(); + Assert.assertEquals("Wrong - barrier", barrierInput, responseOfCall); + // -- echo + adapter.echo(echoInput); + embChannel.runPendingTasks(); + Assert.assertEquals("Wrong - echo", echoInput, responseOfCall); + // -- echoReply + adapter.echoReply(echoReplyInput); + embChannel.runPendingTasks(); + Assert.assertEquals("Wrong - echoReply",echoReplyInput, responseOfCall); + // -- experimenter + adapter.experimenter(experimenterInput); + embChannel.runPendingTasks(); + Assert.assertEquals("Wrong - experimenter",experimenterInput, responseOfCall); + // -- flowMod + adapter.flowMod(flowModInput); + embChannel.runPendingTasks(); + Assert.assertEquals("Wrong - flowMod", flowModInput, responseOfCall); + // -- getConfig + adapter.getConfig(getConfigInput); + embChannel.runPendingTasks(); + Assert.assertEquals("Wrong - getConfig", getConfigInput, responseOfCall); + // -- getFeatures + adapter.getFeatures(getFeaturesInput); + embChannel.runPendingTasks(); + Assert.assertEquals("Wrong - getFeatures",getFeaturesInput, responseOfCall); + // -- getQueueConfig + adapter.getQueueConfig(getQueueConfigInput); + embChannel.runPendingTasks(); + Assert.assertEquals("Wrong - getQueueConfig",getQueueConfigInput, responseOfCall); + // -- groupMod + adapter.groupMod(groupModInput); + embChannel.runPendingTasks(); + Assert.assertEquals("Wrong - groupMod", groupModInput, responseOfCall); + // -- hello + adapter.hello(helloInput); + embChannel.runPendingTasks(); + Assert.assertEquals("Wrong - helloInput",helloInput, responseOfCall); + // -- meterMod + adapter.meterMod(meterModInput); + embChannel.runPendingTasks(); + Assert.assertEquals("Wrong - meterMod",meterModInput, responseOfCall); + // -- packetOut + adapter.packetOut(packetOutInput); + embChannel.runPendingTasks(); + Assert.assertEquals("Wrong - packetOut",packetOutInput, responseOfCall); + // -- multipartRequest + adapter.multipartRequest(multipartRequestInput); + embChannel.runPendingTasks(); + Assert.assertEquals("Wrong - multipartRequest", multipartRequestInput, responseOfCall); + // -- portMod + adapter.portMod(portModInput); + embChannel.runPendingTasks(); + Assert.assertEquals("Wrong - portMod", portModInput, responseOfCall); + // -- roleRequest + adapter.roleRequest(roleRequestInput); + embChannel.runPendingTasks(); + Assert.assertEquals("Wrong - roleRequest", roleRequestInput, responseOfCall); + // -- setConfig + adapter.setConfig(setConfigInput); + embChannel.runPendingTasks(); + Assert.assertEquals("Wrong - setConfig",setConfigInput, responseOfCall); + // -- tableMod + adapter.tableMod(tableModInput); + embChannel.runPendingTasks(); + Assert.assertEquals("Wrong - tableMod", tableModInput, responseOfCall); + // -- getAsync + adapter.getAsync(getAsyncInput); + embChannel.runPendingTasks(); + Assert.assertEquals("Wrong - getAsync", getAsyncInput, responseOfCall); + // -- setAsync + adapter.setAsync(setAsyncInput); + embChannel.runPendingTasks(); + Assert.assertEquals("Wrong - setAsync", setAsyncInput, responseOfCall); + adapter.disconnect(); + } + + /** + * Channel Handler for testing + * @author madamjak + * + */ + private class EmbededChannelHandler extends ChannelOutboundHandlerAdapter { + @Override + public void write(ChannelHandlerContext ctx, Object msg, + ChannelPromise promise) throws Exception { + responseOfCall = null; + if(msg instanceof MessageListenerWrapper){ + MessageListenerWrapper listener = (MessageListenerWrapper) msg; + OfHeader ofHeader = listener.getMsg(); + responseOfCall = ofHeader; + } + } + } +} diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/connection/ConnectionAdapterImplTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/connection/ConnectionAdapterImplTest.java index 65cbd6a8..80d53dd4 100644 --- a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/connection/ConnectionAdapterImplTest.java +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/connection/ConnectionAdapterImplTest.java @@ -83,7 +83,8 @@ public class ConnectionAdapterImplTest { @Mock SystemNotificationsListener systemListener; @Mock ConnectionReadyListener readyListener; @Mock Cache> mockCache; - @Mock ChannelFuture channelFuture; + @Mock ChannelFuture channelFuture; + private ConnectionAdapterImpl adapter; private Cache> cache; @@ -189,11 +190,11 @@ public class ConnectionAdapterImplTest { Assert.assertEquals("Wrong - diffrence between channel.isOpen() and ConnectionAdapterImpl.isAlive()", channel.isOpen(), connAddapter.isAlive()); connAddapter.disconnect(); - Assert.assertFalse("Wrong - ConnectionAdapterImpl can not be aliva after disconnet.", connAddapter.isAlive()); + Assert.assertFalse("Wrong - ConnectionAdapterImpl can not be alive after disconnet.", connAddapter.isAlive()); } /** - * Test throw exception if no listener are present + * Test throw exception if no listeners are present */ @Test(expected = java.lang.IllegalStateException.class) public void testMissingListeners(){ @@ -206,5 +207,4 @@ public class ConnectionAdapterImplTest { connAddapter.setConnectionReadyListener(null); connAddapter.checkListeners(); } - } diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/connection/SwitchConnectionProviderImpl02Test.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/connection/SwitchConnectionProviderImpl02Test.java new file mode 100644 index 00000000..e0fecc27 --- /dev/null +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/connection/SwitchConnectionProviderImpl02Test.java @@ -0,0 +1,238 @@ +/* + * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * 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.connection; + +import java.net.InetAddress; +import java.net.UnknownHostException; + +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler; +import org.opendaylight.openflowjava.protocol.api.connection.TlsConfiguration; +import org.opendaylight.openflowjava.protocol.api.connection.TlsConfigurationImpl; +import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer; +import org.opendaylight.openflowjava.protocol.api.extensibility.OFGeneralDeserializer; +import org.opendaylight.openflowjava.protocol.api.extensibility.OFGeneralSerializer; +import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer; +import org.opendaylight.openflowjava.protocol.api.keys.MatchEntryDeserializerKey; +import org.opendaylight.openflowjava.protocol.api.keys.MatchEntrySerializerKey; +import org.opendaylight.openflowjava.protocol.api.keys.experimenter.ExperimenterActionDeserializerKey; +import org.opendaylight.openflowjava.protocol.api.keys.experimenter.ExperimenterActionSerializerKey; +import org.opendaylight.openflowjava.protocol.api.keys.experimenter.ExperimenterIdDeserializerKey; +import org.opendaylight.openflowjava.protocol.api.keys.experimenter.ExperimenterIdSerializerKey; +import org.opendaylight.openflowjava.protocol.api.keys.experimenter.ExperimenterInstructionDeserializerKey; +import org.opendaylight.openflowjava.protocol.api.keys.experimenter.ExperimenterInstructionSerializerKey; +import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants; +import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.ExperimenterActionSubType; +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.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.InPort; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OpenflowBasicClass; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.MeterBandExperimenterCase; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestExperimenterCase; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.queue.property.header.QueueProperty; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.table.features.properties.grouping.TableFeatureProperties; + +import com.google.common.util.concurrent.ListenableFuture; + +/** + * @author madamjak + * @author michal.polkorab + */ +public class SwitchConnectionProviderImpl02Test { + @Mock SwitchConnectionHandler handler; + @Mock OFGeneralSerializer serializer; + @Mock OFGeneralDeserializer deserializer; + @Mock OFDeserializer deserializerError; + @Mock OFDeserializer deserializerExpMsg; + @Mock OFDeserializer deserializerMultipartRplMsg; + @Mock OFDeserializer deserializerQueueProperty; + @Mock OFDeserializer deserializerMeterBandExpCase; + @Mock OFSerializer serializerExperimenterInput; + @Mock OFSerializer serializerMultipartRequestExpCase; + @Mock OFSerializer serializerMeterBandExpCase; + private static final int SWITCH_IDLE_TIMEOUT = 2000; + private InetAddress startupAddress; + private TlsConfiguration tlsConfiguration; + private SwitchConnectionProviderImpl provider; + private ConnectionConfigurationImpl config; + + /** + * Creates new {@link SwitchConnectionProvider} instance for each test + * @param protocol communication protocol + */ + public void startUp(TransportProtocol protocol) { + MockitoAnnotations.initMocks(this); + config = null; + if (protocol != null) { + createConfig(protocol); + } + provider = new SwitchConnectionProviderImpl(); + } + + private void createConfig(TransportProtocol protocol) { + try { + startupAddress = InetAddress.getLocalHost(); + } catch (UnknownHostException e) { + e.printStackTrace(); + } + tlsConfiguration = null; + if (protocol.equals(TransportProtocol.TLS)) { + tlsConfiguration = new TlsConfigurationImpl(KeystoreType.JKS, + "/selfSignedSwitch", PathType.CLASSPATH, KeystoreType.JKS, + "/selfSignedController", PathType.CLASSPATH) ; + } + config = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration, SWITCH_IDLE_TIMEOUT); + config.setTransferProtocol(protocol); + } + + + /** + * Test getServerFacade + */ + @Test + public void testServerFacade(){ + startUp(TransportProtocol.TCP); + provider.setConfiguration(config); + ListenableFuture future = provider.startup(); + ServerFacade serverFacade = provider.getServerFacade(); + Assert.assertNotNull("Wrong -- getServerFacade return null",serverFacade); + } + + /** + * Test unregister by wrong key + */ + @Test + public void testUnregisterWrongKeys(){ + startUp(TransportProtocol.TCP); + provider.setConfiguration(config); + ExperimenterInstructionSerializerKey testSerKey + = new ExperimenterInstructionSerializerKey(EncodeConstants.OF10_VERSION_ID,42L); + Assert.assertFalse("Wrong -- unregisterSerializer",provider.unregisterSerializer(testSerKey)); + ExperimenterInstructionDeserializerKey tesDeserKey + = new ExperimenterInstructionDeserializerKey(EncodeConstants.OF10_VERSION_ID,24L); + Assert.assertFalse("Wrong -- unregisterDeserializer",provider.unregisterDeserializer(tesDeserKey)); + } + + /** + * Test register and unregister method + */ + @Test + public void testUnregisterExistingKeys(){ + startUp(TransportProtocol.TCP); + provider.setConfiguration(config); + // -- registerActionSerializer + ExperimenterActionSerializerKey key1 + = new ExperimenterActionSerializerKey(EncodeConstants.OF10_VERSION_ID, 42L, TestSubType.class); + provider.registerActionSerializer(key1, serializer); + Assert.assertTrue("Wrong -- unregister ActionSerializer", provider.unregisterSerializer(key1)); + Assert.assertFalse("Wrong -- unregister ActionSerializer by not existing key", provider.unregisterSerializer(key1)); + // -- registerActionDeserializer + ExperimenterActionDeserializerKey key2 + = new ExperimenterActionDeserializerKey(EncodeConstants.OF10_VERSION_ID, 42L); + provider.registerActionDeserializer(key2, deserializer); + Assert.assertTrue("Wrong -- unregister ActionDeserializer", provider.unregisterDeserializer(key2)); + Assert.assertFalse("Wrong -- unregister ActionDeserializer by not existing key", provider.unregisterDeserializer(key2)); + // -- registerInstructionSerializer + ExperimenterInstructionSerializerKey key3 + = new ExperimenterInstructionSerializerKey(EncodeConstants.OF10_VERSION_ID,42L); + provider.registerInstructionSerializer(key3, serializer); + Assert.assertTrue("Wrong -- unregister InstructionSerializer", provider.unregisterSerializer(key3)); + Assert.assertFalse("Wrong -- unregister InstructionSerializer by not existing key", provider.unregisterSerializer(key3)); + // -- registerInstructionDeserializer + ExperimenterInstructionDeserializerKey key4 + = new ExperimenterInstructionDeserializerKey(EncodeConstants.OF10_VERSION_ID,42L); + provider.registerInstructionDeserializer(key4, deserializer); + Assert.assertTrue("Wrong -- unregister InstructionDeserializer", provider.unregisterDeserializer(key4)); + Assert.assertFalse("Wrong -- unregister InstructionDeserializer by not existing key", provider.unregisterDeserializer(key4)); + // -- registerMatchEntryDeserializer + MatchEntryDeserializerKey key5 + = new MatchEntryDeserializerKey(EncodeConstants.OF10_VERSION_ID, 0x8000, 42); + provider.registerMatchEntryDeserializer(key5, deserializer); + Assert.assertTrue("Wrong -- unregister MatchEntryDeserializer", provider.unregisterDeserializer(key5)); + Assert.assertFalse("Wrong -- unregister MatchEntryDeserializer by not existing key", provider.unregisterDeserializer(key5)); + // -- registerErrorDeserializer + ExperimenterIdDeserializerKey key6 + = new ExperimenterIdDeserializerKey(EncodeConstants.OF10_VERSION_ID, 42L, ErrorMessage.class); + provider.registerErrorDeserializer(key6, deserializerError); + Assert.assertTrue("Wrong -- unregister ErrorDeserializer", provider.unregisterDeserializer(key6)); + Assert.assertFalse("Wrong -- unregister ErrorDeserializer by not existing key", provider.unregisterDeserializer(key6)); + // -- registerExperimenterMessageDeserializer + ExperimenterIdDeserializerKey key7 + = new ExperimenterIdDeserializerKey(EncodeConstants.OF10_VERSION_ID, 42L, ExperimenterMessage.class); + provider.registerExperimenterMessageDeserializer(key7, deserializerExpMsg); + Assert.assertTrue("Wrong -- unregister ExperimenterMessageDeserializer", provider.unregisterDeserializer(key7)); + Assert.assertFalse("Wrong -- unregister ExperimenterMessageDeserializer by not existing key", provider.unregisterDeserializer(key7)); + // -- registerMultipartReplyMessageDeserializer + ExperimenterIdDeserializerKey key8 + = new ExperimenterIdDeserializerKey(EncodeConstants.OF10_VERSION_ID, 42L, MultipartReplyMessage.class); + provider.registerMultipartReplyMessageDeserializer(key8, deserializerMultipartRplMsg); + Assert.assertTrue("Wrong -- unregister MultipartReplyMessageDeserializer", provider.unregisterDeserializer(key8)); + Assert.assertFalse("Wrong -- unregister MultipartReplyMessageDeserializer by not existing key", provider.unregisterDeserializer(key8)); + // -- registerMultipartReplyTFDeserializer + ExperimenterIdDeserializerKey key9 = + new ExperimenterIdDeserializerKey(EncodeConstants.OF10_VERSION_ID, 42L, MultipartReplyMessage.class); + provider.registerMultipartReplyTFDeserializer(key9, deserializer); + Assert.assertTrue("Wrong -- unregister MultipartReplyTFDeserializer", provider.unregisterDeserializer(key9)); + Assert.assertFalse("Wrong -- unregister MultipartReplyTFDeserializer by non existing key", provider.unregisterDeserializer(key9)); + // -- registerQueuePropertyDeserializer + ExperimenterIdDeserializerKey key10 + = new ExperimenterIdDeserializerKey(EncodeConstants.OF10_VERSION_ID, 42L, QueueProperty.class); + provider.registerQueuePropertyDeserializer(key10, deserializerQueueProperty); + Assert.assertTrue("Wrong -- unregister QueuePropertyDeserializer", provider.unregisterDeserializer(key10)); + Assert.assertFalse("Wrong -- unregister QueuePropertyDeserializer by not existing key", provider.unregisterDeserializer(key10)); + // -- registerMeterBandDeserializer + ExperimenterIdDeserializerKey key11 + = new ExperimenterIdDeserializerKey(EncodeConstants.OF10_VERSION_ID, 42L, MeterBandExperimenterCase.class); + provider.registerMeterBandDeserializer(key11, deserializerMeterBandExpCase); + Assert.assertTrue("Wrong -- unregister MeterBandDeserializer", provider.unregisterDeserializer(key11)); + Assert.assertFalse("Wrong -- unregister MeterBandDeserializer by not existing key", provider.unregisterDeserializer(key11)); + // -- registerExperimenterMessageSerializer + ExperimenterIdSerializerKey key12 + = new ExperimenterIdSerializerKey<>(EncodeConstants.OF10_VERSION_ID,42L,ExperimenterInput.class); + provider.registerExperimenterMessageSerializer(key12, serializerExperimenterInput); + Assert.assertTrue("Wrong -- unregister ExperimenterMessageSerializer", provider.unregisterSerializer(key12)); + Assert.assertFalse("Wrong -- unregister ExperimenterMessageSerializer by not existing key", provider.unregisterSerializer(key12)); + //registerMultipartRequestSerializer + ExperimenterIdSerializerKey key13 + = new ExperimenterIdSerializerKey<>(EncodeConstants.OF10_VERSION_ID,42L,MultipartRequestExperimenterCase.class); + provider.registerMultipartRequestSerializer(key13, serializerMultipartRequestExpCase); + Assert.assertTrue("Wrong -- unregister MultipartRequestSerializer", provider.unregisterSerializer(key13)); + Assert.assertFalse("Wrong -- unregister MultipartRequestSerializer by not existing key", provider.unregisterSerializer(key13)); + // -- registerMultipartRequestTFSerializer + ExperimenterIdSerializerKey key14 + = new ExperimenterIdSerializerKey<>(EncodeConstants.OF10_VERSION_ID,42L,TableFeatureProperties.class); + provider.registerMultipartRequestTFSerializer(key14, serializer); + Assert.assertTrue("Wrong -- unregister MultipartRequestTFSerializer", provider.unregisterSerializer(key14)); + Assert.assertFalse("Wrong -- unregister MultipartRequestTFSerializer by not existing key", provider.unregisterSerializer(key14)); + // -- registerMeterBandSerializer + ExperimenterIdSerializerKey key15 + = new ExperimenterIdSerializerKey<>(EncodeConstants.OF10_VERSION_ID,42L,MeterBandExperimenterCase.class); + provider.registerMeterBandSerializer(key15, serializerMeterBandExpCase); + Assert.assertTrue("Wrong -- unregister MeterBandSerializer", provider.unregisterSerializer(key15)); + Assert.assertFalse("Wrong -- unregister MeterBandSerializer by not existing key", provider.unregisterSerializer(key15)); + // -- registerMatchEntrySerializer + MatchEntrySerializerKey key16 + = new MatchEntrySerializerKey<>(EncodeConstants.OF13_VERSION_ID, OpenflowBasicClass.class, InPort.class); + provider.registerMatchEntrySerializer(key16, serializer); + Assert.assertTrue("Wrong -- unregister MatchEntrySerializer", provider.unregisterSerializer(key16)); + Assert.assertFalse("Wrong -- unregister MatchEntrySerializer by not existing key", provider.unregisterSerializer(key15)); + } + + private static class TestSubType extends ExperimenterActionSubType { + // empty class - only used in test for comparation + } +} diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/connection/SwitchConnectionProviderImplTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/connection/SwitchConnectionProviderImplTest.java index 5c33b8d2..7a70f757 100644 --- a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/connection/SwitchConnectionProviderImplTest.java +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/connection/SwitchConnectionProviderImplTest.java @@ -177,4 +177,5 @@ public class SwitchConnectionProviderImplTest { e.printStackTrace(); } } + } \ No newline at end of file diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/connection/UdpHandlerTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/connection/UdpHandlerTest.java new file mode 100644 index 00000000..e6922c9a --- /dev/null +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/connection/UdpHandlerTest.java @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * 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.connection; + +import java.io.IOException; +import java.net.InetAddress; +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.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.opendaylight.openflowjava.protocol.impl.core.UdpChannelInitializer; +import org.opendaylight.openflowjava.protocol.impl.core.UdpHandler; + +import com.google.common.util.concurrent.ListenableFuture; + +/** + * @author madamjak + * + */ +public class UdpHandlerTest { + @Mock UdpChannelInitializer udpChannelInitializerMock; + UdpHandler udpHandler; + /** + * Mock init + */ + @Before + public void startUp() { + MockitoAnnotations.initMocks(this); + } + + /** + * Test to create UdpHandler with empty address and zero port + * @throws InterruptedException + * @throws ExecutionException + * @throws IOException + */ + @Test + public void testWithEmptyAddress() throws InterruptedException, ExecutionException, IOException { + udpHandler = new UdpHandler(null, 0); + udpHandler.setChannelInitializer(udpChannelInitializerMock); + Assert.assertTrue("Wrong - start server", startupServer()); + try { + Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS).booleanValue()); + } catch (TimeoutException e) { + Assert.fail("Wrong - getIsOnlineFuture timed out"); + } + Assert.assertFalse("Wrong - port has been set to zero", udpHandler.getPort() == 0); + shutdownServer(); + } + + /** + * Test to create UdpHandler with fill address and given port + * @throws InterruptedException + * @throws ExecutionException + * @throws IOException + */ + @Test + public void testWithAddressAndPort() throws InterruptedException, ExecutionException, IOException{ + int port = 9874; + udpHandler = new UdpHandler(InetAddress.getLocalHost(), port); + udpHandler.setChannelInitializer(udpChannelInitializerMock); + Assert.assertTrue("Wrong - start server", startupServer()); + try { + Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS).booleanValue()); + } catch (TimeoutException e) { + Assert.fail("Wrong - getIsOnlineFuture timed out"); + } + Assert.assertEquals("Wrong - bad port number has been set", port, udpHandler.getPort()); + shutdownServer(); + } + + private Boolean startupServer() throws InterruptedException, IOException, ExecutionException { + ListenableFuture online = udpHandler.getIsOnlineFuture(); + + (new Thread(udpHandler)).start(); + int retry = 0; + while (online.isDone() != true && retry++ < 20) { + Thread.sleep(100); + } + return online.isDone() ; + } + + private void shutdownServer() throws InterruptedException, ExecutionException { + ListenableFuture shutdownRet = udpHandler.shutdown() ; + while ( shutdownRet.isDone() != true ) + Thread.sleep(100) ; + Assert.assertTrue("Wrong - shutdown failed", shutdownRet.get()); + } +} diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/EchoInputMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/EchoInputMessageFactoryTest.java index ebe8351b..f9df80d2 100644 --- a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/EchoInputMessageFactoryTest.java +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/EchoInputMessageFactoryTest.java @@ -8,18 +8,18 @@ package org.opendaylight.openflowjava.protocol.impl.serialization.factories; -import org.junit.Assert; import io.netty.buffer.ByteBuf; import io.netty.buffer.UnpooledByteBufAllocator; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.opendaylight.openflowjava.protocol.api.extensibility.MessageTypeKey; import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer; import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry; +import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants; import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl; import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper; -import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder; @@ -88,7 +88,7 @@ public class EchoInputMessageFactoryTest { BufferHelper.checkHeaderV13(out, ECHO_REQUEST_MESSAGE_CODE_TYPE, 8+dataToTest.length); byte[] outData = new byte[dataToTest.length]; out.readBytes(outData); - Assert.assertArrayEquals(dataToTest, outData); + Assert.assertArrayEquals("Wrong - different output data.", dataToTest, outData); out.release(); } } diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/EchoReplyInputMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/EchoReplyInputMessageFactoryTest.java index 970bace0..64a5f078 100644 --- a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/EchoReplyInputMessageFactoryTest.java +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/EchoReplyInputMessageFactoryTest.java @@ -11,15 +11,14 @@ package org.opendaylight.openflowjava.protocol.impl.serialization.factories; import org.junit.Assert; import io.netty.buffer.ByteBuf; import io.netty.buffer.UnpooledByteBufAllocator; - import org.junit.Before; import org.junit.Test; import org.opendaylight.openflowjava.protocol.api.extensibility.MessageTypeKey; import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer; import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry; +import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants; import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl; import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper; -import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInputBuilder; -- 2.36.6