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; ...@@ -39,7 +39,6 @@ import com.eclipsesource.json.JsonObject;
39 import com.google.common.collect.ImmutableList; 39 import com.google.common.collect.ImmutableList;
40 import com.sun.jersey.api.client.UniformInterfaceException; 40 import com.sun.jersey.api.client.UniformInterfaceException;
41 import com.sun.jersey.api.client.WebResource; 41 import com.sun.jersey.api.client.WebResource;
42 -import com.sun.jersey.test.framework.JerseyTest;
43 42
44 import static org.easymock.EasyMock.createMock; 43 import static org.easymock.EasyMock.createMock;
45 import static org.easymock.EasyMock.expect; 44 import static org.easymock.EasyMock.expect;
...@@ -60,17 +59,10 @@ import static org.onosproject.net.PortNumber.portNumber; ...@@ -60,17 +59,10 @@ import static org.onosproject.net.PortNumber.portNumber;
60 /** 59 /**
61 * Unit tests for devices REST APIs. 60 * Unit tests for devices REST APIs.
62 */ 61 */
63 -public class DevicesResourceTest extends JerseyTest { 62 +public class DevicesResourceTest extends ResourceTest {
64 DeviceService mockDeviceService; 63 DeviceService mockDeviceService;
65 64
66 /** 65 /**
67 - * Constructs the test.
68 - */
69 - public DevicesResourceTest() {
70 - super("org.onosproject.rest");
71 - }
72 -
73 - /**
74 * Hamcrest matcher to check that an device representation in JSON matches 66 * Hamcrest matcher to check that an device representation in JSON matches
75 * the actual device. 67 * the actual device.
76 */ 68 */
......
...@@ -52,7 +52,6 @@ import com.eclipsesource.json.JsonObject; ...@@ -52,7 +52,6 @@ import com.eclipsesource.json.JsonObject;
52 import com.google.common.collect.ImmutableSet; 52 import com.google.common.collect.ImmutableSet;
53 import com.sun.jersey.api.client.UniformInterfaceException; 53 import com.sun.jersey.api.client.UniformInterfaceException;
54 import com.sun.jersey.api.client.WebResource; 54 import com.sun.jersey.api.client.WebResource;
55 -import com.sun.jersey.test.framework.JerseyTest;
56 55
57 import static org.easymock.EasyMock.anyObject; 56 import static org.easymock.EasyMock.anyObject;
58 import static org.easymock.EasyMock.createMock; 57 import static org.easymock.EasyMock.createMock;
...@@ -70,7 +69,7 @@ import static org.junit.Assert.fail; ...@@ -70,7 +69,7 @@ import static org.junit.Assert.fail;
70 /** 69 /**
71 * Unit tests for Flows REST APIs. 70 * Unit tests for Flows REST APIs.
72 */ 71 */
73 -public class FlowsResourceTest extends JerseyTest { 72 +public class FlowsResourceTest extends ResourceTest {
74 final FlowRuleService mockFlowService = createMock(FlowRuleService.class); 73 final FlowRuleService mockFlowService = createMock(FlowRuleService.class);
75 final HashMap<DeviceId, Set<FlowEntry>> rules = new HashMap<>(); 74 final HashMap<DeviceId, Set<FlowEntry>> rules = new HashMap<>();
76 75
...@@ -189,10 +188,6 @@ public class FlowsResourceTest extends JerseyTest { ...@@ -189,10 +188,6 @@ public class FlowsResourceTest extends JerseyTest {
189 } 188 }
190 } 189 }
191 190
192 - public FlowsResourceTest() {
193 - super("org.onosproject.rest");
194 - }
195 -
196 /** 191 /**
197 * Populates some flows used as testing data. 192 * Populates some flows used as testing data.
198 */ 193 */
......
...@@ -44,7 +44,6 @@ import com.eclipsesource.json.JsonObject; ...@@ -44,7 +44,6 @@ import com.eclipsesource.json.JsonObject;
44 import com.google.common.collect.ImmutableSet; 44 import com.google.common.collect.ImmutableSet;
45 import com.sun.jersey.api.client.UniformInterfaceException; 45 import com.sun.jersey.api.client.UniformInterfaceException;
46 import com.sun.jersey.api.client.WebResource; 46 import com.sun.jersey.api.client.WebResource;
47 -import com.sun.jersey.test.framework.JerseyTest;
48 47
49 import static org.easymock.EasyMock.createMock; 48 import static org.easymock.EasyMock.createMock;
50 import static org.easymock.EasyMock.expect; 49 import static org.easymock.EasyMock.expect;
...@@ -64,14 +63,10 @@ import static org.onosproject.net.PortNumber.portNumber; ...@@ -64,14 +63,10 @@ import static org.onosproject.net.PortNumber.portNumber;
64 * Simple example on how to write a JAX-RS unit test using Jersey test framework. 63 * Simple example on how to write a JAX-RS unit test using Jersey test framework.
65 * A base class should/will be created to provide further assistance for testing. 64 * A base class should/will be created to provide further assistance for testing.
66 */ 65 */
67 -public class HostResourceTest extends JerseyTest { 66 +public class HostResourceTest extends ResourceTest {
68 final HostService mockHostService = createMock(HostService.class); 67 final HostService mockHostService = createMock(HostService.class);
69 final HashSet<Host> hosts = new HashSet<>(); 68 final HashSet<Host> hosts = new HashSet<>();
70 69
71 - public HostResourceTest() {
72 - super("org.onosproject.rest");
73 - }
74 -
75 @Before 70 @Before
76 public void setUp() { 71 public void setUp() {
77 expect(mockHostService.getHosts()).andReturn(hosts).anyTimes(); 72 expect(mockHostService.getHosts()).andReturn(hosts).anyTimes();
......
...@@ -44,7 +44,6 @@ import com.eclipsesource.json.JsonValue; ...@@ -44,7 +44,6 @@ import com.eclipsesource.json.JsonValue;
44 import com.google.common.base.MoreObjects; 44 import com.google.common.base.MoreObjects;
45 import com.sun.jersey.api.client.UniformInterfaceException; 45 import com.sun.jersey.api.client.UniformInterfaceException;
46 import com.sun.jersey.api.client.WebResource; 46 import com.sun.jersey.api.client.WebResource;
47 -import com.sun.jersey.test.framework.JerseyTest;
48 47
49 import static org.easymock.EasyMock.createMock; 48 import static org.easymock.EasyMock.createMock;
50 import static org.easymock.EasyMock.expect; 49 import static org.easymock.EasyMock.expect;
...@@ -60,7 +59,7 @@ import static org.junit.Assert.fail; ...@@ -60,7 +59,7 @@ import static org.junit.Assert.fail;
60 /** 59 /**
61 * Unit tests for Intents REST APIs. 60 * Unit tests for Intents REST APIs.
62 */ 61 */
63 -public class IntentsResourceTest extends JerseyTest { 62 +public class IntentsResourceTest extends ResourceTest {
64 final IntentService mockIntentService = createMock(IntentService.class); 63 final IntentService mockIntentService = createMock(IntentService.class);
65 final HashSet<Intent> intents = new HashSet<>(); 64 final HashSet<Intent> intents = new HashSet<>();
66 private static final ApplicationId APP_ID = 65 private static final ApplicationId APP_ID =
...@@ -110,10 +109,6 @@ public class IntentsResourceTest extends JerseyTest { ...@@ -110,10 +109,6 @@ public class IntentsResourceTest extends JerseyTest {
110 } 109 }
111 } 110 }
112 111
113 - public IntentsResourceTest() {
114 - super("org.onosproject.rest");
115 - }
116 -
117 /** 112 /**
118 * Hamcrest matcher to check that an intent representation in JSON matches 113 * Hamcrest matcher to check that an intent representation in JSON matches
119 * the actual intent. 114 * the actual intent.
......
...@@ -35,7 +35,6 @@ import com.eclipsesource.json.JsonObject; ...@@ -35,7 +35,6 @@ import com.eclipsesource.json.JsonObject;
35 import com.google.common.collect.ImmutableList; 35 import com.google.common.collect.ImmutableList;
36 import com.google.common.collect.ImmutableSet; 36 import com.google.common.collect.ImmutableSet;
37 import com.sun.jersey.api.client.WebResource; 37 import com.sun.jersey.api.client.WebResource;
38 -import com.sun.jersey.test.framework.JerseyTest;
39 38
40 import static org.easymock.EasyMock.createMock; 39 import static org.easymock.EasyMock.createMock;
41 import static org.easymock.EasyMock.expect; 40 import static org.easymock.EasyMock.expect;
...@@ -52,7 +51,7 @@ import static org.onosproject.net.NetTestTools.link; ...@@ -52,7 +51,7 @@ import static org.onosproject.net.NetTestTools.link;
52 /** 51 /**
53 * Unit tests for links REST APIs. 52 * Unit tests for links REST APIs.
54 */ 53 */
55 -public class LinksResourceTest extends JerseyTest { 54 +public class LinksResourceTest extends ResourceTest {
56 LinkService mockLinkService; 55 LinkService mockLinkService;
57 56
58 Link link1 = link("src1", 1, "dst1", 1); 57 Link link1 = link("src1", 1, "dst1", 1);
...@@ -60,13 +59,6 @@ public class LinksResourceTest extends JerseyTest { ...@@ -60,13 +59,6 @@ public class LinksResourceTest extends JerseyTest {
60 Link link3 = link("src3", 3, "dst3", 3); 59 Link link3 = link("src3", 3, "dst3", 3);
61 60
62 /** 61 /**
63 - * Constructs the test.
64 - */
65 - public LinksResourceTest() {
66 - super("org.onosproject.rest");
67 - }
68 -
69 - /**
70 * Hamcrest matcher to check that an link representation in JSON matches 62 * Hamcrest matcher to check that an link representation in JSON matches
71 * the actual link. 63 * the actual link.
72 */ 64 */
......
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.rest;
17 +
18 +import java.io.IOException;
19 +import java.net.ServerSocket;
20 +
21 +import com.sun.jersey.test.framework.AppDescriptor;
22 +import com.sun.jersey.test.framework.JerseyTest;
23 +import com.sun.jersey.test.framework.WebAppDescriptor;
24 +
25 +/**
26 + * Base class for REST API tests. Performs common configuration operations.
27 + */
28 +public class ResourceTest extends JerseyTest {
29 +
30 + /**
31 + * Assigns an available port for the test.
32 + *
33 + * @param defaultPort If a port cannot be determined, this one is used.
34 + * @return free port
35 + */
36 + @Override
37 + public int getPort(int defaultPort) {
38 + try {
39 + ServerSocket socket = new ServerSocket(0);
40 + socket.setReuseAddress(true);
41 + int port = socket.getLocalPort();
42 + socket.close();
43 + return port;
44 + } catch (IOException ioe) {
45 + return defaultPort;
46 + }
47 + }
48 +
49 + @Override
50 + public AppDescriptor configure() {
51 + return new WebAppDescriptor.Builder("org.onosproject.rest").build();
52 + }
53 +
54 +}
...@@ -43,7 +43,6 @@ import com.eclipsesource.json.JsonArray; ...@@ -43,7 +43,6 @@ import com.eclipsesource.json.JsonArray;
43 import com.eclipsesource.json.JsonObject; 43 import com.eclipsesource.json.JsonObject;
44 import com.google.common.collect.ImmutableSet; 44 import com.google.common.collect.ImmutableSet;
45 import com.sun.jersey.api.client.WebResource; 45 import com.sun.jersey.api.client.WebResource;
46 -import com.sun.jersey.test.framework.JerseyTest;
47 46
48 import static org.hamcrest.Matchers.hasSize; 47 import static org.hamcrest.Matchers.hasSize;
49 import static org.hamcrest.Matchers.is; 48 import static org.hamcrest.Matchers.is;
...@@ -56,7 +55,7 @@ import static org.onosproject.net.NetTestTools.link; ...@@ -56,7 +55,7 @@ import static org.onosproject.net.NetTestTools.link;
56 /** 55 /**
57 * Unit tests for Topology REST APIs. 56 * Unit tests for Topology REST APIs.
58 */ 57 */
59 -public class TopologyResourceTest extends JerseyTest { 58 +public class TopologyResourceTest extends ResourceTest {
60 59
61 private static class MockTopology implements Topology { 60 private static class MockTopology implements Topology {
62 @Override 61 @Override
...@@ -172,11 +171,6 @@ public class TopologyResourceTest extends JerseyTest { ...@@ -172,11 +171,6 @@ public class TopologyResourceTest extends JerseyTest {
172 } 171 }
173 } 172 }
174 173
175 - public TopologyResourceTest() {
176 - super("org.onosproject.rest");
177 - }
178 -
179 -
180 /** 174 /**
181 * Initializes the test harness. 175 * Initializes the test harness.
182 */ 176 */
......