blob: 76792ab2a00d4694a38801d63368f839c607dcdf [file] [log] [blame]
Ray Milkey531bb232014-06-03 09:08:15 -07001package net.onrc.onos.api.rest;
2
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -07003import com.codahale.metrics.MetricFilter;
Ray Milkey531bb232014-06-03 09:08:15 -07004import net.floodlightcontroller.restserver.RestletRoutable;
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -07005import net.onrc.onos.core.metrics.OnosMetrics;
Ray Milkey213e56a2014-06-23 10:45:45 -07006import org.json.JSONArray;
7import org.json.JSONException;
8import org.json.JSONObject;
9import org.restlet.resource.ClientResource;
Ray Milkey531bb232014-06-03 09:08:15 -070010
11import java.util.LinkedList;
12import java.util.List;
13import java.util.Random;
14
15/**
16 * Base class for REST API tests. This class exposes common code for setting
17 * up REST tests.
18 *
19 * This class maintains the web server and restlets that allow a test to call
20 * REST APIs. It is intended to be used as a base class for a class that
21 * allows testing of a specific REST API resource (an example is Intents).
22 * See TestRestIntent as an example of an implementation which uses the
23 * TestRest framework.
24 */
25public class TestRest {
26
27 private final List<RestletRoutable> restlets = new LinkedList<>();
28 private TestRestApiServer restApiServer;
29 private int restPort;
30
31 /**
32 * Add a restlet to the web server. Tests call this to add the specific
33 * REST APIs they are testing. Call this before starting the server via
34 * setUp().
35 *
36 * @param newRestlet restlet to add to the web server
37 */
38 void addRestlet(final RestletRoutable newRestlet) {
39 restlets.add(newRestlet);
40 }
41
42 /**
43 * Assign the TCP port for the web server.
44 *
45 * @param newPort port number the web server will use
46 */
47 void setRestPort(int newPort) {
48 restPort = newPort;
49 }
50
51 /**
52 * Fetch the REST API Web Server object.
53 *
54 * @return REST API web server
55 */
56 TestRestApiServer getRestApiServer() {
57 return restApiServer;
58 }
59
60 /**
61 * Set up the REST API web server and start it.
62 */
63 public void setUp() {
64 restApiServer = new TestRestApiServer(restPort);
65 restApiServer.startServer(restlets);
66 }
67
68 /**
69 * Remove anything that will interfere with the next test running correctly.
70 * Shuts down the test REST web server.
71 */
72
73 public void tearDown() {
74 getRestApiServer().stopServer();
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -070075 OnosMetrics.removeMatching(MetricFilter.ALL);
Ray Milkey531bb232014-06-03 09:08:15 -070076 }
77
78 /**
79 * Get the base URL to use for REST requests.
80 *
81 * @return base URL
82 */
83 String getBaseRestUrl() {
84 return "http://localhost:" + Integer.toString(restPort) + "/wm/onos";
85 }
86
87 /**
88 * Generate a random port number for the REST API web server to use. For
89 * now, a random port between 50000 and 55000 is selected
90 *
91 * @return a port number that the web server can use
92 */
93 int generateRandomPort() {
94 final int portStartRange = 50000;
95 final int portEndRange = 55000;
96
97 final Random random = new Random();
98
99 return portStartRange + (random.nextInt(portEndRange - portStartRange));
100 }
Ray Milkey213e56a2014-06-23 10:45:45 -0700101
102 /**
103 * Get the JSON object representation for the top level object referred
104 * to by the given client.
105 *
106 * @param client the ClientResource that references the JSON object
107 * @return JSONObject that represents the object, null if it can't be
108 * fetched
109 */
110 protected static JSONObject getJSONObject(final ClientResource client) {
111 try {
112 final String responseJSONString = client.get(String.class);
113 return new JSONObject(responseJSONString);
114 } catch (JSONException jsonException) {
115 return null;
116 }
117 }
118
119 /**
120 * Get the JSON array representation for the array referred to by
121 * the given client.
122 *
123 * @param client the ClientResource that references the JSON array
124 * @return JSONArray that represents the array, null if it can't be
125 * fetched.
126 */
127 protected static JSONArray getJSONArray(final ClientResource client) {
128 try {
129 final String responseJSONString = client.get(String.class);
130 return new JSONArray(responseJSONString);
131 } catch (JSONException jsonException) {
132 return null;
133 }
134 }
Ray Milkey531bb232014-06-03 09:08:15 -0700135}