Draft implementation of integration tests
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / OFFrameDecoderTest.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
2 package org.opendaylight.openflowjava.protocol.impl.core;\r
3 \r
4 import io.netty.buffer.ByteBuf;\r
5 import io.netty.channel.ChannelHandlerContext;\r
6 import io.netty.channel.ChannelPipeline;\r
7 \r
8 import java.util.ArrayList;\r
9 import java.util.List;\r
10 \r
11 import org.junit.Assert;\r
12 import org.junit.Before;\r
13 import org.junit.Test;\r
14 import org.junit.runner.RunWith;\r
15 import org.mockito.Matchers;\r
16 import org.mockito.Mock;\r
17 import org.mockito.Mockito;\r
18 import org.mockito.runners.MockitoJUnitRunner;\r
19 import org.opendaylight.openflowjava.protocol.impl.core.TcpHandler.COMPONENT_NAMES;\r
20 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;\r
21 \r
22 import com.google.common.collect.Lists;\r
23 \r
24 /**\r
25  * Testing class of {@link OFFrameDecoder}\r
26  * \r
27  * @author michal.polkorab\r
28  */\r
29 @RunWith(MockitoJUnitRunner.class)\r
30 public class OFFrameDecoderTest {\r
31 \r
32     @Mock\r
33     ChannelHandlerContext channelHandlerContext;\r
34 \r
35     @Mock\r
36     ChannelPipeline channelPipeline;\r
37 \r
38     private OFFrameDecoder decoder;\r
39     private List<Object> list = new ArrayList<>();\r
40 \r
41     /**\r
42      * Sets up tests\r
43      */\r
44     @Before\r
45     public void setUp() {\r
46         Mockito.when(channelHandlerContext.pipeline()).thenReturn(\r
47                 channelPipeline);\r
48         Mockito.when(channelPipeline.get(Matchers.anyString()))\r
49                 .thenReturn(null);\r
50         Mockito.when(channelPipeline.names()).thenReturn(\r
51                 Lists.newArrayList("xx"));\r
52         list.clear();\r
53         decoder = new OFFrameDecoder();\r
54     }\r
55 \r
56     /**\r
57      * Test of decoding\r
58      * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}\r
59      * \r
60      * @throws Exception\r
61      */\r
62     @Test\r
63     public void testDecode8BMessage() throws Exception {\r
64         decoder.decode(channelHandlerContext,\r
65                 ByteBufUtils.hexStringToByteBuf("04 00 00 08 00 00 00 01"),\r
66                 list);\r
67 \r
68         Assert.assertEquals(8, ((ByteBuf) list.get(0)).readableBytes());\r
69         verifyMockCalls(1);\r
70     }\r
71 \r
72     /**\r
73      * Test of decoding\r
74      * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}\r
75      * \r
76      * @throws Exception\r
77      */\r
78     @Test\r
79     public void testDecode16BMessage() throws Exception {\r
80         decoder.decode(channelHandlerContext,\r
81                 ByteBufUtils.hexStringToByteBuf("04 00 00 10 00 00 00 00 00 00 00 00 00 00 00 42"),\r
82                 list);\r
83 \r
84         Assert.assertEquals(16, ((ByteBuf) list.get(0)).readableBytes());\r
85         verifyMockCalls(1);\r
86     }\r
87 \r
88     /**\r
89      * Test of decoding\r
90      * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}\r
91      * \r
92      * @throws Exception\r
93      */\r
94     @Test\r
95     public void testDecodeIncompleteMessage() throws Exception {\r
96         decoder.decode(channelHandlerContext,\r
97                 ByteBufUtils.hexStringToByteBuf("04 00 00 08 00"),\r
98                 list);\r
99 \r
100         Assert.assertEquals("List is not empty", 0, list.size());\r
101         verifyMockCalls(0);\r
102     }\r
103 \r
104     /**\r
105      * Test of decoding\r
106      * {@link OFFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}\r
107      * \r
108      * @throws Exception\r
109      */\r
110     @Test\r
111     public void testDecodeCompleteAndPartialMessage() throws Exception {\r
112         decoder.decode(channelHandlerContext,\r
113                 ByteBufUtils.hexStringToByteBuf("04 00 00 08 00 00 00 01 04 00 00 08 00"),\r
114                 list);\r
115 \r
116         Assert.assertEquals(8, ((ByteBuf) list.get(0)).readableBytes());\r
117         Assert.assertEquals(1, list.size());\r
118         verifyMockCalls(1);\r
119     }\r
120     \r
121     private void verifyMockCalls(int numberOfCalls) {\r
122         if (numberOfCalls > 0) {\r
123             Mockito.verify(channelPipeline, Mockito.times(numberOfCalls)).get(\r
124                     COMPONENT_NAMES.OF_VERSION_DETECTOR.name());\r
125             Mockito.verify(channelPipeline, Mockito.times(numberOfCalls)).addAfter(\r
126                     Matchers.eq(COMPONENT_NAMES.OF_FRAME_DECODER.name()),\r
127                     Matchers.eq(COMPONENT_NAMES.OF_VERSION_DETECTOR.name()),\r
128                     Matchers.isA(OFVersionDetector.class));\r
129             Mockito.verify(channelPipeline, Mockito.times(numberOfCalls)).names();\r
130         } else {\r
131             Mockito.verify(channelPipeline, Mockito.never()).get(\r
132                     COMPONENT_NAMES.OF_VERSION_DETECTOR.name());\r
133             Mockito.verify(channelPipeline, Mockito.never()).addAfter(\r
134                     Matchers.eq(COMPONENT_NAMES.OF_FRAME_DECODER.name()),\r
135                     Matchers.eq(COMPONENT_NAMES.OF_VERSION_DETECTOR.name()),\r
136                     Matchers.isA(OFVersionDetector.class));\r
137             Mockito.verify(channelPipeline, Mockito.never()).names();\r
138         }\r
139     }\r
140 }