Add codec decode tests for mapping address, action and instruction

Change-Id: Iec336e86cb0dae60b6015e8b4853d3d266ac6111
diff --git a/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingActionCodecTest.java b/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingActionCodecTest.java
index c80837f..9627413 100644
--- a/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingActionCodecTest.java
+++ b/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingActionCodecTest.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.mapping.web.codec;
 
+import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.node.ObjectNode;
 import org.junit.After;
 import org.junit.Before;
@@ -30,7 +31,11 @@
 import org.onosproject.mapping.actions.NativeForwardMappingAction;
 import org.onosproject.mapping.web.MappingCodecRegistrator;
 
+import java.io.IOException;
+import java.io.InputStream;
+
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
 import static org.hamcrest.Matchers.notNullValue;
 import static org.onosproject.mapping.web.codec.MappingActionJsonMatcher.matchesAction;
 
@@ -39,6 +44,8 @@
  */
 public class MappingActionCodecTest {
 
+    private static final String NO_ACTION_STRING = "NO_ACTION";
+
     private CodecContext context;
     private JsonCodec<MappingAction> actionCodec;
     private MappingCodecRegistrator registrator;
@@ -106,4 +113,31 @@
         final ObjectNode actionJson = actionCodec.encode(action, context);
         assertThat(actionJson, matchesAction(action));
     }
-}
+
+    /**
+     * Tests decoding of a mapping key JSON object.
+     *
+     * @throws IOException if processing the resource fails
+     */
+    @Test
+    public void testMappingActionDecode() throws IOException {
+        MappingAction action = getAction("MappingAction.json");
+        assertThat(action.toString(), is(NO_ACTION_STRING));
+    }
+
+    /**
+     * Reads in a mapping action from the given resource and decodes it.
+     *
+     * @param resourceName resource to use to read the JSON for the rule
+     * @return decoded mappingAction
+     * @throws IOException if processing the resource fails
+     */
+    private MappingAction getAction(String resourceName) throws IOException {
+        InputStream jsonStream = MappingActionCodecTest.class.getResourceAsStream(resourceName);
+        JsonNode json = context.mapper().readTree(jsonStream);
+        assertThat(json, notNullValue());
+        MappingAction action = actionCodec.decode((ObjectNode) json, context);
+        assertThat(action, notNullValue());
+        return action;
+    }
+ }
diff --git a/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingAddressCodecTest.java b/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingAddressCodecTest.java
index f5eb825..11e2d9b 100644
--- a/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingAddressCodecTest.java
+++ b/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingAddressCodecTest.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.mapping.web.codec;
 
+import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.node.ObjectNode;
 import org.junit.After;
 import org.junit.Before;
@@ -28,7 +29,11 @@
 import org.onosproject.mapping.addresses.MappingAddresses;
 import org.onosproject.mapping.web.MappingCodecRegistrator;
 
+import java.io.IOException;
+import java.io.InputStream;
+
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
 import static org.hamcrest.Matchers.notNullValue;
 import static org.onosproject.mapping.web.codec.MappingAddressJsonMatcher.matchesMappingAddress;
 
@@ -40,7 +45,10 @@
     private CodecContext context;
     private JsonCodec<MappingAddress> addressCodec;
     private MappingCodecRegistrator registrator;
-    private static final IpPrefix IPV4_PREFIX = IpPrefix.valueOf("10.1.1.0/24");
+    private static final String IPV4_STRING = "1.2.3.4";
+    private static final String PORT_STRING = "32";
+    private static final IpPrefix IPV4_PREFIX =
+            IpPrefix.valueOf(IPV4_STRING + "/" + PORT_STRING);
     private static final IpPrefix IPV6_PREFIX = IpPrefix.valueOf("fe80::/64");
     private static final MacAddress MAC = MacAddress.valueOf("00:00:11:00:00:01");
     private static final String DN = "onos";
@@ -117,4 +125,32 @@
         ObjectNode result = addressCodec.encode(address, context);
         assertThat(result, matchesMappingAddress(address));
     }
+
+    /**
+     * Tests the decoding of mapping address from JSON object.
+     *
+     * @throws IOException if processing the resource fails
+     */
+    @Test
+    public void testMappingAddressDecode() throws IOException {
+        MappingAddress address = getAddress("MappingAddress.json");
+        assertThat(address.toString(),
+                is("IPV4:" + IPV4_STRING + "/" + PORT_STRING));
+    }
+
+    /**
+     * Reads in a mapping address from the given resource and decodes it.
+     *
+     * @param resourceName resource to use to read the JSON for the rule
+     * @return decoded mappingAddress
+     * @throws IOException if processing the resource fails
+     */
+    private MappingAddress getAddress(String resourceName) throws IOException {
+        InputStream jsonStream = MappingAddressCodecTest.class.getResourceAsStream(resourceName);
+        JsonNode json = context.mapper().readTree(jsonStream);
+        assertThat(json, notNullValue());
+        MappingAddress address = addressCodec.decode((ObjectNode) json, context);
+        assertThat(address, notNullValue());
+        return address;
+    }
 }
diff --git a/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingInstructionCodecTest.java b/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingInstructionCodecTest.java
index b2fb3d4..9fc13aa 100644
--- a/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingInstructionCodecTest.java
+++ b/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingInstructionCodecTest.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.mapping.web.codec;
 
+import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.node.ObjectNode;
 import org.junit.After;
 import org.junit.Before;
@@ -28,7 +29,11 @@
 import org.onosproject.mapping.instructions.UnicastMappingInstruction;
 import org.onosproject.mapping.web.MappingCodecRegistrator;
 
+import java.io.IOException;
+import java.io.InputStream;
+
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
 import static org.hamcrest.Matchers.notNullValue;
 import static org.onosproject.mapping.web.codec.MappingInstructionJsonMatcher.matchesInstruction;
 
@@ -46,6 +51,9 @@
     private static final int MULTICAST_WEIGHT = 2;
     private static final int MULTICAST_PRIORITY = 2;
 
+    private static final String UNICAST_TYPE_STRING = "UNICAST";
+    private static final String WEIGHT_SUBTYPE_STRING = "WEIGHT";
+
     /**
      * Sets up for each test.
      * Creates a context and fetches the mapping instruction codec.
@@ -119,4 +127,32 @@
                 instructionCodec.encode(instruction, context);
         assertThat(instructionJson, matchesInstruction(instruction));
     }
+
+    /**
+     * Tests the decoding of mapping instruction from JSON object.
+     *
+     * @throws IOException if processing the resource fails
+     */
+    @Test
+    public void testMappingInstructionDecode() throws IOException {
+        UnicastMappingInstruction instruction = (UnicastMappingInstruction) getInstruction("MappingInstruction.json");
+        assertThat(instruction.type().toString(), is(UNICAST_TYPE_STRING));
+        assertThat(instruction.subtype().toString(), is(WEIGHT_SUBTYPE_STRING));
+    }
+
+    /**
+     * Reads in a mapping instruction from the given resource and decodes it.
+     *
+     * @param resourceName resource to use to read the JSON for the rule
+     * @return decoded mappingInstruction
+     * @throws IOException if processing the resource fails
+     */
+    private MappingInstruction getInstruction(String resourceName) throws IOException {
+        InputStream jsonStream = MappingInstructionCodecTest.class.getResourceAsStream(resourceName);
+        JsonNode json = context.mapper().readTree(jsonStream);
+        assertThat(json, notNullValue());
+        MappingInstruction instruction = instructionCodec.decode((ObjectNode) json, context);
+        assertThat(instruction, notNullValue());
+        return instruction;
+    }
 }
diff --git a/apps/mappingmanagement/web/src/test/resources/org/onosproject/mapping/web/codec/MappingAction.json b/apps/mappingmanagement/web/src/test/resources/org/onosproject/mapping/web/codec/MappingAction.json
new file mode 100644
index 0000000..6180224
--- /dev/null
+++ b/apps/mappingmanagement/web/src/test/resources/org/onosproject/mapping/web/codec/MappingAction.json
@@ -0,0 +1,3 @@
+{
+  "type": "NO_ACTION"
+}
\ No newline at end of file
diff --git a/apps/mappingmanagement/web/src/test/resources/org/onosproject/mapping/web/codec/MappingAddress.json b/apps/mappingmanagement/web/src/test/resources/org/onosproject/mapping/web/codec/MappingAddress.json
new file mode 100644
index 0000000..376f313
--- /dev/null
+++ b/apps/mappingmanagement/web/src/test/resources/org/onosproject/mapping/web/codec/MappingAddress.json
@@ -0,0 +1,4 @@
+{
+  "type": "IPV4",
+  "ipv4": "1.2.3.4/32"
+}
\ No newline at end of file
diff --git a/apps/mappingmanagement/web/src/test/resources/org/onosproject/mapping/web/codec/MappingInstruction.json b/apps/mappingmanagement/web/src/test/resources/org/onosproject/mapping/web/codec/MappingInstruction.json
new file mode 100644
index 0000000..244563f
--- /dev/null
+++ b/apps/mappingmanagement/web/src/test/resources/org/onosproject/mapping/web/codec/MappingInstruction.json
@@ -0,0 +1,5 @@
+{
+  "type": "UNICAST",
+  "subtype": "WEIGHT",
+  "unicastWeight": 1
+}
\ No newline at end of file