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 | */ |
| 16 | package org.onosproject.rest; |
| 17 | |
| 18 | import java.io.IOException; |
| 19 | import java.net.ServerSocket; |
| 20 | |
Thomas Vachuska | a026be7 | 2015-12-07 16:00:37 -0800 | [diff] [blame] | 21 | import com.sun.jersey.spi.container.servlet.ServletContainer; |
Ray Milkey | 9c3d336 | 2015-01-28 10:39:56 -0800 | [diff] [blame] | 22 | import com.sun.jersey.test.framework.AppDescriptor; |
| 23 | import com.sun.jersey.test.framework.JerseyTest; |
| 24 | import com.sun.jersey.test.framework.WebAppDescriptor; |
| 25 | |
| 26 | /** |
| 27 | * Base class for REST API tests. Performs common configuration operations. |
| 28 | */ |
| 29 | public class ResourceTest extends JerseyTest { |
| 30 | |
| 31 | /** |
Thomas Vachuska | a026be7 | 2015-12-07 16:00:37 -0800 | [diff] [blame] | 32 | * Creates a new web-resource test. |
| 33 | */ |
| 34 | public ResourceTest() { |
| 35 | super(); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Creates a new web-resource test initialized according to the specified |
| 40 | * web application class. |
| 41 | */ |
| 42 | protected ResourceTest(Class<?> webAppClass) { |
| 43 | super(new WebAppDescriptor.Builder("javax.ws.rs.Application", |
| 44 | webAppClass.getCanonicalName()) |
| 45 | .servletClass(ServletContainer.class).build()); |
| 46 | } |
| 47 | |
| 48 | /** |
Ray Milkey | 9c3d336 | 2015-01-28 10:39:56 -0800 | [diff] [blame] | 49 | * Assigns an available port for the test. |
| 50 | * |
| 51 | * @param defaultPort If a port cannot be determined, this one is used. |
| 52 | * @return free port |
| 53 | */ |
| 54 | @Override |
| 55 | public int getPort(int defaultPort) { |
| 56 | try { |
| 57 | ServerSocket socket = new ServerSocket(0); |
| 58 | socket.setReuseAddress(true); |
| 59 | int port = socket.getLocalPort(); |
| 60 | socket.close(); |
| 61 | return port; |
| 62 | } catch (IOException ioe) { |
| 63 | return defaultPort; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public AppDescriptor configure() { |
| 69 | return new WebAppDescriptor.Builder("org.onosproject.rest").build(); |
| 70 | } |
| 71 | |
| 72 | } |