022b334951df8f4915dc9229f9a0506d1bfe152a
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / OF10VendorInputMessageFactoryTest.java
1 /*\r
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 \r
9 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;\r
10 \r
11 import io.netty.buffer.ByteBuf;\r
12 import io.netty.buffer.UnpooledByteBufAllocator;\r
13 \r
14 import org.junit.Assert;\r
15 import org.junit.Before;\r
16 import org.junit.Test;\r
17 import org.opendaylight.openflowjava.protocol.api.extensibility.MessageTypeKey;\r
18 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;\r
19 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;\r
20 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;\r
21 import org.opendaylight.openflowjava.protocol.impl.serialization.experimenters.OF10VendorInputMessageFactory;\r
22 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;\r
23 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput;\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInputBuilder;\r
26 \r
27 /**\r
28  * @author michal.polkorab\r
29  *\r
30  */\r
31 public class OF10VendorInputMessageFactoryTest {\r
32 \r
33     private SerializerRegistry registry;\r
34     private OFSerializer<ExperimenterInput> vendorFactory;\r
35 \r
36     /**\r
37      * Initializes serializer registry and stores correct factory in field\r
38      */\r
39     @Before\r
40     public void startUp() {\r
41         registry = new SerializerRegistryImpl();\r
42         registry.init();\r
43         vendorFactory = registry.getSerializer(\r
44                 new MessageTypeKey<>(EncodeConstants.OF10_VERSION_ID, ExperimenterInput.class));\r
45     }\r
46 \r
47 \r
48     /**\r
49      * Testing of {@link OF10VendorInputMessageFactory} for correct translation from POJO\r
50      * @throws Exception \r
51      */\r
52     @Test\r
53     public void test() throws Exception {\r
54         ExperimenterInputBuilder builder = new ExperimenterInputBuilder();\r
55         BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);\r
56         builder.setExperimenter(0x0001020304L);\r
57         builder.setData(new byte[] {0x01, 0x02, 0x03, 0x04});\r
58         ExperimenterInput message = builder.build();\r
59         \r
60         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();\r
61         vendorFactory.serialize(message, out);\r
62         \r
63         BufferHelper.checkHeaderV10(out, (byte) 4, 16);\r
64         Assert.assertEquals("Wrong experimenter", 0x0001020304L, out.readUnsignedInt());\r
65         byte[] data = new byte[4];\r
66         out.readBytes(data);\r
67         Assert.assertArrayEquals("Wrong data", message.getData(), data);\r
68         Assert.assertTrue("Unread data", out.readableBytes() == 0);\r
69     }\r
70 \r
71 }