Add unit test and a JSON matcher for openstacknode codec

Change-Id: Ida4d5b7bedbf45df0c8580a4d50d7d725e8c7650
diff --git a/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/codec/OpenstackNodeCodec.java b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/codec/OpenstackNodeCodec.java
index f488429..8bae1ea 100644
--- a/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/codec/OpenstackNodeCodec.java
+++ b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/codec/OpenstackNodeCodec.java
@@ -44,8 +44,6 @@
 
     private static final String TYPE = "type";
     private static final String INTEGRATION_BRIDGE = "integrationBridge";
-    private static final String TUNNEL_PORT_NUM = "tunnelPortNum";
-    private static final String VLAN_PORT_NUM = "vlanPortNum";
     private static final String STATE = "state";
 
     private static final String MISSING_MESSAGE = " is required in OpenstackNode";
@@ -75,13 +73,8 @@
             result.put(DATA_IP, node.dataIp().toString());
         }
 
-        if (node.tunnelPortNum() != null) {
-            result.put(TUNNEL_PORT_NUM, node.tunnelPortNum().toString());
-        }
-
-        if (node.vlanPortNum() != null) {
-            result.put(VLAN_PORT_NUM, node.vlanPortNum().toString());
-        }
+        // TODO: need to find a way to not refer to ServiceDirectory from
+        // DefaultOpenstackNode
 
         return result;
     }
diff --git a/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackNodeCodecTest.java b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackNodeCodecTest.java
new file mode 100644
index 0000000..e009209
--- /dev/null
+++ b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackNodeCodecTest.java
@@ -0,0 +1,150 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.openstacknode.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.hamcrest.MatcherAssert;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.IpAddress;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.codec.impl.CodecManager;
+import org.onosproject.core.CoreService;
+import org.onosproject.net.DeviceId;
+import org.onosproject.openstacknode.api.NodeState;
+import org.onosproject.openstacknode.api.OpenstackNode;
+import org.onosproject.openstacknode.impl.DefaultOpenstackNode;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.onosproject.net.NetTestTools.APP_ID;
+import static org.onosproject.openstacknode.codec.OpenstackNodeJsonMatcher.matchesOpenstackNode;
+
+/**
+ * Unit tests for OpenstackNode codec.
+ */
+public class OpenstackNodeCodecTest {
+    MockCodecContext context;
+    JsonCodec<OpenstackNode> openstackNodeCodec;
+    final CoreService mockCoreService = createMock(CoreService.class);
+    private static final String REST_APP_ID = "org.onosproject.rest";
+
+    @Before
+    public void setUp() {
+        context = new MockCodecContext();
+        openstackNodeCodec = new OpenstackNodeCodec();
+        assertThat(openstackNodeCodec, notNullValue());
+
+        expect(mockCoreService.registerApplication(REST_APP_ID))
+                .andReturn(APP_ID).anyTimes();
+        replay(mockCoreService);
+        context.registerService(CoreService.class, mockCoreService);
+    }
+
+    @Test
+    public void testOpenstackNodeEncode() {
+        OpenstackNode node = DefaultOpenstackNode.builder()
+                                .hostname("compute")
+                                .type(OpenstackNode.NodeType.COMPUTE)
+                                .state(NodeState.INIT)
+                                .managementIp(IpAddress.valueOf("10.10.10.1"))
+                                .intgBridge(DeviceId.deviceId("br-int"))
+                                .vlanIntf("vxlan")
+                                .dataIp(IpAddress.valueOf("20.20.20.2"))
+                                .build();
+
+        ObjectNode nodeJson = openstackNodeCodec.encode(node, context);
+        assertThat(nodeJson, matchesOpenstackNode(node));
+    }
+
+    @Test
+    public void testOpenstackNodeDecode() throws IOException {
+        OpenstackNode node = getOpenstackNode("OpenstackNode.json");
+
+        assertThat(node.hostname(), is("compute-01"));
+        assertThat(node.type().name(), is("COMPUTE"));
+        assertThat(node.managementIp().toString(), is("172.16.130.4"));
+        assertThat(node.dataIp().toString(), is("172.16.130.4"));
+        assertThat(node.intgBridge().toString(), is("of:00000000000000a1"));
+        assertThat(node.vlanIntf(), is("eth2"));
+    }
+
+    /**
+     * Reads in an openstack node from the given resource and decodes it.
+     *
+     * @param resourceName resource to use to read the JSON for the rule
+     * @return decoded openstack node
+     * @throws IOException if processing the resource fails
+     */
+    private OpenstackNode getOpenstackNode(String resourceName) throws IOException {
+        InputStream jsonStream = OpenstackNodeCodecTest.class.getResourceAsStream(resourceName);
+        JsonNode json = context.mapper().readTree(jsonStream);
+        MatcherAssert.assertThat(json, notNullValue());
+        OpenstackNode node = openstackNodeCodec.decode((ObjectNode) json, context);
+        assertThat(node, notNullValue());
+        return node;
+    }
+
+    /**
+     * Mock codec context for use in codec unit tests.
+     */
+    private class MockCodecContext implements CodecContext {
+        private final ObjectMapper mapper = new ObjectMapper();
+        private final CodecManager manager = new CodecManager();
+        private final Map<Class<?>, Object> services = new HashMap<>();
+
+        /**
+         * Constructs a new mock codec context.
+         */
+        public MockCodecContext() {
+            manager.activate();
+        }
+
+        @Override
+        public ObjectMapper mapper() {
+            return mapper;
+        }
+
+        @Override
+        @SuppressWarnings("unchecked")
+        public <T> JsonCodec<T> codec(Class<T> entityClass) {
+            return manager.getCodec(entityClass);
+        }
+
+        @SuppressWarnings("unchecked")
+        @Override
+        public <T> T getService(Class<T> serviceClass) {
+            return (T) services.get(serviceClass);
+        }
+
+        // for registering mock services
+        public <T> void registerService(Class<T> serviceClass, T impl) {
+            services.put(serviceClass, impl);
+        }
+    }
+}
diff --git a/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackNodeJsonMatcher.java b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackNodeJsonMatcher.java
new file mode 100644
index 0000000..74382b1
--- /dev/null
+++ b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackNodeJsonMatcher.java
@@ -0,0 +1,120 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.openstacknode.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.onosproject.openstacknode.api.Constants;
+import org.onosproject.openstacknode.api.OpenstackNode;
+
+import static org.onosproject.openstacknode.api.Constants.DATA_IP;
+import static org.onosproject.openstacknode.api.Constants.MANAGEMENT_IP;
+import static org.onosproject.openstacknode.api.Constants.VLAN_INTF_NAME;
+
+/**
+ * Hamcrest matcher for meters.
+ */
+public final class OpenstackNodeJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
+
+    private final OpenstackNode node;
+    private static final String INTEGRATION_BRIDGE = "integrationBridge";
+    private static final String STATE = "state";
+
+    private OpenstackNodeJsonMatcher(OpenstackNode node) {
+        this.node = node;
+    }
+
+    @Override
+    protected boolean matchesSafely(JsonNode jsonNode, Description description) {
+        // check hostname
+        String jsonHostname = jsonNode.get(Constants.HOST_NAME).asText();
+        String hostname = node.hostname();
+        if (!jsonHostname.equals(hostname)) {
+            description.appendText("hostname was " + jsonHostname);
+            return false;
+        }
+
+        // check type
+        String jsonType = jsonNode.get(Constants.TYPE).asText();
+        String type = node.type().name();
+        if (!jsonType.equals(type)) {
+            description.appendText("type was " + jsonType);
+            return false;
+        }
+
+        // check management IP
+        String jsonMgmtIp = jsonNode.get(MANAGEMENT_IP).asText();
+        String mgmtIp = node.managementIp().toString();
+        if (!jsonMgmtIp.equals(mgmtIp)) {
+            description.appendText("management IP was " + jsonMgmtIp);
+            return false;
+        }
+
+        // check integration bridge
+        String jsonIntgBridge = jsonNode.get(INTEGRATION_BRIDGE).asText();
+        String intgBridge = node.intgBridge().toString();
+        if (!jsonIntgBridge.equals(intgBridge)) {
+            description.appendText("integration bridge was " + jsonIntgBridge);
+            return false;
+        }
+
+        // check state
+        String jsonState = jsonNode.get(STATE).asText();
+        String state = node.state().name();
+        if (!jsonState.equals(state)) {
+            description.appendText("state was " + jsonState);
+            return false;
+        }
+
+        // check VLAN interface
+        JsonNode jsonVlanIntf = jsonNode.get(VLAN_INTF_NAME);
+        if (jsonVlanIntf != null) {
+            String vlanIntf = node.vlanIntf();
+            if (!jsonVlanIntf.asText().equals(vlanIntf)) {
+                description.appendText("VLAN interface was " + jsonVlanIntf.asText());
+                return false;
+            }
+        }
+
+        // check data IP
+        JsonNode jsonDataIp = jsonNode.get(DATA_IP);
+        if (jsonDataIp != null) {
+            String dataIp = node.dataIp().toString();
+            if (!jsonDataIp.asText().equals(dataIp)) {
+                description.appendText("Data IP was " + jsonDataIp.asText());
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    @Override
+    public void describeTo(Description description) {
+        description.appendText(node.toString());
+    }
+
+    /**
+     * Factory to allocate an openstack node matcher.
+     *
+     * @param node openstack node object we are looking for
+     * @return matcher
+     */
+    public static OpenstackNodeJsonMatcher matchesOpenstackNode(OpenstackNode node) {
+        return new OpenstackNodeJsonMatcher(node);
+    }
+}
diff --git a/apps/openstacknode/app/src/test/resources/org/onosproject/openstacknode/codec/OpenstackNode.json b/apps/openstacknode/app/src/test/resources/org/onosproject/openstacknode/codec/OpenstackNode.json
new file mode 100644
index 0000000..dfa07e3
--- /dev/null
+++ b/apps/openstacknode/app/src/test/resources/org/onosproject/openstacknode/codec/OpenstackNode.json
@@ -0,0 +1,8 @@
+{
+  "hostname" : "compute-01",
+  "type" : "COMPUTE",
+  "managementIp" : "172.16.130.4",
+  "dataIp" : "172.16.130.4",
+  "vlanPort" : "eth2",
+  "integrationBridge" : "of:00000000000000a1"
+}
\ No newline at end of file