Use ByteBuf.readRetainedSlice()
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / RoleReplyMessageFactoryTest.java
1 /*
2  * Copyright (c) 2015 NetIDE Consortium 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 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
9
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.UnpooledByteBufAllocator;
12 import java.math.BigInteger;
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
18 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
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.common.types.rev130731.ControllerRole;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutputBuilder;
25 import org.opendaylight.yangtools.yang.common.Uint64;
26
27 /**
28  * Unit tests for RoleReplyMessageFactory.
29  *
30  * @author giuseppex.petralia@intel.com
31  */
32 public class RoleReplyMessageFactoryTest {
33     private OFSerializer<RoleRequestOutput> factory;
34     private static final byte MESSAGE_TYPE = 25;
35     private static final byte PADDING = 4;
36
37     @Before
38     public void startUp() {
39         SerializerRegistry registry = new SerializerRegistryImpl();
40         registry.init();
41         factory = registry
42                 .getSerializer(new MessageTypeKey<>(EncodeConstants.OF_VERSION_1_3, RoleRequestOutput.class));
43     }
44
45     @Test
46     public void testSerialize() throws Exception {
47         RoleRequestOutputBuilder builder = new RoleRequestOutputBuilder();
48         BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
49         builder.setRole(ControllerRole.forValue(0));
50         builder.setGenerationId(Uint64.ONE);
51         RoleRequestOutput message = builder.build();
52
53         ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
54         factory.serialize(message, serializedBuffer);
55         BufferHelper.checkHeaderV13(serializedBuffer, MESSAGE_TYPE, 24);
56         Assert.assertEquals("Wrong role", message.getRole().getIntValue(),
57                 ControllerRole.forValue((int) serializedBuffer.readUnsignedInt()).getIntValue());
58         serializedBuffer.skipBytes(PADDING);
59         byte[] genId = new byte[Long.BYTES];
60         serializedBuffer.readBytes(genId);
61         Assert.assertEquals("Wrong generation ID", message.getGenerationId(), Uint64.valueOf(new BigInteger(1, genId)));
62     }
63 }