Add method to register listener for unknown msg
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / OFDatagramPacketHandlerTest.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.openflowjava.protocol.impl.core;
9
10 import static org.mockito.Mockito.when;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.channel.Channel;
13 import io.netty.channel.ChannelHandlerContext;
14 import io.netty.channel.socket.DatagramPacket;
15
16 import java.net.InetSocketAddress;
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.Mock;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
26 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
27 import org.opendaylight.openflowjava.protocol.impl.core.connection.MessageConsumer;
28 import org.opendaylight.openflowjava.util.ByteBufUtils;
29
30 /**
31  * @author madamjak
32  *
33  */
34 public class OFDatagramPacketHandlerTest {
35     @Mock ChannelHandlerContext ctxMock;
36     @Mock SwitchConnectionHandler switchConnHandler;
37     @Mock MessageConsumer consumerMock;
38     @Mock Channel channelMock;
39
40     @Before
41     public void startUp(){
42         MockitoAnnotations.initMocks(this);
43         when(ctxMock.channel()).thenReturn(channelMock);
44     }
45
46     /**
47      * Test {@link OFDatagramPacketHandler}
48      */
49     @Test
50     public void test(){
51         OFDatagramPacketHandler handler = new OFDatagramPacketHandler(switchConnHandler);
52         byte version = EncodeConstants.OF13_VERSION_ID;
53         ByteBuf messageBuffer = ByteBufUtils.hexStringToByteBuf("04 02 00 08 01 02 03 04");
54         InetSocketAddress recipientISA = InetSocketAddress.createUnresolved("localhost", 9876);
55         InetSocketAddress senderISA = InetSocketAddress.createUnresolved("192.168.15.24", 21021);
56         DatagramPacket datagramPacket = new DatagramPacket(messageBuffer, recipientISA, senderISA);
57         UdpConnectionMap.addConnection(datagramPacket.sender(), consumerMock);
58         List<Object> outList = new ArrayList<>();
59         try {
60             handler.decode(ctxMock, datagramPacket, outList);
61         } catch (Exception e) {
62             Assert.fail("Wrong - Unexcepted exception occurred");
63         }
64         VersionMessageUdpWrapper versionUdpWrapper = (VersionMessageUdpWrapper) outList.get(0);
65         Assert.assertEquals("Wrong - incorrect version has been decoded",version, versionUdpWrapper.getVersion());
66         Assert.assertEquals("Wrong - sender addresses are different", senderISA, versionUdpWrapper.getAddress());
67         messageBuffer.readerIndex(1);
68         Assert.assertEquals("Wrong - undecoded part of input ByteBuff is differnt to output",0, messageBuffer.compareTo(versionUdpWrapper.getMessageBuffer()));
69     }
70 }