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