Merge "add basic lib - plugin communication"
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / openflow / lib / OFFrameDecoderTest.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
2 package org.openflow.lib;\r
3 \r
4 import io.netty.buffer.ByteBuf;\r
5 import io.netty.channel.embedded.EmbeddedChannel;\r
6 \r
7 import org.junit.Assert;\r
8 import org.junit.Before;\r
9 import org.junit.Test;\r
10 import org.openflow.lib.OfFrameDecoder;\r
11 \r
12 /**\r
13  * Testing class of {@link OfFrameDecoder}\r
14  * @author michal.polkorab\r
15  */\r
16 public class OFFrameDecoderTest {\r
17 \r
18     private EmbeddedChannel embch;\r
19 \r
20     /**\r
21      * Sets up test environment\r
22      */\r
23     @Before\r
24     public void setUp() {\r
25         embch = new EmbeddedChannel(new OfFrameDecoder());\r
26     }\r
27 \r
28     /**\r
29      * Test of decoding {@link OfFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}\r
30      * @throws Exception \r
31      */\r
32     @Test\r
33     public void testDecode8BMessage() throws Exception {\r
34         byte[] msgs = new byte[]{0x04, 0x0, 0x0, 0x08, 0x0, 0x0, 0x0, 0x01};\r
35         ByteBuf writeObj = embch.alloc().buffer(64);\r
36         writeObj.writeBytes(msgs);\r
37         embch.writeInbound(writeObj);\r
38 \r
39         ByteBuf inObj = (ByteBuf) embch.readInbound();\r
40         Assert.assertEquals(8, inObj.readableBytes());\r
41     }\r
42 \r
43     /**\r
44      * Test of decoding {@link OfFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}\r
45      * @throws Exception \r
46      */\r
47     @Test\r
48     public void testDecode16BMessage() throws Exception {\r
49         byte[] msgs = new byte[]{0x04, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00,\r
50             0x00, 0x08, 0x00, 0x00, 0x00, 0x01};\r
51         ByteBuf writeObj = embch.alloc().buffer(64);\r
52         writeObj.writeBytes(msgs);\r
53         embch.writeInbound(writeObj);\r
54 \r
55         ByteBuf inObj = (ByteBuf) embch.readInbound();\r
56         Assert.assertEquals(16, inObj.readableBytes());\r
57     }\r
58 \r
59     /**\r
60      * Test of decoding {@link OfFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}\r
61      * @throws Exception \r
62      */\r
63     @Test\r
64     public void testDecodeIncompleteMessage() throws Exception {\r
65         byte[] msgs = new byte[]{0x04, 0x0, 0x0, 0x08, 0x0};\r
66         ByteBuf writeObj = embch.alloc().buffer(64);\r
67         writeObj.writeBytes(msgs);\r
68         embch.writeInbound(writeObj);\r
69 \r
70         ByteBuf inObj = (ByteBuf) embch.readInbound();\r
71         Assert.assertNull(inObj);\r
72     }\r
73 \r
74     /**\r
75      * Test of decoding {@link OfFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}\r
76      * @throws Exception \r
77      */\r
78     @Test\r
79     public void testDecodeCompleteAndPartialMessage() throws Exception {\r
80         byte[] msgs = new byte[]{0x04, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01,\r
81             0x04, 0x00, 0x00, 0x08, 0x00};\r
82         ByteBuf writeObj = embch.alloc().buffer(64);\r
83         writeObj.writeBytes(msgs);\r
84         embch.writeInbound(writeObj);\r
85 \r
86         ByteBuf inObj = (ByteBuf) embch.readInbound();\r
87         Assert.assertEquals(8, inObj.readableBytes());\r
88         inObj = (ByteBuf) embch.readInbound();\r
89         Assert.assertNull(inObj);\r
90     }\r
91 }