Remove EncodeConstants.SIZE_OF_{BYTE,SHORT,INT,LONG}_IN_BYTES
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / RoleRequestInputMessageFactoryTest.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 java.math.BigInteger;
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.common.types.rev130731.ControllerRole;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInputBuilder;
26 import org.opendaylight.yangtools.yang.common.Uint64;
27
28 /**
29  * Unit tests for RoleRequestInputMessageFactory.
30  *
31  * @author timotej.kubas
32  * @author michal.polkorab
33  */
34 public class RoleRequestInputMessageFactoryTest {
35     private static final byte MESSAGE_TYPE = 24;
36     private static final int MESSAGE_LENGTH = 24;
37     private static final byte PADDING_IN_ROLE_REQUEST_MESSAGE = 4;
38     private SerializerRegistry registry;
39     private OFSerializer<RoleRequestInput> roleFactory;
40
41     /**
42      * Initializes serializer registry and stores correct factory in field.
43      */
44     @Before
45     public void startUp() {
46         registry = new SerializerRegistryImpl();
47         registry.init();
48         roleFactory = registry.getSerializer(
49                 new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, RoleRequestInput.class));
50     }
51
52     /**
53      * Testing of {@link RoleRequestInputMessageFactory} for correct translation from POJO.
54      */
55     @Test
56     public void testRoleRequestInputMessage() throws Exception {
57         RoleRequestInputBuilder builder = new RoleRequestInputBuilder();
58         BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
59         builder.setRole(ControllerRole.forValue(2));
60         byte[] generationId = new byte[]{(byte) 0xFF, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
61         builder.setGenerationId(new BigInteger(1, generationId));
62         RoleRequestInput message = builder.build();
63
64         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
65         roleFactory.serialize(message, out);
66
67         BufferHelper.checkHeaderV13(out, MESSAGE_TYPE, MESSAGE_LENGTH);
68         Assert.assertEquals("Wrong role", message.getRole().getIntValue(),
69                 ControllerRole.forValue((int) out.readUnsignedInt()).getIntValue());
70         out.skipBytes(PADDING_IN_ROLE_REQUEST_MESSAGE);
71         byte[] genId = new byte[Long.BYTES];
72         out.readBytes(genId);
73         Assert.assertEquals("Wrong generation ID", message.getGenerationId(), Uint64.valueOf(new BigInteger(1, genId)));
74     }
75 }