Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 1 | package net.onrc.onos.api.rest; |
| 2 | |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 3 | import com.codahale.metrics.MetricFilter; |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 4 | import net.floodlightcontroller.restserver.RestletRoutable; |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 5 | import net.onrc.onos.core.metrics.OnosMetrics; |
Ray Milkey | 3830135 | 2014-07-28 08:51:54 -0700 | [diff] [blame] | 6 | import net.onrc.onos.core.util.UnitTest; |
Ray Milkey | 213e56a | 2014-06-23 10:45:45 -0700 | [diff] [blame] | 7 | import org.json.JSONArray; |
| 8 | import org.json.JSONException; |
| 9 | import org.json.JSONObject; |
Ray Milkey | 3830135 | 2014-07-28 08:51:54 -0700 | [diff] [blame] | 10 | import org.junit.After; |
| 11 | import org.junit.Before; |
Ray Milkey | 213e56a | 2014-06-23 10:45:45 -0700 | [diff] [blame] | 12 | import org.restlet.resource.ClientResource; |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 13 | |
| 14 | import java.util.LinkedList; |
| 15 | import java.util.List; |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 16 | |
| 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 Milkey | 3830135 | 2014-07-28 08:51:54 -0700 | [diff] [blame] | 27 | public class TestRest extends UnitTest { |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 28 | |
| 29 | private final List<RestletRoutable> restlets = new LinkedList<>(); |
| 30 | private TestRestApiServer restApiServer; |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 31 | |
| 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 Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 44 | * 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 Milkey | 3830135 | 2014-07-28 08:51:54 -0700 | [diff] [blame] | 55 | @Before |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 56 | public void setUp() { |
Ray Milkey | 1c03030 | 2014-07-30 11:03:29 -0700 | [diff] [blame] | 57 | restApiServer = new TestRestApiServer(); |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 58 | 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 Milkey | 3830135 | 2014-07-28 08:51:54 -0700 | [diff] [blame] | 65 | @After |
| 66 | public void tearDownRest() { |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 67 | getRestApiServer().stopServer(); |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 68 | OnosMetrics.removeMatching(MetricFilter.ALL); |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get the base URL to use for REST requests. |
| 73 | * |
| 74 | * @return base URL |
| 75 | */ |
| 76 | String getBaseRestUrl() { |
Ray Milkey | 1c03030 | 2014-07-30 11:03:29 -0700 | [diff] [blame] | 77 | return "http://localhost:" + Integer.toString(restApiServer.getRestPort()) + "/wm/onos"; |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 78 | } |
Ray Milkey | 213e56a | 2014-06-23 10:45:45 -0700 | [diff] [blame] | 79 | |
| 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 Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 113 | } |