Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 1 | package net.onrc.onos.api.rest; |
| 2 | |
| 3 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 4 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 5 | import net.onrc.onos.core.intent.runtime.IntentTestMocks; |
| 6 | import net.onrc.onos.core.intent.runtime.PathCalcRuntimeModule; |
| 7 | import net.onrc.onos.core.intent.runtime.web.IntentWebRoutable; |
| 8 | import net.onrc.onos.core.topology.ITopologyService; |
| 9 | import net.onrc.onos.core.topology.web.TopologyWebRoutable; |
| 10 | import org.json.JSONArray; |
| 11 | import org.json.JSONException; |
| 12 | import org.json.JSONObject; |
| 13 | import org.restlet.resource.ClientResource; |
| 14 | |
| 15 | /** |
| 16 | * Test harness for Topology based REST API tests. This class maintains the |
| 17 | * web server and mocks required for testing topology APIs. REST API tests |
| 18 | * for topology should inherit from this class. |
| 19 | */ |
| 20 | public class TestRestTopology extends TestRest { |
| 21 | |
| 22 | private IntentTestMocks mocks; |
| 23 | |
| 24 | /** |
| 25 | * Fetch the Intent mocking object. |
| 26 | * |
| 27 | * @return intent mocking object |
| 28 | */ |
| 29 | IntentTestMocks getMocks() { |
| 30 | return mocks; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Create the web server and mocks required for the topology tests. |
| 35 | */ |
| 36 | @Override |
| 37 | public void setUp() { |
| 38 | mocks = new IntentTestMocks(); |
| 39 | mocks.setUpIntentMocks(); |
| 40 | |
| 41 | addRestlet(new TopologyWebRoutable()); |
| 42 | super.setUp(); |
| 43 | |
| 44 | final PathCalcRuntimeModule runtime = new PathCalcRuntimeModule(); |
| 45 | final FloodlightModuleContext moduleContext = getMocks().getModuleContext(); |
| 46 | try { |
| 47 | runtime.init(moduleContext); |
| 48 | } catch (FloodlightModuleException floodlightEx) { |
| 49 | throw new IllegalArgumentException(floodlightEx); |
| 50 | } |
| 51 | runtime.startUp(moduleContext); |
| 52 | |
| 53 | getRestApiServer().addAttribute(ITopologyService.class.getCanonicalName(), |
| 54 | mocks.getTopologyService()); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Remove anything that will interfere with the next test running correctly. |
| 59 | * Shuts down the test REST web server and removes the mocks. |
| 60 | */ |
| 61 | @Override |
| 62 | public void tearDown() { |
| 63 | getMocks().tearDownIntentMocks(); |
| 64 | super.tearDown(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Fetch the base URL for Topology REST APIs. |
| 69 | * |
| 70 | * @return base URL |
| 71 | */ |
| 72 | String getBaseRestTopologyUrl() { |
| 73 | return getBaseRestUrl() + "/topology"; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Get the JSON object representation for the top level object referred |
| 78 | * to by the given client. |
| 79 | * |
| 80 | * @param client the ClientResource that references the JSON object |
| 81 | * @return JSONObject that represents the object, null if it can't be |
| 82 | * fetched |
| 83 | */ |
| 84 | JSONObject getJSONObject(final ClientResource client) { |
| 85 | try { |
| 86 | final String topologyJSONString = client.get(String.class); |
| 87 | return new JSONObject(topologyJSONString); |
| 88 | } catch (JSONException jsonException) { |
| 89 | return null; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get the JSON array representation for the array referred to by |
| 95 | * the given client. |
| 96 | * |
| 97 | * @param client the ClientResource that references the JSON array |
| 98 | * @return JSONArray that represents the array, null if it can't be |
| 99 | * fetched. |
| 100 | */ |
| 101 | JSONArray getJSONArray(final ClientResource client) { |
| 102 | try { |
| 103 | final String topologyJSONString = client.get(String.class); |
| 104 | return new JSONArray(topologyJSONString); |
| 105 | } catch (JSONException jsonException) { |
| 106 | return null; |
| 107 | } |
| 108 | } |
| 109 | } |