Centralized pipeline creation
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / OFFrameDecoderTest.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
2 package org.opendaylight.openflowjava.protocol.impl.core;\r
3 \r
4 import io.netty.buffer.ByteBuf;\r
5 import io.netty.channel.ChannelHandlerContext;\r
6 \r
7 import java.util.ArrayList;\r
8 import java.util.List;\r
9 \r
10 import org.junit.Assert;\r
11 import org.junit.Before;\r
12 import org.junit.Test;\r
13 import org.junit.runner.RunWith;\r
14 import org.mockito.Mock;\r
15 import org.mockito.runners.MockitoJUnitRunner;\r
16 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;\r
17 \r
18 /**\r
19  * Testing class of {@link OFFrameDecoder}\r
20  * \r
21  * @author michal.polkorab\r
22  */\r
23 @RunWith(MockitoJUnitRunner.class)\r
24 public class OFFrameDecoderTest {\r
25 \r
26     @Mock\r
27     ChannelHandlerContext channelHandlerContext;\r
28 \r
29     private OFFrameDecoder decoder;\r
30     private List<Object> list = new ArrayList<>();\r
31 \r
32     /**\r
33      * Sets up tests\r
34      */\r
35     @Before\r
36     public void setUp() {\r
37         list.clear();\r
38         decoder = new OFFrameDecoder();\r
39     }\r
40 \r
41     /**\r
42      * Test of decoding\r
43      * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}\r
44      * \r
45      * @throws Exception\r
46      */\r
47     @Test\r
48     public void testDecode8BMessage() throws Exception {\r
49         decoder.decode(channelHandlerContext,\r
50                 ByteBufUtils.hexStringToByteBuf("04 00 00 08 00 00 00 01"),\r
51                 list);\r
52 \r
53         Assert.assertEquals(8, ((ByteBuf) list.get(0)).readableBytes());\r
54     }\r
55 \r
56     /**\r
57      * Test of decoding\r
58      * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}\r
59      * \r
60      * @throws Exception\r
61      */\r
62     @Test\r
63     public void testDecode16BMessage() throws Exception {\r
64         decoder.decode(channelHandlerContext,\r
65                 ByteBufUtils.hexStringToByteBuf("04 00 00 10 00 00 00 00 00 00 00 00 00 00 00 42"),\r
66                 list);\r
67 \r
68         Assert.assertEquals(16, ((ByteBuf) list.get(0)).readableBytes());\r
69     }\r
70 \r
71     /**\r
72      * Test of decoding\r
73      * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}\r
74      * \r
75      * @throws Exception\r
76      */\r
77     @Test\r
78     public void testDecodeIncompleteMessage() throws Exception {\r
79         decoder.decode(channelHandlerContext,\r
80                 ByteBufUtils.hexStringToByteBuf("04 00 00 08 00"),\r
81                 list);\r
82 \r
83         Assert.assertEquals("List is not empty", 0, list.size());\r
84     }\r
85 \r
86     /**\r
87      * Test of decoding\r
88      * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}\r
89      * \r
90      * @throws Exception\r
91      */\r
92     @Test\r
93     public void testDecodeCompleteAndPartialMessage() throws Exception {\r
94         decoder.decode(channelHandlerContext,\r
95                 ByteBufUtils.hexStringToByteBuf("04 00 00 08 00 00 00 01 04 00 00 08 00"),\r
96                 list);\r
97 \r
98         Assert.assertEquals(8, ((ByteBuf) list.get(0)).readableBytes());\r
99         Assert.assertEquals(1, list.size());\r
100     }\r
101     \r
102 }