blob: 026a9bc2ccb5c02a34c4e92fc538bd34740d60a9 [file] [log] [blame]
Jovana Vuletafe32db7d2017-05-01 12:18:00 +02001/*
2 * Copyright 2017-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.ofagent.rest;
18
19import com.eclipsesource.json.Json;
20import com.eclipsesource.json.JsonObject;
21import com.google.common.collect.Sets;
22import org.glassfish.jersey.server.ResourceConfig;
23import org.glassfish.jersey.test.JerseyTest;
24import org.junit.After;
25import org.junit.Before;
26import org.junit.Test;
27import org.onlab.osgi.ServiceDirectory;
28import org.onlab.osgi.TestServiceDirectory;
29import org.onlab.packet.IpAddress;
30import org.onlab.packet.TpPort;
31import org.onlab.rest.BaseResource;
32import org.onosproject.incubator.net.virtual.NetworkId;
33import org.onosproject.ofagent.api.OFAgent;
34import org.onosproject.ofagent.api.OFAgentAdminService;
35import org.onosproject.ofagent.api.OFAgentService;
36import org.onosproject.ofagent.api.OFController;
37import org.onosproject.ofagent.impl.DefaultOFAgent;
38import org.onosproject.ofagent.impl.DefaultOFController;
39
40import javax.ws.rs.client.Entity;
41import javax.ws.rs.client.WebTarget;
42import javax.ws.rs.core.MediaType;
43import javax.ws.rs.core.Response;
44import java.io.IOException;
45import java.io.InputStream;
46import java.net.HttpURLConnection;
47import java.util.Set;
48
49import static org.easymock.EasyMock.*;
50import static org.hamcrest.Matchers.*;
51import static org.junit.Assert.assertNotNull;
52import static org.junit.Assert.assertThat;
53import static org.onosproject.ofagent.api.OFAgent.State.STOPPED;
54
55
56/**
57 * Test class for OFAgent application REST resource.
58 */
59public class OFAgentWebResourceTest extends JerseyTest {
60
61
62 private static final Set<OFController> CONTROLLER_SET_1 = Sets.newHashSet(
63 DefaultOFController.of(
64 IpAddress.valueOf("147.91.1.4"),
65 TpPort.tpPort(6633)));
66
67 private static final Set<OFController> CONTROLLER_SET_2 = Sets.newHashSet(
68 DefaultOFController.of(
69 IpAddress.valueOf("147.91.4.25"),
70 TpPort.tpPort(6633)),
71 DefaultOFController.of(
72 IpAddress.valueOf("147.91.4.27"),
73 TpPort.tpPort(6653)));
74
75 private static final Set<OFController> CONTROLLER_SET = Sets.newHashSet(
76 DefaultOFController.of(
77 IpAddress.valueOf("147.91.2.11"),
78 TpPort.tpPort(6633)),
79 DefaultOFController.of(
80 IpAddress.valueOf("147.91.2.9"),
81 TpPort.tpPort(6633)),
82 DefaultOFController.of(
83 IpAddress.valueOf("147.91.2.17"),
84 TpPort.tpPort(6653)));
85
86 private static final NetworkId NETWORK_1 = NetworkId.networkId(1);
87 private static final NetworkId NETWORK_2 = NetworkId.networkId(2);
88 private static final NetworkId NETWORK = NetworkId.networkId(3);
89
90 private static final OFAgent OF_AGENT = DefaultOFAgent.builder()
91 .networkId(NETWORK)
92 .controllers(CONTROLLER_SET)
93 .state(STOPPED)
94 .build();
95
96 private Set<OFAgent> agents = Sets.newHashSet(DefaultOFAgent.builder()
97 .networkId(NETWORK_1)
98 .controllers(CONTROLLER_SET_1)
99 .state(STOPPED)
100 .build(),
101 DefaultOFAgent.builder()
102 .networkId(NETWORK_2)
103 .controllers(CONTROLLER_SET_2)
104 .state(STOPPED)
105 .build(),
106 OF_AGENT);
107
108 private Set<OFAgent> empty = Sets.newHashSet();
109
110 private final OFAgentAdminService mockOFAgentAdminService = createMock(OFAgentAdminService.class);
111 private final OFAgentService mockOFAgentService = createMock(OFAgentService.class);
112
113 /**
114 * Constructs OFAgent Web application test instance.
115 */
116 public OFAgentWebResourceTest() {
117 super(ResourceConfig.forApplicationClass(OFAgentWebApplication.class));
118 }
119
120 /**
121 * Sets up the global values for all the tests.
122 */
123 @Before
124 public void setUpMocks() {
125 ServiceDirectory testDirectory = new TestServiceDirectory()
126 .add(OFAgentAdminService.class, mockOFAgentAdminService)
127 .add(OFAgentService.class, mockOFAgentService);
128 BaseResource.setServiceDirectory(testDirectory);
129 }
130
131 /**
132 * Cleans up.
133 */
134 @After
135 public void tearDownMocks() {
136 }
137
138 /**
139 * Tests the result of the rest api GET when there are OFAgents.
140 *
141 * @throws IOException
142 */
143 @Test
144 public void testNonEmptyOFAgentSet() throws IOException {
145 expect(mockOFAgentService.agents()).andReturn(agents).anyTimes();
146 replay(mockOFAgentService);
147
148 final WebTarget wt = target();
149 assertNotNull("WebTarget is null", wt);
150 assertNotNull("WebTarget request is null", wt.request());
151 final String response = wt.path("service/ofagents").request().get(String.class);
152 final JsonObject result = Json.parse(response).asObject();
153 assertThat(result, notNullValue());
154 assertThat(result.names(), hasSize(1));
155 assertThat(result.names().get(0), is("ofAgents"));
156
157 mockOFAgentService.agents().forEach(ofAgent -> {
158
159 String expectedJsonStringNetworkId = "\"networkId\":\"" + ofAgent.networkId().id() + "\"";
160 assertThat(response, containsString(expectedJsonStringNetworkId));
161
162 String expectedJsonStringState = "\"state\":\"" + ofAgent.state() + "\"";
163 assertThat(response, containsString(expectedJsonStringState));
164
165 ofAgent.controllers().forEach(ofController -> {
166 String expectedJsonStringIP = "\"ip\":\"" + ofController.ip() + "\"";
167 assertThat(response, containsString(expectedJsonStringIP));
168
169 String expectedJsonStringPort = "\"port\":\"" + ofController.port() + "\"";
170 assertThat(response, containsString(expectedJsonStringPort));
171 });
172 });
173
174 verify(mockOFAgentService);
175 }
176
177 /**
178 * Tests the result of the rest api GET when there are no OFAgents.
179 *
180 * @throws IOException
181 */
182 @Test
183 public void testEmptyOFAgentSet() throws IOException {
184 expect(mockOFAgentService.agents()).andReturn(empty).anyTimes();
185 replay(mockOFAgentService);
186
187 final WebTarget wt = target();
188 assertNotNull("WebTarget is null", wt);
189 assertNotNull("WebTarget request is null", wt.request());
190 final String response = wt.path("service/ofagents").request().get(String.class);
191 final JsonObject result = Json.parse(response).asObject();
192 assertThat(result, notNullValue());
193 assertThat(result.names(), hasSize(1));
194 assertThat(response, is("{\"ofAgents\":[]}"));
195
196 verify(mockOFAgentService);
197 }
198
199 /**
200 * Tests the result of the rest api GET for OFAgent.
201 *
202 * @throws IOException
203 */
204 @Test
205 public void testOFAgent() throws IOException {
206 expect(mockOFAgentService.agent(eq(NETWORK))).andReturn(OF_AGENT).anyTimes();
207 replay(mockOFAgentService);
208
209 final WebTarget wt = target();
210 assertNotNull("WebTarget is null", wt);
211 assertNotNull("WebTarget request is null", wt.request());
212 final Response response = wt.path("service/ofagent/" + NETWORK).request().get();
213 final JsonObject result = Json.parse(response.readEntity(String.class)).asObject();
214 assertThat(result, notNullValue());
215 assertThat(result.names(), hasSize(3));
216 assertThat(result.get("networkId").asString(), is(NETWORK.id().toString()));
217 assertThat(result.get("state").asString(), is(STOPPED.toString()));
218
219 verify(mockOFAgentService);
220 }
221
222
223 /**
224 * Tests the result of the rest api GET for non-existent OFAgent.
225 *
226 * @throws IOException
227 */
228 @Test
229 public void testNonExistentOFAgent() throws IOException {
230 expect(mockOFAgentService.agent(anyObject())).andReturn(null).anyTimes();
231 replay(mockOFAgentService);
232
233 final WebTarget wt = target();
234 assertNotNull("WebTarget is null", wt);
235 assertNotNull("WebTarget request is null", wt.request());
236 final Response response = wt.path("service/ofagent/" + NETWORK_1).request().get();
237 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_NOT_FOUND));
238
239 verify(mockOFAgentService);
240 }
241
242
243 /**
244 * Tests creating an OFAgent with POST.
245 */
246 @Test
247 public void testOFAgentCreate() {
248 mockOFAgentAdminService.createAgent(anyObject());
249 expectLastCall().anyTimes();
250 replay(mockOFAgentAdminService);
251
252
253 InputStream jsonStream = OFAgentWebResourceTest.class
254 .getResourceAsStream("post-ofagent-create.json");
255 assertNotNull("post-ofagent-create.json is null", jsonStream);
256 WebTarget wt = target();
257
258 Response response = wt.path("service/ofagent-create")
259 .request(MediaType.APPLICATION_JSON_TYPE)
260 .post(Entity.json(jsonStream));
261 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED));
262
263 verify(mockOFAgentAdminService);
264 }
265
266 /**
267 * Tests creating an OFAgent with bad POST request.
268 */
269 @Test
270 public void testOFAgentCreateBadRequest() {
271 InputStream jsonStream = OFAgentWebResourceTest.class
272 .getResourceAsStream("post-bad-request.json");
273 assertNotNull("post-bad-request.json is null", jsonStream);
274 WebTarget wt = target();
275
276 Response response = wt.path("service/ofagent-create")
277 .request(MediaType.APPLICATION_JSON_TYPE)
278 .post(Entity.json(jsonStream));
279 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_INTERNAL_ERROR));
280 }
281
282 /**
283 * Tests updating an OFAgent with PUT.
284 */
285 @Test
286 public void testOFAgentUpdate() {
287 expect(mockOFAgentService.agent(eq(NETWORK))).andReturn(OF_AGENT).anyTimes();
288 replay(mockOFAgentService);
289
290 mockOFAgentAdminService.updateAgent(anyObject());
291 expectLastCall().anyTimes();
292 replay(mockOFAgentAdminService);
293
294 InputStream jsonStream = OFAgentWebResourceTest.class
295 .getResourceAsStream("put-ofagent-update.json");
296 assertNotNull("put-ofagent-update.json is null", jsonStream);
297 WebTarget wt = target();
298 Response response = wt.path("service/ofagent-update")
299 .request(MediaType.APPLICATION_JSON_TYPE)
300 .put(Entity.json(jsonStream));
301 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
302 assertThat(response.readEntity(String.class), containsString("OFAgent updated"));
303
304 verify(mockOFAgentService);
305 verify(mockOFAgentAdminService);
306
307 }
308
309 /**
310 * Tests non-existent OFAgent updating with PUT.
311 */
312 @Test
313 public void testNonExistentOFAgentUpdate() {
314 expect(mockOFAgentService.agent(anyObject())).andReturn(null).anyTimes();
315 replay(mockOFAgentService);
316
317 InputStream jsonStream = OFAgentWebResourceTest.class
318 .getResourceAsStream("put-non-existent-ofagent-update.json");
319 assertNotNull("put-non-existent-ofagent-update.json is null", jsonStream);
320 WebTarget wt = target();
321 Response response = wt.path("service/ofagent-update")
322 .request(MediaType.APPLICATION_JSON_TYPE)
323 .put(Entity.json(jsonStream));
324 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_NOT_FOUND));
325
326 verify(mockOFAgentService);
327
328 }
329
330 /**
331 * Tests OFAgent updating with bad PUT request.
332 */
333 @Test
334 public void testOFAgentUpdateBadRequest() {
335 expect(mockOFAgentService.agent(anyObject())).andReturn(null).anyTimes();
336 replay(mockOFAgentService);
337
338 InputStream jsonStream = OFAgentWebResourceTest.class
339 .getResourceAsStream("put-bad-request.json");
340 assertNotNull("put-bad-request.json is null", jsonStream);
341 WebTarget wt = target();
342 Response response = wt.path("service/ofagent-update")
343 .request(MediaType.APPLICATION_JSON_TYPE)
344 .put(Entity.json(jsonStream));
345 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_INTERNAL_ERROR));
346
347 verify(mockOFAgentService);
348 }
349
350 /**
351 * Tests starting an OFAgent with POST.
352 */
353 @Test
354 public void testOFAgentStart() {
355 expect(mockOFAgentService.agent(eq(NETWORK))).andReturn(OF_AGENT).anyTimes();
356 replay(mockOFAgentService);
357
358 mockOFAgentAdminService.startAgent(anyObject());
359 expectLastCall().anyTimes();
360 replay(mockOFAgentAdminService);
361
362 InputStream jsonStream = OFAgentWebResourceTest.class
363 .getResourceAsStream("post-ofagent-start.json");
364 assertNotNull("post-ofagent-create.json is null", jsonStream);
365 WebTarget wt = target();
366
367 Response response = wt.path("service/ofagent-start")
368 .request(MediaType.APPLICATION_JSON_TYPE)
369 .post(Entity.json(jsonStream));
370 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
371// assertThat(response.readEntity(String.class), containsString("OFAgent started"));
372 assertThat(response.readEntity(String.class), is("OFAgent started"));
373
374 verify(mockOFAgentService);
375 verify(mockOFAgentAdminService);
376 }
377
378 /**
379 * Tests non-existent OFAgent starting with POST.
380 */
381 @Test
382 public void testNonExistentOFAgentStart() {
383 expect(mockOFAgentService.agent(eq(NETWORK))).andReturn(null).anyTimes();
384 replay(mockOFAgentService);
385
386 InputStream jsonStream = OFAgentWebResourceTest.class
387 .getResourceAsStream("post-non-existent-ofagent-start.json");
388 assertNotNull("post-non-existent-ofagent-start.json is null", jsonStream);
389 WebTarget wt = target();
390
391 Response response = wt.path("service/ofagent-start")
392 .request(MediaType.APPLICATION_JSON_TYPE)
393 .post(Entity.json(jsonStream));
394 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_NOT_FOUND));
395
396 verify(mockOFAgentService);
397 }
398
399 /**
400 * Tests OFAgent starting with bad POST request.
401 */
402 @Test
403 public void testOFAgentStartBadRequest() {
404
405 InputStream jsonStream = OFAgentWebResourceTest.class
406 .getResourceAsStream("post-bad-request.json");
407 assertNotNull("post-bad-request.json is null", jsonStream);
408 WebTarget wt = target();
409
410 Response response = wt.path("service/ofagent-start")
411 .request(MediaType.APPLICATION_JSON_TYPE)
412 .post(Entity.json(jsonStream));
413 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_BAD_REQUEST));
414
415 }
416
417 /**
418 * Tests stopping an OFAgent with POST.
419 */
420 @Test
421 public void testOFAgentStop() {
422 expect(mockOFAgentService.agent(eq(NETWORK))).andReturn(OF_AGENT).anyTimes();
423 replay(mockOFAgentService);
424
425 mockOFAgentAdminService.stopAgent(anyObject());
426 expectLastCall().anyTimes();
427 replay(mockOFAgentAdminService);
428
429 InputStream jsonStream = OFAgentWebResourceTest.class
430 .getResourceAsStream("post-ofagent-stop.json");
431 assertNotNull("post-ofagent-stop.json is null", jsonStream);
432 WebTarget wt = target();
433 Response response = wt.path("service/ofagent-stop")
434 .request(MediaType.APPLICATION_JSON_TYPE)
435 .post(Entity.json(jsonStream));
436 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_NO_CONTENT));
437
438 verify(mockOFAgentService);
439 verify(mockOFAgentAdminService);
440 }
441
442 /**
443 * Tests stopping non-existent OFAgent with POST.
444 */
445 @Test
446 public void testNonExistentOFAgentStop() {
447 expect(mockOFAgentService.agent(NETWORK)).andReturn(null).anyTimes();
448 replay(mockOFAgentService);
449
450 InputStream jsonStream = OFAgentWebResourceTest.class
451 .getResourceAsStream("post-non-existent-ofagent-stop.json");
452 assertNotNull("post-non-existent-ofagent-stop.json is null", jsonStream);
453 WebTarget wt = target();
454
455 Response response = wt.path("service/ofagent-stop")
456 .request(MediaType.APPLICATION_JSON_TYPE)
457 .post(Entity.json(jsonStream));
458 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_NOT_FOUND));
459
460 verify(mockOFAgentService);
461 }
462
463 /**
464 * Tests stopping FAgent with bad POST request.
465 */
466 @Test
467 public void testOFAgentStopBadRequest() {
468 InputStream jsonStream = OFAgentWebResourceTest.class
469 .getResourceAsStream("post-bad-request.json");
470 assertNotNull("post-bad-request.json is null", jsonStream);
471 WebTarget wt = target();
472
473 Response response = wt.path("service/ofagent-stop")
474 .request(MediaType.APPLICATION_JSON_TYPE)
475 .post(Entity.json(jsonStream));
476 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_INTERNAL_ERROR));
477 }
478
479
480 /**
481 * Tests deleting an OFAgent with DELETE.
482 */
483 @Test
484 public void testOFAgentRemove() {
485 expect(mockOFAgentService.agent(eq(NETWORK))).andReturn(OF_AGENT).anyTimes();
486 replay(mockOFAgentService);
487
488 expect(mockOFAgentAdminService.removeAgent(NETWORK)).andReturn(OF_AGENT).anyTimes();
489 replay(mockOFAgentAdminService);
490
491 WebTarget wt = target();
492 Response response = wt.path("service/ofagent-remove/" + NETWORK.toString())
493 .request(MediaType.APPLICATION_JSON_TYPE)
494 .delete();
495 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
496 final JsonObject result = Json.parse(response.readEntity(String.class)).asObject();
497 assertThat(result.get("networkId").asString(), is(NETWORK.id().toString()));
498 assertThat(result.get("state").asString(), is(STOPPED.toString()));
499
500 verify(mockOFAgentService);
501 verify(mockOFAgentAdminService);
502 }
503
504 /**
505 * Tests deleting a non-existent OFAgent with DELETE.
506 */
507 @Test
508 public void testNonExistentOFAgentRemove() {
509 expect(mockOFAgentService.agent(eq(NETWORK))).andReturn(null).anyTimes();
510 replay(mockOFAgentService);
511
512 expect(mockOFAgentAdminService.removeAgent(NETWORK)).andReturn(null).anyTimes();
513 replay(mockOFAgentAdminService);
514
515 WebTarget wt = target();
516 Response response = wt.path("service/ofagent-remove/" + NETWORK.toString())
517 .request(MediaType.APPLICATION_JSON_TYPE)
518 .delete();
519 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_BAD_REQUEST));
520 assertThat(response.readEntity(String.class), containsString("OFAgent not found"));
521
522 verify(mockOFAgentService);
523 verify(mockOFAgentAdminService);
524 }
525}