package org.opendaylight.openflowjava.protocol.impl.core;\r
\r
import io.netty.buffer.ByteBuf;\r
-import io.netty.channel.embedded.EmbeddedChannel;\r
+import io.netty.channel.ChannelHandlerContext;\r
+import io.netty.channel.ChannelPipeline;\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\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
+import org.junit.runner.RunWith;\r
+import org.mockito.Matchers;\r
+import org.mockito.Mock;\r
+import org.mockito.Mockito;\r
+import org.mockito.runners.MockitoJUnitRunner;\r
+import org.opendaylight.openflowjava.protocol.impl.core.TcpHandler.COMPONENT_NAMES;\r
+import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;\r
+\r
+import com.google.common.collect.Lists;\r
\r
/**\r
* Testing class of {@link OFFrameDecoder}\r
+ * \r
* @author michal.polkorab\r
*/\r
+@RunWith(MockitoJUnitRunner.class)\r
public class OFFrameDecoderTest {\r
\r
- private EmbeddedChannel embch;\r
+ @Mock\r
+ ChannelHandlerContext channelHandlerContext;\r
+\r
+ @Mock\r
+ ChannelPipeline channelPipeline;\r
+\r
+ private OFFrameDecoder decoder;\r
+ private List<Object> list = new ArrayList<>();\r
\r
/**\r
- * Sets up test environment\r
+ * Sets up tests\r
*/\r
@Before\r
public void setUp() {\r
- embch = new EmbeddedChannel(new OFFrameDecoder());\r
+ Mockito.when(channelHandlerContext.pipeline()).thenReturn(\r
+ channelPipeline);\r
+ Mockito.when(channelPipeline.get(Matchers.anyString()))\r
+ .thenReturn(null);\r
+ Mockito.when(channelPipeline.names()).thenReturn(\r
+ Lists.newArrayList("xx"));\r
+ list.clear();\r
+ decoder = 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
+ * Test of decoding\r
+ * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}\r
+ * \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
+ decoder.decode(channelHandlerContext,\r
+ ByteBufUtils.hexStringToByteBuf("04 00 00 08 00 00 00 01"),\r
+ list);\r
\r
- ByteBuf inObj = (ByteBuf) embch.readInbound();\r
- Assert.assertEquals(8, inObj.readableBytes());\r
+ Assert.assertEquals(8, ((ByteBuf) list.get(0)).readableBytes());\r
+ verifyMockCalls(1);\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
+ * Test of decoding\r
+ * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}\r
+ * \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
+ decoder.decode(channelHandlerContext,\r
+ ByteBufUtils.hexStringToByteBuf("04 00 00 10 00 00 00 00 00 00 00 00 00 00 00 42"),\r
+ list);\r
+\r
+ Assert.assertEquals(16, ((ByteBuf) list.get(0)).readableBytes());\r
+ verifyMockCalls(1);\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
+ * Test of decoding\r
+ * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}\r
+ * \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
+ decoder.decode(channelHandlerContext,\r
+ ByteBufUtils.hexStringToByteBuf("04 00 00 08 00"),\r
+ list);\r
\r
- ByteBuf inObj = (ByteBuf) embch.readInbound();\r
- Assert.assertNull(inObj);\r
+ Assert.assertEquals("List is not empty", 0, list.size());\r
+ verifyMockCalls(0);\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
+ * Test of decoding\r
+ * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}\r
+ * \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
+ decoder.decode(channelHandlerContext,\r
+ ByteBufUtils.hexStringToByteBuf("04 00 00 08 00 00 00 01 04 00 00 08 00"),\r
+ list);\r
+\r
+ Assert.assertEquals(8, ((ByteBuf) list.get(0)).readableBytes());\r
+ Assert.assertEquals(1, list.size());\r
+ verifyMockCalls(1);\r
+ }\r
+ \r
+ private void verifyMockCalls(int numberOfCalls) {\r
+ if (numberOfCalls > 0) {\r
+ Mockito.verify(channelPipeline, Mockito.times(numberOfCalls)).get(\r
+ COMPONENT_NAMES.OF_VERSION_DETECTOR.name());\r
+ Mockito.verify(channelPipeline, Mockito.times(numberOfCalls)).addAfter(\r
+ Matchers.eq(COMPONENT_NAMES.OF_FRAME_DECODER.name()),\r
+ Matchers.eq(COMPONENT_NAMES.OF_VERSION_DETECTOR.name()),\r
+ Matchers.isA(OFVersionDetector.class));\r
+ Mockito.verify(channelPipeline, Mockito.times(numberOfCalls)).names();\r
+ } else {\r
+ Mockito.verify(channelPipeline, Mockito.never()).get(\r
+ COMPONENT_NAMES.OF_VERSION_DETECTOR.name());\r
+ Mockito.verify(channelPipeline, Mockito.never()).addAfter(\r
+ Matchers.eq(COMPONENT_NAMES.OF_FRAME_DECODER.name()),\r
+ Matchers.eq(COMPONENT_NAMES.OF_VERSION_DETECTOR.name()),\r
+ Matchers.isA(OFVersionDetector.class));\r
+ Mockito.verify(channelPipeline, Mockito.never()).names();\r
+ }\r
}\r
}
\ No newline at end of file