Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 1 | package net.onrc.onos.api.rest; |
| 2 | |
| 3 | import net.onrc.onos.core.intent.runtime.PathCalcRuntimeModule; |
| 4 | import org.json.JSONArray; |
| 5 | import org.json.JSONException; |
| 6 | import org.json.JSONObject; |
| 7 | import org.junit.After; |
| 8 | import org.junit.Before; |
| 9 | import org.junit.Test; |
| 10 | import org.junit.runner.RunWith; |
| 11 | import org.powermock.core.classloader.annotations.PrepareForTest; |
| 12 | import org.powermock.modules.junit4.PowerMockRunner; |
| 13 | import org.restlet.data.Status; |
| 14 | import org.restlet.resource.ClientResource; |
| 15 | |
| 16 | import java.util.HashSet; |
| 17 | import java.util.Set; |
| 18 | |
| 19 | import static net.onrc.onos.api.rest.ClientResourceStatusMatcher.hasStatusOf; |
| 20 | import static org.hamcrest.MatcherAssert.assertThat; |
| 21 | import static org.hamcrest.Matchers.equalTo; |
| 22 | import static org.hamcrest.Matchers.hasItems; |
| 23 | import static org.hamcrest.Matchers.is; |
| 24 | import static org.hamcrest.Matchers.notNullValue; |
| 25 | |
| 26 | /** |
| 27 | * Tests for topology REST get operations. |
| 28 | */ |
| 29 | @RunWith(PowerMockRunner.class) |
| 30 | @PrepareForTest(PathCalcRuntimeModule.class) |
| 31 | public class TestRestTopologyGet extends TestRestTopology { |
| 32 | |
| 33 | /** |
| 34 | * Create the web server and mocks required for |
| 35 | * all of the tests. |
| 36 | */ |
| 37 | @Before |
| 38 | @SuppressWarnings("ununsed") |
| 39 | public void beforeTest() { |
| 40 | setRestPort(generateRandomPort()); |
| 41 | setUp(); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | /** |
| 46 | * Remove anything that will interfere with the next test running correctly. |
| 47 | * Shuts down the test REST web server and removes the mocks. |
| 48 | */ |
| 49 | @After |
| 50 | @SuppressWarnings("unused") |
| 51 | public void afterTest() { |
| 52 | tearDown(); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Check that the JSON array returned for the switches element matches |
| 57 | * the data in the mocked topology. |
| 58 | * |
| 59 | * @param switches JSON array of switches |
| 60 | * @throws JSONException if the JSON is not properly specified |
| 61 | */ |
| 62 | private void checkSwitches(final JSONArray switches) throws JSONException { |
| 63 | assertThat(switches.length(), is(equalTo(4))); |
| 64 | |
| 65 | // Check that the first switch has the proper data |
| 66 | final JSONObject switch0 = switches.getJSONObject(0); |
| 67 | assertThat(switch0.length(), is(equalTo(3))); |
| 68 | assertThat(switch0.getString("dpid"), is(equalTo("00:00:00:00:00:00:00:02"))); |
| 69 | assertThat(switch0.getString("state"), is(equalTo("ACTIVE"))); |
| 70 | |
| 71 | // Check that the ports array for the switch is correct |
| 72 | final JSONArray switch0Ports = switch0.getJSONArray("ports"); |
| 73 | |
| 74 | // check the length of the port array |
| 75 | assertThat(switch0Ports.length(), equalTo(4)); |
| 76 | |
| 77 | // check the contents of the ports array. All of the ports should be |
| 78 | // active and refer to this switch. |
| 79 | for (int portIndex = 0; portIndex < switch0Ports.length(); portIndex++) { |
| 80 | final JSONObject switchPort = switch0Ports.getJSONObject(portIndex); |
| 81 | assertThat(switchPort.getString("dpid"), is(equalTo("00:00:00:00:00:00:00:02"))); |
| 82 | assertThat(switchPort.getString("state"), is(equalTo("ACTIVE"))); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Check that the JSON array returned for the links element matches |
| 88 | * the data in the mocked topology. |
| 89 | * |
| 90 | * @param links JSON array of links |
| 91 | * @throws JSONException if the JSON is not properly specified |
| 92 | */ |
| 93 | private void checkLinks(final JSONArray links) throws JSONException { |
| 94 | // Check the length of the links array |
| 95 | assertThat(links.length(), is(equalTo(10))); |
| 96 | |
| 97 | final Set<String> fromLinks = new HashSet<>(); |
| 98 | final Set<String> toLinks = new HashSet<>(); |
| 99 | |
| 100 | // Check that the source and destination of links to switch 0 are |
| 101 | // correct |
| 102 | for (int linkIndex = 0; linkIndex < links.length(); linkIndex++) { |
| 103 | final JSONObject link = links.getJSONObject(linkIndex); |
| 104 | final String src = link.getString("src-switch"); |
| 105 | final String dst = link.getString("dst-switch"); |
| 106 | assertThat(src, is(notNullValue())); |
| 107 | assertThat(dst, is(notNullValue())); |
| 108 | |
| 109 | if (src.equals("00:00:00:00:00:00:00:02")) { |
| 110 | toLinks.add(dst); |
| 111 | } |
| 112 | |
| 113 | if (dst.equals("00:00:00:00:00:00:00:02")) { |
| 114 | fromLinks.add(src); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | assertThat(toLinks, hasItems("00:00:00:00:00:00:00:01", |
| 119 | "00:00:00:00:00:00:00:03", |
| 120 | "00:00:00:00:00:00:00:04")); |
| 121 | |
| 122 | assertThat(fromLinks, hasItems("00:00:00:00:00:00:00:01", |
| 123 | "00:00:00:00:00:00:00:03", |
| 124 | "00:00:00:00:00:00:00:04")); |
| 125 | } |
| 126 | |
| 127 | /** |
Pavlin Radoslavov | 308337c | 2014-06-11 10:25:44 -0700 | [diff] [blame] | 128 | * Check that the JSON array returned for the hosts element matches |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 129 | * the data in the mocked topology. |
| 130 | * |
Pavlin Radoslavov | 308337c | 2014-06-11 10:25:44 -0700 | [diff] [blame] | 131 | * @param hosts JSON array of hosts |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 132 | */ |
Pavlin Radoslavov | 308337c | 2014-06-11 10:25:44 -0700 | [diff] [blame] | 133 | private void checkHosts(final JSONArray hosts) { |
| 134 | // hosts array should be empty |
| 135 | assertThat(hosts.length(), is(equalTo(0))); |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Test that the GET of all Topology REST call returns the proper result. |
| 140 | * The call to get all Topology should return 3 items (switches, links, |
Pavlin Radoslavov | 308337c | 2014-06-11 10:25:44 -0700 | [diff] [blame] | 141 | * and hosts), an HTTP status of OK, and the proper topology data. |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 142 | */ |
| 143 | @Test |
| 144 | public void testFetchOfAllTopology() throws Exception { |
| 145 | final ClientResource client = new ClientResource(getBaseRestTopologyUrl()); |
| 146 | final JSONObject topology = getJSONObject(client); |
| 147 | |
| 148 | // HTTP status should be OK |
| 149 | assertThat(client, hasStatusOf(Status.SUCCESS_OK)); |
| 150 | |
| 151 | // Check the number of top level members in the topology object |
| 152 | assertThat(topology.length(), is(equalTo(3))); |
| 153 | |
| 154 | // Check the switches element |
| 155 | final JSONArray switches = topology.getJSONArray("switches"); |
| 156 | checkSwitches(switches); |
| 157 | |
| 158 | // Check the values in the links array |
| 159 | final JSONArray links = topology.getJSONArray("links"); |
| 160 | checkLinks(links); |
| 161 | |
Pavlin Radoslavov | 308337c | 2014-06-11 10:25:44 -0700 | [diff] [blame] | 162 | // Check the hosts array |
| 163 | final JSONArray hosts = topology.getJSONArray("hosts"); |
| 164 | checkHosts(hosts); |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Test that the GET of all switches REST call returns the proper result. |
| 169 | * The call to get all switches should return the correct switch data. |
| 170 | */ |
| 171 | @Test |
| 172 | public void testFetchOfAllSwitches() throws Exception { |
| 173 | final ClientResource client = new ClientResource(getBaseRestTopologyUrl() + "/switches"); |
| 174 | final JSONArray switches = getJSONArray(client); |
| 175 | |
| 176 | // HTTP status should be OK |
| 177 | assertThat(client, hasStatusOf(Status.SUCCESS_OK)); |
| 178 | |
| 179 | checkSwitches(switches); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Test that the GET of all links REST call returns the proper result. |
| 184 | * The call to get all links should return the proper link data. |
| 185 | */ |
| 186 | @Test |
| 187 | public void testFetchOfAllLinks() throws Exception { |
| 188 | final ClientResource client = new ClientResource(getBaseRestTopologyUrl() + "/links"); |
| 189 | final JSONArray links = getJSONArray(client); |
| 190 | |
| 191 | // HTTP status should be OK |
| 192 | assertThat(client, hasStatusOf(Status.SUCCESS_OK)); |
| 193 | |
| 194 | checkLinks(links); |
| 195 | } |
| 196 | |
| 197 | /** |
Pavlin Radoslavov | 308337c | 2014-06-11 10:25:44 -0700 | [diff] [blame] | 198 | * Test that the GET of all hosts REST call returns the proper result. |
| 199 | * The call to get all hosts should return no hosts. |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 200 | */ |
| 201 | @Test |
Pavlin Radoslavov | 308337c | 2014-06-11 10:25:44 -0700 | [diff] [blame] | 202 | public void testFetchOfAllHosts() throws Exception { |
| 203 | final ClientResource client = new ClientResource(getBaseRestTopologyUrl() + "/hosts"); |
| 204 | final JSONArray hosts = getJSONArray(client); |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 205 | |
| 206 | // HTTP status should be OK |
| 207 | assertThat(client, hasStatusOf(Status.SUCCESS_OK)); |
| 208 | |
Pavlin Radoslavov | 308337c | 2014-06-11 10:25:44 -0700 | [diff] [blame] | 209 | checkHosts(hosts); |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 210 | } |
| 211 | } |