blob: 3308f3c9cfd10621dbe5d15fbfb609593c7d8953 [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 */
16package org.onosproject.rest;
17
18import java.io.IOException;
19import java.net.ServerSocket;
20
Thomas Vachuskaa026be72015-12-07 16:00:37 -080021import com.sun.jersey.spi.container.servlet.ServletContainer;
Ray Milkey9c3d3362015-01-28 10:39:56 -080022import com.sun.jersey.test.framework.AppDescriptor;
23import com.sun.jersey.test.framework.JerseyTest;
24import com.sun.jersey.test.framework.WebAppDescriptor;
25
26/**
27 * Base class for REST API tests. Performs common configuration operations.
28 */
29public class ResourceTest extends JerseyTest {
30
31 /**
Thomas Vachuskaa026be72015-12-07 16:00:37 -080032 * 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 Milkey9c3d3362015-01-28 10:39:56 -080049 * 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}