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 | 213e56a | 2014-06-23 10:45:45 -0700 | [diff] [blame] | 6 | import org.json.JSONArray; |
| 7 | import org.json.JSONException; |
| 8 | import org.json.JSONObject; |
| 9 | import org.restlet.resource.ClientResource; |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 10 | |
| 11 | import java.util.LinkedList; |
| 12 | import java.util.List; |
| 13 | import 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 | */ |
| 25 | public 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 Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame^] | 75 | OnosMetrics.removeMatching(MetricFilter.ALL); |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 76 | } |
| 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 Milkey | 213e56a | 2014-06-23 10:45:45 -0700 | [diff] [blame] | 101 | |
| 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 Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 135 | } |