blob: 5305a8a3bc3dd66055df4f12a9680c6239b3fdb6 [file] [log] [blame]
Ray Milkey9c3d3362015-01-28 10:39:56 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Ray Milkey9c3d3362015-01-28 10:39:56 -08003 *
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;
Jian Li4fb71772016-04-20 09:41:56 -070020import org.glassfish.jersey.test.jetty.JettyTestContainerFactory;
21import org.glassfish.jersey.test.spi.TestContainerException;
22import org.glassfish.jersey.test.spi.TestContainerFactory;
Ray Milkey9c3d3362015-01-28 10:39:56 -080023
Jian Li7ff79782016-03-28 10:27:22 -070024import java.io.IOException;
25import java.net.ServerSocket;
26
Ray Milkey9c3d3362015-01-28 10:39:56 -080027/**
Jian Li4fb71772016-04-20 09:41:56 -070028 * Base class for REST API tests.
29 * Performs common configuration operations.
Ray Milkey9c3d3362015-01-28 10:39:56 -080030 */
31public class ResourceTest extends JerseyTest {
Jian Li7ff79782016-03-28 10:27:22 -070032 private static final int DEFAULT_PORT = 9998;
Ray Milkey9c3d3362015-01-28 10:39:56 -080033
34 /**
Thomas Vachuskaa026be72015-12-07 16:00:37 -080035 * Creates a new web-resource test.
36 */
37 public ResourceTest() {
Jian Li9d616492016-03-09 10:52:49 -080038 super(ResourceConfig.forApplicationClass(CoreWebApplication.class));
Jian Li7ff79782016-03-28 10:27:22 -070039 this.set("jersey.config.test.container.port", getRandomPort(DEFAULT_PORT));
40 }
41
42 /**
Ray Milkey7c251822016-04-06 17:38:25 -070043 * Creates a new web-resource test.
44 */
45 public ResourceTest(ResourceConfig config) {
46 super(config);
47 this.set("jersey.config.test.container.port", getRandomPort(DEFAULT_PORT));
48 }
49
50 /**
Jian Li7ff79782016-03-28 10:27:22 -070051 * Returns an unused port number to make sure that each unit test runs in
52 * different port number.
53 *
54 * @param defaultPort default port number
55 * @return a randomized unique port number
56 */
57 private int getRandomPort(int defaultPort) {
58 try {
59 ServerSocket socket = new ServerSocket(0);
60 socket.setReuseAddress(true);
61 int port = socket.getLocalPort();
62 socket.close();
63 return port;
64 } catch (IOException ioe) {
65 return defaultPort;
66 }
Thomas Vachuskaa026be72015-12-07 16:00:37 -080067 }
Jian Li4fb71772016-04-20 09:41:56 -070068
69 /**
70 * Configures the jetty test container as default test container.
71 *
72 * @return test container factory
73 * @throws TestContainerException
74 */
75 @Override
76 protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
77 return new JettyTestContainerFactory();
78 }
Ray Milkey9c3d3362015-01-28 10:39:56 -080079}