Add unit tests for EchoInputMessageFactory, EchoReplyInputMessageFactory
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / EchoReplyInputMessageFactoryTest.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 org.junit.Assert;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.UnpooledByteBufAllocator;
14
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.MessageTypeKey;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
19 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
20 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
21 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
22 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInputBuilder;
25
26 /**
27  * @author michal.polkorab
28  *
29  */
30 public class EchoReplyInputMessageFactoryTest {
31
32     private static final byte ECHO_REPLY_MESSAGE_CODE_TYPE = 3;
33     private SerializerRegistry registry;
34     private OFSerializer<EchoReplyInput> 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, EchoReplyInput.class));
45     }
46
47     /**
48      * Testing of {@link EchoReplyInputMessageFactory} for correct translation from POJO
49      * @throws Exception 
50      */
51     @Test
52     public void testV13() throws Exception {
53         EchoReplyInputBuilder erib = new EchoReplyInputBuilder();
54         BufferHelper.setupHeader(erib, EncodeConstants.OF13_VERSION_ID);
55         EchoReplyInput eri = erib.build();
56         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
57         echoFactory.serialize(eri, out);
58         BufferHelper.checkHeaderV13(out, ECHO_REPLY_MESSAGE_CODE_TYPE, 8);
59     }
60     
61     /**
62      * Testing of {@link EchoReplyInputMessageFactory} for correct translation from POJO
63      * @throws Exception 
64      */
65     @Test
66     public void testV10() throws Exception {
67         EchoReplyInputBuilder erib = new EchoReplyInputBuilder();
68         BufferHelper.setupHeader(erib, EncodeConstants.OF10_VERSION_ID);
69         EchoReplyInput eri = erib.build();
70         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
71         echoFactory.serialize(eri, out);
72         BufferHelper.checkHeaderV10(out, ECHO_REPLY_MESSAGE_CODE_TYPE, 8);
73     }
74
75     /**
76      * Testing of {@link EchoReplyInputMessageFactory} for correct message serialization
77      * @throws Exception
78      */
79     @Test
80     public void testDataSerialize()throws Exception {
81         byte[] dataToTest = new byte[]{91,92,93,94,95,96,97,98};
82         EchoReplyInputBuilder erib = new EchoReplyInputBuilder();
83         BufferHelper.setupHeader(erib, EncodeConstants.OF13_VERSION_ID);
84         erib.setData(dataToTest);
85         EchoReplyInput eri = erib.build();
86         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
87         echoFactory.serialize(eri, out);
88         BufferHelper.checkHeaderV13(out, ECHO_REPLY_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);
92         out.release();
93     }
94 }