blob: b7b8d65f777b87b23f0b6767d3180333dd5fdba9 [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);
104 final String src = link.getString("src-switch");
105 final String dst = link.getString("dst-switch");
106 assertThat(src, is(notNullValue()));
107 assertThat(dst, is(notNullValue()));
108
109 if (src.equals("00:00:00:00:00:00:00:02")) {
110 toLinks.add(dst);
111 }
112
113 if (dst.equals("00:00:00:00:00:00:00:02")) {
114 fromLinks.add(src);
115 }
116 }
117
118 assertThat(toLinks, hasItems("00:00:00:00:00:00:00:01",
119 "00:00:00:00:00:00:00:03",
120 "00:00:00:00:00:00:00:04"));
121
122 assertThat(fromLinks, 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
127 /**
Pavlin Radoslavov308337c2014-06-11 10:25:44 -0700128 * Check that the JSON array returned for the hosts element matches
Ray Milkey0fe43852014-06-05 14:41:46 -0700129 * the data in the mocked topology.
130 *
Pavlin Radoslavov308337c2014-06-11 10:25:44 -0700131 * @param hosts JSON array of hosts
Ray Milkey0fe43852014-06-05 14:41:46 -0700132 */
Pavlin Radoslavov308337c2014-06-11 10:25:44 -0700133 private void checkHosts(final JSONArray hosts) {
134 // hosts array should be empty
135 assertThat(hosts.length(), is(equalTo(0)));
Ray Milkey0fe43852014-06-05 14:41:46 -0700136 }
137
138 /**
139 * Test that the GET of all Topology REST call returns the proper result.
140 * The call to get all Topology should return 3 items (switches, links,
Pavlin Radoslavov308337c2014-06-11 10:25:44 -0700141 * and hosts), an HTTP status of OK, and the proper topology data.
Ray Milkey0fe43852014-06-05 14:41:46 -0700142 */
143 @Test
144 public void testFetchOfAllTopology() throws Exception {
145 final ClientResource client = new ClientResource(getBaseRestTopologyUrl());
146 final JSONObject topology = getJSONObject(client);
147
148 // HTTP status should be OK
149 assertThat(client, hasStatusOf(Status.SUCCESS_OK));
150
151 // Check the number of top level members in the topology object
152 assertThat(topology.length(), is(equalTo(3)));
153
154 // Check the switches element
155 final JSONArray switches = topology.getJSONArray("switches");
156 checkSwitches(switches);
157
158 // Check the values in the links array
159 final JSONArray links = topology.getJSONArray("links");
160 checkLinks(links);
161
Pavlin Radoslavov308337c2014-06-11 10:25:44 -0700162 // Check the hosts array
163 final JSONArray hosts = topology.getJSONArray("hosts");
164 checkHosts(hosts);
Ray Milkey0fe43852014-06-05 14:41:46 -0700165 }
166
167 /**
168 * Test that the GET of all switches REST call returns the proper result.
169 * The call to get all switches should return the correct switch data.
170 */
171 @Test
172 public void testFetchOfAllSwitches() throws Exception {
173 final ClientResource client = new ClientResource(getBaseRestTopologyUrl() + "/switches");
174 final JSONArray switches = getJSONArray(client);
175
176 // HTTP status should be OK
177 assertThat(client, hasStatusOf(Status.SUCCESS_OK));
178
179 checkSwitches(switches);
180 }
181
182 /**
183 * Test that the GET of all links REST call returns the proper result.
184 * The call to get all links should return the proper link data.
185 */
186 @Test
187 public void testFetchOfAllLinks() throws Exception {
188 final ClientResource client = new ClientResource(getBaseRestTopologyUrl() + "/links");
189 final JSONArray links = getJSONArray(client);
190
191 // HTTP status should be OK
192 assertThat(client, hasStatusOf(Status.SUCCESS_OK));
193
194 checkLinks(links);
195 }
196
197 /**
Pavlin Radoslavov308337c2014-06-11 10:25:44 -0700198 * Test that the GET of all hosts REST call returns the proper result.
199 * The call to get all hosts should return no hosts.
Ray Milkey0fe43852014-06-05 14:41:46 -0700200 */
201 @Test
Pavlin Radoslavov308337c2014-06-11 10:25:44 -0700202 public void testFetchOfAllHosts() throws Exception {
203 final ClientResource client = new ClientResource(getBaseRestTopologyUrl() + "/hosts");
204 final JSONArray hosts = getJSONArray(client);
Ray Milkey0fe43852014-06-05 14:41:46 -0700205
206 // HTTP status should be OK
207 assertThat(client, hasStatusOf(Status.SUCCESS_OK));
208
Pavlin Radoslavov308337c2014-06-11 10:25:44 -0700209 checkHosts(hosts);
Ray Milkey0fe43852014-06-05 14:41:46 -0700210 }
211}