blob: d65f0406827c95c6b6a85d0deb7a49bc4e18966e [file] [log] [blame]
Ray Milkey4f5de002014-12-17 19:26:11 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Ray Milkey4f5de002014-12-17 19:26:11 -08003 *
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 */
16package org.onosproject.rest;
17
Jian Li80cfe452016-01-14 16:04:58 -080018import com.eclipsesource.json.Json;
Jonathan Hartf44e42c2015-08-04 09:58:46 -070019import com.eclipsesource.json.JsonArray;
20import com.eclipsesource.json.JsonObject;
21import com.google.common.collect.ImmutableSet;
22import com.sun.jersey.api.client.ClientResponse;
23import com.sun.jersey.api.client.UniformInterfaceException;
24import com.sun.jersey.api.client.WebResource;
Ray Milkey4f5de002014-12-17 19:26:11 -080025import org.hamcrest.Description;
Ray Milkeyeb5c7172015-06-23 14:59:27 -070026import org.hamcrest.Matchers;
Ray Milkey4f5de002014-12-17 19:26:11 -080027import org.hamcrest.TypeSafeMatcher;
28import org.junit.After;
29import org.junit.Before;
30import org.junit.Test;
31import org.onlab.osgi.ServiceDirectory;
32import org.onlab.osgi.TestServiceDirectory;
33import org.onlab.packet.MacAddress;
34import org.onlab.rest.BaseResource;
35import org.onosproject.codec.CodecService;
36import org.onosproject.codec.impl.CodecManager;
Ray Milkeyeb5c7172015-06-23 14:59:27 -070037import org.onosproject.codec.impl.FlowRuleCodec;
Ray Milkeyd43fe452015-05-29 09:35:12 -070038import org.onosproject.core.CoreService;
Ray Milkey4f5de002014-12-17 19:26:11 -080039import org.onosproject.core.DefaultGroupId;
40import org.onosproject.core.GroupId;
41import org.onosproject.net.DefaultDevice;
42import org.onosproject.net.Device;
43import org.onosproject.net.DeviceId;
Ray Milkeyeb5c7172015-06-23 14:59:27 -070044import org.onosproject.net.IndexedLambda;
Ray Milkeyd43fe452015-05-29 09:35:12 -070045import org.onosproject.net.NetTestTools;
Ray Milkey4f5de002014-12-17 19:26:11 -080046import org.onosproject.net.device.DeviceService;
47import org.onosproject.net.flow.DefaultTrafficSelector;
48import org.onosproject.net.flow.DefaultTrafficTreatment;
49import org.onosproject.net.flow.FlowEntry;
50import org.onosproject.net.flow.FlowId;
Jonathan Hartf44e42c2015-08-04 09:58:46 -070051import org.onosproject.net.flow.FlowRule;
jcc3d4e14a2015-04-21 11:32:05 +080052import org.onosproject.net.flow.FlowRuleExtPayLoad;
Ray Milkey4f5de002014-12-17 19:26:11 -080053import org.onosproject.net.flow.FlowRuleService;
54import org.onosproject.net.flow.TrafficSelector;
55import org.onosproject.net.flow.TrafficTreatment;
56import org.onosproject.net.flow.criteria.Criterion;
57import org.onosproject.net.flow.instructions.Instruction;
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -080058import org.onosproject.net.flow.instructions.Instructions;
Andrea Campanella5df35952015-12-08 15:46:49 -080059import org.onosproject.rest.resources.CoreWebApplication;
jcc3d4e14a2015-04-21 11:32:05 +080060
Jonathan Hartf44e42c2015-08-04 09:58:46 -070061import javax.ws.rs.core.MediaType;
62import java.io.InputStream;
63import java.net.HttpURLConnection;
64import java.util.HashMap;
65import java.util.HashSet;
66import java.util.Set;
Ray Milkey4f5de002014-12-17 19:26:11 -080067
68import static org.easymock.EasyMock.anyObject;
Ray Milkeyd43fe452015-05-29 09:35:12 -070069import static org.easymock.EasyMock.anyShort;
Ray Milkey4f5de002014-12-17 19:26:11 -080070import static org.easymock.EasyMock.createMock;
71import static org.easymock.EasyMock.expect;
Ray Milkeyd43fe452015-05-29 09:35:12 -070072import static org.easymock.EasyMock.expectLastCall;
Ray Milkey4f5de002014-12-17 19:26:11 -080073import static org.easymock.EasyMock.replay;
74import static org.easymock.EasyMock.verify;
75import static org.hamcrest.Matchers.containsString;
76import static org.hamcrest.Matchers.hasSize;
77import static org.hamcrest.Matchers.is;
78import static org.hamcrest.Matchers.not;
79import static org.hamcrest.Matchers.notNullValue;
80import static org.junit.Assert.assertThat;
81import static org.junit.Assert.fail;
Ray Milkeyeb5c7172015-06-23 14:59:27 -070082import static org.onosproject.net.NetTestTools.APP_ID;
Ray Milkey4f5de002014-12-17 19:26:11 -080083
84/**
85 * Unit tests for Flows REST APIs.
86 */
Ray Milkey9c3d3362015-01-28 10:39:56 -080087public class FlowsResourceTest extends ResourceTest {
Ray Milkey4f5de002014-12-17 19:26:11 -080088 final FlowRuleService mockFlowService = createMock(FlowRuleService.class);
Ray Milkeyd43fe452015-05-29 09:35:12 -070089 CoreService mockCoreService = createMock(CoreService.class);
90
Ray Milkey4f5de002014-12-17 19:26:11 -080091 final HashMap<DeviceId, Set<FlowEntry>> rules = new HashMap<>();
92
93 final DeviceService mockDeviceService = createMock(DeviceService.class);
94
95 final DeviceId deviceId1 = DeviceId.deviceId("1");
96 final DeviceId deviceId2 = DeviceId.deviceId("2");
97 final DeviceId deviceId3 = DeviceId.deviceId("3");
98 final Device device1 = new DefaultDevice(null, deviceId1, Device.Type.OTHER,
99 "", "", "", "", null);
100 final Device device2 = new DefaultDevice(null, deviceId2, Device.Type.OTHER,
101 "", "", "", "", null);
102
103 final MockFlowEntry flow1 = new MockFlowEntry(deviceId1, 1);
104 final MockFlowEntry flow2 = new MockFlowEntry(deviceId1, 2);
105
106 final MockFlowEntry flow3 = new MockFlowEntry(deviceId2, 3);
107 final MockFlowEntry flow4 = new MockFlowEntry(deviceId2, 4);
108
109 final MockFlowEntry flow5 = new MockFlowEntry(deviceId2, 5);
110 final MockFlowEntry flow6 = new MockFlowEntry(deviceId2, 6);
111
112 /**
113 * Mock class for a flow entry.
114 */
115 private static class MockFlowEntry implements FlowEntry {
116 final DeviceId deviceId;
117 final long baseValue;
118 TrafficTreatment treatment;
119 TrafficSelector selector;
120
121 public MockFlowEntry(DeviceId deviceId, long id) {
122 this.deviceId = deviceId;
123 this.baseValue = id * 100;
124 }
125
126 @Override
127 public FlowEntryState state() {
128 return FlowEntryState.ADDED;
129 }
130
131 @Override
132 public long life() {
133 return baseValue + 11;
134 }
135
136 @Override
137 public long packets() {
138 return baseValue + 22;
139 }
140
141 @Override
142 public long bytes() {
143 return baseValue + 33;
144 }
145
146 @Override
147 public long lastSeen() {
148 return baseValue + 44;
149 }
150
151 @Override
152 public int errType() {
153 return 0;
154 }
155
156 @Override
157 public int errCode() {
158 return 0;
159 }
160
161 @Override
162 public FlowId id() {
163 final long id = baseValue + 55;
164 return FlowId.valueOf(id);
165 }
166
167 @Override
alshabib08d98982015-04-21 16:25:50 -0700168 public GroupId groupId() {
169 return new DefaultGroupId(3);
Ray Milkey4f5de002014-12-17 19:26:11 -0800170 }
171
172 @Override
alshabib08d98982015-04-21 16:25:50 -0700173 public short appId() {
174 return 2;
Ray Milkey4f5de002014-12-17 19:26:11 -0800175 }
176
177 @Override
178 public int priority() {
179 return (int) (baseValue + 66);
180 }
181
182 @Override
183 public DeviceId deviceId() {
184 return deviceId;
185 }
186
187 @Override
188 public TrafficSelector selector() {
189 return selector;
190 }
191
192 @Override
193 public TrafficTreatment treatment() {
194 return treatment;
195 }
196
197 @Override
198 public int timeout() {
199 return (int) (baseValue + 77);
200 }
201
202 @Override
203 public boolean isPermanent() {
204 return false;
205 }
sangho11c30ac2015-01-22 14:30:55 -0800206
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800207 @Override
alshabibdb774072015-04-20 13:13:51 -0700208 public int tableId() {
209 return 0;
210 }
jcc3d4e14a2015-04-21 11:32:05 +0800211
212 @Override
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700213 public boolean exactMatch(FlowRule rule) {
214 return false;
215 }
216
217 @Override
jcc3d4e14a2015-04-21 11:32:05 +0800218 public FlowRuleExtPayLoad payLoad() {
219 return null;
220 }
Ray Milkey4f5de002014-12-17 19:26:11 -0800221 }
222
Ray Milkey4f5de002014-12-17 19:26:11 -0800223 /**
224 * Populates some flows used as testing data.
225 */
226 private void setupMockFlows() {
227 flow2.treatment = DefaultTrafficTreatment.builder()
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700228 .add(Instructions.modL0Lambda(new IndexedLambda((short) 4)))
229 .add(Instructions.modL0Lambda(new IndexedLambda((short) 5)))
Ray Milkey4f5de002014-12-17 19:26:11 -0800230 .setEthDst(MacAddress.BROADCAST)
231 .build();
232 flow2.selector = DefaultTrafficSelector.builder()
233 .matchEthType((short) 3)
234 .matchIPProtocol((byte) 9)
235 .build();
236 flow4.treatment = DefaultTrafficTreatment.builder()
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700237 .add(Instructions.modL0Lambda(new IndexedLambda((short) 6)))
Ray Milkey4f5de002014-12-17 19:26:11 -0800238 .build();
239 final Set<FlowEntry> flows1 = new HashSet<>();
240 flows1.add(flow1);
241 flows1.add(flow2);
242
243 final Set<FlowEntry> flows2 = new HashSet<>();
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700244 flows2.add(flow3);
245 flows2.add(flow4);
Ray Milkey4f5de002014-12-17 19:26:11 -0800246
247 rules.put(deviceId1, flows1);
248 rules.put(deviceId2, flows2);
249
250 expect(mockFlowService.getFlowEntries(deviceId1))
251 .andReturn(rules.get(deviceId1)).anyTimes();
252 expect(mockFlowService.getFlowEntries(deviceId2))
253 .andReturn(rules.get(deviceId2)).anyTimes();
254 }
255
Andrea Campanella5df35952015-12-08 15:46:49 -0800256 public FlowsResourceTest() {
257 super(CoreWebApplication.class);
258 }
259
Ray Milkey4f5de002014-12-17 19:26:11 -0800260 /**
261 * Sets up the global values for all the tests.
262 */
263 @Before
Ray Milkeyed0b1662015-02-05 09:34:29 -0800264 public void setUpTest() {
Ray Milkey4f5de002014-12-17 19:26:11 -0800265 // Mock device service
266 expect(mockDeviceService.getDevice(deviceId1))
267 .andReturn(device1);
268 expect(mockDeviceService.getDevice(deviceId2))
269 .andReturn(device2);
270 expect(mockDeviceService.getDevices())
271 .andReturn(ImmutableSet.of(device1, device2));
272
Ray Milkeyd43fe452015-05-29 09:35:12 -0700273 // Mock Core Service
274 expect(mockCoreService.getAppId(anyShort()))
275 .andReturn(NetTestTools.APP_ID).anyTimes();
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700276 expect(mockCoreService.registerApplication(FlowRuleCodec.REST_APP_ID))
277 .andReturn(APP_ID).anyTimes();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700278 replay(mockCoreService);
279
Ray Milkey4f5de002014-12-17 19:26:11 -0800280 // Register the services needed for the test
281 final CodecManager codecService = new CodecManager();
282 codecService.activate();
283 ServiceDirectory testDirectory =
284 new TestServiceDirectory()
285 .add(FlowRuleService.class, mockFlowService)
286 .add(DeviceService.class, mockDeviceService)
Ray Milkeyd43fe452015-05-29 09:35:12 -0700287 .add(CodecService.class, codecService)
288 .add(CoreService.class, mockCoreService);
Ray Milkey4f5de002014-12-17 19:26:11 -0800289
290 BaseResource.setServiceDirectory(testDirectory);
291 }
292
293 /**
294 * Cleans up and verifies the mocks.
Ray Milkey4f5de002014-12-17 19:26:11 -0800295 */
296 @After
Ray Milkeyed0b1662015-02-05 09:34:29 -0800297 public void tearDownTest() {
Ray Milkey4f5de002014-12-17 19:26:11 -0800298 verify(mockFlowService);
Ray Milkeyd43fe452015-05-29 09:35:12 -0700299 verify(mockCoreService);
Ray Milkey4f5de002014-12-17 19:26:11 -0800300 }
301
302 /**
303 * Hamcrest matcher to check that a flow representation in JSON matches
304 * the actual flow entry.
305 */
306 public static class FlowJsonMatcher extends TypeSafeMatcher<JsonObject> {
307 private final FlowEntry flow;
Ray Milkeybe539db2015-09-04 11:00:43 -0700308 private final String expectedAppId;
Ray Milkey4f5de002014-12-17 19:26:11 -0800309 private String reason = "";
310
Ray Milkeybe539db2015-09-04 11:00:43 -0700311 public FlowJsonMatcher(FlowEntry flowValue, String expectedAppIdValue) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800312 flow = flowValue;
Ray Milkeybe539db2015-09-04 11:00:43 -0700313 expectedAppId = expectedAppIdValue;
Ray Milkey4f5de002014-12-17 19:26:11 -0800314 }
315
316 @Override
317 public boolean matchesSafely(JsonObject jsonFlow) {
318 // check id
319 final String jsonId = jsonFlow.get("id").asString();
320 final String flowId = Long.toString(flow.id().value());
321 if (!jsonId.equals(flowId)) {
322 reason = "id " + flow.id().toString();
323 return false;
324 }
325
326 // check application id
Ray Milkeybe539db2015-09-04 11:00:43 -0700327 final String jsonAppId = jsonFlow.get("appId").asString();
328 if (!jsonAppId.equals(expectedAppId)) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800329 reason = "appId " + Short.toString(flow.appId());
330 return false;
331 }
332
333 // check device id
334 final String jsonDeviceId = jsonFlow.get("deviceId").asString();
335 if (!jsonDeviceId.equals(flow.deviceId().toString())) {
336 reason = "deviceId " + flow.deviceId();
337 return false;
338 }
339
340 // check treatment and instructions array
341 if (flow.treatment() != null) {
342 final JsonObject jsonTreatment = jsonFlow.get("treatment").asObject();
343 final JsonArray jsonInstructions = jsonTreatment.get("instructions").asArray();
Ray Milkey42507352015-03-20 15:16:10 -0700344 if (flow.treatment().immediate().size() != jsonInstructions.size()) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800345 reason = "instructions array size of " +
Ray Milkey42507352015-03-20 15:16:10 -0700346 Integer.toString(flow.treatment().immediate().size());
Ray Milkey4f5de002014-12-17 19:26:11 -0800347 return false;
348 }
Ray Milkey42507352015-03-20 15:16:10 -0700349 for (final Instruction instruction : flow.treatment().immediate()) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800350 boolean instructionFound = false;
Ray Milkey4f5de002014-12-17 19:26:11 -0800351 for (int instructionIndex = 0; instructionIndex < jsonInstructions.size(); instructionIndex++) {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800352 final String jsonType =
353 jsonInstructions.get(instructionIndex)
354 .asObject().get("type").asString();
355 final String instructionType = instruction.type().name();
356 if (jsonType.equals(instructionType)) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800357 instructionFound = true;
358 }
359 }
360 if (!instructionFound) {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800361 reason = "instruction " + instruction.toString();
Ray Milkey4f5de002014-12-17 19:26:11 -0800362 return false;
363 }
364 }
365 }
366
367 // check selector and criteria array
368 if (flow.selector() != null) {
369 final JsonObject jsonTreatment = jsonFlow.get("selector").asObject();
370 final JsonArray jsonCriteria = jsonTreatment.get("criteria").asArray();
371 if (flow.selector().criteria().size() != jsonCriteria.size()) {
372 reason = "criteria array size of " +
373 Integer.toString(flow.selector().criteria().size());
374 return false;
375 }
376 for (final Criterion criterion : flow.selector().criteria()) {
377 boolean criterionFound = false;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800378
Ray Milkey4f5de002014-12-17 19:26:11 -0800379 for (int criterionIndex = 0; criterionIndex < jsonCriteria.size(); criterionIndex++) {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800380 final String jsonType =
381 jsonCriteria.get(criterionIndex)
382 .asObject().get("type").asString();
383 final String criterionType = criterion.type().name();
384 if (jsonType.equals(criterionType)) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800385 criterionFound = true;
386 }
387 }
388 if (!criterionFound) {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800389 reason = "criterion " + criterion.toString();
Ray Milkey4f5de002014-12-17 19:26:11 -0800390 return false;
391 }
392 }
393 }
394
395 return true;
396 }
397
398 @Override
399 public void describeTo(Description description) {
400 description.appendText(reason);
401 }
402 }
403
404 /**
405 * Factory to allocate a flow matcher.
406 *
407 * @param flow flow object we are looking for
408 * @return matcher
409 */
Ray Milkeybe539db2015-09-04 11:00:43 -0700410 private static FlowJsonMatcher matchesFlow(FlowEntry flow, String expectedAppName) {
411 return new FlowJsonMatcher(flow, expectedAppName);
Ray Milkey4f5de002014-12-17 19:26:11 -0800412 }
413
414 /**
415 * Hamcrest matcher to check that a flow is represented properly in a JSON
416 * array of flows.
417 */
418 public static class FlowJsonArrayMatcher extends TypeSafeMatcher<JsonArray> {
419 private final FlowEntry flow;
420 private String reason = "";
421
422 public FlowJsonArrayMatcher(FlowEntry flowValue) {
423 flow = flowValue;
424 }
425
426 @Override
427 public boolean matchesSafely(JsonArray json) {
428 boolean flowFound = false;
429
430 for (int jsonFlowIndex = 0; jsonFlowIndex < json.size();
431 jsonFlowIndex++) {
432
433 final JsonObject jsonFlow = json.get(jsonFlowIndex).asObject();
434
435 final String flowId = Long.toString(flow.id().value());
436 final String jsonFlowId = jsonFlow.get("id").asString();
437 if (jsonFlowId.equals(flowId)) {
438 flowFound = true;
439
440 // We found the correct flow, check attribute values
Ray Milkeybe539db2015-09-04 11:00:43 -0700441 assertThat(jsonFlow, matchesFlow(flow, APP_ID.name()));
Ray Milkey4f5de002014-12-17 19:26:11 -0800442 }
443 }
444 if (!flowFound) {
445 reason = "Flow with id " + flow.id().toString() + " not found";
446 return false;
447 } else {
448 return true;
449 }
450 }
451
452 @Override
453 public void describeTo(Description description) {
454 description.appendText(reason);
455 }
456 }
457
458 /**
459 * Factory to allocate a flow array matcher.
460 *
461 * @param flow flow object we are looking for
462 * @return matcher
463 */
464 private static FlowJsonArrayMatcher hasFlow(FlowEntry flow) {
465 return new FlowJsonArrayMatcher(flow);
466 }
467
468 /**
469 * Tests the result of the rest api GET when there are no flows.
470 */
471 @Test
472 public void testFlowsEmptyArray() {
473 expect(mockFlowService.getFlowEntries(deviceId1))
474 .andReturn(null).anyTimes();
475 expect(mockFlowService.getFlowEntries(deviceId2))
476 .andReturn(null).anyTimes();
477 replay(mockFlowService);
478 replay(mockDeviceService);
479 final WebResource rs = resource();
480 final String response = rs.path("flows").get(String.class);
481 assertThat(response, is("{\"flows\":[]}"));
482 }
483
484 /**
485 * Tests the result of the rest api GET when there are active flows.
486 */
487 @Test
488 public void testFlowsPopulatedArray() {
489 setupMockFlows();
490 replay(mockFlowService);
491 replay(mockDeviceService);
492 final WebResource rs = resource();
493 final String response = rs.path("flows").get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800494 final JsonObject result = Json.parse(response).asObject();
Ray Milkey4f5de002014-12-17 19:26:11 -0800495 assertThat(result, notNullValue());
496
497 assertThat(result.names(), hasSize(1));
498 assertThat(result.names().get(0), is("flows"));
499 final JsonArray jsonFlows = result.get("flows").asArray();
500 assertThat(jsonFlows, notNullValue());
501 assertThat(jsonFlows, hasFlow(flow1));
502 assertThat(jsonFlows, hasFlow(flow2));
503 assertThat(jsonFlows, hasFlow(flow3));
504 assertThat(jsonFlows, hasFlow(flow4));
505 }
506
507 /**
508 * Tests the result of a rest api GET for a device.
509 */
510 @Test
511 public void testFlowsSingleDevice() {
512 setupMockFlows();
513 final Set<FlowEntry> flows = new HashSet<>();
514 flows.add(flow5);
515 flows.add(flow6);
516 expect(mockFlowService.getFlowEntries(anyObject()))
517 .andReturn(flows).anyTimes();
518 replay(mockFlowService);
519 replay(mockDeviceService);
520 final WebResource rs = resource();
521 final String response = rs.path("flows/" + deviceId3).get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800522 final JsonObject result = Json.parse(response).asObject();
Ray Milkey4f5de002014-12-17 19:26:11 -0800523 assertThat(result, notNullValue());
524
525 assertThat(result.names(), hasSize(1));
526 assertThat(result.names().get(0), is("flows"));
527 final JsonArray jsonFlows = result.get("flows").asArray();
528 assertThat(jsonFlows, notNullValue());
529 assertThat(jsonFlows, hasFlow(flow5));
530 assertThat(jsonFlows, hasFlow(flow6));
531 }
532
533 /**
534 * Tests the result of a rest api GET for a device.
535 */
536 @Test
537 public void testFlowsSingleDeviceWithFlowId() {
538 setupMockFlows();
539 final Set<FlowEntry> flows = new HashSet<>();
540 flows.add(flow5);
541 flows.add(flow6);
542 expect(mockFlowService.getFlowEntries(anyObject()))
543 .andReturn(flows).anyTimes();
544 replay(mockFlowService);
545 replay(mockDeviceService);
546 final WebResource rs = resource();
547 final String response = rs.path("flows/" + deviceId3 + "/"
548 + Long.toString(flow5.id().value())).get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800549 final JsonObject result = Json.parse(response).asObject();
Ray Milkey4f5de002014-12-17 19:26:11 -0800550 assertThat(result, notNullValue());
551
552 assertThat(result.names(), hasSize(1));
553 assertThat(result.names().get(0), is("flows"));
554 final JsonArray jsonFlows = result.get("flows").asArray();
555 assertThat(jsonFlows, notNullValue());
556 assertThat(jsonFlows, hasFlow(flow5));
557 assertThat(jsonFlows, not(hasFlow(flow6)));
558 }
559
560 /**
561 * Tests that a fetch of a non-existent device object throws an exception.
562 */
563 @Test
564 public void testBadGet() {
565 expect(mockFlowService.getFlowEntries(deviceId1))
566 .andReturn(null).anyTimes();
567 expect(mockFlowService.getFlowEntries(deviceId2))
568 .andReturn(null).anyTimes();
569 replay(mockFlowService);
570 replay(mockDeviceService);
571
572 WebResource rs = resource();
573 try {
574 rs.path("flows/0").get(String.class);
575 fail("Fetch of non-existent device did not throw an exception");
576 } catch (UniformInterfaceException ex) {
577 assertThat(ex.getMessage(),
578 containsString("returned a response status of"));
579 }
580 }
Ray Milkeyd43fe452015-05-29 09:35:12 -0700581
582 /**
583 * Tests creating a flow with POST.
584 */
585 @Test
586 public void testPost() {
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700587
Ray Milkeyd43fe452015-05-29 09:35:12 -0700588
589 mockFlowService.applyFlowRules(anyObject());
590 expectLastCall();
591 replay(mockFlowService);
592
593 WebResource rs = resource();
Phaneendra Manda2be2f882015-11-11 15:23:40 +0530594 InputStream jsonStream = FlowsResourceTest.class
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700595 .getResourceAsStream("post-flow.json");
Ray Milkeyd43fe452015-05-29 09:35:12 -0700596
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700597 ClientResponse response = rs.path("flows/of:0000000000000001")
Ray Milkeyd43fe452015-05-29 09:35:12 -0700598 .type(MediaType.APPLICATION_JSON_TYPE)
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700599 .post(ClientResponse.class, jsonStream);
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700600 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED));
601 String location = response.getLocation().getPath();
602 assertThat(location, Matchers.startsWith("/flows/of:0000000000000001/"));
603 }
604
605 /**
606 * Tests deleting a flow.
607 */
608 @Test
609 public void testDelete() {
610 setupMockFlows();
611 mockFlowService.removeFlowRules(anyObject());
612 expectLastCall();
613 replay(mockFlowService);
614
615 WebResource rs = resource();
616
617 String location = "/flows/1/155";
618
619 ClientResponse deleteResponse = rs.path(location)
620 .type(MediaType.APPLICATION_JSON_TYPE)
621 .delete(ClientResponse.class);
622 assertThat(deleteResponse.getStatus(),
623 is(HttpURLConnection.HTTP_NO_CONTENT));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700624 }
Ray Milkey4f5de002014-12-17 19:26:11 -0800625}