28add5c658bc9a777c970bf86aa307a8977edabd
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / SerializationFactoryTest.java
1 /*\r
2  * Copyright (c) 2014 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;\r
10 \r
11 import static org.junit.Assert.assertEquals;\r
12 import io.netty.buffer.ByteBuf;\r
13 import io.netty.buffer.PooledByteBufAllocator;\r
14 \r
15 import org.junit.Test;\r
16 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;\r
17 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;\r
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInputBuilder;\r
19 \r
20 /**\r
21  * @author michal.polkorab\r
22  *\r
23  */\r
24 public class SerializationFactoryTest {\r
25 \r
26     /**\r
27      * Test serializer lookup & serialization\r
28      */\r
29     @Test\r
30     public void test() {\r
31         SerializerRegistry registry = new SerializerRegistryImpl();\r
32         registry.init();\r
33         SerializationFactory factory = new SerializationFactory();\r
34         factory.setSerializerTable(registry);\r
35         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();\r
36         HelloInputBuilder helloBuilder = new HelloInputBuilder();\r
37         helloBuilder.setVersion((short) EncodeConstants.OF10_VERSION_ID);\r
38         helloBuilder.setXid(123456L);\r
39         helloBuilder.setElements(null);\r
40         factory.messageToBuffer(EncodeConstants.OF10_VERSION_ID, buffer, helloBuilder.build());\r
41         assertEquals("Serialization failed", EncodeConstants.OFHEADER_SIZE, buffer.readableBytes());\r
42     }\r
43 \r
44     /**\r
45      * Test serializer not found scenario\r
46      */\r
47     @Test(expected=IllegalStateException.class)\r
48     public void testNotExistingSerializer() {\r
49         SerializerRegistry registry = new SerializerRegistryImpl();\r
50         registry.init();\r
51         SerializationFactory factory = new SerializationFactory();\r
52         factory.setSerializerTable(registry);\r
53         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();\r
54         HelloInputBuilder helloBuilder = new HelloInputBuilder();\r
55         helloBuilder.setVersion((short) EncodeConstants.OF10_VERSION_ID);\r
56         helloBuilder.setXid(123456L);\r
57         helloBuilder.setElements(null);\r
58         factory.messageToBuffer((short) 0, buffer, helloBuilder.build());\r
59     }\r
60 }