Extend openflow-protocol-impl serialization
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / ErrorMessageFactoryTest.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 org.junit.Assert;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
16 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
17 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
18 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
19 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
20 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
21 import org.opendaylight.openflowjava.util.ByteBufUtils;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessageBuilder;
24
25 /**
26  * @author giuseppex.petralia@intel.com
27  *
28  */
29 public class ErrorMessageFactoryTest {
30     private static final byte MESSAGE_TYPE = 1;
31     private OFSerializer<ErrorMessage> factory;
32
33     @Before
34     public void startUp() {
35         SerializerRegistry registry = new SerializerRegistryImpl();
36         registry.init();
37         factory = registry.getSerializer(new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, ErrorMessage.class));
38     }
39
40     @Test
41     public void testSerialize() throws Exception {
42         ErrorMessageBuilder builder = new ErrorMessageBuilder();
43         BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
44         builder.setType(10);
45         builder.setCode(20);
46         byte[] data = ByteBufUtils.hexStringToBytes("00 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14");
47         builder.setData(data);
48         ErrorMessage message = builder.build();
49
50         ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
51         factory.serialize(message, serializedBuffer);
52         BufferHelper.checkHeaderV13(serializedBuffer, MESSAGE_TYPE, 28);
53         Assert.assertEquals("Wrong Type", message.getType().intValue(), serializedBuffer.readShort());
54         Assert.assertEquals("Wrong Code", message.getCode().intValue(), serializedBuffer.readShort());
55         Assert.assertArrayEquals("Wrong data", message.getData(),
56                 serializedBuffer.readBytes(serializedBuffer.readableBytes()).array());
57     }
58 }