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.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
18 import org.opendaylight.openflowjava.protocol.api.keys.MessageCodeKey;
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;
25 * @author michal.polkorab
27 public class OF10ErrorMessageFactoryTest {
28 private OFDeserializer<ErrorMessage> errorFactory;
31 * Initializes deserializer registry and lookups correct deserializer
34 public void startUp() {
35 DeserializerRegistry registry = new DeserializerRegistryImpl();
37 errorFactory = registry.getDeserializer(
38 new MessageCodeKey(EncodeConstants.OF10_VERSION_ID, 1, ErrorMessage.class));
42 * Test of {@link ErrorMessageFactory} for correct translation into POJO
45 public void testWithoutData() {
46 ByteBuf bb = BufferHelper.buildBuffer("00 00 00 00");
47 ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
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());
56 bb = BufferHelper.buildBuffer("00 01 00 00");
57 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
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());
66 bb = BufferHelper.buildBuffer("00 02 00 00");
67 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
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());
76 bb = BufferHelper.buildBuffer("00 03 00 00");
77 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
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());
86 bb = BufferHelper.buildBuffer("00 04 00 00");
87 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
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());
96 bb = BufferHelper.buildBuffer("00 05 00 00");
97 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
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());
108 * Test of {@link ErrorMessageFactory} for correct translation into POJO
109 * - not existing code used
112 public void testWithoutData2() {
113 ByteBuf bb = BufferHelper.buildBuffer("00 00 FF FF");
114 ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
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());
123 bb = BufferHelper.buildBuffer("00 01 FF FF");
124 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
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());
133 bb = BufferHelper.buildBuffer("00 02 FF FF");
134 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
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());
143 bb = BufferHelper.buildBuffer("00 03 FF FF");
144 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
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());
153 bb = BufferHelper.buildBuffer("00 04 FF FF");
154 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
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());
163 bb = BufferHelper.buildBuffer("00 05 FF FF");
164 builtByFactory = BufferHelper.deserialize(errorFactory, bb);
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());
175 * Test of {@link OF10ErrorMessageFactory} for correct translation into POJO
178 public void testWithData() {
179 ByteBuf bb = BufferHelper.buildBuffer("00 00 00 01 00 01 02 03");
180 ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
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());
191 * Test of {@link OF10ErrorMessageFactory} for correct translation into POJO
194 public void testWithIncorrectTypeEnum() {
195 ByteBuf bb = BufferHelper.buildBuffer("00 0A 00 05 00 01 02 03");
196 ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
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());
207 * Test of {@link OF10ErrorMessageFactory} for correct translation into POJO
210 public void testWithIncorrectCodeEnum() {
211 ByteBuf bb = BufferHelper.buildBuffer("00 03 00 06 00 01 02 03");
212 ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
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());