2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowjava.protocol.impl.core;
11 import static org.junit.Assert.assertEquals;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.channel.ChannelHandlerContext;
15 import java.util.ArrayList;
16 import java.util.List;
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;
29 * Testing class of {@link OFFrameDecoder}
31 * @author michal.polkorab
33 @RunWith(MockitoJUnitRunner.class)
34 public class OFFrameDecoderTest {
37 ChannelHandlerContext channelHandlerContext;
40 ConnectionFacade connectionFacade;
41 private OFFrameDecoder decoder;
42 private List<Object> list = new ArrayList<>();
49 MockitoAnnotations.initMocks(this);
50 decoder = new OFFrameDecoder(connectionFacade, false);
57 * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}
60 public void testDecode8BMessage() {
62 decoder.decode(channelHandlerContext,
63 ByteBufUtils.hexStringToByteBuf("04 00 00 08 00 00 00 01"),
65 } catch (Exception e) {
69 assertEquals(8, ((ByteBuf) list.get(0)).readableBytes());
74 * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}
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");
81 decoder.decode(channelHandlerContext, byteBuffer, list);
82 } catch (Exception e) {
86 assertEquals(16, ((ByteBuf) list.get(0)).readableBytes());
87 assertEquals(0, byteBuffer.readableBytes());
92 * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}
95 public void testDecode5BIncompleteMessage() {
96 ByteBuf byteBuffer = ByteBufUtils.hexStringToByteBuf("04 00 00 08 00");
98 decoder.decode(channelHandlerContext, byteBuffer, list);
99 } catch (Exception e) {
103 Assert.assertEquals("List is not empty", 0, list.size());
104 assertEquals(5, byteBuffer.readableBytes());
109 * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}
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");
116 decoder.decode(channelHandlerContext, byteBuffer, list);
117 } catch (Exception e) {
121 Assert.assertEquals("List is not empty", 0, list.size());
122 assertEquals(16, byteBuffer.readableBytes());
127 * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}
130 public void testDecodeCompleteAndPartialMessage() {
131 ByteBuf byteBuffer = ByteBufUtils
132 .hexStringToByteBuf("04 00 00 08 00 00 00 01 04 00 00 08 00");
134 decoder.decode(channelHandlerContext, byteBuffer, list);
135 } catch (Exception e) {
139 Assert.assertEquals(8, ((ByteBuf) list.get(0)).readableBytes());
140 Assert.assertEquals(1, list.size());
141 assertEquals(5, byteBuffer.readableBytes());
146 public void testExceptionCaught() throws Exception {
147 decoder.exceptionCaught(channelHandlerContext, new Throwable());
152 * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}
155 public void testDecode8BMessageWithTls() {
156 decoder = new OFFrameDecoder(connectionFacade, true);
158 decoder.decode(channelHandlerContext,
159 ByteBufUtils.hexStringToByteBuf("04 00 00 08 00 00 00 01"),
161 } catch (Exception e) {
165 assertEquals(8, ((ByteBuf) list.get(0)).readableBytes());