Remove trailing whitespace
[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.api.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 ErrorMessageFactory} for correct translation into POJO
43      */
44     @Test
45     public void testWithoutData() {
46         ByteBuf bb = BufferHelper.buildBuffer("00 00 00 00");
47         ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
48
49         BufferHelper.checkHeaderV10(builtByFactory);
50         Assert.assertEquals("Wrong type", 0, builtByFactory.getType().intValue());
51         Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
52         Assert.assertEquals("Wrong type string", "HELLOFAILED", builtByFactory.getTypeString());
53         Assert.assertEquals("Wrong code string", "INCOMPATIBLE", builtByFactory.getCodeString());
54         Assert.assertNull("Data is not null", builtByFactory.getData());
55
56         bb = BufferHelper.buildBuffer("00 01 00 00");
57         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
58
59         BufferHelper.checkHeaderV10(builtByFactory);
60         Assert.assertEquals("Wrong type", 1, builtByFactory.getType().intValue());
61         Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
62         Assert.assertEquals("Wrong type string", "BADREQUEST", builtByFactory.getTypeString());
63         Assert.assertEquals("Wrong code string", "BADVERSION", builtByFactory.getCodeString());
64         Assert.assertNull("Data is not null", builtByFactory.getData());
65
66         bb = BufferHelper.buildBuffer("00 02 00 00");
67         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
68
69         BufferHelper.checkHeaderV10(builtByFactory);
70         Assert.assertEquals("Wrong type", 2, builtByFactory.getType().intValue());
71         Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
72         Assert.assertEquals("Wrong type string", "BADACTION", builtByFactory.getTypeString());
73         Assert.assertEquals("Wrong code string", "BADTYPE", builtByFactory.getCodeString());
74         Assert.assertNull("Data is not null", builtByFactory.getData());
75
76         bb = BufferHelper.buildBuffer("00 03 00 00");
77         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
78
79         BufferHelper.checkHeaderV10(builtByFactory);
80         Assert.assertEquals("Wrong type", 3, builtByFactory.getType().intValue());
81         Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
82         Assert.assertEquals("Wrong type string", "FLOWMODFAILED", builtByFactory.getTypeString());
83         Assert.assertEquals("Wrong code string", "ALLTABLESFULL", builtByFactory.getCodeString());
84         Assert.assertNull("Data is not null", builtByFactory.getData());
85
86         bb = BufferHelper.buildBuffer("00 04 00 00");
87         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
88
89         BufferHelper.checkHeaderV10(builtByFactory);
90         Assert.assertEquals("Wrong type", 4, builtByFactory.getType().intValue());
91         Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
92         Assert.assertEquals("Wrong type string", "PORTMODFAILED", builtByFactory.getTypeString());
93         Assert.assertEquals("Wrong code string", "BADPORT", builtByFactory.getCodeString());
94         Assert.assertNull("Data is not null", builtByFactory.getData());
95
96         bb = BufferHelper.buildBuffer("00 05 00 00");
97         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
98
99         BufferHelper.checkHeaderV10(builtByFactory);
100         Assert.assertEquals("Wrong type", 5, builtByFactory.getType().intValue());
101         Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
102         Assert.assertEquals("Wrong type string", "QUEUEOPFAILED", builtByFactory.getTypeString());
103         Assert.assertEquals("Wrong code string", "BADPORT", builtByFactory.getCodeString());
104         Assert.assertNull("Data is not null", builtByFactory.getData());
105     }
106
107     /**
108      * Test of {@link ErrorMessageFactory} for correct translation into POJO
109      * - not existing code used
110      */
111     @Test
112     public void testWithoutData2() {
113         ByteBuf bb = BufferHelper.buildBuffer("00 00 FF FF");
114         ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
115
116         BufferHelper.checkHeaderV10(builtByFactory);
117         Assert.assertEquals("Wrong type", 0, builtByFactory.getType().intValue());
118         Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
119         Assert.assertEquals("Wrong type string", "HELLOFAILED", builtByFactory.getTypeString());
120         Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
121         Assert.assertNull("Data is not null", builtByFactory.getData());
122
123         bb = BufferHelper.buildBuffer("00 01 FF FF");
124         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
125
126         BufferHelper.checkHeaderV10(builtByFactory);
127         Assert.assertEquals("Wrong type", 1, builtByFactory.getType().intValue());
128         Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
129         Assert.assertEquals("Wrong type string", "BADREQUEST", builtByFactory.getTypeString());
130         Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
131         Assert.assertNull("Data is not null", builtByFactory.getData());
132
133         bb = BufferHelper.buildBuffer("00 02 FF FF");
134         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
135
136         BufferHelper.checkHeaderV10(builtByFactory);
137         Assert.assertEquals("Wrong type", 2, builtByFactory.getType().intValue());
138         Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
139         Assert.assertEquals("Wrong type string", "BADACTION", builtByFactory.getTypeString());
140         Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
141         Assert.assertNull("Data is not null", builtByFactory.getData());
142
143         bb = BufferHelper.buildBuffer("00 03 FF FF");
144         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
145
146         BufferHelper.checkHeaderV10(builtByFactory);
147         Assert.assertEquals("Wrong type", 3, builtByFactory.getType().intValue());
148         Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
149         Assert.assertEquals("Wrong type string", "FLOWMODFAILED", builtByFactory.getTypeString());
150         Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
151         Assert.assertNull("Data is not null", builtByFactory.getData());
152
153         bb = BufferHelper.buildBuffer("00 04 FF FF");
154         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
155
156         BufferHelper.checkHeaderV10(builtByFactory);
157         Assert.assertEquals("Wrong type", 4, builtByFactory.getType().intValue());
158         Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
159         Assert.assertEquals("Wrong type string", "PORTMODFAILED", builtByFactory.getTypeString());
160         Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
161         Assert.assertNull("Data is not null", builtByFactory.getData());
162
163         bb = BufferHelper.buildBuffer("00 05 FF FF");
164         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
165
166         BufferHelper.checkHeaderV10(builtByFactory);
167         Assert.assertEquals("Wrong type", 5, builtByFactory.getType().intValue());
168         Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
169         Assert.assertEquals("Wrong type string", "QUEUEOPFAILED", builtByFactory.getTypeString());
170         Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
171         Assert.assertNull("Data is not null", builtByFactory.getData());
172     }
173         
174         /**
175      * Test of {@link OF10ErrorMessageFactory} for correct translation into POJO
176      */
177         @Test
178         public void testWithData() {
179                 ByteBuf bb = BufferHelper.buildBuffer("00 00 00 01 00 01 02 03");
180                 ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
181
182                 BufferHelper.checkHeaderV10(builtByFactory);
183                 Assert.assertEquals("Wrong type", 0, builtByFactory.getType().intValue());
184                 Assert.assertEquals("Wrong code", 1, builtByFactory.getCode().intValue());
185                 Assert.assertEquals("Wrong type string", "HELLOFAILED", builtByFactory.getTypeString());
186                 Assert.assertEquals("Wrong code string", "EPERM", builtByFactory.getCodeString());
187                 Assert.assertArrayEquals("Wrong data", new byte[]{0x00, 0x01, 0x02, 0x03}, builtByFactory.getData());
188         }
189         
190         /**
191      * Test of {@link OF10ErrorMessageFactory} for correct translation into POJO
192      */
193         @Test
194         public void testWithIncorrectTypeEnum() {
195                 ByteBuf bb = BufferHelper.buildBuffer("00 0A 00 05 00 01 02 03");
196                 ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
197
198                 BufferHelper.checkHeaderV10(builtByFactory);
199                 Assert.assertEquals("Wrong type", 10, builtByFactory.getType().intValue());
200                 Assert.assertEquals("Wrong code", 5, builtByFactory.getCode().intValue());
201                 Assert.assertEquals("Wrong type string", "UNKNOWN_TYPE", builtByFactory.getTypeString());
202                 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
203                 Assert.assertArrayEquals("Wrong data", new byte[]{0x00, 0x01, 0x02, 0x03}, builtByFactory.getData());
204         }
205         
206         /**
207      * Test of {@link OF10ErrorMessageFactory} for correct translation into POJO
208      */
209         @Test
210         public void testWithIncorrectCodeEnum() {
211                 ByteBuf bb = BufferHelper.buildBuffer("00 03 00 06 00 01 02 03");
212                 ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
213
214                 BufferHelper.checkHeaderV10(builtByFactory);
215                 Assert.assertEquals("Wrong type", 3, builtByFactory.getType().intValue());
216                 Assert.assertEquals("Wrong code", 6, builtByFactory.getCode().intValue());
217                 Assert.assertEquals("Wrong type string", "FLOWMODFAILED", builtByFactory.getTypeString());
218                 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
219                 Assert.assertArrayEquals("Wrong data", new byte[]{0x00, 0x01, 0x02, 0x03}, builtByFactory.getData());
220         }
221
222 }