Renamed packages to org.opendaylight.openflowjava.protocol.impl.*
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / OFFrameDecoderTest.java
diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/OFFrameDecoderTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/OFFrameDecoderTest.java
new file mode 100644 (file)
index 0000000..6ad32ad
--- /dev/null
@@ -0,0 +1,91 @@
+/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
+package org.opendaylight.openflowjava.protocol.impl.core;\r
+\r
+import io.netty.buffer.ByteBuf;\r
+import io.netty.channel.embedded.EmbeddedChannel;\r
+\r
+import org.junit.Assert;\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+import org.opendaylight.openflowjava.protocol.impl.core.OFFrameDecoder;\r
+\r
+/**\r
+ * Testing class of {@link OFFrameDecoder}\r
+ * @author michal.polkorab\r
+ */\r
+public class OFFrameDecoderTest {\r
+\r
+    private EmbeddedChannel embch;\r
+\r
+    /**\r
+     * Sets up test environment\r
+     */\r
+    @Before\r
+    public void setUp() {\r
+        embch = new EmbeddedChannel(new OFFrameDecoder());\r
+    }\r
+\r
+    /**\r
+     * Test of decoding {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}\r
+     * @throws Exception \r
+     */\r
+    @Test\r
+    public void testDecode8BMessage() throws Exception {\r
+        byte[] msgs = new byte[]{0x04, 0x0, 0x0, 0x08, 0x0, 0x0, 0x0, 0x01};\r
+        ByteBuf writeObj = embch.alloc().buffer(64);\r
+        writeObj.writeBytes(msgs);\r
+        embch.writeInbound(writeObj);\r
+\r
+        ByteBuf inObj = (ByteBuf) embch.readInbound();\r
+        Assert.assertEquals(8, inObj.readableBytes());\r
+    }\r
+\r
+    /**\r
+     * Test of decoding {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}\r
+     * @throws Exception \r
+     */\r
+    @Test\r
+    public void testDecode16BMessage() throws Exception {\r
+        byte[] msgs = new byte[]{0x04, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00,\r
+            0x00, 0x08, 0x00, 0x00, 0x00, 0x01};\r
+        ByteBuf writeObj = embch.alloc().buffer(64);\r
+        writeObj.writeBytes(msgs);\r
+        embch.writeInbound(writeObj);\r
+\r
+        ByteBuf inObj = (ByteBuf) embch.readInbound();\r
+        Assert.assertEquals(16, inObj.readableBytes());\r
+    }\r
+\r
+    /**\r
+     * Test of decoding {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}\r
+     * @throws Exception \r
+     */\r
+    @Test\r
+    public void testDecodeIncompleteMessage() throws Exception {\r
+        byte[] msgs = new byte[]{0x04, 0x0, 0x0, 0x08, 0x0};\r
+        ByteBuf writeObj = embch.alloc().buffer(64);\r
+        writeObj.writeBytes(msgs);\r
+        embch.writeInbound(writeObj);\r
+\r
+        ByteBuf inObj = (ByteBuf) embch.readInbound();\r
+        Assert.assertNull(inObj);\r
+    }\r
+\r
+    /**\r
+     * Test of decoding {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}\r
+     * @throws Exception \r
+     */\r
+    @Test\r
+    public void testDecodeCompleteAndPartialMessage() throws Exception {\r
+        byte[] msgs = new byte[]{0x04, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01,\r
+            0x04, 0x00, 0x00, 0x08, 0x00};\r
+        ByteBuf writeObj = embch.alloc().buffer(64);\r
+        writeObj.writeBytes(msgs);\r
+        embch.writeInbound(writeObj);\r
+\r
+        ByteBuf inObj = (ByteBuf) embch.readInbound();\r
+        Assert.assertEquals(8, inObj.readableBytes());\r
+        inObj = (ByteBuf) embch.readInbound();\r
+        Assert.assertNull(inObj);\r
+    }\r
+}
\ No newline at end of file