blob: 7a93eacd2ca557c9cc24878a1b3d3bceb33e7c46 [file] [log] [blame]
Ray Milkey9c3d3362015-01-28 10:39:56 -08001/*
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 */
Jian Li8ae91202016-03-24 14:36:16 -070016package org.onosproject.rest.resources;
Ray Milkey9c3d3362015-01-28 10:39:56 -080017
Jian Li9d616492016-03-09 10:52:49 -080018import org.glassfish.jersey.server.ResourceConfig;
19import org.glassfish.jersey.test.JerseyTest;
Ray Milkey9c3d3362015-01-28 10:39:56 -080020
Jian Li7ff79782016-03-28 10:27:22 -070021import java.io.IOException;
22import java.net.ServerSocket;
23
Ray Milkey9c3d3362015-01-28 10:39:56 -080024/**
25 * Base class for REST API tests. Performs common configuration operations.
26 */
27public class ResourceTest extends JerseyTest {
Jian Li7ff79782016-03-28 10:27:22 -070028 private static final int DEFAULT_PORT = 9998;
Ray Milkey9c3d3362015-01-28 10:39:56 -080029
30 /**
Thomas Vachuskaa026be72015-12-07 16:00:37 -080031 * Creates a new web-resource test.
32 */
33 public ResourceTest() {
Jian Li9d616492016-03-09 10:52:49 -080034 super(ResourceConfig.forApplicationClass(CoreWebApplication.class));
Jian Li7ff79782016-03-28 10:27:22 -070035 this.set("jersey.config.test.container.port", getRandomPort(DEFAULT_PORT));
36 }
37
38 /**
Ray Milkey7c251822016-04-06 17:38:25 -070039 * Creates a new web-resource test.
40 */
41 public ResourceTest(ResourceConfig config) {
42 super(config);
43 this.set("jersey.config.test.container.port", getRandomPort(DEFAULT_PORT));
44 }
45
46 /**
Jian Li7ff79782016-03-28 10:27:22 -070047 * Returns an unused port number to make sure that each unit test runs in
48 * different port number.
49 *
50 * @param defaultPort default port number
51 * @return a randomized unique port number
52 */
53 private int getRandomPort(int defaultPort) {
54 try {
55 ServerSocket socket = new ServerSocket(0);
56 socket.setReuseAddress(true);
57 int port = socket.getLocalPort();
58 socket.close();
59 return port;
60 } catch (IOException ioe) {
61 return defaultPort;
62 }
Thomas Vachuskaa026be72015-12-07 16:00:37 -080063 }
Ray Milkey9c3d3362015-01-28 10:39:56 -080064}