X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=openflow-protocol-impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fopenflowjava%2Fprotocol%2Fimpl%2Fserialization%2Ffactories%2FExperimenterInputMessageFactoryTest.java;h=77cc5c32d2e88eb9e69669ba80fabb938c0d88ba;hb=519a86d1ddcfb8f0fe264174e62e5424e1efba1f;hp=a7de110d04318a7f5becd0f65015be46b14f35de;hpb=221d25d81c123970bcc912f0a6329a9aa45fb5f7;p=openflowjava.git diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/ExperimenterInputMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/ExperimenterInputMessageFactoryTest.java index a7de110d..77cc5c32 100644 --- a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/ExperimenterInputMessageFactoryTest.java +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/ExperimenterInputMessageFactoryTest.java @@ -1,44 +1,129 @@ -/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */ -package org.opendaylight.openflowjava.protocol.impl.serialization.factories; - -import io.netty.buffer.ByteBuf; -import io.netty.buffer.UnpooledByteBufAllocator; - -import org.junit.Assert; -import org.junit.Test; -import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.HelloMessageFactoryTest; -import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInputBuilder; - -/** - * @author michal.polkorab - * - */ -public class ExperimenterInputMessageFactoryTest { - - private static final byte EXPERIMENTER_REQUEST_MESSAGE_CODE_TYPE = ExperimenterInputMessageFactory.MESSAGE_TYPE; - - /** - * Testing of {@link ExperimenterInputMessageFactory} for correct translation from POJO - * @throws Exception - */ - @Test - public void test() throws Exception { - ExperimenterInputBuilder eib = new ExperimenterInputBuilder(); - BufferHelper.setupHeader(eib); - eib.setExperimenter(0x0001020304L); - eib.setExpType(0x0001020304L); - ExperimenterInput ei = eib.build(); - - ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); - ExperimenterInputMessageFactory eimf = ExperimenterInputMessageFactory.getInstance(); - eimf.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, ei); - - BufferHelper.checkHeaderV13(out, EXPERIMENTER_REQUEST_MESSAGE_CODE_TYPE, 16); - Assert.assertEquals("Wrong experimenter", 0x0001020304L, out.readUnsignedInt()); - Assert.assertEquals("Wrong expType", 0x0001020304L, out.readUnsignedInt()); - } - - -} +/* + * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.openflowjava.protocol.impl.serialization.factories; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.UnpooledByteBufAllocator; + +import org.junit.Test; +import org.mockito.Matchers; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer; +import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry; +import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector; +import org.opendaylight.openflowjava.protocol.api.keys.experimenter.ExperimenterIdSerializerKey; +import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants; +import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl; +import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ExperimenterId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInputBuilder; + +/** + * @author michal.polkorab + * + */ +public class ExperimenterInputMessageFactoryTest { + + @Mock SerializerRegistry registry; + @Mock OFSerializer serializer; + private OFSerializer expFactory; + + /** + * Sets up ExperimenterInputMessageFactory + * @param real true if setup should use real registry, false when mock is desired + */ + public void startUp(boolean real) { + MockitoAnnotations.initMocks(this); + expFactory = new ExperimenterInputMessageFactory(); + if (real) { + SerializerRegistry realRegistry = new SerializerRegistryImpl(); + realRegistry.init(); + ((SerializerRegistryInjector) expFactory).injectSerializerRegistry(realRegistry); + } else { + ((SerializerRegistryInjector) expFactory).injectSerializerRegistry(registry); + } + } + + /** + * Testing of {@link ExperimenterInputMessageFactory} for correct serializer + * lookup and serialization + * @throws Exception + */ + @Test(expected=IllegalStateException.class) + public void testV10Real() throws Exception { + startUp(true); + ExperimenterInputBuilder builder = new ExperimenterInputBuilder(); + BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID); + builder.setExperimenter(new ExperimenterId(42L)); + ExperimenterInput input = builder.build(); + + ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); + expFactory.serialize(input, out); + } + + /** + * Testing of {@link ExperimenterInputMessageFactory} for correct serializer + * lookup and serialization + * @throws Exception + */ + @Test(expected=IllegalStateException.class) + public void testV13Real() throws Exception { + startUp(true); + ExperimenterInputBuilder builder = new ExperimenterInputBuilder(); + BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID); + builder.setExperimenter(new ExperimenterId(42L)); + ExperimenterInput input = builder.build(); + + ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); + expFactory.serialize(input, out); + } + + /** + * Testing of {@link ExperimenterInputMessageFactory} for correct serializer + * lookup and serialization + * @throws Exception + */ + @Test + public void testV10() throws Exception { + startUp(false); + ExperimenterInputBuilder builder = new ExperimenterInputBuilder(); + BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID); + builder.setExperimenter(new ExperimenterId(42L)); + ExperimenterInput input = builder.build(); + + Mockito.when(registry.getSerializer( + (ExperimenterIdSerializerKey) Matchers.any())).thenReturn(serializer); + + ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); + expFactory.serialize(input, out); + } + + /** + * Testing of {@link ExperimenterInputMessageFactory} for correct serializer + * lookup and serialization + * @throws Exception + */ + @Test + public void testV13() throws Exception { + startUp(false); + ExperimenterInputBuilder builder = new ExperimenterInputBuilder(); + BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID); + builder.setExperimenter(new ExperimenterId(42L)); + ExperimenterInput input = builder.build(); + + Mockito.when(registry.getSerializer( + (ExperimenterIdSerializerKey) Matchers.any())).thenReturn(serializer); + + ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); + expFactory.serialize(input, out); + } +} \ No newline at end of file