2 * Copyright (c) 2013 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.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
19 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
20 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
21 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
22 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder;
27 * @author michal.polkorab
30 public class EchoInputMessageFactoryTest {
32 private static final byte ECHO_REQUEST_MESSAGE_CODE_TYPE = 2;
33 private SerializerRegistry registry;
34 private OFSerializer<EchoInput> echoFactory;
37 * Initializes serializer registry and stores correct factory in field
40 public void startUp() {
41 registry = new SerializerRegistryImpl();
43 echoFactory = registry.getSerializer(
44 new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, EchoInput.class));
48 * Testing of {@link EchoInputMessageFactory} for correct translation from POJO
52 public void testV13() throws Exception {
53 EchoInputBuilder eib = new EchoInputBuilder();
54 BufferHelper.setupHeader(eib, EncodeConstants.OF13_VERSION_ID);
55 EchoInput ei = eib.build();
56 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
57 echoFactory.serialize(ei, out);
58 BufferHelper.checkHeaderV13(out, ECHO_REQUEST_MESSAGE_CODE_TYPE, 8);
62 * Testing of {@link EchoInputMessageFactory} for correct translation from POJO
66 public void testV10() throws Exception {
67 EchoInputBuilder eib = new EchoInputBuilder();
68 BufferHelper.setupHeader(eib, EncodeConstants.OF10_VERSION_ID);
69 EchoInput ei = eib.build();
70 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
71 echoFactory.serialize(ei, out);
72 BufferHelper.checkHeaderV10(out, ECHO_REQUEST_MESSAGE_CODE_TYPE, 8);
76 * Testing of {@link EchoInputMessageFactory} for correct data serialization
80 public void testData() throws Exception{
81 byte[] dataToTest = new byte[]{91,92,93,94,95,96,97,98};
82 EchoInputBuilder eib = new EchoInputBuilder();
83 BufferHelper.setupHeader(eib, EncodeConstants.OF13_VERSION_ID);
84 eib.setData(dataToTest);
85 EchoInput ei = eib.build();
86 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
87 echoFactory.serialize(ei, out);
88 BufferHelper.checkHeaderV13(out, ECHO_REQUEST_MESSAGE_CODE_TYPE, 8+dataToTest.length);
89 byte[] outData = new byte[dataToTest.length];
90 out.readBytes(outData);
91 Assert.assertArrayEquals("Wrong - different output data.", dataToTest, outData);