Implemented unit tests for ServiceNetwork json codec

Change-Id: I150c831d51d2bb7eb08d945e56c99bece5dbfd72
diff --git a/pom.xml b/pom.xml
index 096db53..e67918a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -183,6 +183,13 @@
             <version>${onos.version}</version>
         </dependency>
 
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onlab-junit</artifactId>
+            <version>${onos.version}</version>
+            <scope>test</scope>
+        </dependency>
+
     </dependencies>
 
     <build>
diff --git a/src/test/java/org/opencord/cordvtn/codec/MockCodecContext.java b/src/test/java/org/opencord/cordvtn/codec/MockCodecContext.java
new file mode 100644
index 0000000..d773317
--- /dev/null
+++ b/src/test/java/org/opencord/cordvtn/codec/MockCodecContext.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * 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.opencord.cordvtn.codec;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.codec.impl.CodecManager;
+
+/**
+ * Mock codec context for use in codec unit tests.
+ */
+public final class MockCodecContext implements CodecContext {
+
+    private final ObjectMapper mapper  = new ObjectMapper();
+    private final CodecManager manager = new CodecManager();
+
+    /**
+     * Constructs a new mock codec context.
+     */
+    public MockCodecContext() {
+        manager.activate();
+        CodecRegistrator registrator = new CodecRegistrator();
+        registrator.codecService = manager;
+        registrator.activate();
+    }
+
+    @Override
+    public ObjectMapper mapper() {
+        return mapper;
+    }
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public <T> JsonCodec<T> codec(Class<T> entityClass) {
+        return manager.getCodec(entityClass);
+    }
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public <T> T getService(Class<T> aClass) {
+        return null;
+    }
+}
diff --git a/src/test/java/org/opencord/cordvtn/codec/ServiceNetworkCodecTest.java b/src/test/java/org/opencord/cordvtn/codec/ServiceNetworkCodecTest.java
new file mode 100644
index 0000000..0886a4a
--- /dev/null
+++ b/src/test/java/org/opencord/cordvtn/codec/ServiceNetworkCodecTest.java
@@ -0,0 +1,207 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * 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.opencord.cordvtn.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.NullNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.ImmutableSet;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.codec.JsonCodec;
+import org.opencord.cordvtn.api.net.NetworkId;
+import org.opencord.cordvtn.api.net.ProviderNetwork;
+import org.opencord.cordvtn.api.net.ServiceNetwork;
+
+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.opencord.cordvtn.api.dependency.Dependency.Type.BIDIRECTIONAL;
+import static org.opencord.cordvtn.api.net.ServiceNetwork.ServiceNetworkType.MANAGEMENT_LOCAL;
+import static org.opencord.cordvtn.api.net.ServiceNetwork.ServiceNetworkType.PRIVATE;
+import static org.opencord.cordvtn.codec.ServiceNetworkJsonMatcher.matchesServiceNetwork;
+
+/**
+ * Unit tests for ServiceNetwork codec.
+ */
+public final class ServiceNetworkCodecTest {
+    private static final String SERVICE_NETWORK = "serviceNetwork";
+    private static final String ID = "id";
+    private static final String TYPE = "type";
+    private static final String PROVIDER_NETWORKS = "providerNetworks";
+
+    private final ProviderNetwork providerA =
+            ProviderNetwork.of(NetworkId.of("A"), BIDIRECTIONAL);
+
+    private final ServiceNetwork networkB = new ServiceNetwork(
+            NetworkId.of("A"),
+            MANAGEMENT_LOCAL,
+            ImmutableSet.of());
+
+    private final ServiceNetwork networkA = new ServiceNetwork(
+            NetworkId.of("B"),
+            PRIVATE,
+            ImmutableSet.of(providerA));
+
+    @Rule
+    public ExpectedException exception = ExpectedException.none();
+
+    private JsonCodec<ServiceNetwork> codec;
+    private MockCodecContext context;
+
+    /**
+     * Sets up for each test.
+     * Creates a context and fetches the ServiceNetwork codec.
+     */
+    @Before
+    public void setUp() {
+        context = new MockCodecContext();
+        codec = context.codec(ServiceNetwork.class);
+        assertThat(codec, notNullValue());
+    }
+
+    @Test
+    public void testServiceNetworkEncode() {
+        ObjectNode networkJson = codec.encode(networkA, context);
+        assertThat(networkJson, notNullValue());
+        assertThat(networkJson, matchesServiceNetwork(networkA));
+
+        networkJson = codec.encode(networkB, context);
+        assertThat(networkJson, notNullValue());
+        assertThat(networkJson, matchesServiceNetwork(networkB));
+    }
+
+    @Test
+    public void testServiceNetworkDecode() throws IOException {
+        ServiceNetwork snet = getServiceNetwork("service-network.json");
+        assertThat(snet.id(), is(NetworkId.of("A")));
+        assertThat(snet.type(), is(MANAGEMENT_LOCAL));
+        assertThat(snet.providers(), is(ImmutableSet.of()));
+
+        snet = getServiceNetwork("service-network-with-provider.json");
+        assertThat(snet.id(), is(NetworkId.of("B")));
+        assertThat(snet.type(), is(PRIVATE));
+        assertThat(snet.providers(), is(ImmutableSet.of(providerA)));
+    }
+
+    @Test
+    public void testServiceNetworkDecodeMissingId() throws IllegalArgumentException {
+        final JsonNode jsonMissingId = context.mapper().createObjectNode()
+                .put(TYPE, PRIVATE.name())
+                .put(PROVIDER_NETWORKS, "");
+        exception.expect(IllegalArgumentException.class);
+        codec.decode((ObjectNode) jsonMissingId, context);
+    }
+
+    @Test
+    public void testServiceNetworkDecodeEmptyId() throws IllegalArgumentException {
+        final JsonNode jsonEmptyId = context.mapper().createObjectNode()
+                .put(ID, "")
+                .put(TYPE, PRIVATE.name())
+                .put(PROVIDER_NETWORKS, "");
+        exception.expect(IllegalArgumentException.class);
+        codec.decode((ObjectNode) jsonEmptyId, context);
+    }
+
+    @Test
+    public void testServiceNetworkDecodeNullId() throws NullPointerException {
+        final JsonNode jsonNullId = context.mapper().createObjectNode()
+                .put(TYPE, PRIVATE.name())
+                .put(PROVIDER_NETWORKS, "")
+                .set(ID, NullNode.getInstance());
+        exception.expect(IllegalArgumentException.class);
+        codec.decode((ObjectNode) jsonNullId, context);
+    }
+
+    @Test
+    public void testServiceNetworkDecodeMissingType() throws IllegalArgumentException {
+        final JsonNode jsonMissingType = context.mapper().createObjectNode()
+                .put(ID, "A")
+                .put(PROVIDER_NETWORKS, "");
+        exception.expect(IllegalArgumentException.class);
+        codec.decode((ObjectNode) jsonMissingType, context);
+    }
+
+    @Test
+    public void testServiceNetworkDecodeEmptyType() throws IllegalArgumentException {
+        final JsonNode jsonEmptyType = context.mapper().createObjectNode()
+                .put(ID, "A")
+                .put(TYPE, "")
+                .put(PROVIDER_NETWORKS, "");
+        exception.expect(IllegalArgumentException.class);
+        codec.decode((ObjectNode) jsonEmptyType, context);
+    }
+
+    @Test
+    public void testServiceNetworkDecodeNullType() throws IllegalArgumentException {
+        final JsonNode jsonNullType = context.mapper().createObjectNode()
+                .put(ID, "A")
+                .put(PROVIDER_NETWORKS, "")
+                .set(TYPE, NullNode.getInstance());
+        exception.expect(IllegalArgumentException.class);
+        codec.decode((ObjectNode) jsonNullType, context);
+    }
+
+    @Test
+    public void testServiceNetworkDecodeWrongType() throws IllegalArgumentException {
+        final JsonNode jsonNoneType = context.mapper().createObjectNode()
+                .put(ID, "A")
+                .put(TYPE, "none")
+                .put(PROVIDER_NETWORKS, "");
+        exception.expect(IllegalArgumentException.class);
+        codec.decode((ObjectNode) jsonNoneType, context);
+    }
+
+    @Test
+    public void testServiceNetworkDecodeWithMissingProviderId() throws NullPointerException {
+        final JsonNode jsonMissingProviderId = context.mapper().createObjectNode()
+                .put(TYPE, "B");
+        final JsonNode jsonWithProvider = context.mapper().createObjectNode()
+                .put(ID, "A")
+                .put(TYPE, PRIVATE.name())
+                .set(PROVIDER_NETWORKS, jsonMissingProviderId);
+        exception.expect(NullPointerException.class);
+        codec.decode((ObjectNode) jsonWithProvider, context);
+    }
+
+    @Test
+    public void testServiceNetworkDecodeWithWrongProviderType() throws NullPointerException {
+        final JsonNode jsonWrongProviderType = context.mapper().createObjectNode()
+                .put(ID, "B")
+                .put(TYPE, "none");
+        final JsonNode jsonWithProvider = context.mapper().createObjectNode()
+                .put(ID, "A")
+                .put(TYPE, PRIVATE.name())
+                .set(PROVIDER_NETWORKS, jsonWrongProviderType);
+        exception.expect(NullPointerException.class);
+        codec.decode((ObjectNode) jsonWithProvider, context);
+    }
+
+    private ServiceNetwork getServiceNetwork(String resource) throws IOException {
+        InputStream jsonStream = ServiceNetworkCodecTest.class.getResourceAsStream(resource);
+        JsonNode jsonNode = context.mapper().readTree(jsonStream).get(SERVICE_NETWORK);
+        assertThat(jsonNode, notNullValue());
+
+        ServiceNetwork snet = codec.decode((ObjectNode) jsonNode, context);
+        assertThat(snet, notNullValue());
+        return snet;
+    }
+}
diff --git a/src/test/java/org/opencord/cordvtn/codec/ServiceNetworkJsonMatcher.java b/src/test/java/org/opencord/cordvtn/codec/ServiceNetworkJsonMatcher.java
new file mode 100644
index 0000000..0544a42
--- /dev/null
+++ b/src/test/java/org/opencord/cordvtn/codec/ServiceNetworkJsonMatcher.java
@@ -0,0 +1,106 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * 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.opencord.cordvtn.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.NullNode;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.opencord.cordvtn.api.net.ProviderNetwork;
+import org.opencord.cordvtn.api.net.ServiceNetwork;
+
+import java.util.Objects;
+
+import static org.opencord.cordvtn.api.dependency.Dependency.Type.BIDIRECTIONAL;
+
+/**
+ * Json matcher for ServiceNetwork.
+ */
+public final class ServiceNetworkJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
+
+    private final ServiceNetwork network;
+
+    private ServiceNetworkJsonMatcher(ServiceNetwork network) {
+        this.network = network;
+    }
+
+    /**
+     * Factory to allocate ServiceNetwork matcher.
+     *
+     * @param network service network object to match
+     * @return matcher
+     */
+    public static ServiceNetworkJsonMatcher matchesServiceNetwork(ServiceNetwork network) {
+        return new ServiceNetworkJsonMatcher(network);
+    }
+
+    @Override
+    protected boolean matchesSafely(JsonNode jsonNet, Description description) {
+        String jsonNetId = jsonNet.get("id").asText();
+        if (!Objects.equals(jsonNetId, network.id().id())) {
+            description.appendText("network id was " + jsonNetId);
+            return false;
+        }
+
+        String jsonType = jsonNet.get("type").asText().toUpperCase();
+        if (!Objects.equals(jsonType, network.type().name())) {
+            description.appendText("type was " + jsonType);
+            return false;
+        }
+
+        if (network.providers().isEmpty()) {
+            return true;
+        }
+
+        JsonNode jsonProviders = jsonNet.get("providerNetworks");
+        if (jsonProviders == null || jsonProviders == NullNode.getInstance()) {
+            description.appendText("provider networks were empty");
+            return false;
+        }
+
+        if (jsonProviders.size() != network.providers().size()) {
+            return false;
+        }
+
+        for (JsonNode provider : jsonProviders) {
+            String id = provider.get("id").asText();
+            boolean bidirectional = provider.get("bidirectional").asBoolean();
+            ProviderNetwork proNet = network.providers().stream()
+                    .filter(p -> p.id().id().equals(id))
+                    .findAny().orElse(null);
+
+            if (proNet == null) {
+                final String msg = String.format("provider id:%s couldn't find", id);
+                description.appendText(msg);
+                return false;
+            }
+
+            if (proNet.type().equals(BIDIRECTIONAL) != bidirectional) {
+                final String msg = String.format(
+                        "mismatch provider id:%s, bidirectional: %s",
+                        id, bidirectional);
+                description.appendText(msg);
+                return false;
+            }
+        }
+        return true;
+    }
+
+    @Override
+    public void describeTo(Description description) {
+        description.appendText(network.toString());
+    }
+}
diff --git a/src/test/resources/org/opencord/cordvtn/codec/service-network-with-provider.json b/src/test/resources/org/opencord/cordvtn/codec/service-network-with-provider.json
new file mode 100644
index 0000000..a5dbb88
--- /dev/null
+++ b/src/test/resources/org/opencord/cordvtn/codec/service-network-with-provider.json
@@ -0,0 +1,12 @@
+{
+  "serviceNetwork": {
+    "id": "B",
+    "type": "private",
+    "providerNetworks": [
+      {
+        "id": "A",
+        "bidirectional": true
+      }
+    ]
+  }
+}
\ No newline at end of file
diff --git a/src/test/resources/org/opencord/cordvtn/codec/service-network.json b/src/test/resources/org/opencord/cordvtn/codec/service-network.json
new file mode 100644
index 0000000..857cca6
--- /dev/null
+++ b/src/test/resources/org/opencord/cordvtn/codec/service-network.json
@@ -0,0 +1,7 @@
+{
+  "serviceNetwork": {
+    "id": "A",
+    "type": "management_local",
+    "providerNetworks": []
+  }
+}
\ No newline at end of file