Add method to register listener for unknown msg
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / OFVersionDetectorTest.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
13 import io.netty.buffer.ByteBuf;
14 import io.netty.channel.ChannelHandlerContext;
15 import java.util.ArrayList;
16 import java.util.List;
17 import org.junit.Assert;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.Mock;
22 import org.mockito.runners.MockitoJUnitRunner;
23 import org.opendaylight.openflowjava.util.ByteBufUtils;
24
25 /**
26  * Test for {@link org.opendaylight.openflowjava.protocol.impl.core.OFVersionDetector}.
27  */
28 @RunWith(MockitoJUnitRunner.class)
29 public class OFVersionDetectorTest {
30
31     @Mock
32     ChannelHandlerContext channelHandlerContext;
33
34     private OFVersionDetector detector;
35     private List<Object> list = new ArrayList<>();
36
37     @Before
38     public void setUp() {
39         list.clear();
40         detector = new OFVersionDetector();
41     }
42
43     @Test
44     public void testDecode13ProtocolMessage() {
45         detector.decode(channelHandlerContext, ByteBufUtils.hexStringToByteBuf("04 00 00 08 00 00 00 01"), list);
46         Assert.assertEquals(7, ((VersionMessageWrapper) list.get(0)).getMessageBuffer().readableBytes());
47     }
48
49     @Test
50     public void testDecode10ProtocolMessage() {
51         detector.decode(channelHandlerContext, ByteBufUtils.hexStringToByteBuf("01 00 00 08 00 00 00 01"), list);
52         Assert.assertEquals(7, ((VersionMessageWrapper) list.get(0)).getMessageBuffer().readableBytes());
53     }
54
55     @Test
56     public void testDecodeEmptyProtocolMessage() {
57         ByteBuf byteBuffer = ByteBufUtils.hexStringToByteBuf("01 00 00 08 00 00 00 01").skipBytes(8);
58         detector.decode(channelHandlerContext, byteBuffer, list);
59         assertEquals(0, byteBuffer.refCnt());
60     }
61
62     @Test
63     public void testDecodeNotSupportedVersionProtocolMessage() {
64         detector.decode(channelHandlerContext, ByteBufUtils.hexStringToByteBuf("02 01 00 08 00 00 00 01"), list);
65         Assert.assertEquals("List is not empty", 0, list.size());
66     }
67
68     @Test
69     public void testDecodeHelloProtocolMessage() {
70         detector.decode(channelHandlerContext, ByteBufUtils.hexStringToByteBuf("05 00 00 08 00 00 00 01"), list);
71         Assert.assertEquals(7, ((VersionMessageWrapper) list.get(0)).getMessageBuffer().readableBytes());
72     }
73 }