2 * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.UnpooledByteBufAllocator;
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;
31 * @author michal.polkorab
34 public class ExperimenterInputMessageFactoryTest {
36 @Mock SerializerRegistry registry;
37 @Mock OFSerializer<ExperimenterInput> serializer;
38 private OFSerializer<ExperimenterInput> expFactory;
41 * Sets up ExperimenterInputMessageFactory
42 * @param real true if setup should use real registry, false when mock is desired
44 public void startUp(boolean real) {
45 MockitoAnnotations.initMocks(this);
46 expFactory = new ExperimenterInputMessageFactory();
48 SerializerRegistry realRegistry = new SerializerRegistryImpl();
50 ((SerializerRegistryInjector) expFactory).injectSerializerRegistry(realRegistry);
52 ((SerializerRegistryInjector) expFactory).injectSerializerRegistry(registry);
57 * Testing of {@link ExperimenterInputMessageFactory} for correct serializer
58 * lookup and serialization
61 @Test(expected=IllegalStateException.class)
62 public void testV10Real() throws Exception {
64 ExperimenterInputBuilder builder = new ExperimenterInputBuilder();
65 BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
66 builder.setExperimenter(new ExperimenterId(42L));
67 ExperimenterInput input = builder.build();
69 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
70 expFactory.serialize(input, out);
74 * Testing of {@link ExperimenterInputMessageFactory} for correct serializer
75 * lookup and serialization
78 @Test(expected=IllegalStateException.class)
79 public void testV13Real() throws Exception {
81 ExperimenterInputBuilder builder = new ExperimenterInputBuilder();
82 BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
83 builder.setExperimenter(new ExperimenterId(42L));
84 ExperimenterInput input = builder.build();
86 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
87 expFactory.serialize(input, out);
91 * Testing of {@link ExperimenterInputMessageFactory} for correct serializer
92 * lookup and serialization
96 public void testV10() throws Exception {
98 ExperimenterInputBuilder builder = new ExperimenterInputBuilder();
99 BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
100 builder.setExperimenter(new ExperimenterId(42L));
101 ExperimenterInput input = builder.build();
103 Mockito.when(registry.getSerializer(
104 (ExperimenterIdSerializerKey<?>) Matchers.any())).thenReturn(serializer);
106 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
107 expFactory.serialize(input, out);
111 * Testing of {@link ExperimenterInputMessageFactory} for correct serializer
112 * lookup and serialization
116 public void testV13() throws Exception {
118 ExperimenterInputBuilder builder = new ExperimenterInputBuilder();
119 BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
120 builder.setExperimenter(new ExperimenterId(42L));
121 ExperimenterInput input = builder.build();
123 Mockito.when(registry.getSerializer(
124 (ExperimenterIdSerializerKey<?>) Matchers.any())).thenReturn(serializer);
126 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
127 expFactory.serialize(input, out);