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 | |
| 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 | } |