blob: 35ed2ec2016b447ba9ce22e048d49c990c663680 [file] [log] [blame]
Ray Milkey0fe43852014-06-05 14:41:46 -07001package net.onrc.onos.api.rest;
2
Ray Milkey38301352014-07-28 08:51:54 -07003import com.google.common.collect.ImmutableList;
Ray Milkey0fe43852014-06-05 14:41:46 -07004import net.onrc.onos.core.intent.runtime.PathCalcRuntimeModule;
5import org.json.JSONArray;
6import org.json.JSONException;
7import org.json.JSONObject;
Ray Milkey0fe43852014-06-05 14:41:46 -07008import org.junit.Test;
9import org.junit.runner.RunWith;
10import org.powermock.core.classloader.annotations.PrepareForTest;
11import org.powermock.modules.junit4.PowerMockRunner;
12import org.restlet.data.Status;
13import org.restlet.resource.ClientResource;
14
15import java.util.HashSet;
Yuta HIGUCHId830aad2014-07-06 15:02:01 -070016import java.util.List;
Ray Milkey0fe43852014-06-05 14:41:46 -070017import java.util.Set;
18
19import static net.onrc.onos.api.rest.ClientResourceStatusMatcher.hasStatusOf;
20import static org.hamcrest.MatcherAssert.assertThat;
Ray Milkey38301352014-07-28 08:51:54 -070021import static org.hamcrest.Matchers.equalTo;
22import static org.hamcrest.Matchers.hasItems;
23import static org.hamcrest.Matchers.is;
24import static org.hamcrest.Matchers.notNullValue;
Ray Milkey0fe43852014-06-05 14:41:46 -070025
26/**
27 * Tests for topology REST get operations.
28 */
29@RunWith(PowerMockRunner.class)
30@PrepareForTest(PathCalcRuntimeModule.class)
31public class TestRestTopologyGet extends TestRestTopology {
32
33 /**
Ray Milkey0fe43852014-06-05 14:41:46 -070034 * 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 HIGUCHId830aad2014-07-06 15:02:01 -070040 @SuppressWarnings("unchecked")
Ray Milkey0fe43852014-06-05 14:41:46 -070041 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 HIGUCHId830aad2014-07-06 15:02:01 -070046 final List<String> keys = ImmutableList.<String>copyOf(switch0.keys());
47 assertThat(keys,
48 hasItems("dpid", "state", "ports"));
Ray Milkey0fe43852014-06-05 14:41:46 -070049 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 Radoslavov5cf1fe02014-07-03 22:52:25 -070085 final JSONObject src = link.getJSONObject("src");
Ray Milkey0fe43852014-06-05 14:41:46 -070086 assertThat(src, is(notNullValue()));
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070087 final JSONObject dst = link.getJSONObject("dst");
Ray Milkey0fe43852014-06-05 14:41:46 -070088 assertThat(dst, is(notNullValue()));
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070089 final String srcDpid = src.getString("dpid");
90 final String dstDpid = dst.getString("dpid");
91 assertThat(srcDpid, is(notNullValue()));
92 assertThat(dstDpid, is(notNullValue()));
Ray Milkey0fe43852014-06-05 14:41:46 -070093
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070094 if (srcDpid.equals("00:00:00:00:00:00:00:02")) {
95 toLinks.add(dstDpid);
Ray Milkey0fe43852014-06-05 14:41:46 -070096 }
97
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070098 if (dstDpid.equals("00:00:00:00:00:00:00:02")) {
99 fromLinks.add(srcDpid);
Ray Milkey0fe43852014-06-05 14:41:46 -0700100 }
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 Radoslavov308337c2014-06-11 10:25:44 -0700113 * Check that the JSON array returned for the hosts element matches
Ray Milkey0fe43852014-06-05 14:41:46 -0700114 * the data in the mocked topology.
115 *
Pavlin Radoslavov308337c2014-06-11 10:25:44 -0700116 * @param hosts JSON array of hosts
Ray Milkey0fe43852014-06-05 14:41:46 -0700117 */
Pavlin Radoslavov308337c2014-06-11 10:25:44 -0700118 private void checkHosts(final JSONArray hosts) {
119 // hosts array should be empty
120 assertThat(hosts.length(), is(equalTo(0)));
Ray Milkey0fe43852014-06-05 14:41:46 -0700121 }
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 Radoslavov308337c2014-06-11 10:25:44 -0700126 * and hosts), an HTTP status of OK, and the proper topology data.
Ray Milkey40eb9c82014-07-18 10:28:11 -0700127 *
128 * @throws JSONException if JSON processing fails
Ray Milkey0fe43852014-06-05 14:41:46 -0700129 */
130 @Test
Ray Milkey40eb9c82014-07-18 10:28:11 -0700131 public void testFetchOfAllTopology() throws JSONException {
Ray Milkey0fe43852014-06-05 14:41:46 -0700132 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 Radoslavov308337c2014-06-11 10:25:44 -0700149 // Check the hosts array
150 final JSONArray hosts = topology.getJSONArray("hosts");
151 checkHosts(hosts);
Ray Milkey0fe43852014-06-05 14:41:46 -0700152 }
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 Milkey40eb9c82014-07-18 10:28:11 -0700157 *
158 * @throws JSONException if JSON processing fails
Ray Milkey0fe43852014-06-05 14:41:46 -0700159 */
160 @Test
Ray Milkey40eb9c82014-07-18 10:28:11 -0700161 public void testFetchOfAllSwitches() throws JSONException {
Ray Milkey0fe43852014-06-05 14:41:46 -0700162 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 Milkey40eb9c82014-07-18 10:28:11 -0700174 *
175 * @throws JSONException if JSON processing fails
Ray Milkey0fe43852014-06-05 14:41:46 -0700176 */
177 @Test
Ray Milkey40eb9c82014-07-18 10:28:11 -0700178 public void testFetchOfAllLinks() throws JSONException {
Ray Milkey0fe43852014-06-05 14:41:46 -0700179 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 Radoslavov308337c2014-06-11 10:25:44 -0700189 * 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 Milkey40eb9c82014-07-18 10:28:11 -0700191 *
192 * @throws JSONException if JSON processing fails
Ray Milkey0fe43852014-06-05 14:41:46 -0700193 */
194 @Test
Ray Milkey40eb9c82014-07-18 10:28:11 -0700195 public void testFetchOfAllHosts() throws JSONException {
Pavlin Radoslavov308337c2014-06-11 10:25:44 -0700196 final ClientResource client = new ClientResource(getBaseRestTopologyUrl() + "/hosts");
197 final JSONArray hosts = getJSONArray(client);
Ray Milkey0fe43852014-06-05 14:41:46 -0700198
199 // HTTP status should be OK
200 assertThat(client, hasStatusOf(Status.SUCCESS_OK));
201
Pavlin Radoslavov308337c2014-06-11 10:25:44 -0700202 checkHosts(hosts);
Ray Milkey0fe43852014-06-05 14:41:46 -0700203 }
204}