17b3f7addbb6814de34e73d00867340071c36c5e
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / ExperimenterInputMessageFactoryTest.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.factories;\r
10 \r
11 import io.netty.buffer.ByteBuf;\r
12 import io.netty.buffer.UnpooledByteBufAllocator;\r
13 \r
14 import org.junit.Test;\r
15 import org.mockito.Matchers;\r
16 import org.mockito.Mock;\r
17 import org.mockito.Mockito;\r
18 import org.mockito.MockitoAnnotations;\r
19 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;\r
20 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;\r
21 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;\r
22 import org.opendaylight.openflowjava.protocol.api.keys.experimenter.ExperimenterIdSerializerKey;\r
23 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;\r
24 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;\r
25 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ExperimenterId;\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput;\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInputBuilder;\r
29 \r
30 /**\r
31  * @author michal.polkorab\r
32  *\r
33  */\r
34 public class ExperimenterInputMessageFactoryTest {\r
35 \r
36     @Mock SerializerRegistry registry;\r
37     @Mock OFSerializer<ExperimenterInput> serializer;\r
38     private OFSerializer<ExperimenterInput> expFactory;\r
39 \r
40     /**\r
41      * Sets up ExperimenterInputMessageFactory\r
42      * @param real true if setup should use real registry, false when mock is desired\r
43      */\r
44     public void startUp(boolean real) {\r
45         MockitoAnnotations.initMocks(this);\r
46         expFactory = new ExperimenterInputMessageFactory();\r
47         if (real) {\r
48             SerializerRegistry realRegistry = new SerializerRegistryImpl();\r
49             realRegistry.init();\r
50             ((SerializerRegistryInjector) expFactory).injectSerializerRegistry(realRegistry);\r
51         } else {\r
52             ((SerializerRegistryInjector) expFactory).injectSerializerRegistry(registry);\r
53         }\r
54     }\r
55 \r
56     /**\r
57      * Testing of {@link ExperimenterInputMessageFactory} for correct serializer\r
58      * lookup and serialization\r
59      * @throws Exception \r
60      */\r
61     @Test(expected=IllegalStateException.class)\r
62     public void testV10Real() throws Exception {\r
63         startUp(true);\r
64         ExperimenterInputBuilder builder = new ExperimenterInputBuilder();\r
65         BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);\r
66         builder.setExperimenter(new ExperimenterId(42L));\r
67         ExperimenterInput input = builder.build();\r
68         \r
69         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();\r
70         expFactory.serialize(input, out);\r
71     }\r
72 \r
73     /**\r
74      * Testing of {@link ExperimenterInputMessageFactory} for correct serializer\r
75      * lookup and serialization\r
76      * @throws Exception \r
77      */\r
78     @Test(expected=IllegalStateException.class)\r
79     public void testV13Real() throws Exception {\r
80         startUp(true);\r
81         ExperimenterInputBuilder builder = new ExperimenterInputBuilder();\r
82         BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);\r
83         builder.setExperimenter(new ExperimenterId(42L));\r
84         ExperimenterInput input = builder.build();\r
85         \r
86         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();\r
87         expFactory.serialize(input, out);\r
88     }\r
89 \r
90     /**\r
91      * Testing of {@link ExperimenterInputMessageFactory} for correct serializer\r
92      * lookup and serialization\r
93      * @throws Exception \r
94      */\r
95     @Test\r
96     public void testV10() throws Exception {\r
97         startUp(false);\r
98         ExperimenterInputBuilder builder = new ExperimenterInputBuilder();\r
99         BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);\r
100         builder.setExperimenter(new ExperimenterId(42L));\r
101         ExperimenterInput input = builder.build();\r
102 \r
103         Mockito.when(registry.getSerializer(\r
104                 (ExperimenterIdSerializerKey<?>) Matchers.any())).thenReturn(serializer);\r
105 \r
106         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();\r
107         expFactory.serialize(input, out);\r
108     }\r
109 \r
110     /**\r
111      * Testing of {@link ExperimenterInputMessageFactory} for correct serializer\r
112      * lookup and serialization\r
113      * @throws Exception \r
114      */\r
115     @Test\r
116     public void testV13() throws Exception {\r
117         startUp(false);\r
118         ExperimenterInputBuilder builder = new ExperimenterInputBuilder();\r
119         BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);\r
120         builder.setExperimenter(new ExperimenterId(42L));\r
121         ExperimenterInput input = builder.build();\r
122 \r
123         Mockito.when(registry.getSerializer(\r
124                 (ExperimenterIdSerializerKey<?>) Matchers.any())).thenReturn(serializer);\r
125 \r
126         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();\r
127         expFactory.serialize(input, out);\r
128     }\r
129 }