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