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