--- /dev/null
+/*\r
+ * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.\r
+ *\r
+ * This program and the accompanying materials are made available under the\r
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
+ * and is available at http://www.eclipse.org/legal/epl-v10.html\r
+ */\r
+\r
+package org.opendaylight.openflowjava.protocol.impl.connection;\r
+\r
+import org.junit.Assert;\r
+import org.junit.Test;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInputBuilder;\r
+\r
+/**\r
+ * @author michal.polkorab\r
+ *\r
+ */\r
+public class MessageListenerWrapperTest {\r
+\r
+ /**\r
+ * Test MessageListenerWrapper creation\r
+ */\r
+ @Test\r
+ public void test() {\r
+ HelloInputBuilder builder = new HelloInputBuilder();\r
+ HelloInput hello = builder.build();\r
+ SimpleRpcListener listener = new SimpleRpcListener(hello, "Error");\r
+ MessageListenerWrapper wrapper = new MessageListenerWrapper(hello, listener);\r
+ Assert.assertEquals("Wrong message", hello, wrapper.getMsg());\r
+ Assert.assertEquals("Wrong listener", listener, wrapper.getListener());\r
+ }\r
+}
\ No newline at end of file
--- /dev/null
+/*\r
+ * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.\r
+ *\r
+ * This program and the accompanying materials are made available under the\r
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
+ * and is available at http://www.eclipse.org/legal/epl-v10.html\r
+ */\r
+\r
+package org.opendaylight.openflowjava.protocol.impl.connection;\r
+\r
+import java.net.InetAddress;\r
+import java.net.UnknownHostException;\r
+import java.util.concurrent.ExecutionException;\r
+import java.util.concurrent.TimeUnit;\r
+import java.util.concurrent.TimeoutException;\r
+\r
+import org.junit.Assert;\r
+import org.junit.Test;\r
+import org.mockito.Mock;\r
+import org.mockito.MockitoAnnotations;\r
+import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;\r
+import org.opendaylight.openflowjava.protocol.api.connection.TlsConfiguration;\r
+import org.opendaylight.openflowjava.protocol.api.connection.TlsConfigurationImpl;\r
+import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.KeystoreType;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.TransportProtocol;\r
+\r
+import com.google.common.util.concurrent.ListenableFuture;\r
+\r
+/**\r
+ * @author michal.polkorab\r
+ *\r
+ */\r
+public class SwitchConnectionProviderImplTest {\r
+\r
+ @Mock SwitchConnectionHandler handler;\r
+\r
+ private static final int SWITCH_IDLE_TIMEOUT = 2000;\r
+ private static final int WAIT_TIMEOUT = 2000;\r
+ private InetAddress startupAddress;\r
+ private TlsConfiguration tlsConfiguration;\r
+ private SwitchConnectionProviderImpl provider;\r
+ private ConnectionConfigurationImpl config;\r
+\r
+ /**\r
+ * Creates new {@link SwitchConnectionProvider} instance for each test\r
+ * @param protocol communication protocol\r
+ */\r
+ public void startUp(TransportProtocol protocol) {\r
+ MockitoAnnotations.initMocks(this);\r
+ config = null;\r
+ if (protocol != null) {\r
+ createConfig(protocol);\r
+ }\r
+ provider = new SwitchConnectionProviderImpl();\r
+ }\r
+\r
+ private void createConfig(TransportProtocol protocol) {\r
+ try {\r
+ startupAddress = InetAddress.getLocalHost();\r
+ } catch (UnknownHostException e) {\r
+ e.printStackTrace();\r
+ }\r
+ tlsConfiguration = null;\r
+ if (protocol.equals(TransportProtocol.TLS)) {\r
+ tlsConfiguration = new TlsConfigurationImpl(KeystoreType.JKS,\r
+ "/selfSignedSwitch", PathType.CLASSPATH, KeystoreType.JKS,\r
+ "/selfSignedController", PathType.CLASSPATH) ;\r
+ }\r
+ config = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration, SWITCH_IDLE_TIMEOUT);\r
+ config.setTransferProtocol(protocol);\r
+ }\r
+\r
+ /**\r
+ * Tests provider startup - without configuration and {@link SwitchConnectionHandler}\r
+ */\r
+ @Test\r
+ public void testStartup1() {\r
+ provider = new SwitchConnectionProviderImpl();\r
+ ListenableFuture<Boolean> future = provider.startup();\r
+ try {\r
+ future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);\r
+ } catch (InterruptedException | ExecutionException | TimeoutException e) {\r
+ Assert.assertEquals("Wrong state", "java.lang.NullPointerException", e.getMessage());\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Tests provider startup - without configuration\r
+ */\r
+ @Test\r
+ public void testStartup2() {\r
+ startUp(null);\r
+ provider.setSwitchConnectionHandler(handler);\r
+ ListenableFuture<Boolean> future = provider.startup();\r
+ try {\r
+ future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);\r
+ } catch (InterruptedException | ExecutionException | TimeoutException e) {\r
+ Assert.assertEquals("Wrong state", "java.lang.NullPointerException", e.getMessage());\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Tests provider startup - without {@link SwitchConnectionHandler}\r
+ */\r
+ @Test\r
+ public void testStartup3() {\r
+ startUp(TransportProtocol.TCP);\r
+ provider.setConfiguration(config);\r
+ ListenableFuture<Boolean> future = provider.startup();\r
+ try {\r
+ future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);\r
+ } catch (InterruptedException | ExecutionException | TimeoutException e) {\r
+ Assert.assertEquals("Wrong state", "java.lang.IllegalStateException:"\r
+ + " SwitchConnectionHandler is not set", e.getMessage());\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Tests correct provider startup - over TCP\r
+ */\r
+ @Test\r
+ public void testStartup4() {\r
+ startUp(TransportProtocol.TCP);\r
+ provider.setConfiguration(config);\r
+ provider.setSwitchConnectionHandler(handler);\r
+ try {\r
+ Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));\r
+ } catch (InterruptedException | ExecutionException | TimeoutException e) {\r
+ Assert.fail();\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Tests correct provider startup - over TLS\r
+ */\r
+ @Test\r
+ public void testStartup5() {\r
+ startUp(TransportProtocol.TLS);\r
+ provider.setConfiguration(config);\r
+ provider.setSwitchConnectionHandler(handler);\r
+ try {\r
+ Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));\r
+ } catch (InterruptedException | ExecutionException | TimeoutException e) {\r
+ Assert.fail();\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Tests correct provider startup - over UDP\r
+ */\r
+ @Test\r
+ public void testStartup6() {\r
+ startUp(TransportProtocol.UDP);\r
+ provider.setConfiguration(config);\r
+ provider.setSwitchConnectionHandler(handler);\r
+ try {\r
+ Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));\r
+ } catch (InterruptedException | ExecutionException | TimeoutException e) {\r
+ Assert.fail();\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Tests correct provider shutdown\r
+ */\r
+ @Test\r
+ public void testShutdown() {\r
+ startUp(TransportProtocol.TCP);\r
+ provider.setConfiguration(config);\r
+ provider.setSwitchConnectionHandler(handler);\r
+ try {\r
+ Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));\r
+ Assert.assertTrue("Failed to stop", provider.shutdown().get(5 * WAIT_TIMEOUT, TimeUnit.MILLISECONDS));\r
+ } catch (InterruptedException | ExecutionException | TimeoutException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+}
\ No newline at end of file
--- /dev/null
+/*\r
+ * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.\r
+ *\r
+ * This program and the accompanying materials are made available under the\r
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
+ * and is available at http://www.eclipse.org/legal/epl-v10.html\r
+ */\r
+\r
+package org.opendaylight.openflowjava.protocol.impl.core;\r
+\r
+import static org.mockito.Mockito.times;\r
+import static org.mockito.Mockito.verify;\r
+import io.netty.channel.ChannelHandlerContext;\r
+import io.netty.util.concurrent.Future;\r
+import io.netty.util.concurrent.GenericFutureListener;\r
+\r
+import java.net.InetSocketAddress;\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+import org.junit.Assert;\r
+import org.junit.Test;\r
+import org.mockito.Mock;\r
+import org.mockito.MockitoAnnotations;\r
+import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;\r
+import org.opendaylight.openflowjava.protocol.impl.connection.UdpMessageListenerWrapper;\r
+import org.opendaylight.openflowjava.protocol.impl.serialization.SerializationFactory;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInputBuilder;\r
+\r
+/**\r
+ * @author michal.polkorab\r
+ *\r
+ */\r
+public class OFDatagramPacketEncoderTest {\r
+\r
+ @Mock ChannelHandlerContext ctx;\r
+ @Mock GenericFutureListener<Future<Void>> listener;\r
+ @Mock SerializationFactory factory;\r
+\r
+ private UdpMessageListenerWrapper wrapper;\r
+ private InetSocketAddress address = new InetSocketAddress("10.0.0.1", 6653);\r
+ private List<Object> out;\r
+\r
+ /**\r
+ * Initializes mocks and other objects\r
+ * @param version openflow protocol wire version\r
+ */\r
+ public void startUp(Short version) {\r
+ MockitoAnnotations.initMocks(this);\r
+ out = new ArrayList<>();\r
+ HelloInputBuilder builder = new HelloInputBuilder();\r
+ builder.setVersion(version);\r
+ HelloInput hello = builder.build();\r
+ wrapper = new UdpMessageListenerWrapper(hello, listener, address);\r
+ }\r
+\r
+ /**\r
+ * Tests encoding\r
+ */\r
+ @Test\r
+ public void testCorrectEncode() {\r
+ startUp((short) EncodeConstants.OF13_VERSION_ID);\r
+ OFDatagramPacketEncoder encoder = new OFDatagramPacketEncoder();\r
+ encoder.setSerializationFactory(factory);\r
+ try {\r
+ encoder.encode(ctx, wrapper, out);\r
+ } catch (Exception e) {\r
+ Assert.fail();\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Tests encoding\r
+ */\r
+ @Test\r
+ public void testIncorrectEncode() {\r
+ startUp(null);\r
+ OFDatagramPacketEncoder encoder = new OFDatagramPacketEncoder();\r
+ encoder.setSerializationFactory(factory);\r
+ try {\r
+ encoder.encode(ctx, wrapper, out);\r
+ } catch (Exception e) {\r
+ verify(wrapper, times(1)).getListener();\r
+ Assert.assertEquals("List should be empty", 0, out.size());\r
+ }\r
+ }\r
+}
\ No newline at end of file
--- /dev/null
+/*\r
+ * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.\r
+ *\r
+ * This program and the accompanying materials are made available under the\r
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
+ * and is available at http://www.eclipse.org/legal/epl-v10.html\r
+ */\r
+\r
+package org.opendaylight.openflowjava.protocol.impl.deserialization.factories;\r
+\r
+import static org.mockito.Matchers.any;\r
+import static org.mockito.Mockito.when;\r
+import io.netty.buffer.ByteBuf;\r
+\r
+import org.junit.Assert;\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+import org.mockito.Mock;\r
+import org.mockito.MockitoAnnotations;\r
+import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;\r
+import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;\r
+import org.opendaylight.openflowjava.protocol.api.keys.experimenter.ExperimenterIdDeserializerKey;\r
+import org.opendaylight.openflowjava.util.ByteBufUtils;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;\r
+\r
+/**\r
+ * @author michal.polkorab\r
+ *\r
+ */\r
+public class ExperimenterMessageFactoryTest {\r
+\r
+ @Mock DeserializerRegistry registry;\r
+ @Mock OFDeserializer<ExperimenterMessage> deserializer;\r
+ @Mock ExperimenterMessage message;\r
+\r
+ /**\r
+ * Initializes mocks\r
+ */\r
+ @Before\r
+ public void startUp() {\r
+ MockitoAnnotations.initMocks(this);\r
+ }\r
+\r
+ /**\r
+ * Test deserializer lookup correctness\r
+ */\r
+ @Test\r
+ public void test() {\r
+ when(registry.getDeserializer(any(ExperimenterIdDeserializerKey.class))).thenReturn(deserializer);\r
+ when(deserializer.deserialize(any(ByteBuf.class))).thenReturn(message);\r
+ \r
+ ByteBuf buffer = ByteBufUtils.hexStringToByteBuf("00 01 02 03 00 00 00 10");\r
+ ExperimenterMessageFactory factory = new ExperimenterMessageFactory();\r
+ factory.injectDeserializerRegistry(registry);\r
+ ExperimenterMessage deserializedMessage = factory.deserialize(buffer);\r
+ Assert.assertEquals("Wrong return value", message, deserializedMessage);\r
+ Assert.assertEquals("ByteBuf index moved", 0, buffer.readerIndex());\r
+ }\r
+}
\ No newline at end of file
--- /dev/null
+/*\r
+ * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.\r
+ *\r
+ * This program and the accompanying materials are made available under the\r
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
+ * and is available at http://www.eclipse.org/legal/epl-v10.html\r
+ */\r
+\r
+package org.opendaylight.openflowjava.protocol.impl.serialization.factories;\r
+\r
+import io.netty.buffer.ByteBuf;\r
+import io.netty.buffer.UnpooledByteBufAllocator;\r
+\r
+import org.junit.Test;\r
+import org.mockito.Matchers;\r
+import org.mockito.Mock;\r
+import org.mockito.Mockito;\r
+import org.mockito.MockitoAnnotations;\r
+import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;\r
+import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;\r
+import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;\r
+import org.opendaylight.openflowjava.protocol.api.keys.experimenter.ExperimenterIdSerializerKey;\r
+import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;\r
+import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;\r
+import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ExperimenterId;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInputBuilder;\r
+\r
+/**\r
+ * @author michal.polkorab\r
+ *\r
+ */\r
+public class ExperimenterInputMessageFactoryTest {\r
+\r
+ @Mock SerializerRegistry registry;\r
+ @Mock OFSerializer<ExperimenterInput> serializer;\r
+ private OFSerializer<ExperimenterInput> expFactory;\r
+\r
+ /**\r
+ * Sets up ExperimenterInputMessageFactory\r
+ * @param real true if setup should use real registry, false when mock is desired\r
+ */\r
+ public void startUp(boolean real) {\r
+ MockitoAnnotations.initMocks(this);\r
+ expFactory = new ExperimenterInputMessageFactory();\r
+ if (real) {\r
+ SerializerRegistry realRegistry = new SerializerRegistryImpl();\r
+ realRegistry.init();\r
+ ((SerializerRegistryInjector) expFactory).injectSerializerRegistry(realRegistry);\r
+ } else {\r
+ ((SerializerRegistryInjector) expFactory).injectSerializerRegistry(registry);\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Testing of {@link ExperimenterInputMessageFactory} for correct serializer\r
+ * lookup and serialization\r
+ * @throws Exception \r
+ */\r
+ @Test(expected=IllegalStateException.class)\r
+ public void testV10Real() throws Exception {\r
+ startUp(true);\r
+ ExperimenterInputBuilder builder = new ExperimenterInputBuilder();\r
+ BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);\r
+ builder.setExperimenter(new ExperimenterId(42L));\r
+ ExperimenterInput input = builder.build();\r
+ \r
+ ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();\r
+ expFactory.serialize(input, out);\r
+ }\r
+\r
+ /**\r
+ * Testing of {@link ExperimenterInputMessageFactory} for correct serializer\r
+ * lookup and serialization\r
+ * @throws Exception \r
+ */\r
+ @Test(expected=IllegalStateException.class)\r
+ public void testV13Real() throws Exception {\r
+ startUp(true);\r
+ ExperimenterInputBuilder builder = new ExperimenterInputBuilder();\r
+ BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);\r
+ builder.setExperimenter(new ExperimenterId(42L));\r
+ ExperimenterInput input = builder.build();\r
+ \r
+ ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();\r
+ expFactory.serialize(input, out);\r
+ }\r
+\r
+ /**\r
+ * Testing of {@link ExperimenterInputMessageFactory} for correct serializer\r
+ * lookup and serialization\r
+ * @throws Exception \r
+ */\r
+ @Test\r
+ public void testV10() throws Exception {\r
+ startUp(false);\r
+ ExperimenterInputBuilder builder = new ExperimenterInputBuilder();\r
+ BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);\r
+ builder.setExperimenter(new ExperimenterId(42L));\r
+ ExperimenterInput input = builder.build();\r
+\r
+ Mockito.when(registry.getSerializer(\r
+ (ExperimenterIdSerializerKey<?>) Matchers.any())).thenReturn(serializer);\r
+\r
+ ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();\r
+ expFactory.serialize(input, out);\r
+ }\r
+\r
+ /**\r
+ * Testing of {@link ExperimenterInputMessageFactory} for correct serializer\r
+ * lookup and serialization\r
+ * @throws Exception \r
+ */\r
+ @Test\r
+ public void testV13() throws Exception {\r
+ startUp(false);\r
+ ExperimenterInputBuilder builder = new ExperimenterInputBuilder();\r
+ BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);\r
+ builder.setExperimenter(new ExperimenterId(42L));\r
+ ExperimenterInput input = builder.build();\r
+\r
+ Mockito.when(registry.getSerializer(\r
+ (ExperimenterIdSerializerKey<?>) Matchers.any())).thenReturn(serializer);\r
+\r
+ ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();\r
+ expFactory.serialize(input, out);\r
+ }\r
+}
\ No newline at end of file