Extensibility support (deserialization part)
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / OF10ErrorMessageFactoryTest.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.deserialization.factories;
10
11 import io.netty.buffer.ByteBuf;
12
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.impl.util.EncodeConstants;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
23
24 /**
25  * @author michal.polkorab
26  */
27 public class OF10ErrorMessageFactoryTest {
28     private OFDeserializer<ErrorMessage> errorFactory;
29
30     /**
31      * Initializes deserializer registry and lookups correct deserializer
32      */
33     @Before
34     public void startUp() {
35         DeserializerRegistry registry = new DeserializerRegistryImpl();
36         registry.init();
37         errorFactory = registry.getDeserializer(
38                 new MessageCodeKey(EncodeConstants.OF10_VERSION_ID, 1, ErrorMessage.class));
39     }
40
41     /**
42      * Test of {@link OF10ErrorMessageFactory} for correct translation into POJO
43      */
44         @Test
45         public void testWithoutData() {
46                 ByteBuf bb = BufferHelper.buildBuffer("00 01 00 02");
47                 ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
48
49                 BufferHelper.checkHeaderV10(builtByFactory);
50                 Assert.assertEquals("Wrong type", 1, builtByFactory.getType().intValue());
51                 Assert.assertEquals("Wrong code", 2, builtByFactory.getCode().intValue());
52                 Assert.assertEquals("Wrong type string", "BADREQUEST", builtByFactory.getTypeString());
53                 Assert.assertEquals("Wrong code string", "BADSTAT", builtByFactory.getCodeString());
54                 Assert.assertNull("Data is not null", builtByFactory.getData());
55         }
56         
57         /**
58      * Test of {@link OF10ErrorMessageFactory} for correct translation into POJO
59      */
60         @Test
61         public void testWithData() {
62                 ByteBuf bb = BufferHelper.buildBuffer("00 00 00 01 00 01 02 03");
63                 ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
64
65                 BufferHelper.checkHeaderV10(builtByFactory);
66                 Assert.assertEquals("Wrong type", 0, builtByFactory.getType().intValue());
67                 Assert.assertEquals("Wrong code", 1, builtByFactory.getCode().intValue());
68                 Assert.assertEquals("Wrong type string", "HELLOFAILED", builtByFactory.getTypeString());
69                 Assert.assertEquals("Wrong code string", "EPERM", builtByFactory.getCodeString());
70                 Assert.assertArrayEquals("Wrong data", new byte[]{0x00, 0x01, 0x02, 0x03}, builtByFactory.getData());
71         }
72         
73         /**
74      * Test of {@link OF10ErrorMessageFactory} for correct translation into POJO
75      */
76         @Test
77         public void testWithIncorrectTypeEnum() {
78                 ByteBuf bb = BufferHelper.buildBuffer("00 0A 00 05 00 01 02 03");
79                 ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
80
81                 BufferHelper.checkHeaderV10(builtByFactory);
82                 Assert.assertEquals("Wrong type", 10, builtByFactory.getType().intValue());
83                 Assert.assertEquals("Wrong code", 5, builtByFactory.getCode().intValue());
84                 Assert.assertEquals("Wrong type string", "UNKNOWN_TYPE", builtByFactory.getTypeString());
85                 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
86                 Assert.assertArrayEquals("Wrong data", new byte[]{0x00, 0x01, 0x02, 0x03}, builtByFactory.getData());
87         }
88         
89         /**
90      * Test of {@link OF10ErrorMessageFactory} for correct translation into POJO
91      */
92         @Test
93         public void testWithIncorrectCodeEnum() {
94                 ByteBuf bb = BufferHelper.buildBuffer("00 03 00 06 00 01 02 03");
95                 ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
96
97                 BufferHelper.checkHeaderV10(builtByFactory);
98                 Assert.assertEquals("Wrong type", 3, builtByFactory.getType().intValue());
99                 Assert.assertEquals("Wrong code", 6, builtByFactory.getCode().intValue());
100                 Assert.assertEquals("Wrong type string", "FLOWMODFAILED", builtByFactory.getTypeString());
101                 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
102                 Assert.assertArrayEquals("Wrong data", new byte[]{0x00, 0x01, 0x02, 0x03}, builtByFactory.getData());
103         }
104
105 }