Remove trailing whitespace
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / ErrorMessageFactoryTest.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.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.extensibility.MessageCodeKey;
23 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
25
26 /**
27  * @author michal.polkorab
28  * @author timotej.kubas
29  */
30 @RunWith(MockitoJUnitRunner.class)
31 public class ErrorMessageFactoryTest {
32
33     @Mock DeserializerRegistry registry;
34     @Mock ErrorMessageFactory deserializer;
35
36     private ErrorMessageFactory errorFactory;
37
38     /**
39      * Initializes deserializer registry and lookups correct deserializer
40      */
41     @Before
42     public void startUp() {
43         errorFactory = new ErrorMessageFactory();
44     }
45
46     /**
47      * Test of {@link ErrorMessageFactory} for correct translation into POJO
48      */
49     @Test
50     public void testWithoutData() {
51         ByteBuf bb = BufferHelper.buildBuffer("00 00 00 00");
52         ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
53
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());
60
61         bb = BufferHelper.buildBuffer("00 01 00 00");
62         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
63
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());
70
71         bb = BufferHelper.buildBuffer("00 02 00 00");
72         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
73
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());
80
81         bb = BufferHelper.buildBuffer("00 03 00 00");
82         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
83
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());
90
91         bb = BufferHelper.buildBuffer("00 04 00 00");
92         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
93
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());
100
101         bb = BufferHelper.buildBuffer("00 05 00 00");
102         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
103
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());
110
111         bb = BufferHelper.buildBuffer("00 06 00 00");
112         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
113
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());
120
121         bb = BufferHelper.buildBuffer("00 07 00 00");
122         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
123
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());
130
131         bb = BufferHelper.buildBuffer("00 08 00 00");
132         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
133
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());
140
141         bb = BufferHelper.buildBuffer("00 09 00 00");
142         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
143
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());
150
151         bb = BufferHelper.buildBuffer("00 0A 00 00");
152         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
153
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());
160
161         bb = BufferHelper.buildBuffer("00 0B 00 00");
162         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
163
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());
170
171         bb = BufferHelper.buildBuffer("00 0C 00 00");
172         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
173
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());
180
181         bb = BufferHelper.buildBuffer("00 0D 00 00");
182         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
183
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());
190     }
191
192     /**
193      * Test of {@link ErrorMessageFactory} for correct translation into POJO
194      * - not existing code used
195      */
196     @Test
197     public void testWithoutData2() {
198         ByteBuf bb = BufferHelper.buildBuffer("00 00 FF FF");
199         ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
200
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());
207
208         bb = BufferHelper.buildBuffer("00 01 FF FF");
209         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
210
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());
217
218         bb = BufferHelper.buildBuffer("00 02 FF FF");
219         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
220
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());
227
228         bb = BufferHelper.buildBuffer("00 03 FF FF");
229         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
230
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());
237
238         bb = BufferHelper.buildBuffer("00 04 FF FF");
239         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
240
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());
247
248         bb = BufferHelper.buildBuffer("00 05 FF FF");
249         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
250
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());
257
258         bb = BufferHelper.buildBuffer("00 06 FF FF");
259         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
260
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());
267
268         bb = BufferHelper.buildBuffer("00 07 FF FF");
269         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
270
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());
277
278         bb = BufferHelper.buildBuffer("00 08 FF FF");
279         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
280
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());
287
288         bb = BufferHelper.buildBuffer("00 09 FF FF");
289         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
290
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());
297
298         bb = BufferHelper.buildBuffer("00 0A FF FF");
299         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
300
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());
307
308         bb = BufferHelper.buildBuffer("00 0B FF FF");
309         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
310
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());
317
318         bb = BufferHelper.buildBuffer("00 0C FF FF");
319         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
320
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());
327
328         bb = BufferHelper.buildBuffer("00 0D FF FF");
329         builtByFactory = BufferHelper.deserialize(errorFactory, bb);
330
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());
337     }
338
339     /**
340      * Test of {@link ErrorMessageFactory} for correct translation into POJO
341      */
342     @Test
343     public void testWithData() {
344         ByteBuf bb = BufferHelper.buildBuffer("00 00 00 01 00 01 02 03");
345         ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
346
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());
353     }
354
355     /**
356      * Test of {@link ErrorMessageFactory} for correct translation into POJO
357      */
358     @Test
359     public void testWithIncorrectTypeEnum() {
360         ByteBuf bb = BufferHelper.buildBuffer("00 20 00 05 00 01 02 03");
361         ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
362
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());
369     }
370
371     /**
372      * Test of {@link ErrorMessageFactory} for correct translation into POJO
373      */
374     @Test
375     public void testWithIncorrectCodeEnum() {
376         ByteBuf bb = BufferHelper.buildBuffer("00 03 00 10 00 01 02 03");
377         ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
378
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());
385     }
386
387     /**
388      * Test of {@link ErrorMessageFactory} for correct translation into POJO
389      */
390     @Test
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);
396
397         Mockito.verify(deserializer, Mockito.times(1)).deserialize(bb);
398     }
399 }