Remove trailing whitespace
[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 static org.junit.Assert.assertEquals;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.channel.ChannelHandlerContext;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.junit.Assert;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.Mock;
23 import org.mockito.MockitoAnnotations;
24 import org.mockito.runners.MockitoJUnitRunner;
25 import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionFacade;
26 import org.opendaylight.openflowjava.util.ByteBufUtils;
27
28 /**
29  * Testing class of {@link OFFrameDecoder}
30  *
31  * @author michal.polkorab
32  */
33 @RunWith(MockitoJUnitRunner.class)
34 public class OFFrameDecoderTest {
35
36     @Mock
37     ChannelHandlerContext channelHandlerContext;
38
39     @Mock
40     ConnectionFacade connectionFacade;
41     private OFFrameDecoder decoder;
42     private List<Object> list = new ArrayList<>();
43
44     /**
45      * Sets up tests
46      */
47     @Before
48     public void setUp() {
49         MockitoAnnotations.initMocks(this);
50         decoder = new OFFrameDecoder(connectionFacade, false);
51         list.clear();
52
53     }
54
55     /**
56      * Test of decoding
57      * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}
58      */
59     @Test
60     public void testDecode8BMessage() {
61         try {
62             decoder.decode(channelHandlerContext,
63                     ByteBufUtils.hexStringToByteBuf("04 00 00 08 00 00 00 01"),
64                     list);
65         } catch (Exception e) {
66             Assert.fail();
67         }
68
69         assertEquals(8, ((ByteBuf) list.get(0)).readableBytes());
70     }
71
72     /**
73      * Test of decoding
74      * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}
75      */
76     @Test
77     public void testDecode16BMessage() {
78         ByteBuf byteBuffer = ByteBufUtils
79                 .hexStringToByteBuf("04 00 00 10 00 00 00 00 00 00 00 00 00 00 00 42");
80         try {
81             decoder.decode(channelHandlerContext, byteBuffer, list);
82         } catch (Exception e) {
83             Assert.fail();
84         }
85
86         assertEquals(16, ((ByteBuf) list.get(0)).readableBytes());
87         assertEquals(0, byteBuffer.readableBytes());
88     }
89
90     /**
91      * Test of decoding
92      * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}
93      */
94     @Test
95     public void testDecode5BIncompleteMessage() {
96         ByteBuf byteBuffer = ByteBufUtils.hexStringToByteBuf("04 00 00 08 00");
97         try {
98             decoder.decode(channelHandlerContext, byteBuffer, list);
99         } catch (Exception e) {
100             Assert.fail();
101         }
102
103         Assert.assertEquals("List is not empty", 0, list.size());
104         assertEquals(5, byteBuffer.readableBytes());
105     }
106
107     /**
108      * Test of decoding
109      * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}
110      */
111     @Test
112     public void testDecode16BIncompleteMessage() {
113         ByteBuf byteBuffer = ByteBufUtils
114                 .hexStringToByteBuf("04 00 00 11 00 00 00 00 00 00 00 00 00 00 00 42");
115         try {
116             decoder.decode(channelHandlerContext, byteBuffer, list);
117         } catch (Exception e) {
118             Assert.fail();
119         }
120
121         Assert.assertEquals("List is not empty", 0, list.size());
122         assertEquals(16, byteBuffer.readableBytes());
123     }
124
125     /**
126      * Test of decoding
127      * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}
128      */
129     @Test
130     public void testDecodeCompleteAndPartialMessage() {
131         ByteBuf byteBuffer = ByteBufUtils
132                 .hexStringToByteBuf("04 00 00 08 00 00 00 01 04 00 00 08 00");
133         try {
134             decoder.decode(channelHandlerContext, byteBuffer, list);
135         } catch (Exception e) {
136             Assert.fail();
137         }
138
139         Assert.assertEquals(8, ((ByteBuf) list.get(0)).readableBytes());
140         Assert.assertEquals(1, list.size());
141         assertEquals(5, byteBuffer.readableBytes());
142
143     }
144
145     @Test
146     public void testExceptionCaught() throws Exception {
147         decoder.exceptionCaught(channelHandlerContext, new Throwable());
148     }
149
150     /**
151      * Test of decoding
152      * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}
153      */
154     @Test
155     public void testDecode8BMessageWithTls() {
156         decoder = new OFFrameDecoder(connectionFacade, true);
157         try {
158             decoder.decode(channelHandlerContext,
159                     ByteBufUtils.hexStringToByteBuf("04 00 00 08 00 00 00 01"),
160                     list);
161         } catch (Exception e) {
162             Assert.fail();
163         }
164
165         assertEquals(8, ((ByteBuf) list.get(0)).readableBytes());
166     }
167 }