From 42517b13c7b53f7b4cb27f7fce43c8addcbb794d Mon Sep 17 00:00:00 2001 From: Michal Polkorab Date: Tue, 16 Sep 2014 14:28:54 +0200 Subject: [PATCH] Added unit tests Change-Id: I1b6b771d5748d348e8420d33ee05676dbac4fed3 Signed-off-by: Michal Polkorab --- .../MessageListenerWrapperTest.java | 34 ++++ .../SwitchConnectionProviderImplTest.java | 180 ++++++++++++++++++ .../core/OFDatagramPacketEncoderTest.java | 88 +++++++++ .../ExperimenterMessageFactoryTest.java | 59 ++++++ .../ExperimenterInputMessageFactoryTest.java | 129 +++++++++++++ 5 files changed, 490 insertions(+) create mode 100644 openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/connection/MessageListenerWrapperTest.java create mode 100644 openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/connection/SwitchConnectionProviderImplTest.java create mode 100644 openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/OFDatagramPacketEncoderTest.java create mode 100644 openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/ExperimenterMessageFactoryTest.java create mode 100644 openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/ExperimenterInputMessageFactoryTest.java diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/connection/MessageListenerWrapperTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/connection/MessageListenerWrapperTest.java new file mode 100644 index 00000000..e34a48a9 --- /dev/null +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/connection/MessageListenerWrapperTest.java @@ -0,0 +1,34 @@ +/* + * 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 org.junit.Assert; +import org.junit.Test; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInputBuilder; + +/** + * @author michal.polkorab + * + */ +public class MessageListenerWrapperTest { + + /** + * Test MessageListenerWrapper creation + */ + @Test + public void test() { + HelloInputBuilder builder = new HelloInputBuilder(); + HelloInput hello = builder.build(); + SimpleRpcListener listener = new SimpleRpcListener(hello, "Error"); + MessageListenerWrapper wrapper = new MessageListenerWrapper(hello, listener); + Assert.assertEquals("Wrong message", hello, wrapper.getMsg()); + Assert.assertEquals("Wrong listener", listener, wrapper.getListener()); + } +} \ No newline at end of file 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 new file mode 100644 index 00000000..5c33b8d2 --- /dev/null +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/connection/SwitchConnectionProviderImplTest.java @@ -0,0 +1,180 @@ +/* + * 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 java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +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.spi.connection.SwitchConnectionProvider; +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 com.google.common.util.concurrent.ListenableFuture; + +/** + * @author michal.polkorab + * + */ +public class SwitchConnectionProviderImplTest { + + @Mock SwitchConnectionHandler handler; + + private static final int SWITCH_IDLE_TIMEOUT = 2000; + private static final int WAIT_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); + } + + /** + * Tests provider startup - without configuration and {@link SwitchConnectionHandler} + */ + @Test + public void testStartup1() { + provider = new SwitchConnectionProviderImpl(); + 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()); + } + } + + /** + * Tests provider startup - without configuration + */ + @Test + public void testStartup2() { + startUp(null); + provider.setSwitchConnectionHandler(handler); + 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()); + } + } + + /** + * Tests provider startup - without {@link SwitchConnectionHandler} + */ + @Test + public void testStartup3() { + startUp(TransportProtocol.TCP); + provider.setConfiguration(config); + 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()); + } + } + + /** + * Tests correct provider startup - over TCP + */ + @Test + public void testStartup4() { + startUp(TransportProtocol.TCP); + provider.setConfiguration(config); + provider.setSwitchConnectionHandler(handler); + try { + Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS)); + } catch (InterruptedException | ExecutionException | TimeoutException e) { + Assert.fail(); + } + } + + /** + * Tests correct provider startup - over TLS + */ + @Test + public void testStartup5() { + startUp(TransportProtocol.TLS); + provider.setConfiguration(config); + provider.setSwitchConnectionHandler(handler); + try { + Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS)); + } catch (InterruptedException | ExecutionException | TimeoutException e) { + Assert.fail(); + } + } + + /** + * Tests correct provider startup - over UDP + */ + @Test + public void testStartup6() { + startUp(TransportProtocol.UDP); + provider.setConfiguration(config); + provider.setSwitchConnectionHandler(handler); + try { + Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS)); + } catch (InterruptedException | ExecutionException | TimeoutException e) { + Assert.fail(); + } + } + + /** + * Tests correct provider shutdown + */ + @Test + public void testShutdown() { + startUp(TransportProtocol.TCP); + provider.setConfiguration(config); + 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) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/OFDatagramPacketEncoderTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/OFDatagramPacketEncoderTest.java new file mode 100644 index 00000000..ac063686 --- /dev/null +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/OFDatagramPacketEncoderTest.java @@ -0,0 +1,88 @@ +/* + * 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.core; + +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import io.netty.channel.ChannelHandlerContext; +import io.netty.util.concurrent.Future; +import io.netty.util.concurrent.GenericFutureListener; + +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants; +import org.opendaylight.openflowjava.protocol.impl.connection.UdpMessageListenerWrapper; +import org.opendaylight.openflowjava.protocol.impl.serialization.SerializationFactory; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInputBuilder; + +/** + * @author michal.polkorab + * + */ +public class OFDatagramPacketEncoderTest { + + @Mock ChannelHandlerContext ctx; + @Mock GenericFutureListener> listener; + @Mock SerializationFactory factory; + + private UdpMessageListenerWrapper wrapper; + private InetSocketAddress address = new InetSocketAddress("10.0.0.1", 6653); + private List out; + + /** + * Initializes mocks and other objects + * @param version openflow protocol wire version + */ + public void startUp(Short version) { + MockitoAnnotations.initMocks(this); + out = new ArrayList<>(); + HelloInputBuilder builder = new HelloInputBuilder(); + builder.setVersion(version); + HelloInput hello = builder.build(); + wrapper = new UdpMessageListenerWrapper(hello, listener, address); + } + + /** + * Tests encoding + */ + @Test + public void testCorrectEncode() { + startUp((short) EncodeConstants.OF13_VERSION_ID); + OFDatagramPacketEncoder encoder = new OFDatagramPacketEncoder(); + encoder.setSerializationFactory(factory); + try { + encoder.encode(ctx, wrapper, out); + } catch (Exception e) { + Assert.fail(); + } + } + + /** + * Tests encoding + */ + @Test + public void testIncorrectEncode() { + startUp(null); + OFDatagramPacketEncoder encoder = new OFDatagramPacketEncoder(); + encoder.setSerializationFactory(factory); + try { + encoder.encode(ctx, wrapper, out); + } catch (Exception e) { + verify(wrapper, times(1)).getListener(); + Assert.assertEquals("List should be empty", 0, out.size()); + } + } +} \ No newline at end of file diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/ExperimenterMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/ExperimenterMessageFactoryTest.java new file mode 100644 index 00000000..ac167642 --- /dev/null +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/ExperimenterMessageFactoryTest.java @@ -0,0 +1,59 @@ +/* + * 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.deserialization.factories; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.when; +import io.netty.buffer.ByteBuf; + +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.api.extensibility.DeserializerRegistry; +import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer; +import org.opendaylight.openflowjava.protocol.api.keys.experimenter.ExperimenterIdDeserializerKey; +import org.opendaylight.openflowjava.util.ByteBufUtils; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage; + +/** + * @author michal.polkorab + * + */ +public class ExperimenterMessageFactoryTest { + + @Mock DeserializerRegistry registry; + @Mock OFDeserializer deserializer; + @Mock ExperimenterMessage message; + + /** + * Initializes mocks + */ + @Before + public void startUp() { + MockitoAnnotations.initMocks(this); + } + + /** + * Test deserializer lookup correctness + */ + @Test + public void test() { + when(registry.getDeserializer(any(ExperimenterIdDeserializerKey.class))).thenReturn(deserializer); + when(deserializer.deserialize(any(ByteBuf.class))).thenReturn(message); + + ByteBuf buffer = ByteBufUtils.hexStringToByteBuf("00 01 02 03 00 00 00 10"); + ExperimenterMessageFactory factory = new ExperimenterMessageFactory(); + factory.injectDeserializerRegistry(registry); + ExperimenterMessage deserializedMessage = factory.deserialize(buffer); + Assert.assertEquals("Wrong return value", message, deserializedMessage); + Assert.assertEquals("ByteBuf index moved", 0, buffer.readerIndex()); + } +} \ No newline at end of file diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/ExperimenterInputMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/ExperimenterInputMessageFactoryTest.java new file mode 100644 index 00000000..17b3f7ad --- /dev/null +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/ExperimenterInputMessageFactoryTest.java @@ -0,0 +1,129 @@ +/* + * 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.serialization.factories; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.UnpooledByteBufAllocator; + +import org.junit.Test; +import org.mockito.Matchers; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer; +import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry; +import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector; +import org.opendaylight.openflowjava.protocol.api.keys.experimenter.ExperimenterIdSerializerKey; +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.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ExperimenterId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInputBuilder; + +/** + * @author michal.polkorab + * + */ +public class ExperimenterInputMessageFactoryTest { + + @Mock SerializerRegistry registry; + @Mock OFSerializer serializer; + private OFSerializer expFactory; + + /** + * Sets up ExperimenterInputMessageFactory + * @param real true if setup should use real registry, false when mock is desired + */ + public void startUp(boolean real) { + MockitoAnnotations.initMocks(this); + expFactory = new ExperimenterInputMessageFactory(); + if (real) { + SerializerRegistry realRegistry = new SerializerRegistryImpl(); + realRegistry.init(); + ((SerializerRegistryInjector) expFactory).injectSerializerRegistry(realRegistry); + } else { + ((SerializerRegistryInjector) expFactory).injectSerializerRegistry(registry); + } + } + + /** + * Testing of {@link ExperimenterInputMessageFactory} for correct serializer + * lookup and serialization + * @throws Exception + */ + @Test(expected=IllegalStateException.class) + public void testV10Real() throws Exception { + startUp(true); + ExperimenterInputBuilder builder = new ExperimenterInputBuilder(); + BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID); + builder.setExperimenter(new ExperimenterId(42L)); + ExperimenterInput input = builder.build(); + + ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); + expFactory.serialize(input, out); + } + + /** + * Testing of {@link ExperimenterInputMessageFactory} for correct serializer + * lookup and serialization + * @throws Exception + */ + @Test(expected=IllegalStateException.class) + public void testV13Real() throws Exception { + startUp(true); + ExperimenterInputBuilder builder = new ExperimenterInputBuilder(); + BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID); + builder.setExperimenter(new ExperimenterId(42L)); + ExperimenterInput input = builder.build(); + + ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); + expFactory.serialize(input, out); + } + + /** + * Testing of {@link ExperimenterInputMessageFactory} for correct serializer + * lookup and serialization + * @throws Exception + */ + @Test + public void testV10() throws Exception { + startUp(false); + ExperimenterInputBuilder builder = new ExperimenterInputBuilder(); + BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID); + builder.setExperimenter(new ExperimenterId(42L)); + ExperimenterInput input = builder.build(); + + Mockito.when(registry.getSerializer( + (ExperimenterIdSerializerKey) Matchers.any())).thenReturn(serializer); + + ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); + expFactory.serialize(input, out); + } + + /** + * Testing of {@link ExperimenterInputMessageFactory} for correct serializer + * lookup and serialization + * @throws Exception + */ + @Test + public void testV13() throws Exception { + startUp(false); + ExperimenterInputBuilder builder = new ExperimenterInputBuilder(); + BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID); + builder.setExperimenter(new ExperimenterId(42L)); + ExperimenterInput input = builder.build(); + + Mockito.when(registry.getSerializer( + (ExperimenterIdSerializerKey) Matchers.any())).thenReturn(serializer); + + ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); + expFactory.serialize(input, out); + } +} \ No newline at end of file -- 2.36.6