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.deserialization.factories;
11 import io.netty.buffer.ByteBuf;
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.MessageCodeKey;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
19 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializerRegistryImpl;
20 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
21 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
25 * @author michal.polkorab
26 * @author timotej.kubas
28 public class ErrorMessageFactoryTest {
30 private OFDeserializer<ErrorMessage> errorFactory;
33 * Initializes deserializer registry and lookups correct deserializer
36 public void startUp() {
37 DeserializerRegistry registry = new DeserializerRegistryImpl();
39 errorFactory = registry.getDeserializer(
40 new MessageCodeKey(EncodeConstants.OF13_VERSION_ID, 1, ErrorMessage.class));
44 * Test of {@link ErrorMessageFactory} for correct translation into POJO
47 public void testWithoutData() {
48 ByteBuf bb = BufferHelper.buildBuffer("00 01 00 02");
49 ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
51 BufferHelper.checkHeaderV13(builtByFactory);
52 Assert.assertEquals("Wrong type", 1, builtByFactory.getType().intValue());
53 Assert.assertEquals("Wrong code", 2, builtByFactory.getCode().intValue());
54 Assert.assertEquals("Wrong type string", "BADREQUEST", builtByFactory.getTypeString());
55 Assert.assertEquals("Wrong code string", "BADMULTIPART", builtByFactory.getCodeString());
56 Assert.assertNull("Data is not null", builtByFactory.getData());
60 * Test of {@link ErrorMessageFactory} for correct translation into POJO
63 public void testWithData() {
64 ByteBuf bb = BufferHelper.buildBuffer("00 00 00 01 00 01 02 03");
65 ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
67 BufferHelper.checkHeaderV13(builtByFactory);
68 Assert.assertEquals("Wrong type", 0, builtByFactory.getType().intValue());
69 Assert.assertEquals("Wrong code", 1, builtByFactory.getCode().intValue());
70 Assert.assertEquals("Wrong type string", "HELLOFAILED", builtByFactory.getTypeString());
71 Assert.assertEquals("Wrong code string", "EPERM", builtByFactory.getCodeString());
72 Assert.assertArrayEquals("Wrong data", new byte[]{0x00, 0x01, 0x02, 0x03}, builtByFactory.getData());
76 * Test of {@link ErrorMessageFactory} for correct translation into POJO
79 public void testWithIncorrectTypeEnum() {
80 ByteBuf bb = BufferHelper.buildBuffer("00 20 00 05 00 01 02 03");
81 ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
83 BufferHelper.checkHeaderV13(builtByFactory);
84 Assert.assertEquals("Wrong type", 32, builtByFactory.getType().intValue());
85 Assert.assertEquals("Wrong code", 5, builtByFactory.getCode().intValue());
86 Assert.assertEquals("Wrong type string", "UNKNOWN_TYPE", builtByFactory.getTypeString());
87 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
88 Assert.assertArrayEquals("Wrong data", new byte[]{0x00, 0x01, 0x02, 0x03}, builtByFactory.getData());
92 * Test of {@link ErrorMessageFactory} for correct translation into POJO
95 public void testWithIncorrectCodeEnum() {
96 ByteBuf bb = BufferHelper.buildBuffer("00 03 00 10 00 01 02 03");
97 ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
99 BufferHelper.checkHeaderV13(builtByFactory);
100 Assert.assertEquals("Wrong type", 3, builtByFactory.getType().intValue());
101 Assert.assertEquals("Wrong code", 16, builtByFactory.getCode().intValue());
102 Assert.assertEquals("Wrong type string", "BADINSTRUCTION", builtByFactory.getTypeString());
103 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
104 Assert.assertArrayEquals("Wrong data", new byte[]{0x00, 0x01, 0x02, 0x03}, builtByFactory.getData());