Added unit tests for Error and Vendor/Experimenter message factories 23/12023/1
authorMichal Polkorab <michal.polkorab@pantheon.sk>
Thu, 16 Oct 2014 14:00:30 +0000 (16:00 +0200)
committerMichal Polkorab <michal.polkorab@pantheon.sk>
Thu, 16 Oct 2014 15:13:34 +0000 (17:13 +0200)
 - + covered AbstractInstructionDeserializer

Change-Id: I80804f7e38b42d0b53200e8e06ed41f415152605
Signed-off-by: Michal Polkorab <michal.polkorab@pantheon.sk>
openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/ErrorMessageFactoryTest.java
openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/OF10ErrorMessageFactoryTest.java
openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/VendorMessageFactoryTest.java [new file with mode: 0644]
openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/instruction/AbstractInstructionDeserializerTest.java [new file with mode: 0644]
openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/VendorInputMessageFactoryTest.java [new file with mode: 0644]

index 5e114778ec45df4275dea05328dcf52bc92b86e9..367062f5f0d13758bbf576efe273df91c689a536 100644 (file)
@@ -13,31 +13,34 @@ import io.netty.buffer.ByteBuf;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Matchers;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
 import org.opendaylight.openflowjava.protocol.api.extensibility.MessageCodeKey;
-import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
-import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializerRegistryImpl;
 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
-import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
 
 /**
  * @author michal.polkorab
  * @author timotej.kubas
  */
+@RunWith(MockitoJUnitRunner.class)
 public class ErrorMessageFactoryTest {
 
-    private OFDeserializer<ErrorMessage> errorFactory;
+    @Mock DeserializerRegistry registry;
+    @Mock ErrorMessageFactory deserializer;
+
+    private ErrorMessageFactory errorFactory;
 
     /**
      * Initializes deserializer registry and lookups correct deserializer
      */
     @Before
     public void startUp() {
-        DeserializerRegistry registry = new DeserializerRegistryImpl();
-        registry.init();
-        errorFactory = registry.getDeserializer(
-                new MessageCodeKey(EncodeConstants.OF13_VERSION_ID, 1, ErrorMessage.class));
+        errorFactory = new ErrorMessageFactory();
     }
 
     /**
@@ -45,17 +48,294 @@ public class ErrorMessageFactoryTest {
      */
     @Test
     public void testWithoutData() {
-        ByteBuf bb = BufferHelper.buildBuffer("00 01 00 02");
+        ByteBuf bb = BufferHelper.buildBuffer("00 00 00 00");
         ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
 
+        BufferHelper.checkHeaderV13(builtByFactory);
+        Assert.assertEquals("Wrong type", 0, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "HELLOFAILED", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "INCOMPATIBLE", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 01 00 00");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
         BufferHelper.checkHeaderV13(builtByFactory);
         Assert.assertEquals("Wrong type", 1, builtByFactory.getType().intValue());
-        Assert.assertEquals("Wrong code", 2, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
         Assert.assertEquals("Wrong type string", "BADREQUEST", builtByFactory.getTypeString());
-        Assert.assertEquals("Wrong code string", "BADMULTIPART", builtByFactory.getCodeString());
+        Assert.assertEquals("Wrong code string", "BADVERSION", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 02 00 00");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV13(builtByFactory);
+        Assert.assertEquals("Wrong type", 2, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "BADACTION", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "BADTYPE", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 03 00 00");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV13(builtByFactory);
+        Assert.assertEquals("Wrong type", 3, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "BADINSTRUCTION", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "UNKNOWNINST", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 04 00 00");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV13(builtByFactory);
+        Assert.assertEquals("Wrong type", 4, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "BADMATCH", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "BADTYPE", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 05 00 00");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV13(builtByFactory);
+        Assert.assertEquals("Wrong type", 5, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "FLOWMODFAILED", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "UNKNOWN", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 06 00 00");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV13(builtByFactory);
+        Assert.assertEquals("Wrong type", 6, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "GROUPMODFAILED", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "GROUPEXISTS", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 07 00 00");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV13(builtByFactory);
+        Assert.assertEquals("Wrong type", 7, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "PORTMODFAILED", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "BADPORT", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 08 00 00");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV13(builtByFactory);
+        Assert.assertEquals("Wrong type", 8, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "TABLEMODFAILED", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "BADTABLE", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 09 00 00");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV13(builtByFactory);
+        Assert.assertEquals("Wrong type", 9, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "QUEUEOPFAILED", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "BADPORT", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 0A 00 00");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV13(builtByFactory);
+        Assert.assertEquals("Wrong type", 10, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "SWITCHCONFIGFAILED", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "BADFLAGS", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 0B 00 00");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV13(builtByFactory);
+        Assert.assertEquals("Wrong type", 11, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "ROLEREQUESTFAILED", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "STALE", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 0C 00 00");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV13(builtByFactory);
+        Assert.assertEquals("Wrong type", 12, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "METERMODFAILED", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "UNKNOWN", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 0D 00 00");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV13(builtByFactory);
+        Assert.assertEquals("Wrong type", 13, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "TABLEFEATURESFAILED", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "BADTABLE", builtByFactory.getCodeString());
         Assert.assertNull("Data is not null", builtByFactory.getData());
     }
-    
+
+    /**
+     * Test of {@link ErrorMessageFactory} for correct translation into POJO
+     * - not existing code used
+     */
+    @Test
+    public void testWithoutData2() {
+        ByteBuf bb = BufferHelper.buildBuffer("00 00 FF FF");
+        ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV13(builtByFactory);
+        Assert.assertEquals("Wrong type", 0, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "HELLOFAILED", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 01 FF FF");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV13(builtByFactory);
+        Assert.assertEquals("Wrong type", 1, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "BADREQUEST", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 02 FF FF");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV13(builtByFactory);
+        Assert.assertEquals("Wrong type", 2, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "BADACTION", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 03 FF FF");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV13(builtByFactory);
+        Assert.assertEquals("Wrong type", 3, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "BADINSTRUCTION", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 04 FF FF");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV13(builtByFactory);
+        Assert.assertEquals("Wrong type", 4, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "BADMATCH", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 05 FF FF");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV13(builtByFactory);
+        Assert.assertEquals("Wrong type", 5, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "FLOWMODFAILED", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 06 FF FF");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV13(builtByFactory);
+        Assert.assertEquals("Wrong type", 6, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "GROUPMODFAILED", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 07 FF FF");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV13(builtByFactory);
+        Assert.assertEquals("Wrong type", 7, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "PORTMODFAILED", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 08 FF FF");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV13(builtByFactory);
+        Assert.assertEquals("Wrong type", 8, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "TABLEMODFAILED", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 09 FF FF");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV13(builtByFactory);
+        Assert.assertEquals("Wrong type", 9, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "QUEUEOPFAILED", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 0A FF FF");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV13(builtByFactory);
+        Assert.assertEquals("Wrong type", 10, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "SWITCHCONFIGFAILED", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 0B FF FF");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV13(builtByFactory);
+        Assert.assertEquals("Wrong type", 11, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "ROLEREQUESTFAILED", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 0C FF FF");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV13(builtByFactory);
+        Assert.assertEquals("Wrong type", 12, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "METERMODFAILED", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 0D FF FF");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV13(builtByFactory);
+        Assert.assertEquals("Wrong type", 13, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "TABLEFEATURESFAILED", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+    }
+
     /**
      * Test of {@link ErrorMessageFactory} for correct translation into POJO
      */
@@ -103,4 +383,17 @@ public class ErrorMessageFactoryTest {
         Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
         Assert.assertArrayEquals("Wrong data", new byte[]{0x00, 0x01, 0x02, 0x03}, builtByFactory.getData());
     }
-}
+
+    /**
+     * Test of {@link ErrorMessageFactory} for correct translation into POJO
+     */
+    @Test
+    public void testExperimenterError() {
+        Mockito.when(registry.getDeserializer(Matchers.any(MessageCodeKey.class))).thenReturn(deserializer);
+        errorFactory.injectDeserializerRegistry(registry);
+        ByteBuf bb = BufferHelper.buildBuffer("FF FF 00 00 00 01");
+        BufferHelper.deserialize(errorFactory, bb);
+
+        Mockito.verify(deserializer, Mockito.times(1)).deserialize(bb);
+    }
+}
\ No newline at end of file
index 98be668f6dffa320e84a2957d24afa33bddbc434..bf058b7ba53a69a3c86f76b08b92420c399beb3b 100644 (file)
@@ -39,20 +39,137 @@ public class OF10ErrorMessageFactoryTest {
     }
 
     /**
-     * Test of {@link OF10ErrorMessageFactory} for correct translation into POJO
+     * Test of {@link ErrorMessageFactory} for correct translation into POJO
      */
-       @Test
-       public void testWithoutData() {
-               ByteBuf bb = BufferHelper.buildBuffer("00 01 00 02");
-               ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+    @Test
+    public void testWithoutData() {
+        ByteBuf bb = BufferHelper.buildBuffer("00 00 00 00");
+        ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
 
-               BufferHelper.checkHeaderV10(builtByFactory);
-               Assert.assertEquals("Wrong type", 1, builtByFactory.getType().intValue());
-               Assert.assertEquals("Wrong code", 2, builtByFactory.getCode().intValue());
-               Assert.assertEquals("Wrong type string", "BADREQUEST", builtByFactory.getTypeString());
-               Assert.assertEquals("Wrong code string", "BADSTAT", builtByFactory.getCodeString());
-               Assert.assertNull("Data is not null", builtByFactory.getData());
-       }
+        BufferHelper.checkHeaderV10(builtByFactory);
+        Assert.assertEquals("Wrong type", 0, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "HELLOFAILED", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "INCOMPATIBLE", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 01 00 00");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV10(builtByFactory);
+        Assert.assertEquals("Wrong type", 1, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "BADREQUEST", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "BADVERSION", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 02 00 00");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV10(builtByFactory);
+        Assert.assertEquals("Wrong type", 2, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "BADACTION", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "BADTYPE", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 03 00 00");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV10(builtByFactory);
+        Assert.assertEquals("Wrong type", 3, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "FLOWMODFAILED", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "ALLTABLESFULL", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 04 00 00");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV10(builtByFactory);
+        Assert.assertEquals("Wrong type", 4, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "PORTMODFAILED", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "BADPORT", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 05 00 00");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV10(builtByFactory);
+        Assert.assertEquals("Wrong type", 5, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 0, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "QUEUEOPFAILED", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "BADPORT", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+    }
+
+    /**
+     * Test of {@link ErrorMessageFactory} for correct translation into POJO
+     * - not existing code used
+     */
+    @Test
+    public void testWithoutData2() {
+        ByteBuf bb = BufferHelper.buildBuffer("00 00 FF FF");
+        ErrorMessage builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV10(builtByFactory);
+        Assert.assertEquals("Wrong type", 0, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "HELLOFAILED", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 01 FF FF");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV10(builtByFactory);
+        Assert.assertEquals("Wrong type", 1, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "BADREQUEST", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 02 FF FF");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV10(builtByFactory);
+        Assert.assertEquals("Wrong type", 2, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "BADACTION", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 03 FF FF");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV10(builtByFactory);
+        Assert.assertEquals("Wrong type", 3, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "FLOWMODFAILED", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 04 FF FF");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV10(builtByFactory);
+        Assert.assertEquals("Wrong type", 4, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "PORTMODFAILED", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+
+        bb = BufferHelper.buildBuffer("00 05 FF FF");
+        builtByFactory = BufferHelper.deserialize(errorFactory, bb);
+
+        BufferHelper.checkHeaderV10(builtByFactory);
+        Assert.assertEquals("Wrong type", 5, builtByFactory.getType().intValue());
+        Assert.assertEquals("Wrong code", 65535, builtByFactory.getCode().intValue());
+        Assert.assertEquals("Wrong type string", "QUEUEOPFAILED", builtByFactory.getTypeString());
+        Assert.assertEquals("Wrong code string", "UNKNOWN_CODE", builtByFactory.getCodeString());
+        Assert.assertNull("Data is not null", builtByFactory.getData());
+    }
        
        /**
      * Test of {@link OF10ErrorMessageFactory} for correct translation into POJO
diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/VendorMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/VendorMessageFactoryTest.java
new file mode 100644 (file)
index 0000000..d293efc
--- /dev/null
@@ -0,0 +1,48 @@
+/*\r
+ * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.\r
+ *\r
+ * This program and the accompanying materials are made available under the\r
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
+ * and is available at http://www.eclipse.org/legal/epl-v10.html\r
+ */\r
+\r
+package org.opendaylight.openflowjava.protocol.impl.deserialization.factories;\r
+\r
+import io.netty.buffer.ByteBuf;\r
+\r
+import org.junit.Assert;\r
+import org.junit.Test;\r
+import org.junit.runner.RunWith;\r
+import org.mockito.Matchers;\r
+import org.mockito.Mock;\r
+import org.mockito.Mockito;\r
+import org.mockito.runners.MockitoJUnitRunner;\r
+import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;\r
+import org.opendaylight.openflowjava.protocol.api.extensibility.MessageCodeKey;\r
+import org.opendaylight.openflowjava.util.ByteBufUtils;\r
+\r
+/**\r
+ * @author michal.polkorab\r
+ *\r
+ */\r
+@RunWith(MockitoJUnitRunner.class)\r
+public class VendorMessageFactoryTest {\r
+\r
+    @Mock DeserializerRegistry registry;\r
+    @Mock ExperimenterMessageFactory deserializer;\r
+\r
+    /**\r
+     * Tests {@link VendorMessageFactory#deserialize(ByteBuf)}\r
+     */\r
+    @Test\r
+    public void test() {\r
+        Mockito.when(registry.getDeserializer(Matchers.any(MessageCodeKey.class))).thenReturn(deserializer);\r
+        ByteBuf buffer = ByteBufUtils.hexStringToByteBuf("04 04 00 10 01 02 03 04 FF FF FF FF 80 00 00 00");\r
+        VendorMessageFactory factory = new VendorMessageFactory();\r
+        factory.injectDeserializerRegistry(registry);\r
+        factory.deserialize(buffer);\r
+\r
+        Mockito.verify(deserializer, Mockito.times(1)).deserialize(buffer);\r
+        Assert.assertEquals("Buffer index modified", 16, buffer.readableBytes());\r
+    }\r
+}
\ No newline at end of file
diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/instruction/AbstractInstructionDeserializerTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/instruction/AbstractInstructionDeserializerTest.java
new file mode 100644 (file)
index 0000000..dcfbe92
--- /dev/null
@@ -0,0 +1,65 @@
+/*\r
+ * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.\r
+ *\r
+ * This program and the accompanying materials are made available under the\r
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
+ * and is available at http://www.eclipse.org/legal/epl-v10.html\r
+ */\r
+\r
+package org.opendaylight.openflowjava.protocol.impl.deserialization.instruction;\r
+\r
+import io.netty.buffer.ByteBuf;\r
+\r
+import org.junit.Assert;\r
+import org.junit.Test;\r
+import org.opendaylight.openflowjava.util.ByteBufUtils;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ClearActions;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Meter;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteActions;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction;\r
+\r
+/**\r
+ * @author michal.polkorab\r
+ *\r
+ */\r
+public class AbstractInstructionDeserializerTest {\r
+\r
+    /**\r
+     * Tests {@link AbstractInstructionDeserializer#deserializeHeader(ByteBuf)} with different\r
+     * instruction types\r
+     */\r
+    @Test(expected=IllegalStateException.class)\r
+    public void test() {\r
+        ByteBuf buffer = ByteBufUtils.hexStringToByteBuf("00 01 00 04");\r
+        GoToTableInstructionDeserializer deserializer = new GoToTableInstructionDeserializer();\r
+        Instruction instruction = deserializer.deserializeHeader(buffer);\r
+        Assert.assertEquals("Wrong type", GotoTable.class, instruction.getType());\r
+\r
+        buffer = ByteBufUtils.hexStringToByteBuf("00 02 00 04");\r
+        instruction = deserializer.deserializeHeader(buffer);\r
+        Assert.assertEquals("Wrong type", WriteMetadata.class, instruction.getType());\r
+\r
+        buffer = ByteBufUtils.hexStringToByteBuf("00 03 00 04");\r
+        instruction = deserializer.deserializeHeader(buffer);\r
+        Assert.assertEquals("Wrong type", WriteActions.class, instruction.getType());\r
+\r
+        buffer = ByteBufUtils.hexStringToByteBuf("00 04 00 04");\r
+        instruction = deserializer.deserializeHeader(buffer);\r
+        Assert.assertEquals("Wrong type", ApplyActions.class, instruction.getType());\r
+\r
+        buffer = ByteBufUtils.hexStringToByteBuf("00 05 00 04");\r
+        instruction = deserializer.deserializeHeader(buffer);\r
+        Assert.assertEquals("Wrong type", ClearActions.class, instruction.getType());\r
+\r
+        buffer = ByteBufUtils.hexStringToByteBuf("00 06 00 04");\r
+        instruction = deserializer.deserializeHeader(buffer);\r
+        Assert.assertEquals("Wrong type", Meter.class, instruction.getType());\r
+\r
+        buffer = ByteBufUtils.hexStringToByteBuf("00 00 00 04");\r
+        instruction = deserializer.deserializeHeader(buffer);\r
+        // exception expected\r
+    }\r
+}
\ No newline at end of file
diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/VendorInputMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/VendorInputMessageFactoryTest.java
new file mode 100644 (file)
index 0000000..df1c345
--- /dev/null
@@ -0,0 +1,57 @@
+/*\r
+ * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.\r
+ *\r
+ * This program and the accompanying materials are made available under the\r
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
+ * and is available at http://www.eclipse.org/legal/epl-v10.html\r
+ */\r
+\r
+package org.opendaylight.openflowjava.protocol.impl.serialization.factories;\r
+\r
+import io.netty.buffer.ByteBuf;\r
+import io.netty.buffer.PooledByteBufAllocator;\r
+\r
+import org.junit.Test;\r
+import org.junit.runner.RunWith;\r
+import org.mockito.Matchers;\r
+import org.mockito.Mock;\r
+import org.mockito.Mockito;\r
+import org.mockito.runners.MockitoJUnitRunner;\r
+import org.opendaylight.openflowjava.protocol.api.extensibility.MessageTypeKey;\r
+import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;\r
+import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ExperimenterId;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInputBuilder;\r
+\r
+/**\r
+ * @author michal.polkorab\r
+ *\r
+ */\r
+@RunWith(MockitoJUnitRunner.class)\r
+public class VendorInputMessageFactoryTest {\r
+\r
+    @Mock SerializerRegistry registry;\r
+    @Mock ExperimenterInputMessageFactory serializer;\r
+\r
+    /**\r
+     * Tests {@link VendorInputMessageFactory#serialize(ExperimenterInput, ByteBuf)}\r
+     */\r
+    @Test\r
+    public void test() {\r
+        Mockito.when(registry.getSerializer((MessageTypeKey<?>) Matchers.any(MessageTypeKey.class)))\r
+            .thenReturn(serializer);\r
+        VendorInputMessageFactory factory = new VendorInputMessageFactory();\r
+        factory.injectSerializerRegistry(registry);\r
+        ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();\r
+        ExperimenterInputBuilder builder = new ExperimenterInputBuilder();\r
+        builder.setVersion((short) EncodeConstants.OF10_VERSION_ID);\r
+        builder.setXid(12345L);\r
+        builder.setExperimenter(new ExperimenterId(42L));\r
+        builder.setExpType(84L);\r
+        ExperimenterInput experimenterInput = builder.build();\r
+\r
+        factory.serialize(experimenterInput, buffer);\r
+        Mockito.verify(serializer, Mockito.times(1)).serialize(experimenterInput, buffer);\r
+    }\r
+}
\ No newline at end of file