2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
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;
28 * Unit tests for RoleRequestInputMessageFactory.
30 * @author timotej.kubas
31 * @author michal.polkorab
33 public class RoleRequestInputMessageFactoryTest {
34 private static final byte MESSAGE_TYPE = 24;
35 private static final int MESSAGE_LENGTH = 24;
36 private static final byte PADDING_IN_ROLE_REQUEST_MESSAGE = 4;
37 private SerializerRegistry registry;
38 private OFSerializer<RoleRequestInput> roleFactory;
41 * Initializes serializer registry and stores correct factory in field.
44 public void startUp() {
45 registry = new SerializerRegistryImpl();
47 roleFactory = registry.getSerializer(
48 new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, RoleRequestInput.class));
52 * Testing of {@link RoleRequestInputMessageFactory} for correct translation from POJO.
55 public void testRoleRequestInputMessage() throws Exception {
56 RoleRequestInputBuilder builder = new RoleRequestInputBuilder();
57 BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
58 builder.setRole(ControllerRole.forValue(2));
59 byte[] generationId = new byte[]{(byte) 0xFF, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
60 builder.setGenerationId(new BigInteger(1, generationId));
61 RoleRequestInput message = builder.build();
63 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
64 roleFactory.serialize(message, out);
66 BufferHelper.checkHeaderV13(out, MESSAGE_TYPE, MESSAGE_LENGTH);
67 Assert.assertEquals("Wrong role", message.getRole().getIntValue(),
68 ControllerRole.forValue((int) out.readUnsignedInt()).getIntValue());
69 out.skipBytes(PADDING_IN_ROLE_REQUEST_MESSAGE);
70 byte[] genId = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
72 Assert.assertEquals("Wrong generation ID", message.getGenerationId(), new BigInteger(1, genId));