Remove trailing whitespace
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / OFFrameDecoderTest.java
index 6ad32adbd39470ae6de9723b8f12bd7a14c47df9..41d692c820820f0c49c22c6ece46d558174c8d8b 100644 (file)
-/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
-package org.opendaylight.openflowjava.protocol.impl.core;\r
-\r
-import io.netty.buffer.ByteBuf;\r
-import io.netty.channel.embedded.EmbeddedChannel;\r
-\r
-import org.junit.Assert;\r
-import org.junit.Before;\r
-import org.junit.Test;\r
-import org.opendaylight.openflowjava.protocol.impl.core.OFFrameDecoder;\r
-\r
-/**\r
- * Testing class of {@link OFFrameDecoder}\r
- * @author michal.polkorab\r
- */\r
-public class OFFrameDecoderTest {\r
-\r
-    private EmbeddedChannel embch;\r
-\r
-    /**\r
-     * Sets up test environment\r
-     */\r
-    @Before\r
-    public void setUp() {\r
-        embch = new EmbeddedChannel(new OFFrameDecoder());\r
-    }\r
-\r
-    /**\r
-     * Test of decoding {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}\r
-     * @throws Exception \r
-     */\r
-    @Test\r
-    public void testDecode8BMessage() throws Exception {\r
-        byte[] msgs = new byte[]{0x04, 0x0, 0x0, 0x08, 0x0, 0x0, 0x0, 0x01};\r
-        ByteBuf writeObj = embch.alloc().buffer(64);\r
-        writeObj.writeBytes(msgs);\r
-        embch.writeInbound(writeObj);\r
-\r
-        ByteBuf inObj = (ByteBuf) embch.readInbound();\r
-        Assert.assertEquals(8, inObj.readableBytes());\r
-    }\r
-\r
-    /**\r
-     * Test of decoding {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}\r
-     * @throws Exception \r
-     */\r
-    @Test\r
-    public void testDecode16BMessage() throws Exception {\r
-        byte[] msgs = new byte[]{0x04, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00,\r
-            0x00, 0x08, 0x00, 0x00, 0x00, 0x01};\r
-        ByteBuf writeObj = embch.alloc().buffer(64);\r
-        writeObj.writeBytes(msgs);\r
-        embch.writeInbound(writeObj);\r
-\r
-        ByteBuf inObj = (ByteBuf) embch.readInbound();\r
-        Assert.assertEquals(16, inObj.readableBytes());\r
-    }\r
-\r
-    /**\r
-     * Test of decoding {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}\r
-     * @throws Exception \r
-     */\r
-    @Test\r
-    public void testDecodeIncompleteMessage() throws Exception {\r
-        byte[] msgs = new byte[]{0x04, 0x0, 0x0, 0x08, 0x0};\r
-        ByteBuf writeObj = embch.alloc().buffer(64);\r
-        writeObj.writeBytes(msgs);\r
-        embch.writeInbound(writeObj);\r
-\r
-        ByteBuf inObj = (ByteBuf) embch.readInbound();\r
-        Assert.assertNull(inObj);\r
-    }\r
-\r
-    /**\r
-     * Test of decoding {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}\r
-     * @throws Exception \r
-     */\r
-    @Test\r
-    public void testDecodeCompleteAndPartialMessage() throws Exception {\r
-        byte[] msgs = new byte[]{0x04, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01,\r
-            0x04, 0x00, 0x00, 0x08, 0x00};\r
-        ByteBuf writeObj = embch.alloc().buffer(64);\r
-        writeObj.writeBytes(msgs);\r
-        embch.writeInbound(writeObj);\r
-\r
-        ByteBuf inObj = (ByteBuf) embch.readInbound();\r
-        Assert.assertEquals(8, inObj.readableBytes());\r
-        inObj = (ByteBuf) embch.readInbound();\r
-        Assert.assertNull(inObj);\r
-    }\r
-}
\ No newline at end of file
+/*
+ * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.openflowjava.protocol.impl.core;
+
+import static org.junit.Assert.assertEquals;
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionFacade;
+import org.opendaylight.openflowjava.util.ByteBufUtils;
+
+/**
+ * Testing class of {@link OFFrameDecoder}
+ *
+ * @author michal.polkorab
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class OFFrameDecoderTest {
+
+    @Mock
+    ChannelHandlerContext channelHandlerContext;
+
+    @Mock
+    ConnectionFacade connectionFacade;
+    private OFFrameDecoder decoder;
+    private List<Object> list = new ArrayList<>();
+
+    /**
+     * Sets up tests
+     */
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        decoder = new OFFrameDecoder(connectionFacade, false);
+        list.clear();
+
+    }
+
+    /**
+     * Test of decoding
+     * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}
+     */
+    @Test
+    public void testDecode8BMessage() {
+        try {
+            decoder.decode(channelHandlerContext,
+                    ByteBufUtils.hexStringToByteBuf("04 00 00 08 00 00 00 01"),
+                    list);
+        } catch (Exception e) {
+            Assert.fail();
+        }
+
+        assertEquals(8, ((ByteBuf) list.get(0)).readableBytes());
+    }
+
+    /**
+     * Test of decoding
+     * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}
+     */
+    @Test
+    public void testDecode16BMessage() {
+        ByteBuf byteBuffer = ByteBufUtils
+                .hexStringToByteBuf("04 00 00 10 00 00 00 00 00 00 00 00 00 00 00 42");
+        try {
+            decoder.decode(channelHandlerContext, byteBuffer, list);
+        } catch (Exception e) {
+            Assert.fail();
+        }
+
+        assertEquals(16, ((ByteBuf) list.get(0)).readableBytes());
+        assertEquals(0, byteBuffer.readableBytes());
+    }
+
+    /**
+     * Test of decoding
+     * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}
+     */
+    @Test
+    public void testDecode5BIncompleteMessage() {
+        ByteBuf byteBuffer = ByteBufUtils.hexStringToByteBuf("04 00 00 08 00");
+        try {
+            decoder.decode(channelHandlerContext, byteBuffer, list);
+        } catch (Exception e) {
+            Assert.fail();
+        }
+
+        Assert.assertEquals("List is not empty", 0, list.size());
+        assertEquals(5, byteBuffer.readableBytes());
+    }
+
+    /**
+     * Test of decoding
+     * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}
+     */
+    @Test
+    public void testDecode16BIncompleteMessage() {
+        ByteBuf byteBuffer = ByteBufUtils
+                .hexStringToByteBuf("04 00 00 11 00 00 00 00 00 00 00 00 00 00 00 42");
+        try {
+            decoder.decode(channelHandlerContext, byteBuffer, list);
+        } catch (Exception e) {
+            Assert.fail();
+        }
+
+        Assert.assertEquals("List is not empty", 0, list.size());
+        assertEquals(16, byteBuffer.readableBytes());
+    }
+
+    /**
+     * Test of decoding
+     * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}
+     */
+    @Test
+    public void testDecodeCompleteAndPartialMessage() {
+        ByteBuf byteBuffer = ByteBufUtils
+                .hexStringToByteBuf("04 00 00 08 00 00 00 01 04 00 00 08 00");
+        try {
+            decoder.decode(channelHandlerContext, byteBuffer, list);
+        } catch (Exception e) {
+            Assert.fail();
+        }
+
+        Assert.assertEquals(8, ((ByteBuf) list.get(0)).readableBytes());
+        Assert.assertEquals(1, list.size());
+        assertEquals(5, byteBuffer.readableBytes());
+
+    }
+
+    @Test
+    public void testExceptionCaught() throws Exception {
+        decoder.exceptionCaught(channelHandlerContext, new Throwable());
+    }
+
+    /**
+     * Test of decoding
+     * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}
+     */
+    @Test
+    public void testDecode8BMessageWithTls() {
+        decoder = new OFFrameDecoder(connectionFacade, true);
+        try {
+            decoder.decode(channelHandlerContext,
+                    ByteBufUtils.hexStringToByteBuf("04 00 00 08 00 00 00 01"),
+                    list);
+        } catch (Exception e) {
+            Assert.fail();
+        }
+
+        assertEquals(8, ((ByteBuf) list.get(0)).readableBytes());
+    }
+}