Improve unit test coverage
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / match / OxmIpv4SrcSerializerTest.java
diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/match/OxmIpv4SrcSerializerTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/match/OxmIpv4SrcSerializerTest.java
new file mode 100644 (file)
index 0000000..5b2834b
--- /dev/null
@@ -0,0 +1,159 @@
+/*\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.match;\r
+\r
+import static org.junit.Assert.assertEquals;\r
+import static org.junit.Assert.assertTrue;\r
+import io.netty.buffer.ByteBuf;\r
+import io.netty.buffer.PooledByteBufAllocator;\r
+\r
+import org.junit.Assert;\r
+import org.junit.Test;\r
+import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;\r
+import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;\r
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv4AddressMatchEntry;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv4AddressMatchEntryBuilder;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntry;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntryBuilder;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv4Src;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OpenflowBasicClass;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntriesBuilder;\r
+\r
+/**\r
+ * @author michal.polkorab\r
+ *\r
+ */\r
+public class OxmIpv4SrcSerializerTest {\r
+\r
+    OxmIpv4SrcSerializer serializer = new OxmIpv4SrcSerializer();\r
+\r
+    /**\r
+     * Test correct serialization\r
+     */\r
+    @Test\r
+    public void testSerializeWithoutMask() {\r
+        MatchEntriesBuilder builder = prepareMatchEntry(false, "10.0.0.1");\r
+        \r
+        ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();\r
+        serializer.serialize(builder.build(), buffer);\r
+\r
+        checkHeader(buffer, false);\r
+        byte[] address = new byte[4];\r
+        buffer.readBytes(address);\r
+        Assert.assertArrayEquals("Wrong address", new byte[]{10, 0, 0, 1}, address);\r
+        assertTrue("Unexpected data", buffer.readableBytes() == 0);\r
+    }\r
+\r
+    /**\r
+     * Test correct serialization\r
+     */\r
+    @Test\r
+    public void testSerializeWithMask() {\r
+        MatchEntriesBuilder builder = prepareMatchEntry(true, "120.121.122.0");\r
+        \r
+        ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();\r
+        serializer.serialize(builder.build(), buffer);\r
+\r
+        checkHeader(buffer, true);\r
+        \r
+        byte[] address = new byte[4];\r
+        buffer.readBytes(address);\r
+        Assert.assertArrayEquals("Wrong address", new byte[]{120, 121, 122, 0}, address);\r
+        byte[] tmp = new byte[4];\r
+        buffer.readBytes(tmp);\r
+        Assert.assertArrayEquals("Wrong mask", new byte[]{15, 15, 0, 0}, tmp);\r
+        assertTrue("Unexpected data", buffer.readableBytes() == 0);\r
+    }\r
+\r
+    /**\r
+     * Test correct header serialization\r
+     */\r
+    @Test\r
+    public void testSerializeHeaderWithoutMask() {\r
+        MatchEntriesBuilder builder = prepareHeader(false);\r
+        \r
+        ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();\r
+        serializer.serializeHeader(builder.build(), buffer);\r
+\r
+        checkHeader(buffer, false);\r
+        assertTrue("Unexpected data", buffer.readableBytes() == 0);\r
+    }\r
+\r
+    /**\r
+     * Test correct header serialization\r
+     */\r
+    @Test\r
+    public void testSerializeHeaderWithMask() {\r
+        MatchEntriesBuilder builder = prepareHeader(true);\r
+        \r
+        ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();\r
+        serializer.serializeHeader(builder.build(), buffer);\r
+\r
+        checkHeader(buffer, true);\r
+        assertTrue("Unexpected data", buffer.readableBytes() == 0);\r
+    }\r
+\r
+    /**\r
+     * Test correct oxm-class return value\r
+     */\r
+    @Test\r
+    public void testGetOxmClassCode() {\r
+        assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());\r
+    }\r
+\r
+    /**\r
+     * Test correct oxm-field return value\r
+     */\r
+    @Test\r
+    public void getOxmFieldCode() {\r
+        assertEquals("Wrong oxm-class", OxmMatchConstants.IPV4_SRC, serializer.getOxmFieldCode());\r
+    }\r
+\r
+    /**\r
+     * Test correct value length return value\r
+     */\r
+    @Test\r
+    public void testGetValueLength() {\r
+        assertEquals("Wrong value length", EncodeConstants.SIZE_OF_INT_IN_BYTES, serializer.getValueLength());\r
+    }\r
+\r
+    private static MatchEntriesBuilder prepareMatchEntry(boolean hasMask, String value) {\r
+        MatchEntriesBuilder builder = prepareHeader(hasMask);\r
+        if (hasMask) {\r
+            MaskMatchEntryBuilder maskBuilder = new MaskMatchEntryBuilder();\r
+            maskBuilder.setMask(new byte[]{15, 15, 0, 0});\r
+            builder.addAugmentation(MaskMatchEntry.class, maskBuilder.build());\r
+        }\r
+        Ipv4AddressMatchEntryBuilder addressBuilder = new Ipv4AddressMatchEntryBuilder();\r
+        addressBuilder.setIpv4Address(new Ipv4Address(value));\r
+        builder.addAugmentation(Ipv4AddressMatchEntry.class, addressBuilder.build());\r
+        return builder;\r
+    }\r
+\r
+    private static MatchEntriesBuilder prepareHeader(boolean hasMask) {\r
+        MatchEntriesBuilder builder = new MatchEntriesBuilder();\r
+        builder.setOxmClass(OpenflowBasicClass.class);\r
+        builder.setOxmMatchField(Ipv4Src.class);\r
+        builder.setHasMask(hasMask);\r
+        return builder;\r
+    }\r
+\r
+    private static void checkHeader(ByteBuf buffer, boolean hasMask) {\r
+        assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());\r
+        short fieldAndMask = buffer.readUnsignedByte();\r
+        assertEquals("Wrong oxm-field", OxmMatchConstants.IPV4_SRC, fieldAndMask >>> 1);\r
+        assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);\r
+        if (hasMask) {\r
+            assertEquals("Wrong length", EncodeConstants.SIZE_OF_LONG_IN_BYTES, buffer.readUnsignedByte());\r
+        } else {\r
+            assertEquals("Wrong length", EncodeConstants.SIZE_OF_INT_IN_BYTES, buffer.readUnsignedByte());\r
+        }\r
+    }\r
+}
\ No newline at end of file