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