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