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