Ray Milkey | 9c3d336 | 2015-01-28 10:39:56 -0800 | [diff] [blame] | 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 | */ |
Jian Li | 8ae9120 | 2016-03-24 14:36:16 -0700 | [diff] [blame] | 16 | package org.onosproject.rest.resources; |
Ray Milkey | 9c3d336 | 2015-01-28 10:39:56 -0800 | [diff] [blame] | 17 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 18 | import org.glassfish.jersey.server.ResourceConfig; |
| 19 | import org.glassfish.jersey.test.JerseyTest; |
Ray Milkey | 9c3d336 | 2015-01-28 10:39:56 -0800 | [diff] [blame] | 20 | |
Jian Li | 7ff7978 | 2016-03-28 10:27:22 -0700 | [diff] [blame] | 21 | import java.io.IOException; |
| 22 | import java.net.ServerSocket; |
| 23 | |
Ray Milkey | 9c3d336 | 2015-01-28 10:39:56 -0800 | [diff] [blame] | 24 | /** |
| 25 | * Base class for REST API tests. Performs common configuration operations. |
| 26 | */ |
| 27 | public class ResourceTest extends JerseyTest { |
Jian Li | 7ff7978 | 2016-03-28 10:27:22 -0700 | [diff] [blame] | 28 | private static final int DEFAULT_PORT = 9998; |
Ray Milkey | 9c3d336 | 2015-01-28 10:39:56 -0800 | [diff] [blame] | 29 | |
| 30 | /** |
Thomas Vachuska | a026be7 | 2015-12-07 16:00:37 -0800 | [diff] [blame] | 31 | * Creates a new web-resource test. |
| 32 | */ |
| 33 | public ResourceTest() { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 34 | super(ResourceConfig.forApplicationClass(CoreWebApplication.class)); |
Jian Li | 7ff7978 | 2016-03-28 10:27:22 -0700 | [diff] [blame] | 35 | this.set("jersey.config.test.container.port", getRandomPort(DEFAULT_PORT)); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Returns an unused port number to make sure that each unit test runs in |
| 40 | * different port number. |
| 41 | * |
| 42 | * @param defaultPort default port number |
| 43 | * @return a randomized unique port number |
| 44 | */ |
| 45 | private int getRandomPort(int defaultPort) { |
| 46 | try { |
| 47 | ServerSocket socket = new ServerSocket(0); |
| 48 | socket.setReuseAddress(true); |
| 49 | int port = socket.getLocalPort(); |
| 50 | socket.close(); |
| 51 | return port; |
| 52 | } catch (IOException ioe) { |
| 53 | return defaultPort; |
| 54 | } |
Thomas Vachuska | a026be7 | 2015-12-07 16:00:37 -0800 | [diff] [blame] | 55 | } |
Ray Milkey | 9c3d336 | 2015-01-28 10:39:56 -0800 | [diff] [blame] | 56 | } |