blob: edad41da3a6e2d6c40269768a71de30432032923 [file] [log] [blame]
Ray Milkey0fe43852014-06-05 14:41:46 -07001package net.onrc.onos.api.rest;
2
3import net.onrc.onos.core.intent.runtime.PathCalcRuntimeModule;
4import org.json.JSONArray;
5import org.json.JSONException;
6import org.json.JSONObject;
7import org.junit.After;
8import org.junit.Before;
9import org.junit.Test;
10import org.junit.runner.RunWith;
11import org.powermock.core.classloader.annotations.PrepareForTest;
12import org.powermock.modules.junit4.PowerMockRunner;
13import org.restlet.data.Status;
14import org.restlet.resource.ClientResource;
15
16import java.util.HashSet;
17import java.util.Set;
18
19import static net.onrc.onos.api.rest.ClientResourceStatusMatcher.hasStatusOf;
20import static org.hamcrest.MatcherAssert.assertThat;
21import static org.hamcrest.Matchers.equalTo;
22import static org.hamcrest.Matchers.hasItems;
23import static org.hamcrest.Matchers.is;
24import static org.hamcrest.Matchers.notNullValue;
25
26/**
27 * Tests for topology REST get operations.
28 */
29@RunWith(PowerMockRunner.class)
30@PrepareForTest(PathCalcRuntimeModule.class)
31public 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);
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -0700104 final JSONObject src = link.getJSONObject("src");
Ray Milkey0fe43852014-06-05 14:41:46 -0700105 assertThat(src, is(notNullValue()));
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -0700106 final JSONObject dst = link.getJSONObject("dst");
Ray Milkey0fe43852014-06-05 14:41:46 -0700107 assertThat(dst, is(notNullValue()));
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -0700108 final String srcDpid = src.getString("dpid");
109 final String dstDpid = dst.getString("dpid");
110 assertThat(srcDpid, is(notNullValue()));
111 assertThat(dstDpid, is(notNullValue()));
Ray Milkey0fe43852014-06-05 14:41:46 -0700112
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -0700113 if (srcDpid.equals("00:00:00:00:00:00:00:02")) {
114 toLinks.add(dstDpid);
Ray Milkey0fe43852014-06-05 14:41:46 -0700115 }
116
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -0700117 if (dstDpid.equals("00:00:00:00:00:00:00:02")) {
118 fromLinks.add(srcDpid);
Ray Milkey0fe43852014-06-05 14:41:46 -0700119 }
120 }
121
122 assertThat(toLinks, 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 assertThat(fromLinks, hasItems("00:00:00:00:00:00:00:01",
127 "00:00:00:00:00:00:00:03",
128 "00:00:00:00:00:00:00:04"));
129 }
130
131 /**
Pavlin Radoslavov308337c2014-06-11 10:25:44 -0700132 * Check that the JSON array returned for the hosts element matches
Ray Milkey0fe43852014-06-05 14:41:46 -0700133 * the data in the mocked topology.
134 *
Pavlin Radoslavov308337c2014-06-11 10:25:44 -0700135 * @param hosts JSON array of hosts
Ray Milkey0fe43852014-06-05 14:41:46 -0700136 */
Pavlin Radoslavov308337c2014-06-11 10:25:44 -0700137 private void checkHosts(final JSONArray hosts) {
138 // hosts array should be empty
139 assertThat(hosts.length(), is(equalTo(0)));
Ray Milkey0fe43852014-06-05 14:41:46 -0700140 }
141
142 /**
143 * Test that the GET of all Topology REST call returns the proper result.
144 * The call to get all Topology should return 3 items (switches, links,
Pavlin Radoslavov308337c2014-06-11 10:25:44 -0700145 * and hosts), an HTTP status of OK, and the proper topology data.
Ray Milkey0fe43852014-06-05 14:41:46 -0700146 */
147 @Test
148 public void testFetchOfAllTopology() throws Exception {
149 final ClientResource client = new ClientResource(getBaseRestTopologyUrl());
150 final JSONObject topology = getJSONObject(client);
151
152 // HTTP status should be OK
153 assertThat(client, hasStatusOf(Status.SUCCESS_OK));
154
155 // Check the number of top level members in the topology object
156 assertThat(topology.length(), is(equalTo(3)));
157
158 // Check the switches element
159 final JSONArray switches = topology.getJSONArray("switches");
160 checkSwitches(switches);
161
162 // Check the values in the links array
163 final JSONArray links = topology.getJSONArray("links");
164 checkLinks(links);
165
Pavlin Radoslavov308337c2014-06-11 10:25:44 -0700166 // Check the hosts array
167 final JSONArray hosts = topology.getJSONArray("hosts");
168 checkHosts(hosts);
Ray Milkey0fe43852014-06-05 14:41:46 -0700169 }
170
171 /**
172 * Test that the GET of all switches REST call returns the proper result.
173 * The call to get all switches should return the correct switch data.
174 */
175 @Test
176 public void testFetchOfAllSwitches() throws Exception {
177 final ClientResource client = new ClientResource(getBaseRestTopologyUrl() + "/switches");
178 final JSONArray switches = getJSONArray(client);
179
180 // HTTP status should be OK
181 assertThat(client, hasStatusOf(Status.SUCCESS_OK));
182
183 checkSwitches(switches);
184 }
185
186 /**
187 * Test that the GET of all links REST call returns the proper result.
188 * The call to get all links should return the proper link data.
189 */
190 @Test
191 public void testFetchOfAllLinks() throws Exception {
192 final ClientResource client = new ClientResource(getBaseRestTopologyUrl() + "/links");
193 final JSONArray links = getJSONArray(client);
194
195 // HTTP status should be OK
196 assertThat(client, hasStatusOf(Status.SUCCESS_OK));
197
198 checkLinks(links);
199 }
200
201 /**
Pavlin Radoslavov308337c2014-06-11 10:25:44 -0700202 * Test that the GET of all hosts REST call returns the proper result.
203 * The call to get all hosts should return no hosts.
Ray Milkey0fe43852014-06-05 14:41:46 -0700204 */
205 @Test
Pavlin Radoslavov308337c2014-06-11 10:25:44 -0700206 public void testFetchOfAllHosts() throws Exception {
207 final ClientResource client = new ClientResource(getBaseRestTopologyUrl() + "/hosts");
208 final JSONArray hosts = getJSONArray(client);
Ray Milkey0fe43852014-06-05 14:41:46 -0700209
210 // HTTP status should be OK
211 assertThat(client, hasStatusOf(Status.SUCCESS_OK));
212
Pavlin Radoslavov308337c2014-06-11 10:25:44 -0700213 checkHosts(hosts);
Ray Milkey0fe43852014-06-05 14:41:46 -0700214 }
215}