blob: bf8d831f9cadd756390eb4e02f00cb2379f218a7 [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;
Yuta HIGUCHId830aad2014-07-06 15:02:01 -07004
Ray Milkey0fe43852014-06-05 14:41:46 -07005import org.json.JSONArray;
6import org.json.JSONException;
7import org.json.JSONObject;
8import org.junit.After;
9import org.junit.Before;
10import org.junit.Test;
11import org.junit.runner.RunWith;
12import org.powermock.core.classloader.annotations.PrepareForTest;
13import org.powermock.modules.junit4.PowerMockRunner;
14import org.restlet.data.Status;
15import org.restlet.resource.ClientResource;
16
Yuta HIGUCHId830aad2014-07-06 15:02:01 -070017import com.google.common.collect.ImmutableList;
Ray Milkey0fe43852014-06-05 14:41:46 -070018import java.util.HashSet;
Yuta HIGUCHId830aad2014-07-06 15:02:01 -070019import java.util.List;
Ray Milkey0fe43852014-06-05 14:41:46 -070020import java.util.Set;
21
22import static net.onrc.onos.api.rest.ClientResourceStatusMatcher.hasStatusOf;
23import static org.hamcrest.MatcherAssert.assertThat;
Yuta HIGUCHId830aad2014-07-06 15:02:01 -070024import static org.hamcrest.Matchers.*;
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 /**
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 HIGUCHId830aad2014-07-06 15:02:01 -070062 @SuppressWarnings("unchecked")
Ray Milkey0fe43852014-06-05 14:41:46 -070063 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 HIGUCHId830aad2014-07-06 15:02:01 -070068 final List<String> keys = ImmutableList.<String>copyOf(switch0.keys());
69 assertThat(keys,
70 hasItems("dpid", "state", "ports"));
Ray Milkey0fe43852014-06-05 14:41:46 -070071 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 Radoslavov5cf1fe02014-07-03 22:52:25 -0700107 final JSONObject src = link.getJSONObject("src");
Ray Milkey0fe43852014-06-05 14:41:46 -0700108 assertThat(src, is(notNullValue()));
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -0700109 final JSONObject dst = link.getJSONObject("dst");
Ray Milkey0fe43852014-06-05 14:41:46 -0700110 assertThat(dst, is(notNullValue()));
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -0700111 final String srcDpid = src.getString("dpid");
112 final String dstDpid = dst.getString("dpid");
113 assertThat(srcDpid, is(notNullValue()));
114 assertThat(dstDpid, is(notNullValue()));
Ray Milkey0fe43852014-06-05 14:41:46 -0700115
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -0700116 if (srcDpid.equals("00:00:00:00:00:00:00:02")) {
117 toLinks.add(dstDpid);
Ray Milkey0fe43852014-06-05 14:41:46 -0700118 }
119
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -0700120 if (dstDpid.equals("00:00:00:00:00:00:00:02")) {
121 fromLinks.add(srcDpid);
Ray Milkey0fe43852014-06-05 14:41:46 -0700122 }
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 Radoslavov308337c2014-06-11 10:25:44 -0700135 * Check that the JSON array returned for the hosts element matches
Ray Milkey0fe43852014-06-05 14:41:46 -0700136 * the data in the mocked topology.
137 *
Pavlin Radoslavov308337c2014-06-11 10:25:44 -0700138 * @param hosts JSON array of hosts
Ray Milkey0fe43852014-06-05 14:41:46 -0700139 */
Pavlin Radoslavov308337c2014-06-11 10:25:44 -0700140 private void checkHosts(final JSONArray hosts) {
141 // hosts array should be empty
142 assertThat(hosts.length(), is(equalTo(0)));
Ray Milkey0fe43852014-06-05 14:41:46 -0700143 }
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 Radoslavov308337c2014-06-11 10:25:44 -0700148 * and hosts), an HTTP status of OK, and the proper topology data.
Ray Milkey0fe43852014-06-05 14:41:46 -0700149 */
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 Radoslavov308337c2014-06-11 10:25:44 -0700169 // Check the hosts array
170 final JSONArray hosts = topology.getJSONArray("hosts");
171 checkHosts(hosts);
Ray Milkey0fe43852014-06-05 14:41:46 -0700172 }
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 Radoslavov308337c2014-06-11 10:25:44 -0700205 * 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 Milkey0fe43852014-06-05 14:41:46 -0700207 */
208 @Test
Pavlin Radoslavov308337c2014-06-11 10:25:44 -0700209 public void testFetchOfAllHosts() throws Exception {
210 final ClientResource client = new ClientResource(getBaseRestTopologyUrl() + "/hosts");
211 final JSONArray hosts = getJSONArray(client);
Ray Milkey0fe43852014-06-05 14:41:46 -0700212
213 // HTTP status should be OK
214 assertThat(client, hasStatusOf(Status.SUCCESS_OK));
215
Pavlin Radoslavov308337c2014-06-11 10:25:44 -0700216 checkHosts(hosts);
Ray Milkey0fe43852014-06-05 14:41:46 -0700217 }
218}