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.junit.runner.RunWith;
17 import org.mockito.Matchers;
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 * @author michal.polkorab
28 * @author timotej.kubas
30 @RunWith(MockitoJUnitRunner.class)
31 public class ErrorMessageFactoryTest {
33 @Mock DeserializerRegistry registry;
34 @Mock ErrorMessageFactory deserializer;
36 private ErrorMessageFactory errorFactory;
39 * Initializes deserializer registry and lookups correct deserializer
42 public void startUp() {
43 errorFactory = new ErrorMessageFactory();
47 * Test of {@link ErrorMessageFactory} for correct translation into POJO
50 public void testWithoutData() {
51 ByteBuf bb = BufferHelper.buildBuffer("00 00 00 00");
52 ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
54 BufferHelper.checkHeaderV13(builtByFactory);
55 Assert.assertEquals("Wrong type", 0, builtByFactory.getType().intValue());
56 Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
57 Assert.assertEquals("Wrong type string", "HELLOFAILED", builtByFactory.getTypeString());
58 Assert.assertEquals("Wrong code string", "INCOMPATIBLE", builtByFactory.getCodeString());
59 Assert.assertNull("Data is not null", builtByFactory.getData());
61 bb = BufferHelper.buildBuffer("00 01 00 00");
62 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
64 BufferHelper.checkHeaderV13(builtByFactory);
65 Assert.assertEquals("Wrong type", 1, builtByFactory.getType().intValue());
66 Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
67 Assert.assertEquals("Wrong type string", "BADREQUEST", builtByFactory.getTypeString());
68 Assert.assertEquals("Wrong code string", "BADVERSION", builtByFactory.getCodeString());
69 Assert.assertNull("Data is not null", builtByFactory.getData());
71 bb = BufferHelper.buildBuffer("00 02 00 00");
72 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
74 BufferHelper.checkHeaderV13(builtByFactory);
75 Assert.assertEquals("Wrong type", 2, builtByFactory.getType().intValue());
76 Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
77 Assert.assertEquals("Wrong type string", "BADACTION", builtByFactory.getTypeString());
78 Assert.assertEquals("Wrong code string", "BADTYPE", builtByFactory.getCodeString());
79 Assert.assertNull("Data is not null", builtByFactory.getData());
81 bb = BufferHelper.buildBuffer("00 03 00 00");
82 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
84 BufferHelper.checkHeaderV13(builtByFactory);
85 Assert.assertEquals("Wrong type", 3, builtByFactory.getType().intValue());
86 Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
87 Assert.assertEquals("Wrong type string", "BADINSTRUCTION", builtByFactory.getTypeString());
88 Assert.assertEquals("Wrong code string", "UNKNOWNINST", builtByFactory.getCodeString());
89 Assert.assertNull("Data is not null", builtByFactory.getData());
91 bb = BufferHelper.buildBuffer("00 04 00 00");
92 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
94 BufferHelper.checkHeaderV13(builtByFactory);
95 Assert.assertEquals("Wrong type", 4, builtByFactory.getType().intValue());
96 Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
97 Assert.assertEquals("Wrong type string", "BADMATCH", builtByFactory.getTypeString());
98 Assert.assertEquals("Wrong code string", "BADTYPE", builtByFactory.getCodeString());
99 Assert.assertNull("Data is not null", builtByFactory.getData());
101 bb = BufferHelper.buildBuffer("00 05 00 00");
102 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
104 BufferHelper.checkHeaderV13(builtByFactory);
105 Assert.assertEquals("Wrong type", 5, builtByFactory.getType().intValue());
106 Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
107 Assert.assertEquals("Wrong type string", "FLOWMODFAILED", builtByFactory.getTypeString());
108 Assert.assertEquals("Wrong code string", "UNKNOWN", builtByFactory.getCodeString());
109 Assert.assertNull("Data is not null", builtByFactory.getData());
111 bb = BufferHelper.buildBuffer("00 06 00 00");
112 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
114 BufferHelper.checkHeaderV13(builtByFactory);
115 Assert.assertEquals("Wrong type", 6, builtByFactory.getType().intValue());
116 Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
117 Assert.assertEquals("Wrong type string", "GROUPMODFAILED", builtByFactory.getTypeString());
118 Assert.assertEquals("Wrong code string", "GROUPEXISTS", builtByFactory.getCodeString());
119 Assert.assertNull("Data is not null", builtByFactory.getData());
121 bb = BufferHelper.buildBuffer("00 07 00 00");
122 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
124 BufferHelper.checkHeaderV13(builtByFactory);
125 Assert.assertEquals("Wrong type", 7, builtByFactory.getType().intValue());
126 Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
127 Assert.assertEquals("Wrong type string", "PORTMODFAILED", builtByFactory.getTypeString());
128 Assert.assertEquals("Wrong code string", "BADPORT", builtByFactory.getCodeString());
129 Assert.assertNull("Data is not null", builtByFactory.getData());
131 bb = BufferHelper.buildBuffer("00 08 00 00");
132 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
134 BufferHelper.checkHeaderV13(builtByFactory);
135 Assert.assertEquals("Wrong type", 8, builtByFactory.getType().intValue());
136 Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
137 Assert.assertEquals("Wrong type string", "TABLEMODFAILED", builtByFactory.getTypeString());
138 Assert.assertEquals("Wrong code string", "BADTABLE", builtByFactory.getCodeString());
139 Assert.assertNull("Data is not null", builtByFactory.getData());
141 bb = BufferHelper.buildBuffer("00 09 00 00");
142 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
144 BufferHelper.checkHeaderV13(builtByFactory);
145 Assert.assertEquals("Wrong type", 9, builtByFactory.getType().intValue());
146 Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
147 Assert.assertEquals("Wrong type string", "QUEUEOPFAILED", builtByFactory.getTypeString());
148 Assert.assertEquals("Wrong code string", "BADPORT", builtByFactory.getCodeString());
149 Assert.assertNull("Data is not null", builtByFactory.getData());
151 bb = BufferHelper.buildBuffer("00 0A 00 00");
152 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
154 BufferHelper.checkHeaderV13(builtByFactory);
155 Assert.assertEquals("Wrong type", 10, builtByFactory.getType().intValue());
156 Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
157 Assert.assertEquals("Wrong type string", "SWITCHCONFIGFAILED", builtByFactory.getTypeString());
158 Assert.assertEquals("Wrong code string", "BADFLAGS", builtByFactory.getCodeString());
159 Assert.assertNull("Data is not null", builtByFactory.getData());
161 bb = BufferHelper.buildBuffer("00 0B 00 00");
162 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
164 BufferHelper.checkHeaderV13(builtByFactory);
165 Assert.assertEquals("Wrong type", 11, builtByFactory.getType().intValue());
166 Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
167 Assert.assertEquals("Wrong type string", "ROLEREQUESTFAILED", builtByFactory.getTypeString());
168 Assert.assertEquals("Wrong code string", "STALE", builtByFactory.getCodeString());
169 Assert.assertNull("Data is not null", builtByFactory.getData());
171 bb = BufferHelper.buildBuffer("00 0C 00 00");
172 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
174 BufferHelper.checkHeaderV13(builtByFactory);
175 Assert.assertEquals("Wrong type", 12, builtByFactory.getType().intValue());
176 Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
177 Assert.assertEquals("Wrong type string", "METERMODFAILED", builtByFactory.getTypeString());
178 Assert.assertEquals("Wrong code string", "UNKNOWN", builtByFactory.getCodeString());
179 Assert.assertNull("Data is not null", builtByFactory.getData());
181 bb = BufferHelper.buildBuffer("00 0D 00 00");
182 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
184 BufferHelper.checkHeaderV13(builtByFactory);
185 Assert.assertEquals("Wrong type", 13, builtByFactory.getType().intValue());
186 Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
187 Assert.assertEquals("Wrong type string", "TABLEFEATURESFAILED", builtByFactory.getTypeString());
188 Assert.assertEquals("Wrong code string", "BADTABLE", builtByFactory.getCodeString());
189 Assert.assertNull("Data is not null", builtByFactory.getData());
193 * Test of {@link ErrorMessageFactory} for correct translation into POJO
194 * - not existing code used
197 public void testWithoutData2() {
198 ByteBuf bb = BufferHelper.buildBuffer("00 00 FF FF");
199 ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
201 BufferHelper.checkHeaderV13(builtByFactory);
202 Assert.assertEquals("Wrong type", 0, builtByFactory.getType().intValue());
203 Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
204 Assert.assertEquals("Wrong type string", "HELLOFAILED", builtByFactory.getTypeString());
205 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
206 Assert.assertNull("Data is not null", builtByFactory.getData());
208 bb = BufferHelper.buildBuffer("00 01 FF FF");
209 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
211 BufferHelper.checkHeaderV13(builtByFactory);
212 Assert.assertEquals("Wrong type", 1, builtByFactory.getType().intValue());
213 Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
214 Assert.assertEquals("Wrong type string", "BADREQUEST", builtByFactory.getTypeString());
215 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
216 Assert.assertNull("Data is not null", builtByFactory.getData());
218 bb = BufferHelper.buildBuffer("00 02 FF FF");
219 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
221 BufferHelper.checkHeaderV13(builtByFactory);
222 Assert.assertEquals("Wrong type", 2, builtByFactory.getType().intValue());
223 Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
224 Assert.assertEquals("Wrong type string", "BADACTION", builtByFactory.getTypeString());
225 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
226 Assert.assertNull("Data is not null", builtByFactory.getData());
228 bb = BufferHelper.buildBuffer("00 03 FF FF");
229 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
231 BufferHelper.checkHeaderV13(builtByFactory);
232 Assert.assertEquals("Wrong type", 3, builtByFactory.getType().intValue());
233 Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
234 Assert.assertEquals("Wrong type string", "BADINSTRUCTION", builtByFactory.getTypeString());
235 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
236 Assert.assertNull("Data is not null", builtByFactory.getData());
238 bb = BufferHelper.buildBuffer("00 04 FF FF");
239 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
241 BufferHelper.checkHeaderV13(builtByFactory);
242 Assert.assertEquals("Wrong type", 4, builtByFactory.getType().intValue());
243 Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
244 Assert.assertEquals("Wrong type string", "BADMATCH", builtByFactory.getTypeString());
245 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
246 Assert.assertNull("Data is not null", builtByFactory.getData());
248 bb = BufferHelper.buildBuffer("00 05 FF FF");
249 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
251 BufferHelper.checkHeaderV13(builtByFactory);
252 Assert.assertEquals("Wrong type", 5, builtByFactory.getType().intValue());
253 Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
254 Assert.assertEquals("Wrong type string", "FLOWMODFAILED", builtByFactory.getTypeString());
255 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
256 Assert.assertNull("Data is not null", builtByFactory.getData());
258 bb = BufferHelper.buildBuffer("00 06 FF FF");
259 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
261 BufferHelper.checkHeaderV13(builtByFactory);
262 Assert.assertEquals("Wrong type", 6, builtByFactory.getType().intValue());
263 Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
264 Assert.assertEquals("Wrong type string", "GROUPMODFAILED", builtByFactory.getTypeString());
265 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
266 Assert.assertNull("Data is not null", builtByFactory.getData());
268 bb = BufferHelper.buildBuffer("00 07 FF FF");
269 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
271 BufferHelper.checkHeaderV13(builtByFactory);
272 Assert.assertEquals("Wrong type", 7, builtByFactory.getType().intValue());
273 Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
274 Assert.assertEquals("Wrong type string", "PORTMODFAILED", builtByFactory.getTypeString());
275 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
276 Assert.assertNull("Data is not null", builtByFactory.getData());
278 bb = BufferHelper.buildBuffer("00 08 FF FF");
279 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
281 BufferHelper.checkHeaderV13(builtByFactory);
282 Assert.assertEquals("Wrong type", 8, builtByFactory.getType().intValue());
283 Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
284 Assert.assertEquals("Wrong type string", "TABLEMODFAILED", builtByFactory.getTypeString());
285 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
286 Assert.assertNull("Data is not null", builtByFactory.getData());
288 bb = BufferHelper.buildBuffer("00 09 FF FF");
289 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
291 BufferHelper.checkHeaderV13(builtByFactory);
292 Assert.assertEquals("Wrong type", 9, builtByFactory.getType().intValue());
293 Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
294 Assert.assertEquals("Wrong type string", "QUEUEOPFAILED", builtByFactory.getTypeString());
295 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
296 Assert.assertNull("Data is not null", builtByFactory.getData());
298 bb = BufferHelper.buildBuffer("00 0A FF FF");
299 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
301 BufferHelper.checkHeaderV13(builtByFactory);
302 Assert.assertEquals("Wrong type", 10, builtByFactory.getType().intValue());
303 Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
304 Assert.assertEquals("Wrong type string", "SWITCHCONFIGFAILED", builtByFactory.getTypeString());
305 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
306 Assert.assertNull("Data is not null", builtByFactory.getData());
308 bb = BufferHelper.buildBuffer("00 0B FF FF");
309 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
311 BufferHelper.checkHeaderV13(builtByFactory);
312 Assert.assertEquals("Wrong type", 11, builtByFactory.getType().intValue());
313 Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
314 Assert.assertEquals("Wrong type string", "ROLEREQUESTFAILED", builtByFactory.getTypeString());
315 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
316 Assert.assertNull("Data is not null", builtByFactory.getData());
318 bb = BufferHelper.buildBuffer("00 0C FF FF");
319 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
321 BufferHelper.checkHeaderV13(builtByFactory);
322 Assert.assertEquals("Wrong type", 12, builtByFactory.getType().intValue());
323 Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
324 Assert.assertEquals("Wrong type string", "METERMODFAILED", builtByFactory.getTypeString());
325 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
326 Assert.assertNull("Data is not null", builtByFactory.getData());
328 bb = BufferHelper.buildBuffer("00 0D FF FF");
329 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
331 BufferHelper.checkHeaderV13(builtByFactory);
332 Assert.assertEquals("Wrong type", 13, builtByFactory.getType().intValue());
333 Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
334 Assert.assertEquals("Wrong type string", "TABLEFEATURESFAILED", builtByFactory.getTypeString());
335 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
336 Assert.assertNull("Data is not null", builtByFactory.getData());
340 * Test of {@link ErrorMessageFactory} for correct translation into POJO
343 public void testWithData() {
344 ByteBuf bb = BufferHelper.buildBuffer("00 00 00 01 00 01 02 03");
345 ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
347 BufferHelper.checkHeaderV13(builtByFactory);
348 Assert.assertEquals("Wrong type", 0, builtByFactory.getType().intValue());
349 Assert.assertEquals("Wrong code", 1, builtByFactory.getCode().intValue());
350 Assert.assertEquals("Wrong type string", "HELLOFAILED", builtByFactory.getTypeString());
351 Assert.assertEquals("Wrong code string", "EPERM", builtByFactory.getCodeString());
352 Assert.assertArrayEquals("Wrong data", new byte[]{0x00, 0x01, 0x02, 0x03}, builtByFactory.getData());
356 * Test of {@link ErrorMessageFactory} for correct translation into POJO
359 public void testWithIncorrectTypeEnum() {
360 ByteBuf bb = BufferHelper.buildBuffer("00 20 00 05 00 01 02 03");
361 ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
363 BufferHelper.checkHeaderV13(builtByFactory);
364 Assert.assertEquals("Wrong type", 32, builtByFactory.getType().intValue());
365 Assert.assertEquals("Wrong code", 5, builtByFactory.getCode().intValue());
366 Assert.assertEquals("Wrong type string", "UNKNOWN_TYPE", builtByFactory.getTypeString());
367 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
368 Assert.assertArrayEquals("Wrong data", new byte[]{0x00, 0x01, 0x02, 0x03}, builtByFactory.getData());
372 * Test of {@link ErrorMessageFactory} for correct translation into POJO
375 public void testWithIncorrectCodeEnum() {
376 ByteBuf bb = BufferHelper.buildBuffer("00 03 00 10 00 01 02 03");
377 ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
379 BufferHelper.checkHeaderV13(builtByFactory);
380 Assert.assertEquals("Wrong type", 3, builtByFactory.getType().intValue());
381 Assert.assertEquals("Wrong code", 16, builtByFactory.getCode().intValue());
382 Assert.assertEquals("Wrong type string", "BADINSTRUCTION", builtByFactory.getTypeString());
383 Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
384 Assert.assertArrayEquals("Wrong data", new byte[]{0x00, 0x01, 0x02, 0x03}, builtByFactory.getData());
388 * Test of {@link ErrorMessageFactory} for correct translation into POJO
391 public void testExperimenterError() {
392 Mockito.when(registry.getDeserializer(Matchers.any(MessageCodeKey.class))).thenReturn(deserializer);
393 errorFactory.injectDeserializerRegistry(registry);
394 ByteBuf bb = BufferHelper.buildBuffer("FF FF 00 00 00 01");
395 BufferHelper.deserialize(errorFactory, bb);
397 Mockito.verify(deserializer, Mockito.times(1)).deserialize(bb);