Bug 1277 - Move ByteBuffUtils to separate bundle
[openflowjava.git] / 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
14 import java.math.BigInteger;
15
16 import junit.framework.Assert;
17
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.openflowjava.protocol.api.extensibility.MessageTypeKey;
21 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
22 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
23 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
24 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
25 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ControllerRole;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInputBuilder;
29
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      * @throws Exception 
55      */
56     @Test
57     public void testRoleRequestInputMessage() throws Exception {
58         RoleRequestInputBuilder builder = new RoleRequestInputBuilder();
59         BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
60         builder.setRole(ControllerRole.forValue(2));
61         byte[] generationId = new byte[]{(byte) 0xFF, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
62         builder.setGenerationId(new BigInteger(1, generationId));
63         RoleRequestInput message = builder.build();
64         
65         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
66         roleFactory.serialize(message, out);
67         
68         BufferHelper.checkHeaderV13(out, MESSAGE_TYPE, MESSAGE_LENGTH);
69         Assert.assertEquals("Wrong role", message.getRole().getIntValue(), ControllerRole.forValue((int) out.readUnsignedInt()).getIntValue());
70         out.skipBytes(PADDING_IN_ROLE_REQUEST_MESSAGE);
71         byte[] genId = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
72         out.readBytes(genId);
73         Assert.assertEquals("Wrong generation ID", message.getGenerationId(), new BigInteger(1, genId));
74     }
75 }