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 static org.mockito.ArgumentMatchers.any;
13 import io.netty.buffer.ByteBuf;
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.Mock;
19 import org.mockito.Mockito;
20 import org.mockito.runners.MockitoJUnitRunner;
21 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
22 import org.opendaylight.openflowjava.protocol.api.keys.MessageCodeKey;
23 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
27 * Unit tests for ErrorMessageFactory.
29 * @author michal.polkorab
30 * @author timotej.kubas
32 @RunWith(MockitoJUnitRunner.class)
33 public class ErrorMessageFactoryTest {
35 @Mock DeserializerRegistry registry;
36 @Mock ErrorMessageFactory deserializer;
38 private ErrorMessageFactory errorFactory;
41 * Initializes deserializer registry and lookups correct deserializer..
44 public void startUp() {
45 errorFactory = new ErrorMessageFactory();
46 Mockito.when(registry.getDeserializer(any(MessageCodeKey.class))).thenReturn(deserializer);
47 errorFactory.injectDeserializerRegistry(registry);
51 * Test of {@link ErrorMessageFactory} for correct translation into POJO.
54 public void testWithoutData() {
55 ByteBuf bb = BufferHelper.buildBuffer("00 00 00 00");
56 ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
58 BufferHelper.checkHeaderV13(builtByFactory);
59 Assert.assertEquals("Wrong type", 0, builtByFactory.getType().intValue());
60 Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
61 Assert.assertEquals("Wrong type string", "HELLOFAILED", builtByFactory.getTypeString());
62 Assert.assertEquals("Wrong code string", "INCOMPATIBLE", builtByFactory.getCodeString());
63 Assert.assertNull("Data is not null", builtByFactory.getData());
65 bb = BufferHelper.buildBuffer("00 01 00 00");
66 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
68 BufferHelper.checkHeaderV13(builtByFactory);
69 Assert.assertEquals("Wrong type", 1, builtByFactory.getType().intValue());
70 Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
71 Assert.assertEquals("Wrong type string", "BADREQUEST", builtByFactory.getTypeString());
72 Assert.assertEquals("Wrong code string", "BADVERSION", builtByFactory.getCodeString());
73 Assert.assertNull("Data is not null", builtByFactory.getData());
75 bb = BufferHelper.buildBuffer("00 02 00 00");
76 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
78 BufferHelper.checkHeaderV13(builtByFactory);
79 Assert.assertEquals("Wrong type", 2, builtByFactory.getType().intValue());
80 Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
81 Assert.assertEquals("Wrong type string", "BADACTION", builtByFactory.getTypeString());
82 Assert.assertEquals("Wrong code string", "BADTYPE", builtByFactory.getCodeString());
83 Assert.assertNull("Data is not null", builtByFactory.getData());
85 bb = BufferHelper.buildBuffer("00 03 00 00");
86 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
88 BufferHelper.checkHeaderV13(builtByFactory);
89 Assert.assertEquals("Wrong type", 3, builtByFactory.getType().intValue());
90 Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
91 Assert.assertEquals("Wrong type string", "BADINSTRUCTION", builtByFactory.getTypeString());
92 Assert.assertEquals("Wrong code string", "UNKNOWNINST", builtByFactory.getCodeString());
93 Assert.assertNull("Data is not null", builtByFactory.getData());
95 bb = BufferHelper.buildBuffer("00 04 00 00");
96 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
98 BufferHelper.checkHeaderV13(builtByFactory);
99 Assert.assertEquals("Wrong type", 4, builtByFactory.getType().intValue());
100 Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
101 Assert.assertEquals("Wrong type string", "BADMATCH", builtByFactory.getTypeString());
102 Assert.assertEquals("Wrong code string", "BADTYPE", builtByFactory.getCodeString());
103 Assert.assertNull("Data is not null", builtByFactory.getData());
105 bb = BufferHelper.buildBuffer("00 05 00 00");
106 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
108 BufferHelper.checkHeaderV13(builtByFactory);
109 Assert.assertEquals("Wrong type", 5, builtByFactory.getType().intValue());
110 Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
111 Assert.assertEquals("Wrong type string", "FLOWMODFAILED", builtByFactory.getTypeString());
112 Assert.assertEquals("Wrong code string", "UNKNOWN", builtByFactory.getCodeString());
113 Assert.assertNull("Data is not null", builtByFactory.getData());
115 bb = BufferHelper.buildBuffer("00 06 00 00");
116 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
118 BufferHelper.checkHeaderV13(builtByFactory);
119 Assert.assertEquals("Wrong type", 6, builtByFactory.getType().intValue());
120 Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
121 Assert.assertEquals("Wrong type string", "GROUPMODFAILED", builtByFactory.getTypeString());
122 Assert.assertEquals("Wrong code string", "GROUPEXISTS", builtByFactory.getCodeString());
123 Assert.assertNull("Data is not null", builtByFactory.getData());
125 bb = BufferHelper.buildBuffer("00 07 00 00");
126 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
128 BufferHelper.checkHeaderV13(builtByFactory);
129 Assert.assertEquals("Wrong type", 7, builtByFactory.getType().intValue());
130 Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
131 Assert.assertEquals("Wrong type string", "PORTMODFAILED", builtByFactory.getTypeString());
132 Assert.assertEquals("Wrong code string", "BADPORT", builtByFactory.getCodeString());
133 Assert.assertNull("Data is not null", builtByFactory.getData());
135 bb = BufferHelper.buildBuffer("00 08 00 00");
136 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
138 BufferHelper.checkHeaderV13(builtByFactory);
139 Assert.assertEquals("Wrong type", 8, builtByFactory.getType().intValue());
140 Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
141 Assert.assertEquals("Wrong type string", "TABLEMODFAILED", builtByFactory.getTypeString());
142 Assert.assertEquals("Wrong code string", "BADTABLE", builtByFactory.getCodeString());
143 Assert.assertNull("Data is not null", builtByFactory.getData());
145 bb = BufferHelper.buildBuffer("00 09 00 00");
146 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
148 BufferHelper.checkHeaderV13(builtByFactory);
149 Assert.assertEquals("Wrong type", 9, builtByFactory.getType().intValue());
150 Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
151 Assert.assertEquals("Wrong type string", "QUEUEOPFAILED", builtByFactory.getTypeString());
152 Assert.assertEquals("Wrong code string", "BADPORT", builtByFactory.getCodeString());
153 Assert.assertNull("Data is not null", builtByFactory.getData());
155 bb = BufferHelper.buildBuffer("00 0A 00 00");
156 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
158 BufferHelper.checkHeaderV13(builtByFactory);
159 Assert.assertEquals("Wrong type", 10, builtByFactory.getType().intValue());
160 Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
161 Assert.assertEquals("Wrong type string", "SWITCHCONFIGFAILED", builtByFactory.getTypeString());
162 Assert.assertEquals("Wrong code string", "BADFLAGS", builtByFactory.getCodeString());
163 Assert.assertNull("Data is not null", builtByFactory.getData());
165 bb = BufferHelper.buildBuffer("00 0B 00 00");
166 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
168 BufferHelper.checkHeaderV13(builtByFactory);
169 Assert.assertEquals("Wrong type", 11, builtByFactory.getType().intValue());
170 Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
171 Assert.assertEquals("Wrong type string", "ROLEREQUESTFAILED", builtByFactory.getTypeString());
172 Assert.assertEquals("Wrong code string", "STALE", builtByFactory.getCodeString());
173 Assert.assertNull("Data is not null", builtByFactory.getData());
175 bb = BufferHelper.buildBuffer("00 0C 00 00");
176 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
178 BufferHelper.checkHeaderV13(builtByFactory);
179 Assert.assertEquals("Wrong type", 12, builtByFactory.getType().intValue());
180 Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
181 Assert.assertEquals("Wrong type string", "METERMODFAILED", builtByFactory.getTypeString());
182 Assert.assertEquals("Wrong code string", "UNKNOWN", builtByFactory.getCodeString());
183 Assert.assertNull("Data is not null", builtByFactory.getData());
185 bb = BufferHelper.buildBuffer("00 0D 00 00");
186 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
188 BufferHelper.checkHeaderV13(builtByFactory);
189 Assert.assertEquals("Wrong type", 13, builtByFactory.getType().intValue());
190 Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
191 Assert.assertEquals("Wrong type string", "TABLEFEATURESFAILED", builtByFactory.getTypeString());
192 Assert.assertEquals("Wrong code string", "BADTABLE", builtByFactory.getCodeString());
193 Assert.assertNull("Data is not null", builtByFactory.getData());
197 * Test of {@link ErrorMessageFactory} for correct translation into POJO
198 * - not existing code used.
201 public void testWithoutData2() {
202 ByteBuf bb = BufferHelper.buildBuffer("00 00 FF FF");
203 ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
205 BufferHelper.checkHeaderV13(builtByFactory);
206 Assert.assertEquals("Wrong type", 0, builtByFactory.getType().intValue());
207 Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
208 Assert.assertEquals("Wrong type string", "HELLOFAILED", builtByFactory.getTypeString());
209 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
210 Assert.assertNull("Data is not null", builtByFactory.getData());
212 bb = BufferHelper.buildBuffer("00 01 FF FF");
213 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
215 BufferHelper.checkHeaderV13(builtByFactory);
216 Assert.assertEquals("Wrong type", 1, builtByFactory.getType().intValue());
217 Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
218 Assert.assertEquals("Wrong type string", "BADREQUEST", builtByFactory.getTypeString());
219 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
220 Assert.assertNull("Data is not null", builtByFactory.getData());
222 bb = BufferHelper.buildBuffer("00 02 FF FF");
223 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
225 BufferHelper.checkHeaderV13(builtByFactory);
226 Assert.assertEquals("Wrong type", 2, builtByFactory.getType().intValue());
227 Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
228 Assert.assertEquals("Wrong type string", "BADACTION", builtByFactory.getTypeString());
229 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
230 Assert.assertNull("Data is not null", builtByFactory.getData());
232 bb = BufferHelper.buildBuffer("00 03 FF FF");
233 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
235 BufferHelper.checkHeaderV13(builtByFactory);
236 Assert.assertEquals("Wrong type", 3, builtByFactory.getType().intValue());
237 Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
238 Assert.assertEquals("Wrong type string", "BADINSTRUCTION", builtByFactory.getTypeString());
239 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
240 Assert.assertNull("Data is not null", builtByFactory.getData());
242 bb = BufferHelper.buildBuffer("00 04 FF FF");
243 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
245 BufferHelper.checkHeaderV13(builtByFactory);
246 Assert.assertEquals("Wrong type", 4, builtByFactory.getType().intValue());
247 Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
248 Assert.assertEquals("Wrong type string", "BADMATCH", builtByFactory.getTypeString());
249 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
250 Assert.assertNull("Data is not null", builtByFactory.getData());
252 bb = BufferHelper.buildBuffer("00 05 FF FF");
253 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
255 BufferHelper.checkHeaderV13(builtByFactory);
256 Assert.assertEquals("Wrong type", 5, builtByFactory.getType().intValue());
257 Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
258 Assert.assertEquals("Wrong type string", "FLOWMODFAILED", builtByFactory.getTypeString());
259 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
260 Assert.assertNull("Data is not null", builtByFactory.getData());
262 bb = BufferHelper.buildBuffer("00 06 FF FF");
263 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
265 BufferHelper.checkHeaderV13(builtByFactory);
266 Assert.assertEquals("Wrong type", 6, builtByFactory.getType().intValue());
267 Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
268 Assert.assertEquals("Wrong type string", "GROUPMODFAILED", builtByFactory.getTypeString());
269 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
270 Assert.assertNull("Data is not null", builtByFactory.getData());
272 bb = BufferHelper.buildBuffer("00 07 FF FF");
273 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
275 BufferHelper.checkHeaderV13(builtByFactory);
276 Assert.assertEquals("Wrong type", 7, builtByFactory.getType().intValue());
277 Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
278 Assert.assertEquals("Wrong type string", "PORTMODFAILED", builtByFactory.getTypeString());
279 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
280 Assert.assertNull("Data is not null", builtByFactory.getData());
282 bb = BufferHelper.buildBuffer("00 08 FF FF");
283 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
285 BufferHelper.checkHeaderV13(builtByFactory);
286 Assert.assertEquals("Wrong type", 8, builtByFactory.getType().intValue());
287 Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
288 Assert.assertEquals("Wrong type string", "TABLEMODFAILED", builtByFactory.getTypeString());
289 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
290 Assert.assertNull("Data is not null", builtByFactory.getData());
292 bb = BufferHelper.buildBuffer("00 09 FF FF");
293 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
295 BufferHelper.checkHeaderV13(builtByFactory);
296 Assert.assertEquals("Wrong type", 9, builtByFactory.getType().intValue());
297 Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
298 Assert.assertEquals("Wrong type string", "QUEUEOPFAILED", builtByFactory.getTypeString());
299 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
300 Assert.assertNull("Data is not null", builtByFactory.getData());
302 bb = BufferHelper.buildBuffer("00 0A FF FF");
303 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
305 BufferHelper.checkHeaderV13(builtByFactory);
306 Assert.assertEquals("Wrong type", 10, builtByFactory.getType().intValue());
307 Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
308 Assert.assertEquals("Wrong type string", "SWITCHCONFIGFAILED", builtByFactory.getTypeString());
309 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
310 Assert.assertNull("Data is not null", builtByFactory.getData());
312 bb = BufferHelper.buildBuffer("00 0B FF FF");
313 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
315 BufferHelper.checkHeaderV13(builtByFactory);
316 Assert.assertEquals("Wrong type", 11, builtByFactory.getType().intValue());
317 Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
318 Assert.assertEquals("Wrong type string", "ROLEREQUESTFAILED", builtByFactory.getTypeString());
319 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
320 Assert.assertNull("Data is not null", builtByFactory.getData());
322 bb = BufferHelper.buildBuffer("00 0C FF FF");
323 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
325 BufferHelper.checkHeaderV13(builtByFactory);
326 Assert.assertEquals("Wrong type", 12, builtByFactory.getType().intValue());
327 Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
328 Assert.assertEquals("Wrong type string", "METERMODFAILED", builtByFactory.getTypeString());
329 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
330 Assert.assertNull("Data is not null", builtByFactory.getData());
332 bb = BufferHelper.buildBuffer("00 0D FF FF");
333 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
335 BufferHelper.checkHeaderV13(builtByFactory);
336 Assert.assertEquals("Wrong type", 13, builtByFactory.getType().intValue());
337 Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
338 Assert.assertEquals("Wrong type string", "TABLEFEATURESFAILED", builtByFactory.getTypeString());
339 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
340 Assert.assertNull("Data is not null", builtByFactory.getData());
344 * Test of {@link ErrorMessageFactory} for correct translation into POJO.
347 public void testWithData() {
348 ByteBuf bb = BufferHelper.buildBuffer("00 00 00 01 00 01 02 03");
349 ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
351 BufferHelper.checkHeaderV13(builtByFactory);
352 Assert.assertEquals("Wrong type", 0, builtByFactory.getType().intValue());
353 Assert.assertEquals("Wrong code", 1, builtByFactory.getCode().intValue());
354 Assert.assertEquals("Wrong type string", "HELLOFAILED", builtByFactory.getTypeString());
355 Assert.assertEquals("Wrong code string", "EPERM", builtByFactory.getCodeString());
356 Assert.assertArrayEquals("Wrong data", new byte[]{0x00, 0x01, 0x02, 0x03}, builtByFactory.getData());
360 * Test of {@link ErrorMessageFactory} for correct translation into POJO.
363 public void testWithIncorrectTypeEnum() {
364 ByteBuf bb = BufferHelper.buildBuffer("00 20 00 05 00 01 02 03");
365 ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
367 BufferHelper.checkHeaderV13(builtByFactory);
368 Assert.assertEquals("Wrong type", 32, builtByFactory.getType().intValue());
369 Assert.assertEquals("Wrong code", 5, builtByFactory.getCode().intValue());
370 Assert.assertEquals("Wrong type string", "UNKNOWN_TYPE", builtByFactory.getTypeString());
371 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
372 Assert.assertArrayEquals("Wrong data", new byte[]{0x00, 0x01, 0x02, 0x03}, builtByFactory.getData());
376 * Test of {@link ErrorMessageFactory} for correct translation into POJO.
379 public void testWithIncorrectCodeEnum() {
380 ByteBuf bb = BufferHelper.buildBuffer("00 03 00 10 00 01 02 03");
381 ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
383 BufferHelper.checkHeaderV13(builtByFactory);
384 Assert.assertEquals("Wrong type", 3, builtByFactory.getType().intValue());
385 Assert.assertEquals("Wrong code", 16, builtByFactory.getCode().intValue());
386 Assert.assertEquals("Wrong type string", "BADINSTRUCTION", builtByFactory.getTypeString());
387 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
388 Assert.assertArrayEquals("Wrong data", new byte[]{0x00, 0x01, 0x02, 0x03}, builtByFactory.getData());
392 * Test of {@link ErrorMessageFactory} for correct translation into POJO.
395 public void testExperimenterError() {
396 ByteBuf bb = BufferHelper.buildBuffer("FF FF 00 00 00 01");
397 BufferHelper.deserialize(errorFactory, bb);
399 Mockito.verify(deserializer, Mockito.times(1)).deserialize(bb);