Initial implementation of k8s networking REST API with unit tests

Change-Id: Ifb11204edb3c1e75b26810c0b104423941b0801d
diff --git a/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/codec/K8sPortJsonMatcher.java b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/codec/K8sPortJsonMatcher.java
index b0f327e..974dfab 100644
--- a/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/codec/K8sPortJsonMatcher.java
+++ b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/codec/K8sPortJsonMatcher.java
@@ -77,25 +77,31 @@
         // check device ID
         String jsonDeviceId = jsonNode.get(DEVICE_ID).asText();
         String deviceId = port.deviceId().toString();
-        if (!jsonDeviceId.equals(deviceId)) {
-            description.appendText("device ID was " + jsonDeviceId);
-            return false;
+        if (jsonDeviceId != null) {
+            if (!jsonDeviceId.equals(deviceId)) {
+                description.appendText("device ID was " + jsonDeviceId);
+                return false;
+            }
         }
 
         // check port number
         String jsonPortNumber = jsonNode.get(PORT_NUMBER).asText();
         String portNumber = port.portNumber().toString();
-        if (!jsonPortNumber.equals(portNumber)) {
-            description.appendText("port number was " + jsonPortNumber);
-            return false;
+        if (jsonPortNumber != null) {
+            if (!jsonPortNumber.equals(portNumber)) {
+                description.appendText("port number was " + jsonPortNumber);
+                return false;
+            }
         }
 
         // check state
         String jsonState = jsonNode.get(STATE).asText();
         String state = port.state().name();
-        if (!jsonState.equals(state)) {
-            description.appendText("state was " + jsonState);
-            return false;
+        if (jsonState != null) {
+            if (!jsonState.equals(state)) {
+                description.appendText("state was " + jsonState);
+                return false;
+            }
         }
 
         return true;
diff --git a/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/web/K8sNetworkWebResourceTest.java b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/web/K8sNetworkWebResourceTest.java
new file mode 100644
index 0000000..40299b2
--- /dev/null
+++ b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/web/K8sNetworkWebResourceTest.java
@@ -0,0 +1,139 @@
+/*
+ * Copyright 2019-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.k8snetworking.web;
+
+import org.glassfish.jersey.server.ResourceConfig;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.osgi.ServiceDirectory;
+import org.onlab.osgi.TestServiceDirectory;
+import org.onosproject.codec.CodecService;
+import org.onosproject.codec.impl.CodecManager;
+import org.onosproject.k8snetworking.api.K8sNetwork;
+import org.onosproject.k8snetworking.api.K8sNetworkAdminService;
+import org.onosproject.k8snetworking.codec.K8sNetworkCodec;
+import org.onosproject.rest.resources.ResourceTest;
+
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.InputStream;
+
+import static org.easymock.EasyMock.anyObject;
+import static org.easymock.EasyMock.anyString;
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Unit tests for kubernetes network REST API.
+ */
+public class K8sNetworkWebResourceTest extends ResourceTest {
+
+    final K8sNetworkAdminService mockAdminService = createMock(K8sNetworkAdminService.class);
+    private static final String PATH = "network";
+
+    private K8sNetwork k8sNetwork;
+
+    /**
+     * Constructs a kubernetes networking resource test instance.
+     */
+    public K8sNetworkWebResourceTest() {
+        super(ResourceConfig.forApplicationClass(K8sNetworkingWebApplication.class));
+    }
+
+    /**
+     * Sets up the global values for all the tests.
+     */
+    @Before
+    public void setUpTest() {
+        final CodecManager codecService = new CodecManager();
+        codecService.activate();
+        codecService.registerCodec(K8sNetwork.class, new K8sNetworkCodec());
+        ServiceDirectory testDirectory =
+                new TestServiceDirectory()
+                        .add(K8sNetworkAdminService.class, mockAdminService)
+                        .add(CodecService.class, codecService);
+        setServiceDirectory(testDirectory);
+    }
+
+    /**
+     * Tests the results of the REST API POST method with creating new network operation.
+     */
+    @Test
+    public void testCreateNetworkWithCreateOperation() {
+        mockAdminService.createNetwork(anyObject());
+        replay(mockAdminService);
+
+        final WebTarget wt = target();
+        InputStream jsonStream = K8sNetworkWebResourceTest.class
+                .getResourceAsStream("k8s-network.json");
+        Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE)
+                .post(Entity.json(jsonStream));
+        final int status = response.getStatus();
+
+        assertThat(status, is(201));
+
+        verify(mockAdminService);
+    }
+
+    /**
+     * Tests the results of the REST API PUT method with modifying the network.
+     */
+    @Test
+    public void testUpdateNetworkWithModifyOperation() {
+        mockAdminService.updateNetwork(anyObject());
+        replay(mockAdminService);
+
+        String location = PATH + "/network-1";
+
+        final WebTarget wt = target();
+        InputStream jsonStream = K8sNetworkWebResourceTest.class
+                .getResourceAsStream("k8s-network.json");
+        Response response = wt.path(location)
+                .request(MediaType.APPLICATION_JSON_TYPE)
+                .put(Entity.json(jsonStream));
+        final int status = response.getStatus();
+
+        assertThat(status, is(200));
+
+        verify(mockAdminService);
+    }
+
+    /**
+     * Tests the results of the REST API DELETE method with deleting the network.
+     */
+    @Test
+    public void testDeleteNetworkWithDeletionOperation() {
+        mockAdminService.removeNetwork(anyString());
+        replay(mockAdminService);
+
+        String location = PATH + "/network-1";
+
+        final WebTarget wt = target();
+        Response response = wt.path(location).request(
+                MediaType.APPLICATION_JSON_TYPE).delete();
+
+        final int status = response.getStatus();
+
+        assertThat(status, is(204));
+
+        verify(mockAdminService);
+    }
+}
diff --git a/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/web/K8sNetworkingCodecRegisterTest.java b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/web/K8sNetworkingCodecRegisterTest.java
new file mode 100644
index 0000000..c536a3c
--- /dev/null
+++ b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/web/K8sNetworkingCodecRegisterTest.java
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2019-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.k8snetworking.web;
+
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Maps;
+import org.junit.Test;
+import org.onlab.junit.TestUtils;
+import org.onosproject.codec.CodecService;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.k8snetworking.api.K8sNetwork;
+import org.onosproject.k8snetworking.api.K8sPort;
+import org.onosproject.k8snetworking.codec.K8sNetworkCodec;
+import org.onosproject.k8snetworking.codec.K8sPortCodec;
+import org.onosproject.k8snode.api.K8sNode;
+
+import java.util.Map;
+import java.util.Set;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+/**
+ * Unit tests for kubernetes networking codec register.
+ */
+public class K8sNetworkingCodecRegisterTest {
+
+    /**
+     * Tests codec register activation and deactivation.
+     */
+    @Test
+    public void testActivateDeactivate() {
+        K8sNetworkingCodecRegister register = new K8sNetworkingCodecRegister();
+        CodecService codecService = new TestCodecService();
+
+        TestUtils.setField(register, "codecService", codecService);
+        register.activate();
+
+        assertEquals(K8sNetworkCodec.class.getName(),
+                codecService.getCodec(K8sNetwork.class).getClass().getName());
+        assertEquals(K8sPortCodec.class.getName(),
+                codecService.getCodec(K8sPort.class).getClass().getName());
+
+        register.deactivate();
+
+        assertNull(codecService.getCodec(K8sNode.class));
+    }
+
+    private static class TestCodecService implements CodecService {
+
+        private Map<String, JsonCodec> codecMap = Maps.newConcurrentMap();
+
+        @Override
+        public Set<Class<?>> getCodecs() {
+            return ImmutableSet.of();
+        }
+
+        @Override
+        public <T> JsonCodec<T> getCodec(Class<T> entityClass) {
+            return codecMap.get(entityClass.getName());
+        }
+
+        @Override
+        public <T> void registerCodec(Class<T> entityClass, JsonCodec<T> codec) {
+            codecMap.put(entityClass.getName(), codec);
+        }
+
+        @Override
+        public void unregisterCodec(Class<?> entityClass) {
+            codecMap.remove(entityClass.getName());
+        }
+    }
+}
diff --git a/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/web/K8sPortWebResourceTest.java b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/web/K8sPortWebResourceTest.java
new file mode 100644
index 0000000..2cb2047
--- /dev/null
+++ b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/web/K8sPortWebResourceTest.java
@@ -0,0 +1,139 @@
+/*
+ * Copyright 2019-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.k8snetworking.web;
+
+import org.glassfish.jersey.server.ResourceConfig;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.osgi.ServiceDirectory;
+import org.onlab.osgi.TestServiceDirectory;
+import org.onosproject.codec.CodecService;
+import org.onosproject.codec.impl.CodecManager;
+import org.onosproject.k8snetworking.api.K8sNetworkAdminService;
+import org.onosproject.k8snetworking.api.K8sPort;
+import org.onosproject.k8snetworking.codec.K8sPortCodec;
+import org.onosproject.rest.resources.ResourceTest;
+
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.InputStream;
+
+import static org.easymock.EasyMock.anyObject;
+import static org.easymock.EasyMock.anyString;
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Unit test for kubernetes port REST API.
+ */
+public class K8sPortWebResourceTest extends ResourceTest {
+
+    final K8sNetworkAdminService mockAdminService = createMock(K8sNetworkAdminService.class);
+    private static final String PATH = "port";
+
+    private K8sPort k8sPort;
+
+    /**
+     * Constructs a kubernetes networking resource test instance.
+     */
+    public K8sPortWebResourceTest() {
+        super(ResourceConfig.forApplicationClass(K8sNetworkingWebApplication.class));
+    }
+
+    /**
+     * Sets up the global values for all the tests.
+     */
+    @Before
+    public void setUpTest() {
+        final CodecManager codecService = new CodecManager();
+        codecService.activate();
+        codecService.registerCodec(K8sPort.class, new K8sPortCodec());
+        ServiceDirectory testDirectory =
+                new TestServiceDirectory()
+                        .add(K8sNetworkAdminService.class, mockAdminService)
+                        .add(CodecService.class, codecService);
+        setServiceDirectory(testDirectory);
+    }
+
+    /**
+     * Tests the results of the REST API POST method with creating new port operation.
+     */
+    @Test
+    public void testCreatePortWithCreateOperation() {
+        mockAdminService.createPort(anyObject());
+        replay(mockAdminService);
+
+        final WebTarget wt = target();
+        InputStream jsonStream = K8sPortWebResourceTest.class
+                .getResourceAsStream("k8s-port.json");
+        Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE)
+                .post(Entity.json(jsonStream));
+        final int status = response.getStatus();
+
+        assertThat(status, is(201));
+
+        verify(mockAdminService);
+    }
+
+    /**
+     * Tests the results of the REST API PUT method with modifying the port.
+     */
+    @Test
+    public void testUpdatePortWithModifyOperation() {
+        mockAdminService.updatePort(anyObject());
+        replay(mockAdminService);
+
+        String location = PATH + "/port-1";
+
+        final WebTarget wt = target();
+        InputStream jsonStream = K8sPortWebResourceTest.class
+                .getResourceAsStream("k8s-port.json");
+        Response response = wt.path(location)
+                .request(MediaType.APPLICATION_JSON_TYPE)
+                .put(Entity.json(jsonStream));
+        final int status = response.getStatus();
+
+        assertThat(status, is(200));
+
+        verify(mockAdminService);
+    }
+
+    /**
+     * Tests the results of the REST API DELETE method with deleting the port.
+     */
+    @Test
+    public void testDeletePortWithDeletionOperation() {
+        mockAdminService.removePort(anyString());
+        replay(mockAdminService);
+
+        String location = PATH + "/port-1";
+
+        final WebTarget wt = target();
+        Response response = wt.path(location).request(
+                MediaType.APPLICATION_JSON_TYPE).delete();
+
+        final int status = response.getStatus();
+
+        assertThat(status, is(204));
+
+        verify(mockAdminService);
+    }
+}
diff --git a/apps/k8s-networking/app/src/test/resources/org/onosproject/k8snetworking/web/k8s-network.json b/apps/k8s-networking/app/src/test/resources/org/onosproject/k8snetworking/web/k8s-network.json
new file mode 100644
index 0000000..6efe965
--- /dev/null
+++ b/apps/k8s-networking/app/src/test/resources/org/onosproject/k8snetworking/web/k8s-network.json
@@ -0,0 +1,9 @@
+{
+  "networkId": "network-1",
+  "name": "network-1",
+  "type": "VXLAN",
+  "mtu": 1500,
+  "segmentId": "1",
+  "gatewayIp": "10.10.10.1",
+  "cidr": "32"
+}
\ No newline at end of file
diff --git a/apps/k8s-networking/app/src/test/resources/org/onosproject/k8snetworking/web/k8s-port.json b/apps/k8s-networking/app/src/test/resources/org/onosproject/k8snetworking/web/k8s-port.json
new file mode 100644
index 0000000..d55ea94
--- /dev/null
+++ b/apps/k8s-networking/app/src/test/resources/org/onosproject/k8snetworking/web/k8s-port.json
@@ -0,0 +1,6 @@
+{
+  "networkId": "network-1",
+  "portId": "port-1",
+  "macAddress": "00:11:22:33:44:55",
+  "ipAddress": "10.10.10.10"
+}
\ No newline at end of file