Ray Milkey

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

Change-Id: I05579d38e9374c40ef67251a30869361e31fc6d8
......@@ -39,7 +39,6 @@ import com.eclipsesource.json.JsonObject;
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 @@ import static org.onosproject.net.PortNumber.portNumber;
/**
* 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.
*/
......
......@@ -52,7 +52,6 @@ import com.eclipsesource.json.JsonObject;
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 @@ import static org.junit.Assert.fail;
/**
* 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 class FlowsResourceTest extends JerseyTest {
}
}
public FlowsResourceTest() {
super("org.onosproject.rest");
}
/**
* Populates some flows used as testing data.
*/
......
......@@ -44,7 +44,6 @@ import com.eclipsesource.json.JsonObject;
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 @@ import static org.onosproject.net.PortNumber.portNumber;
* 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();
......
......@@ -44,7 +44,6 @@ import com.eclipsesource.json.JsonValue;
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 @@ import static org.junit.Assert.fail;
/**
* 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 class IntentsResourceTest extends JerseyTest {
}
}
public IntentsResourceTest() {
super("org.onosproject.rest");
}
/**
* Hamcrest matcher to check that an intent representation in JSON matches
* the actual intent.
......
......@@ -35,7 +35,6 @@ import com.eclipsesource.json.JsonObject;
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 @@ import static org.onosproject.net.NetTestTools.link;
/**
* 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 @@ public class LinksResourceTest extends JerseyTest {
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.
*/
......
/*
* 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();
}
}
......@@ -43,7 +43,6 @@ import com.eclipsesource.json.JsonArray;
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 @@ import static org.onosproject.net.NetTestTools.link;
/**
* 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 class TopologyResourceTest extends JerseyTest {
}
}
public TopologyResourceTest() {
super("org.onosproject.rest");
}
/**
* Initializes the test harness.
*/
......