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; |
Yuta HIGUCHI | d830aad | 2014-07-06 15:02:01 -0700 | [diff] [blame^] | 4 | |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 5 | import org.json.JSONArray; |
| 6 | import org.json.JSONException; |
| 7 | import org.json.JSONObject; |
| 8 | import org.junit.After; |
| 9 | import org.junit.Before; |
| 10 | import org.junit.Test; |
| 11 | import org.junit.runner.RunWith; |
| 12 | import org.powermock.core.classloader.annotations.PrepareForTest; |
| 13 | import org.powermock.modules.junit4.PowerMockRunner; |
| 14 | import org.restlet.data.Status; |
| 15 | import org.restlet.resource.ClientResource; |
| 16 | |
Yuta HIGUCHI | d830aad | 2014-07-06 15:02:01 -0700 | [diff] [blame^] | 17 | import com.google.common.collect.ImmutableList; |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 18 | import java.util.HashSet; |
Yuta HIGUCHI | d830aad | 2014-07-06 15:02:01 -0700 | [diff] [blame^] | 19 | import java.util.List; |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 20 | import java.util.Set; |
| 21 | |
| 22 | import static net.onrc.onos.api.rest.ClientResourceStatusMatcher.hasStatusOf; |
| 23 | import static org.hamcrest.MatcherAssert.assertThat; |
Yuta HIGUCHI | d830aad | 2014-07-06 15:02:01 -0700 | [diff] [blame^] | 24 | import static org.hamcrest.Matchers.*; |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 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 | */ |
Yuta HIGUCHI | d830aad | 2014-07-06 15:02:01 -0700 | [diff] [blame^] | 62 | @SuppressWarnings("unchecked") |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 63 | private void checkSwitches(final JSONArray switches) throws JSONException { |
| 64 | assertThat(switches.length(), is(equalTo(4))); |
| 65 | |
| 66 | // Check that the first switch has the proper data |
| 67 | final JSONObject switch0 = switches.getJSONObject(0); |
Yuta HIGUCHI | d830aad | 2014-07-06 15:02:01 -0700 | [diff] [blame^] | 68 | final List<String> keys = ImmutableList.<String>copyOf(switch0.keys()); |
| 69 | assertThat(keys, |
| 70 | hasItems("dpid", "state", "ports")); |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 71 | assertThat(switch0.getString("dpid"), is(equalTo("00:00:00:00:00:00:00:02"))); |
| 72 | assertThat(switch0.getString("state"), is(equalTo("ACTIVE"))); |
| 73 | |
| 74 | // Check that the ports array for the switch is correct |
| 75 | final JSONArray switch0Ports = switch0.getJSONArray("ports"); |
| 76 | |
| 77 | // check the length of the port array |
| 78 | assertThat(switch0Ports.length(), equalTo(4)); |
| 79 | |
| 80 | // check the contents of the ports array. All of the ports should be |
| 81 | // active and refer to this switch. |
| 82 | for (int portIndex = 0; portIndex < switch0Ports.length(); portIndex++) { |
| 83 | final JSONObject switchPort = switch0Ports.getJSONObject(portIndex); |
| 84 | assertThat(switchPort.getString("dpid"), is(equalTo("00:00:00:00:00:00:00:02"))); |
| 85 | assertThat(switchPort.getString("state"), is(equalTo("ACTIVE"))); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Check that the JSON array returned for the links element matches |
| 91 | * the data in the mocked topology. |
| 92 | * |
| 93 | * @param links JSON array of links |
| 94 | * @throws JSONException if the JSON is not properly specified |
| 95 | */ |
| 96 | private void checkLinks(final JSONArray links) throws JSONException { |
| 97 | // Check the length of the links array |
| 98 | assertThat(links.length(), is(equalTo(10))); |
| 99 | |
| 100 | final Set<String> fromLinks = new HashSet<>(); |
| 101 | final Set<String> toLinks = new HashSet<>(); |
| 102 | |
| 103 | // Check that the source and destination of links to switch 0 are |
| 104 | // correct |
| 105 | for (int linkIndex = 0; linkIndex < links.length(); linkIndex++) { |
| 106 | final JSONObject link = links.getJSONObject(linkIndex); |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 107 | final JSONObject src = link.getJSONObject("src"); |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 108 | assertThat(src, is(notNullValue())); |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 109 | final JSONObject dst = link.getJSONObject("dst"); |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 110 | assertThat(dst, is(notNullValue())); |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 111 | final String srcDpid = src.getString("dpid"); |
| 112 | final String dstDpid = dst.getString("dpid"); |
| 113 | assertThat(srcDpid, is(notNullValue())); |
| 114 | assertThat(dstDpid, is(notNullValue())); |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 115 | |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 116 | if (srcDpid.equals("00:00:00:00:00:00:00:02")) { |
| 117 | toLinks.add(dstDpid); |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 120 | if (dstDpid.equals("00:00:00:00:00:00:00:02")) { |
| 121 | fromLinks.add(srcDpid); |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | assertThat(toLinks, hasItems("00:00:00:00:00:00:00:01", |
| 126 | "00:00:00:00:00:00:00:03", |
| 127 | "00:00:00:00:00:00:00:04")); |
| 128 | |
| 129 | assertThat(fromLinks, hasItems("00:00:00:00:00:00:00:01", |
| 130 | "00:00:00:00:00:00:00:03", |
| 131 | "00:00:00:00:00:00:00:04")); |
| 132 | } |
| 133 | |
| 134 | /** |
Pavlin Radoslavov | 308337c | 2014-06-11 10:25:44 -0700 | [diff] [blame] | 135 | * Check that the JSON array returned for the hosts element matches |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 136 | * the data in the mocked topology. |
| 137 | * |
Pavlin Radoslavov | 308337c | 2014-06-11 10:25:44 -0700 | [diff] [blame] | 138 | * @param hosts JSON array of hosts |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 139 | */ |
Pavlin Radoslavov | 308337c | 2014-06-11 10:25:44 -0700 | [diff] [blame] | 140 | private void checkHosts(final JSONArray hosts) { |
| 141 | // hosts array should be empty |
| 142 | assertThat(hosts.length(), is(equalTo(0))); |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Test that the GET of all Topology REST call returns the proper result. |
| 147 | * The call to get all Topology should return 3 items (switches, links, |
Pavlin Radoslavov | 308337c | 2014-06-11 10:25:44 -0700 | [diff] [blame] | 148 | * and hosts), an HTTP status of OK, and the proper topology data. |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 149 | */ |
| 150 | @Test |
| 151 | public void testFetchOfAllTopology() throws Exception { |
| 152 | final ClientResource client = new ClientResource(getBaseRestTopologyUrl()); |
| 153 | final JSONObject topology = getJSONObject(client); |
| 154 | |
| 155 | // HTTP status should be OK |
| 156 | assertThat(client, hasStatusOf(Status.SUCCESS_OK)); |
| 157 | |
| 158 | // Check the number of top level members in the topology object |
| 159 | assertThat(topology.length(), is(equalTo(3))); |
| 160 | |
| 161 | // Check the switches element |
| 162 | final JSONArray switches = topology.getJSONArray("switches"); |
| 163 | checkSwitches(switches); |
| 164 | |
| 165 | // Check the values in the links array |
| 166 | final JSONArray links = topology.getJSONArray("links"); |
| 167 | checkLinks(links); |
| 168 | |
Pavlin Radoslavov | 308337c | 2014-06-11 10:25:44 -0700 | [diff] [blame] | 169 | // Check the hosts array |
| 170 | final JSONArray hosts = topology.getJSONArray("hosts"); |
| 171 | checkHosts(hosts); |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Test that the GET of all switches REST call returns the proper result. |
| 176 | * The call to get all switches should return the correct switch data. |
| 177 | */ |
| 178 | @Test |
| 179 | public void testFetchOfAllSwitches() throws Exception { |
| 180 | final ClientResource client = new ClientResource(getBaseRestTopologyUrl() + "/switches"); |
| 181 | final JSONArray switches = getJSONArray(client); |
| 182 | |
| 183 | // HTTP status should be OK |
| 184 | assertThat(client, hasStatusOf(Status.SUCCESS_OK)); |
| 185 | |
| 186 | checkSwitches(switches); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Test that the GET of all links REST call returns the proper result. |
| 191 | * The call to get all links should return the proper link data. |
| 192 | */ |
| 193 | @Test |
| 194 | public void testFetchOfAllLinks() throws Exception { |
| 195 | final ClientResource client = new ClientResource(getBaseRestTopologyUrl() + "/links"); |
| 196 | final JSONArray links = getJSONArray(client); |
| 197 | |
| 198 | // HTTP status should be OK |
| 199 | assertThat(client, hasStatusOf(Status.SUCCESS_OK)); |
| 200 | |
| 201 | checkLinks(links); |
| 202 | } |
| 203 | |
| 204 | /** |
Pavlin Radoslavov | 308337c | 2014-06-11 10:25:44 -0700 | [diff] [blame] | 205 | * Test that the GET of all hosts REST call returns the proper result. |
| 206 | * The call to get all hosts should return no hosts. |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 207 | */ |
| 208 | @Test |
Pavlin Radoslavov | 308337c | 2014-06-11 10:25:44 -0700 | [diff] [blame] | 209 | public void testFetchOfAllHosts() throws Exception { |
| 210 | final ClientResource client = new ClientResource(getBaseRestTopologyUrl() + "/hosts"); |
| 211 | final JSONArray hosts = getJSONArray(client); |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 212 | |
| 213 | // HTTP status should be OK |
| 214 | assertThat(client, hasStatusOf(Status.SUCCESS_OK)); |
| 215 | |
Pavlin Radoslavov | 308337c | 2014-06-11 10:25:44 -0700 | [diff] [blame] | 216 | checkHosts(hosts); |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 217 | } |
| 218 | } |