blob: b03b7c43f520c0916f8e6b9eb527092f0125bcfb [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 Milkey38301352014-07-28 08:51:54 -07006import net.onrc.onos.core.util.UnitTest;
Ray Milkey213e56a2014-06-23 10:45:45 -07007import org.json.JSONArray;
8import org.json.JSONException;
9import org.json.JSONObject;
Ray Milkey38301352014-07-28 08:51:54 -070010import org.junit.After;
11import org.junit.Before;
Ray Milkey213e56a2014-06-23 10:45:45 -070012import org.restlet.resource.ClientResource;
Ray Milkey531bb232014-06-03 09:08:15 -070013
14import java.util.LinkedList;
15import java.util.List;
Ray Milkey531bb232014-06-03 09:08:15 -070016
17/**
18 * Base class for REST API tests. This class exposes common code for setting
19 * up REST tests.
20 *
21 * This class maintains the web server and restlets that allow a test to call
22 * REST APIs. It is intended to be used as a base class for a class that
23 * allows testing of a specific REST API resource (an example is Intents).
24 * See TestRestIntent as an example of an implementation which uses the
25 * TestRest framework.
26 */
Ray Milkey38301352014-07-28 08:51:54 -070027public class TestRest extends UnitTest {
Ray Milkey531bb232014-06-03 09:08:15 -070028
29 private final List<RestletRoutable> restlets = new LinkedList<>();
30 private TestRestApiServer restApiServer;
Ray Milkey531bb232014-06-03 09:08:15 -070031
32 /**
33 * Add a restlet to the web server. Tests call this to add the specific
34 * REST APIs they are testing. Call this before starting the server via
35 * setUp().
36 *
37 * @param newRestlet restlet to add to the web server
38 */
39 void addRestlet(final RestletRoutable newRestlet) {
40 restlets.add(newRestlet);
41 }
42
43 /**
Ray Milkey531bb232014-06-03 09:08:15 -070044 * Fetch the REST API Web Server object.
45 *
46 * @return REST API web server
47 */
48 TestRestApiServer getRestApiServer() {
49 return restApiServer;
50 }
51
52 /**
53 * Set up the REST API web server and start it.
54 */
Ray Milkey38301352014-07-28 08:51:54 -070055 @Before
Ray Milkey531bb232014-06-03 09:08:15 -070056 public void setUp() {
Ray Milkey1c030302014-07-30 11:03:29 -070057 restApiServer = new TestRestApiServer();
Ray Milkey531bb232014-06-03 09:08:15 -070058 restApiServer.startServer(restlets);
59 }
60
61 /**
62 * Remove anything that will interfere with the next test running correctly.
63 * Shuts down the test REST web server.
64 */
Ray Milkey38301352014-07-28 08:51:54 -070065 @After
66 public void tearDownRest() {
Ray Milkey531bb232014-06-03 09:08:15 -070067 getRestApiServer().stopServer();
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -070068 OnosMetrics.removeMatching(MetricFilter.ALL);
Ray Milkey531bb232014-06-03 09:08:15 -070069 }
70
71 /**
72 * Get the base URL to use for REST requests.
73 *
74 * @return base URL
75 */
76 String getBaseRestUrl() {
Ray Milkey1c030302014-07-30 11:03:29 -070077 return "http://localhost:" + Integer.toString(restApiServer.getRestPort()) + "/wm/onos";
Ray Milkey531bb232014-06-03 09:08:15 -070078 }
Ray Milkey213e56a2014-06-23 10:45:45 -070079
80 /**
81 * Get the JSON object representation for the top level object referred
82 * to by the given client.
83 *
84 * @param client the ClientResource that references the JSON object
85 * @return JSONObject that represents the object, null if it can't be
86 * fetched
87 */
88 protected static JSONObject getJSONObject(final ClientResource client) {
89 try {
90 final String responseJSONString = client.get(String.class);
91 return new JSONObject(responseJSONString);
92 } catch (JSONException jsonException) {
93 return null;
94 }
95 }
96
97 /**
98 * Get the JSON array representation for the array referred to by
99 * the given client.
100 *
101 * @param client the ClientResource that references the JSON array
102 * @return JSONArray that represents the array, null if it can't be
103 * fetched.
104 */
105 protected static JSONArray getJSONArray(final ClientResource client) {
106 try {
107 final String responseJSONString = client.get(String.class);
108 return new JSONArray(responseJSONString);
109 } catch (JSONException jsonException) {
110 return null;
111 }
112 }
Ray Milkey531bb232014-06-03 09:08:15 -0700113}