Check TrafficTreatment and TrafficSelector in FlowRuleCodecTest

Change-Id: I1694dd9103899731c4e0fdbb1ee531ef36ed6a86
diff --git a/core/common/src/test/java/org/onosproject/codec/impl/FlowRuleCodecTest.java b/core/common/src/test/java/org/onosproject/codec/impl/FlowRuleCodecTest.java
index 11b7eb6..2625db2 100644
--- a/core/common/src/test/java/org/onosproject/codec/impl/FlowRuleCodecTest.java
+++ b/core/common/src/test/java/org/onosproject/codec/impl/FlowRuleCodecTest.java
@@ -39,7 +39,12 @@
 import org.onosproject.net.OduSignalType;
 import org.onosproject.net.PortNumber;
 import org.onosproject.net.flow.DefaultFlowRule;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.DefaultTrafficTreatment;
 import org.onosproject.net.flow.FlowRule;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.flow.criteria.Criteria;
 import org.onosproject.net.flow.criteria.Criterion;
 import org.onosproject.net.flow.criteria.EthCriterion;
 import org.onosproject.net.flow.criteria.EthTypeCriterion;
@@ -178,11 +183,37 @@
     public void testFlowRuleEncode() {
 
         DeviceId deviceId = DeviceId.deviceId("of:000000000000000a");
+
+        Instruction output = Instructions.createOutput(PortNumber.portNumber(0));
+        Instruction modL2Src = Instructions.modL2Src(MacAddress.valueOf("11:22:33:44:55:66"));
+        Instruction modL2Dst = Instructions.modL2Dst(MacAddress.valueOf("44:55:66:77:88:99"));
+        TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
+        TrafficTreatment treatment = tBuilder
+                .add(output)
+                .add(modL2Src)
+                .add(modL2Dst)
+                .build();
+
+        Criterion inPort = Criteria.matchInPort(PortNumber.portNumber(0));
+        Criterion ethSrc = Criteria.matchEthSrc(MacAddress.valueOf("11:22:33:44:55:66"));
+        Criterion ethDst = Criteria.matchEthDst(MacAddress.valueOf("44:55:66:77:88:99"));
+        Criterion ethType = Criteria.matchEthType(Ethernet.TYPE_IPV4);
+
+        TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
+        TrafficSelector selector = sBuilder
+                .add(inPort)
+                .add(ethSrc)
+                .add(ethDst)
+                .add(ethType)
+                .build();
+
         FlowRule permFlowRule = DefaultFlowRule.builder()
                                                     .withCookie(1)
                                                     .forTable(1)
                                                     .withPriority(1)
                                                     .makePermanent()
+                                                    .withTreatment(treatment)
+                                                    .withSelector(selector)
                                                     .forDevice(deviceId).build();
 
         FlowRule tempFlowRule =  DefaultFlowRule.builder()
@@ -190,6 +221,8 @@
                                                 .forTable(1)
                                                 .withPriority(1)
                                                 .makeTemporary(1000)
+                                                .withTreatment(treatment)
+                                                .withSelector(selector)
                                                 .forDevice(deviceId).build();
 
         ObjectNode permFlowRuleJson = flowRuleCodec.encode(permFlowRule, context);
@@ -260,9 +293,27 @@
                 return false;
             }
 
-            // TODO: need to check traffic treatment
+            // check traffic treatment
+            JsonNode jsonTreatment = jsonNode.get("treatment");
+            TrafficTreatmentCodecTest.TrafficTreatmentJsonMatcher treatmentMatcher =
+                    TrafficTreatmentCodecTest.TrafficTreatmentJsonMatcher
+                            .matchesTrafficTreatment(flowRule.treatment());
 
-            // TODO: need to check selector
+            if (!treatmentMatcher.matches(jsonTreatment)) {
+                description.appendText("treatment is not matched");
+                return false;
+            }
+
+            // check traffic selector
+            JsonNode jsonSelector = jsonNode.get("selector");
+            TrafficSelectorCodecTest.TrafficSelectorJsonMatcher selectorMatcher =
+                    TrafficSelectorCodecTest.TrafficSelectorJsonMatcher
+                            .matchesTrafficSelector(flowRule.selector());
+
+            if (!selectorMatcher.matches(jsonSelector)) {
+                description.appendText("selector is not matched");
+                return false;
+            }
 
             return true;
         }
diff --git a/core/common/src/test/java/org/onosproject/codec/impl/TrafficTreatmentCodecTest.java b/core/common/src/test/java/org/onosproject/codec/impl/TrafficTreatmentCodecTest.java
index f23e846..3a26a92 100644
--- a/core/common/src/test/java/org/onosproject/codec/impl/TrafficTreatmentCodecTest.java
+++ b/core/common/src/test/java/org/onosproject/codec/impl/TrafficTreatmentCodecTest.java
@@ -95,7 +95,7 @@
         assertThat(types.contains(insts.get(1).type().name()), is(true));
     }
 
-    private static final class TrafficTreatmentJsonMatcher
+    public static final class TrafficTreatmentJsonMatcher
             extends TypeSafeDiagnosingMatcher<JsonNode> {
 
         private final TrafficTreatment trafficTreatment;
@@ -163,19 +163,23 @@
             // check metered
             JsonNode meterNode = getInstNode(jsonInstructions, "METER");
             String jsonMeterId = meterNode != null ? meterNode.get("meterId").asText() : null;
-            String meterId = trafficTreatment.metered().meterId().toString();
-            if (!StringUtils.equals(jsonMeterId, meterId)) {
-                description.appendText("meter id was " + jsonMeterId);
-                return false;
+            if (trafficTreatment.metered() != null) {
+                String meterId = trafficTreatment.metered().meterId().toString();
+                if (!StringUtils.equals(jsonMeterId, meterId)) {
+                    description.appendText("meter id was " + jsonMeterId);
+                    return false;
+                }
             }
 
             // check table transition
             JsonNode tableNode = getInstNode(jsonInstructions, "TABLE");
             String jsonTableId = tableNode != null ? tableNode.get("tableId").asText() : null;
-            String tableId = trafficTreatment.tableTransition().tableId().toString();
-            if (!StringUtils.equals(jsonTableId, tableId)) {
-                description.appendText("table id was " + jsonMeterId);
-                return false;
+            if (trafficTreatment.tableTransition() != null) {
+                String tableId = trafficTreatment.tableTransition().tableId().toString();
+                if (!StringUtils.equals(jsonTableId, tableId)) {
+                    description.appendText("table id was " + jsonMeterId);
+                    return false;
+                }
             }
 
             // TODO: check deferred