Created unit tests for OFDatagramPacketDecoder, OFDatagramPacketHandler 63/12163/5
authorMarian Adamjak <marian.adamjak@pantheon.sk>
Wed, 22 Oct 2014 14:31:08 +0000 (16:31 +0200)
committerMarian Adamjak <marian.adamjak@pantheon.sk>
Thu, 23 Oct 2014 12:15:22 +0000 (14:15 +0200)
Change-Id: I4984189ebc2e93fb186a718bce6e8d787095ed76
Signed-off-by: Marian Adamjak <marian.adamjak@pantheon.sk>
openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/OFDatagramPacketDecoderTest.java [new file with mode: 0644]
openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/OFDatagramPacketHandlerTest.java [new file with mode: 0644]

diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/OFDatagramPacketDecoderTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/OFDatagramPacketDecoderTest.java
new file mode 100644 (file)
index 0000000..4175647
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * 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.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+
+import java.net.InetSocketAddress;
+import java.util.ArrayList;
+import java.util.List;
+
+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.util.EncodeConstants;
+import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializationFactory;
+
+/**
+ * @author madamjak
+ *
+ */
+public class OFDatagramPacketDecoderTest {
+    @Mock DeserializationFactory deserializationFactory;
+    @Mock ChannelHandlerContext ctx;
+    @Mock ByteBuf messageBufferMock;
+
+    private VersionMessageUdpWrapper msgWrapper;
+    private List<Object> out;
+
+    @Before
+    public void startUp(){
+        MockitoAnnotations.initMocks(this);
+        out = new ArrayList<>();
+    }
+
+    @Test
+    public void test() {
+        OFDatagramPacketDecoder decoder = new OFDatagramPacketDecoder();
+        decoder.setDeserializationFactory(deserializationFactory);
+        msgWrapper = new VersionMessageUdpWrapper(EncodeConstants.OF13_VERSION_ID, messageBufferMock, new InetSocketAddress("10.0.0.1", 6653));
+        try {
+            decoder.decode(ctx, msgWrapper, out);
+        } catch (Exception e) {
+            Assert.fail("Exception occured");
+        }
+        verify(messageBufferMock, times(1)).release();
+    }
+}
diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/OFDatagramPacketHandlerTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/OFDatagramPacketHandlerTest.java
new file mode 100644 (file)
index 0000000..9deef14
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * 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.when;
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.socket.DatagramPacket;
+
+import java.net.InetSocketAddress;
+import java.util.ArrayList;
+import java.util.List;
+
+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.connection.SwitchConnectionHandler;
+import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
+import org.opendaylight.openflowjava.protocol.impl.connection.MessageConsumer;
+import org.opendaylight.openflowjava.util.ByteBufUtils;
+
+/**
+ * @author madamjak
+ *
+ */
+public class OFDatagramPacketHandlerTest {
+    @Mock ChannelHandlerContext ctxMock;
+    @Mock SwitchConnectionHandler switchConnHandler;
+    @Mock MessageConsumer consumerMock;
+    @Mock Channel channelMock;
+
+    @Before
+    public void startUp(){
+        MockitoAnnotations.initMocks(this);
+        when(ctxMock.channel()).thenReturn(channelMock);
+    }
+
+    /**
+     * Test {@link OFDatagramPacketHandler}
+     */
+    @Test
+    public void test(){
+        OFDatagramPacketHandler handler = new OFDatagramPacketHandler(switchConnHandler);
+        byte version = EncodeConstants.OF13_VERSION_ID;
+        ByteBuf messageBuffer = ByteBufUtils.hexStringToByteBuf("04 02 00 08 01 02 03 04");
+        InetSocketAddress recipientISA = InetSocketAddress.createUnresolved("localhost", 9876);
+        InetSocketAddress senderISA = InetSocketAddress.createUnresolved("192.168.15.24", 21021);
+        DatagramPacket datagramPacket = new DatagramPacket(messageBuffer, recipientISA, senderISA);
+        UdpConnectionMap.addConnection(datagramPacket.sender(), consumerMock);
+        List<Object> outList = new ArrayList<>();
+        try {
+            handler.decode(ctxMock, datagramPacket, outList);
+        } catch (Exception e) {
+            Assert.fail("Wrong - Unexcepted exception occurred");
+        }
+        VersionMessageUdpWrapper versionUdpWrapper = (VersionMessageUdpWrapper) outList.get(0);
+        Assert.assertEquals("Wrong - incorrect version has been decoded",version, versionUdpWrapper.getVersion());
+        Assert.assertEquals("Wrong - sender addresses are different", senderISA, versionUdpWrapper.getAddress());
+        messageBuffer.readerIndex(1);
+        Assert.assertEquals("Wrong - undecoded part of input ByteBuff is differnt to output",0, messageBuffer.compareTo(versionUdpWrapper.getMessageBuffer()));
+    }
+}