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