Make REST API unit tests choose a free port to allow concurrent builds

Change-Id: I05579d38e9374c40ef67251a30869361e31fc6d8
diff --git a/web/api/src/test/java/org/onosproject/rest/DevicesResourceTest.java b/web/api/src/test/java/org/onosproject/rest/DevicesResourceTest.java
index 67b04c8..722665e 100644
--- a/web/api/src/test/java/org/onosproject/rest/DevicesResourceTest.java
+++ b/web/api/src/test/java/org/onosproject/rest/DevicesResourceTest.java
@@ -39,7 +39,6 @@
 import com.google.common.collect.ImmutableList;
 import com.sun.jersey.api.client.UniformInterfaceException;
 import com.sun.jersey.api.client.WebResource;
-import com.sun.jersey.test.framework.JerseyTest;
 
 import static org.easymock.EasyMock.createMock;
 import static org.easymock.EasyMock.expect;
@@ -60,17 +59,10 @@
 /**
  * Unit tests for devices REST APIs.
  */
-public class DevicesResourceTest extends JerseyTest {
+public class DevicesResourceTest extends ResourceTest {
     DeviceService mockDeviceService;
 
     /**
-     * Constructs the test.
-     */
-    public DevicesResourceTest() {
-        super("org.onosproject.rest");
-    }
-
-    /**
      * Hamcrest matcher to check that an device representation in JSON matches
      * the actual device.
      */
diff --git a/web/api/src/test/java/org/onosproject/rest/FlowsResourceTest.java b/web/api/src/test/java/org/onosproject/rest/FlowsResourceTest.java
index 8d6232e..5f52081 100644
--- a/web/api/src/test/java/org/onosproject/rest/FlowsResourceTest.java
+++ b/web/api/src/test/java/org/onosproject/rest/FlowsResourceTest.java
@@ -52,7 +52,6 @@
 import com.google.common.collect.ImmutableSet;
 import com.sun.jersey.api.client.UniformInterfaceException;
 import com.sun.jersey.api.client.WebResource;
-import com.sun.jersey.test.framework.JerseyTest;
 
 import static org.easymock.EasyMock.anyObject;
 import static org.easymock.EasyMock.createMock;
@@ -70,7 +69,7 @@
 /**
  * Unit tests for Flows REST APIs.
  */
-public class FlowsResourceTest extends JerseyTest {
+public class FlowsResourceTest extends ResourceTest {
     final FlowRuleService mockFlowService = createMock(FlowRuleService.class);
     final HashMap<DeviceId, Set<FlowEntry>> rules = new HashMap<>();
 
@@ -189,10 +188,6 @@
         }
     }
 
-    public FlowsResourceTest() {
-        super("org.onosproject.rest");
-    }
-
     /**
      * Populates some flows used as testing data.
      */
diff --git a/web/api/src/test/java/org/onosproject/rest/HostResourceTest.java b/web/api/src/test/java/org/onosproject/rest/HostResourceTest.java
index 3a3a16a..2d9cdeb 100644
--- a/web/api/src/test/java/org/onosproject/rest/HostResourceTest.java
+++ b/web/api/src/test/java/org/onosproject/rest/HostResourceTest.java
@@ -44,7 +44,6 @@
 import com.google.common.collect.ImmutableSet;
 import com.sun.jersey.api.client.UniformInterfaceException;
 import com.sun.jersey.api.client.WebResource;
-import com.sun.jersey.test.framework.JerseyTest;
 
 import static org.easymock.EasyMock.createMock;
 import static org.easymock.EasyMock.expect;
@@ -64,14 +63,10 @@
  * Simple example on how to write a JAX-RS unit test using Jersey test framework.
  * A base class should/will be created to provide further assistance for testing.
  */
-public class HostResourceTest extends JerseyTest {
+public class HostResourceTest extends ResourceTest {
     final HostService mockHostService = createMock(HostService.class);
     final HashSet<Host> hosts = new HashSet<>();
 
-    public HostResourceTest() {
-        super("org.onosproject.rest");
-    }
-
     @Before
     public void setUp() {
         expect(mockHostService.getHosts()).andReturn(hosts).anyTimes();
diff --git a/web/api/src/test/java/org/onosproject/rest/IntentsResourceTest.java b/web/api/src/test/java/org/onosproject/rest/IntentsResourceTest.java
index 4a6c2e5..2d650a5 100644
--- a/web/api/src/test/java/org/onosproject/rest/IntentsResourceTest.java
+++ b/web/api/src/test/java/org/onosproject/rest/IntentsResourceTest.java
@@ -44,7 +44,6 @@
 import com.google.common.base.MoreObjects;
 import com.sun.jersey.api.client.UniformInterfaceException;
 import com.sun.jersey.api.client.WebResource;
-import com.sun.jersey.test.framework.JerseyTest;
 
 import static org.easymock.EasyMock.createMock;
 import static org.easymock.EasyMock.expect;
@@ -60,7 +59,7 @@
 /**
  * Unit tests for Intents REST APIs.
  */
-public class IntentsResourceTest extends JerseyTest {
+public class IntentsResourceTest extends ResourceTest {
     final IntentService mockIntentService = createMock(IntentService.class);
     final HashSet<Intent> intents = new HashSet<>();
     private static final ApplicationId APP_ID =
@@ -110,10 +109,6 @@
         }
     }
 
-    public IntentsResourceTest() {
-        super("org.onosproject.rest");
-    }
-
     /**
      * Hamcrest matcher to check that an intent representation in JSON matches
      * the actual intent.
diff --git a/web/api/src/test/java/org/onosproject/rest/LinksResourceTest.java b/web/api/src/test/java/org/onosproject/rest/LinksResourceTest.java
index f408e18..274a8f2 100644
--- a/web/api/src/test/java/org/onosproject/rest/LinksResourceTest.java
+++ b/web/api/src/test/java/org/onosproject/rest/LinksResourceTest.java
@@ -35,7 +35,6 @@
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 import com.sun.jersey.api.client.WebResource;
-import com.sun.jersey.test.framework.JerseyTest;
 
 import static org.easymock.EasyMock.createMock;
 import static org.easymock.EasyMock.expect;
@@ -52,7 +51,7 @@
 /**
  * Unit tests for links REST APIs.
  */
-public class LinksResourceTest extends JerseyTest {
+public class LinksResourceTest extends ResourceTest {
     LinkService mockLinkService;
 
     Link link1 = link("src1", 1, "dst1", 1);
@@ -60,13 +59,6 @@
     Link link3 = link("src3", 3, "dst3", 3);
 
     /**
-     * Constructs the test.
-     */
-    public LinksResourceTest() {
-        super("org.onosproject.rest");
-    }
-
-    /**
      * Hamcrest matcher to check that an link representation in JSON matches
      * the actual link.
      */
diff --git a/web/api/src/test/java/org/onosproject/rest/ResourceTest.java b/web/api/src/test/java/org/onosproject/rest/ResourceTest.java
new file mode 100644
index 0000000..d88b84a6
--- /dev/null
+++ b/web/api/src/test/java/org/onosproject/rest/ResourceTest.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2015 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.onosproject.rest;
+
+import java.io.IOException;
+import java.net.ServerSocket;
+
+import com.sun.jersey.test.framework.AppDescriptor;
+import com.sun.jersey.test.framework.JerseyTest;
+import com.sun.jersey.test.framework.WebAppDescriptor;
+
+/**
+ * Base class for REST API tests.  Performs common configuration operations.
+ */
+public class ResourceTest extends JerseyTest {
+
+    /**
+     * Assigns an available port for the test.
+     *
+     * @param defaultPort If a port cannot be determined, this one is used.
+     * @return free port
+     */
+    @Override
+    public int getPort(int defaultPort) {
+        try {
+            ServerSocket socket = new ServerSocket(0);
+            socket.setReuseAddress(true);
+            int port = socket.getLocalPort();
+            socket.close();
+            return port;
+        } catch (IOException ioe) {
+            return defaultPort;
+        }
+    }
+
+    @Override
+    public AppDescriptor configure() {
+        return new WebAppDescriptor.Builder("org.onosproject.rest").build();
+    }
+
+}
diff --git a/web/api/src/test/java/org/onosproject/rest/TopologyResourceTest.java b/web/api/src/test/java/org/onosproject/rest/TopologyResourceTest.java
index 7f3938c..d25fd59 100644
--- a/web/api/src/test/java/org/onosproject/rest/TopologyResourceTest.java
+++ b/web/api/src/test/java/org/onosproject/rest/TopologyResourceTest.java
@@ -43,7 +43,6 @@
 import com.eclipsesource.json.JsonObject;
 import com.google.common.collect.ImmutableSet;
 import com.sun.jersey.api.client.WebResource;
-import com.sun.jersey.test.framework.JerseyTest;
 
 import static org.hamcrest.Matchers.hasSize;
 import static org.hamcrest.Matchers.is;
@@ -56,7 +55,7 @@
 /**
  * Unit tests for Topology REST APIs.
  */
-public class TopologyResourceTest extends JerseyTest {
+public class TopologyResourceTest extends ResourceTest {
 
     private static class MockTopology implements Topology {
         @Override
@@ -172,11 +171,6 @@
         }
     }
 
-    public TopologyResourceTest() {
-        super("org.onosproject.rest");
-    }
-
-
     /**
      * Initializes the test harness.
      */