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