[ONOS-7492] Add unit test for OpenstackFloatingIp REST API

Change-Id: I5d750405b63ffef8049f3b23f2129dac5532c6d0
diff --git a/apps/openstacknetworking/BUCK b/apps/openstacknetworking/BUCK
index f6a3a5d..c096724 100644
--- a/apps/openstacknetworking/BUCK
+++ b/apps/openstacknetworking/BUCK
@@ -21,6 +21,8 @@
     '//lib:TEST_ADAPTERS',
     '//core/api:onos-api-tests',
     '//core/common:onos-core-common-tests',
+    '//web/api:onos-rest-tests',
+    '//lib:TEST_REST',
 ]
 
 BUNDLES = [
diff --git a/apps/openstacknetworking/pom.xml b/apps/openstacknetworking/pom.xml
index 516ba82..648d038 100644
--- a/apps/openstacknetworking/pom.xml
+++ b/apps/openstacknetworking/pom.xml
@@ -119,10 +119,6 @@
             <artifactId>javax.ws.rs-api</artifactId>
         </dependency>
         <dependency>
-            <groupId>org.glassfish.jersey.containers</groupId>
-            <artifactId>jersey-container-servlet</artifactId>
-        </dependency>
-        <dependency>
             <groupId>com.fasterxml.jackson.core</groupId>
             <artifactId>jackson-databind</artifactId>
         </dependency>
@@ -140,10 +136,6 @@
         </dependency>
         <dependency>
             <groupId>org.glassfish.jersey.core</groupId>
-            <artifactId>jersey-client</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>org.glassfish.jersey.core</groupId>
             <artifactId>jersey-common</artifactId>
             <version>2.25</version>
         </dependency>
@@ -187,6 +179,42 @@
             <artifactId>snakeyaml</artifactId>
             <version>1.15</version>
         </dependency>
+
+        <dependency>
+            <groupId>org.glassfish.jersey.core</groupId>
+            <artifactId>jersey-client</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.glassfish.jersey.containers</groupId>
+            <artifactId>jersey-container-servlet</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.glassfish.jersey.test-framework</groupId>
+            <artifactId>jersey-test-framework-core</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.glassfish.jersey.test-framework.providers</groupId>
+            <artifactId>jersey-test-framework-provider-jetty</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onlab-osgi</artifactId>
+            <classifier>tests</classifier>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-rest</artifactId>
+            <version>${project.version}</version>
+            <classifier>tests</classifier>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>
diff --git a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackFloatingIpWebResource.java b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackFloatingIpWebResource.java
index 3a1961a..d7ba4a9 100644
--- a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackFloatingIpWebResource.java
+++ b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackFloatingIpWebResource.java
@@ -16,7 +16,6 @@
 
 package org.onosproject.openstacknetworking.web;
 
-import org.onlab.osgi.DefaultServiceDirectory;
 import org.onosproject.openstacknetworking.api.OpenstackRouterAdminService;
 import org.onosproject.rest.AbstractWebResource;
 import org.openstack4j.openstack.networking.domain.NeutronFloatingIP;
@@ -53,7 +52,7 @@
     private static final String FLOATING_IPS = "floatingips";
 
     private final OpenstackRouterAdminService adminService =
-            DefaultServiceDirectory.getService(OpenstackRouterAdminService.class);
+                                        get(OpenstackRouterAdminService.class);
 
     @Context
     private UriInfo uriInfo;
diff --git a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackNetworkingWebApplication.java b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackNetworkingWebApplication.java
new file mode 100644
index 0000000..8f9e623
--- /dev/null
+++ b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackNetworkingWebApplication.java
@@ -0,0 +1,38 @@
+/*
+ * 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.openstacknetworking.web;
+
+import org.onlab.rest.AbstractWebApplication;
+
+import java.util.Set;
+
+/**
+ * Openstack networking REST APIs web application.
+ */
+public class OpenstackNetworkingWebApplication extends AbstractWebApplication {
+    @Override
+    public Set<Class<?>> getClasses() {
+        return getClasses(
+                OpenstackFloatingIpWebResource.class,
+                OpenstackNetworkWebResource.class,
+                OpenstackPortWebResource.class,
+                OpenstackRouterWebResource.class,
+                OpenstackSecurityGroupRuleWebResource.class,
+                OpenstackSecurityGroupWebResource.class,
+                OpenstackSubnetWebResource.class
+        );
+    }
+}
diff --git a/apps/openstacknetworking/src/main/webapp/WEB-INF/web.xml b/apps/openstacknetworking/src/main/webapp/WEB-INF/web.xml
index 89f5be2..9194183 100644
--- a/apps/openstacknetworking/src/main/webapp/WEB-INF/web.xml
+++ b/apps/openstacknetworking/src/main/webapp/WEB-INF/web.xml
@@ -20,20 +20,31 @@
          id="ONOS" version="2.5">
     <display-name>Openstack Switching REST API v1.0</display-name>
 
+    <security-constraint>
+        <web-resource-collection>
+            <web-resource-name>Secured</web-resource-name>
+            <url-pattern>/*</url-pattern>
+        </web-resource-collection>
+        <auth-constraint>
+            <role-name>admin</role-name>
+        </auth-constraint>
+    </security-constraint>
+
+    <security-role>
+        <role-name>admin</role-name>
+    </security-role>
+
+    <login-config>
+        <auth-method>BASIC</auth-method>
+        <realm-name>karaf</realm-name>
+    </login-config>
+
     <servlet>
         <servlet-name>JAX-RS Service</servlet-name>
         <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
         <init-param>
-            <param-name>jersey.config.server.provider.classnames</param-name>
-            <param-value>
-                org.onosproject.openstacknetworking.web.OpenstackPortWebResource,
-                org.onosproject.openstacknetworking.web.OpenstackNetworkWebResource,
-                org.onosproject.openstacknetworking.web.OpenstackSubnetWebResource,
-                org.onosproject.openstacknetworking.web.OpenstackRouterWebResource,
-                org.onosproject.openstacknetworking.web.OpenstackFloatingIpWebResource,
-                org.onosproject.openstacknetworking.web.OpenstackSecurityGroupWebResource,
-                org.onosproject.openstacknetworking.web.OpenstackSecurityGroupRuleWebResource
-            </param-value>
+            <param-name>javax.ws.rs.Application</param-name>
+            <param-value>org.onosproject.openstacknetworking.web.OpenstackNetworkingWebApplication</param-value>
         </init-param>
         <load-on-startup>1</load-on-startup>
     </servlet>
diff --git a/apps/openstacknetworking/src/test/java/org/onosproject/openstacknetworking/web/OpenstackFloatingIpWebResourceTest.java b/apps/openstacknetworking/src/test/java/org/onosproject/openstacknetworking/web/OpenstackFloatingIpWebResourceTest.java
new file mode 100644
index 0000000..edc562d
--- /dev/null
+++ b/apps/openstacknetworking/src/test/java/org/onosproject/openstacknetworking/web/OpenstackFloatingIpWebResourceTest.java
@@ -0,0 +1,231 @@
+/*
+ * 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.openstacknetworking.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.openstacknetworking.api.OpenstackRouterAdminService;
+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.expectLastCall;
+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 openstack floating IP REST API.
+ */
+public class OpenstackFloatingIpWebResourceTest extends ResourceTest {
+
+    final OpenstackRouterAdminService mockOpenstackRouterAdminService =
+            createMock(OpenstackRouterAdminService.class);
+    private static final String PATH = "floatingips";
+
+    /**
+     * Constructs an openstack floating IP test instance.
+     */
+    public OpenstackFloatingIpWebResourceTest() {
+        super(ResourceConfig.forApplicationClass(OpenstackNetworkingWebApplication.class));
+    }
+
+    /**
+     * Sets up the global values for all tests.
+     */
+    @Before
+    public void setUpTest() {
+        ServiceDirectory testDirectory =
+                new TestServiceDirectory()
+                        .add(OpenstackRouterAdminService.class,
+                                mockOpenstackRouterAdminService);
+        setServiceDirectory(testDirectory);
+
+    }
+
+    /**
+     * Tests the results of the REST API POST with creation operation.
+     */
+    @Test
+    public void testCreateFloatingIpWithCreationOperation() {
+        mockOpenstackRouterAdminService.createFloatingIp(anyObject());
+        replay(mockOpenstackRouterAdminService);
+
+        final WebTarget wt = target();
+        InputStream jsonStream = OpenstackFloatingIpWebResourceTest.class
+                .getResourceAsStream("openstack-floatingip.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(mockOpenstackRouterAdminService);
+    }
+
+    /**
+     * Tests the results of the REST API POST with incorrect input.
+     */
+    @Test
+    public void testCreateFloatingIpWithIncorrectInput() {
+        final WebTarget wt = target();
+        InputStream jsonStream = OpenstackFloatingIpWebResourceTest.class
+                .getResourceAsStream("dummy.json");
+
+        Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE)
+                .post(Entity.json(jsonStream));
+        final int status = response.getStatus();
+
+        assertThat(status, is(400));
+    }
+
+    /**
+     * Tests the results of the REST API POST with duplicated floating IP.
+     */
+    @Test
+    public void testCreateFloatingIpWithDuplicatedIp() {
+        mockOpenstackRouterAdminService.createFloatingIp(anyObject());
+        expectLastCall().andThrow(new IllegalArgumentException());
+        replay(mockOpenstackRouterAdminService);
+
+        final WebTarget wt = target();
+        InputStream jsonStream = OpenstackFloatingIpWebResourceTest.class
+                .getResourceAsStream("openstack-floatingip.json");
+
+        Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE)
+                .post(Entity.json(jsonStream));
+        final int status = response.getStatus();
+
+        assertThat(status, is(400));
+
+        verify(mockOpenstackRouterAdminService);
+    }
+
+    /**
+     * Tests the results of the REST API PUT with updating operation.
+     */
+    @Test
+    public void testUpdateFloatingIpWithUpdatingOperation() {
+        mockOpenstackRouterAdminService.updateFloatingIp(anyObject());
+        replay(mockOpenstackRouterAdminService);
+
+        final WebTarget wt = target();
+        InputStream jsonStream = OpenstackFloatingIpWebResourceTest.class
+                .getResourceAsStream("openstack-floatingip.json");
+
+        Response response = wt.path(PATH + "/2f245a7b-796b-4f26-9cf9-9e82d248fda7")
+                .request(MediaType.APPLICATION_JSON_TYPE)
+                .put(Entity.json(jsonStream));
+        final int status = response.getStatus();
+
+        assertThat(status, is(200));
+
+        verify(mockOpenstackRouterAdminService);
+    }
+
+    /**
+     * Tests the results of the REST API PUT with incorrect input.
+     */
+    @Test
+    public void testUpdateFloatingIpWithIncorrectInput() {
+        final WebTarget wt = target();
+        InputStream jsonStream = OpenstackFloatingIpWebResourceTest.class
+                .getResourceAsStream("dummy.json");
+
+        Response response = wt.path(PATH + "/2f245a7b-796b-4f26-9cf9-9e82d248fda7")
+                .request(MediaType.APPLICATION_JSON_TYPE)
+                .put(Entity.json(jsonStream));
+        final int status = response.getStatus();
+
+        assertThat(status, is(400));
+    }
+
+    /**
+     * Tests the results of the REST API PUT with non-existing ID.
+     */
+    @Test
+    public void testUpdateFloatingIpWithNonexistId() {
+        mockOpenstackRouterAdminService.updateFloatingIp(anyObject());
+        expectLastCall().andThrow(new IllegalArgumentException());
+        replay(mockOpenstackRouterAdminService);
+
+        final WebTarget wt = target();
+        InputStream jsonStream = OpenstackFloatingIpWebResourceTest.class
+                .getResourceAsStream("openstack-floatingip.json");
+
+        Response response = wt.path(PATH + "/2f245a7b-796b-4f26-9cf9-9e82d248fda7")
+                .request(MediaType.APPLICATION_JSON_TYPE)
+                .put(Entity.json(jsonStream));
+        final int status = response.getStatus();
+
+        assertThat(status, is(400));
+
+        verify(mockOpenstackRouterAdminService);
+    }
+
+    /**
+     * Tests the results of the REST API DELETE with deletion operation.
+     */
+    @Test
+    public void testDeleteFloatingIpDeletionOperation() {
+        mockOpenstackRouterAdminService.removeFloatingIp(anyString());
+        replay(mockOpenstackRouterAdminService);
+
+        final WebTarget wt = target();
+
+        Response response = wt.path(PATH + "/2f245a7b-796b-4f26-9cf9-9e82d248fda7")
+                .request(MediaType.APPLICATION_JSON_TYPE)
+                .delete();
+        final int status = response.getStatus();
+
+        assertThat(status, is(204));
+
+        verify(mockOpenstackRouterAdminService);
+    }
+
+    /**
+     * Tests the results of the REST API DELETE with non-existing ID.
+     */
+    @Test
+    public void testDeleteFloatingIpWithNonexistId() {
+        mockOpenstackRouterAdminService.removeFloatingIp(anyString());
+        expectLastCall().andThrow(new IllegalArgumentException());
+        replay(mockOpenstackRouterAdminService);
+
+        final WebTarget wt = target();
+
+        Response response = wt.path(PATH + "/non-exist-id")
+                .request(MediaType.APPLICATION_JSON_TYPE)
+                .delete();
+        final int status = response.getStatus();
+
+        assertThat(status, is(400));
+
+        verify(mockOpenstackRouterAdminService);
+    }
+}
diff --git a/apps/openstacknetworking/src/test/resources/org/onosproject/openstacknetworking/web/dummy.json b/apps/openstacknetworking/src/test/resources/org/onosproject/openstacknetworking/web/dummy.json
new file mode 100644
index 0000000..ee3e6ae
--- /dev/null
+++ b/apps/openstacknetworking/src/test/resources/org/onosproject/openstacknetworking/web/dummy.json
@@ -0,0 +1,3 @@
+{
+  "dummy": "object"
+}
\ No newline at end of file
diff --git a/apps/openstacknetworking/src/test/resources/org/onosproject/openstacknetworking/web/openstack-floatingip.json b/apps/openstacknetworking/src/test/resources/org/onosproject/openstacknetworking/web/openstack-floatingip.json
new file mode 100644
index 0000000..44f78ba
--- /dev/null
+++ b/apps/openstacknetworking/src/test/resources/org/onosproject/openstacknetworking/web/openstack-floatingip.json
@@ -0,0 +1,11 @@
+{
+  "floatingip": {
+    "id": "2f245a7b-796b-4f26-9cf9-9e82d248fda7",
+    "router_id": "d23abc8d-2991-4a55-ba98-2aaea84cc72",
+    "tenant_id": "4969c491a3c74ee4af974e6d800c62de",
+    "floating_network_id": "376da547-b977-4cfe-9cba-275c80debf57",
+    "floating_ip_address": "172.24.4.228",
+    "fixed_ip_address": "10.0.0.3",
+    "port_id": "ce705c24-c1ef-408a-bda3-7bbd946164ab"
+  }
+}