new serialization factories and tests
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / ExperimenterInputMessageFactoryTest.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
2 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;\r
3 \r
4 import io.netty.buffer.ByteBuf;\r
5 import io.netty.buffer.UnpooledByteBufAllocator;\r
6 \r
7 import org.junit.Assert;\r
8 import org.junit.Test;\r
9 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.HelloMessageFactoryTest;\r
10 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;\r
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput;\r
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInputBuilder;\r
13 \r
14 /**\r
15  * @author michal.polkorab\r
16  * @author timotej.kubas\r
17  */\r
18 public class ExperimenterInputMessageFactoryTest {\r
19 \r
20     private static final byte EXPERIMENTER_REQUEST_MESSAGE_CODE_TYPE = ExperimenterInputMessageFactory.MESSAGE_TYPE;\r
21     \r
22     /**\r
23      * Testing of {@link ExperimenterInputMessageFactory} for correct translation from POJO\r
24      * @throws Exception \r
25      */\r
26     @Test\r
27     public void test() throws Exception {\r
28         ExperimenterInputBuilder builder = new ExperimenterInputBuilder();\r
29         BufferHelper.setupHeader(builder);\r
30         builder.setExperimenter(0x0001020304L);\r
31         builder.setExpType(0x0001020304L);\r
32         builder.setData(new byte[] {0x01, 0x02, 0x03});\r
33         ExperimenterInput message = builder.build();\r
34         \r
35         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();\r
36         ExperimenterInputMessageFactory factory = ExperimenterInputMessageFactory.getInstance();\r
37         factory.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, message);\r
38         \r
39         BufferHelper.checkHeaderV13(out, EXPERIMENTER_REQUEST_MESSAGE_CODE_TYPE, factory.computeLength());\r
40         Assert.assertEquals("Wrong experimenter", 0x0001020304L, out.readUnsignedInt());\r
41         Assert.assertEquals("Wrong expType", 0x0001020304L, out.readUnsignedInt());\r
42         Assert.assertArrayEquals("Wrong data", message.getData(), readData(out));\r
43     }\r
44     \r
45     private static byte[] readData(ByteBuf input) {\r
46         byte[] data = new byte[input.readableBytes()]; \r
47         input.readBytes(data);\r
48         return data;\r
49     }\r
50 }\r