blob: 9cb63e7bb95480f3267b7ab072ac25525ee19caa [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
Jonathan Hartf44e42c2015-08-04 09:58:46 -070018import com.eclipsesource.json.JsonArray;
19import com.eclipsesource.json.JsonObject;
20import com.google.common.collect.ImmutableSet;
21import com.sun.jersey.api.client.ClientResponse;
22import com.sun.jersey.api.client.UniformInterfaceException;
23import com.sun.jersey.api.client.WebResource;
Ray Milkey4f5de002014-12-17 19:26:11 -080024import org.hamcrest.Description;
Ray Milkeyeb5c7172015-06-23 14:59:27 -070025import org.hamcrest.Matchers;
Ray Milkey4f5de002014-12-17 19:26:11 -080026import org.hamcrest.TypeSafeMatcher;
27import org.junit.After;
28import org.junit.Before;
29import org.junit.Test;
30import org.onlab.osgi.ServiceDirectory;
31import org.onlab.osgi.TestServiceDirectory;
32import org.onlab.packet.MacAddress;
33import org.onlab.rest.BaseResource;
34import org.onosproject.codec.CodecService;
35import org.onosproject.codec.impl.CodecManager;
Ray Milkeyeb5c7172015-06-23 14:59:27 -070036import org.onosproject.codec.impl.FlowRuleCodec;
Ray Milkeyd43fe452015-05-29 09:35:12 -070037import org.onosproject.core.CoreService;
Ray Milkey4f5de002014-12-17 19:26:11 -080038import org.onosproject.core.DefaultGroupId;
39import org.onosproject.core.GroupId;
40import org.onosproject.net.DefaultDevice;
41import org.onosproject.net.Device;
42import org.onosproject.net.DeviceId;
Ray Milkeyeb5c7172015-06-23 14:59:27 -070043import org.onosproject.net.IndexedLambda;
Ray Milkeyd43fe452015-05-29 09:35:12 -070044import org.onosproject.net.NetTestTools;
Ray Milkey4f5de002014-12-17 19:26:11 -080045import org.onosproject.net.device.DeviceService;
46import org.onosproject.net.flow.DefaultTrafficSelector;
47import org.onosproject.net.flow.DefaultTrafficTreatment;
48import org.onosproject.net.flow.FlowEntry;
49import org.onosproject.net.flow.FlowId;
Jonathan Hartf44e42c2015-08-04 09:58:46 -070050import org.onosproject.net.flow.FlowRule;
jcc3d4e14a2015-04-21 11:32:05 +080051import org.onosproject.net.flow.FlowRuleExtPayLoad;
Ray Milkey4f5de002014-12-17 19:26:11 -080052import org.onosproject.net.flow.FlowRuleService;
53import org.onosproject.net.flow.TrafficSelector;
54import org.onosproject.net.flow.TrafficTreatment;
55import org.onosproject.net.flow.criteria.Criterion;
56import org.onosproject.net.flow.instructions.Instruction;
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -080057import org.onosproject.net.flow.instructions.Instructions;
jcc3d4e14a2015-04-21 11:32:05 +080058
Jonathan Hartf44e42c2015-08-04 09:58:46 -070059import javax.ws.rs.core.MediaType;
60import java.io.InputStream;
61import java.net.HttpURLConnection;
62import java.util.HashMap;
63import java.util.HashSet;
64import java.util.Set;
Ray Milkey4f5de002014-12-17 19:26:11 -080065
66import static org.easymock.EasyMock.anyObject;
Ray Milkeyd43fe452015-05-29 09:35:12 -070067import static org.easymock.EasyMock.anyShort;
Ray Milkey4f5de002014-12-17 19:26:11 -080068import static org.easymock.EasyMock.createMock;
69import static org.easymock.EasyMock.expect;
Ray Milkeyd43fe452015-05-29 09:35:12 -070070import static org.easymock.EasyMock.expectLastCall;
Ray Milkey4f5de002014-12-17 19:26:11 -080071import static org.easymock.EasyMock.replay;
72import static org.easymock.EasyMock.verify;
73import static org.hamcrest.Matchers.containsString;
74import static org.hamcrest.Matchers.hasSize;
75import static org.hamcrest.Matchers.is;
76import static org.hamcrest.Matchers.not;
77import static org.hamcrest.Matchers.notNullValue;
78import static org.junit.Assert.assertThat;
79import static org.junit.Assert.fail;
Ray Milkeyeb5c7172015-06-23 14:59:27 -070080import static org.onosproject.net.NetTestTools.APP_ID;
Ray Milkey4f5de002014-12-17 19:26:11 -080081
82/**
83 * Unit tests for Flows REST APIs.
84 */
Ray Milkey9c3d3362015-01-28 10:39:56 -080085public class FlowsResourceTest extends ResourceTest {
Ray Milkey4f5de002014-12-17 19:26:11 -080086 final FlowRuleService mockFlowService = createMock(FlowRuleService.class);
Ray Milkeyd43fe452015-05-29 09:35:12 -070087 CoreService mockCoreService = createMock(CoreService.class);
88
Ray Milkey4f5de002014-12-17 19:26:11 -080089 final HashMap<DeviceId, Set<FlowEntry>> rules = new HashMap<>();
90
91 final DeviceService mockDeviceService = createMock(DeviceService.class);
92
93 final DeviceId deviceId1 = DeviceId.deviceId("1");
94 final DeviceId deviceId2 = DeviceId.deviceId("2");
95 final DeviceId deviceId3 = DeviceId.deviceId("3");
96 final Device device1 = new DefaultDevice(null, deviceId1, Device.Type.OTHER,
97 "", "", "", "", null);
98 final Device device2 = new DefaultDevice(null, deviceId2, Device.Type.OTHER,
99 "", "", "", "", null);
100
101 final MockFlowEntry flow1 = new MockFlowEntry(deviceId1, 1);
102 final MockFlowEntry flow2 = new MockFlowEntry(deviceId1, 2);
103
104 final MockFlowEntry flow3 = new MockFlowEntry(deviceId2, 3);
105 final MockFlowEntry flow4 = new MockFlowEntry(deviceId2, 4);
106
107 final MockFlowEntry flow5 = new MockFlowEntry(deviceId2, 5);
108 final MockFlowEntry flow6 = new MockFlowEntry(deviceId2, 6);
109
110 /**
111 * Mock class for a flow entry.
112 */
113 private static class MockFlowEntry implements FlowEntry {
114 final DeviceId deviceId;
115 final long baseValue;
116 TrafficTreatment treatment;
117 TrafficSelector selector;
118
119 public MockFlowEntry(DeviceId deviceId, long id) {
120 this.deviceId = deviceId;
121 this.baseValue = id * 100;
122 }
123
124 @Override
125 public FlowEntryState state() {
126 return FlowEntryState.ADDED;
127 }
128
129 @Override
130 public long life() {
131 return baseValue + 11;
132 }
133
134 @Override
135 public long packets() {
136 return baseValue + 22;
137 }
138
139 @Override
140 public long bytes() {
141 return baseValue + 33;
142 }
143
144 @Override
145 public long lastSeen() {
146 return baseValue + 44;
147 }
148
149 @Override
150 public int errType() {
151 return 0;
152 }
153
154 @Override
155 public int errCode() {
156 return 0;
157 }
158
159 @Override
160 public FlowId id() {
161 final long id = baseValue + 55;
162 return FlowId.valueOf(id);
163 }
164
165 @Override
alshabib08d98982015-04-21 16:25:50 -0700166 public GroupId groupId() {
167 return new DefaultGroupId(3);
Ray Milkey4f5de002014-12-17 19:26:11 -0800168 }
169
170 @Override
alshabib08d98982015-04-21 16:25:50 -0700171 public short appId() {
172 return 2;
Ray Milkey4f5de002014-12-17 19:26:11 -0800173 }
174
175 @Override
176 public int priority() {
177 return (int) (baseValue + 66);
178 }
179
180 @Override
181 public DeviceId deviceId() {
182 return deviceId;
183 }
184
185 @Override
186 public TrafficSelector selector() {
187 return selector;
188 }
189
190 @Override
191 public TrafficTreatment treatment() {
192 return treatment;
193 }
194
195 @Override
196 public int timeout() {
197 return (int) (baseValue + 77);
198 }
199
200 @Override
201 public boolean isPermanent() {
202 return false;
203 }
sangho11c30ac2015-01-22 14:30:55 -0800204
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800205 @Override
alshabibdb774072015-04-20 13:13:51 -0700206 public int tableId() {
207 return 0;
208 }
jcc3d4e14a2015-04-21 11:32:05 +0800209
210 @Override
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700211 public boolean exactMatch(FlowRule rule) {
212 return false;
213 }
214
215 @Override
jcc3d4e14a2015-04-21 11:32:05 +0800216 public FlowRuleExtPayLoad payLoad() {
217 return null;
218 }
Ray Milkey4f5de002014-12-17 19:26:11 -0800219 }
220
Ray Milkey4f5de002014-12-17 19:26:11 -0800221 /**
222 * Populates some flows used as testing data.
223 */
224 private void setupMockFlows() {
225 flow2.treatment = DefaultTrafficTreatment.builder()
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700226 .add(Instructions.modL0Lambda(new IndexedLambda((short) 4)))
227 .add(Instructions.modL0Lambda(new IndexedLambda((short) 5)))
Ray Milkey4f5de002014-12-17 19:26:11 -0800228 .setEthDst(MacAddress.BROADCAST)
229 .build();
230 flow2.selector = DefaultTrafficSelector.builder()
231 .matchEthType((short) 3)
232 .matchIPProtocol((byte) 9)
233 .build();
234 flow4.treatment = DefaultTrafficTreatment.builder()
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700235 .add(Instructions.modL0Lambda(new IndexedLambda((short) 6)))
Ray Milkey4f5de002014-12-17 19:26:11 -0800236 .build();
237 final Set<FlowEntry> flows1 = new HashSet<>();
238 flows1.add(flow1);
239 flows1.add(flow2);
240
241 final Set<FlowEntry> flows2 = new HashSet<>();
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700242 flows2.add(flow3);
243 flows2.add(flow4);
Ray Milkey4f5de002014-12-17 19:26:11 -0800244
245 rules.put(deviceId1, flows1);
246 rules.put(deviceId2, flows2);
247
248 expect(mockFlowService.getFlowEntries(deviceId1))
249 .andReturn(rules.get(deviceId1)).anyTimes();
250 expect(mockFlowService.getFlowEntries(deviceId2))
251 .andReturn(rules.get(deviceId2)).anyTimes();
252 }
253
254 /**
255 * Sets up the global values for all the tests.
256 */
257 @Before
Ray Milkeyed0b1662015-02-05 09:34:29 -0800258 public void setUpTest() {
Ray Milkey4f5de002014-12-17 19:26:11 -0800259 // Mock device service
260 expect(mockDeviceService.getDevice(deviceId1))
261 .andReturn(device1);
262 expect(mockDeviceService.getDevice(deviceId2))
263 .andReturn(device2);
264 expect(mockDeviceService.getDevices())
265 .andReturn(ImmutableSet.of(device1, device2));
266
Ray Milkeyd43fe452015-05-29 09:35:12 -0700267 // Mock Core Service
268 expect(mockCoreService.getAppId(anyShort()))
269 .andReturn(NetTestTools.APP_ID).anyTimes();
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700270 expect(mockCoreService.registerApplication(FlowRuleCodec.REST_APP_ID))
271 .andReturn(APP_ID).anyTimes();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700272 replay(mockCoreService);
273
Ray Milkey4f5de002014-12-17 19:26:11 -0800274 // Register the services needed for the test
275 final CodecManager codecService = new CodecManager();
276 codecService.activate();
277 ServiceDirectory testDirectory =
278 new TestServiceDirectory()
279 .add(FlowRuleService.class, mockFlowService)
280 .add(DeviceService.class, mockDeviceService)
Ray Milkeyd43fe452015-05-29 09:35:12 -0700281 .add(CodecService.class, codecService)
282 .add(CoreService.class, mockCoreService);
Ray Milkey4f5de002014-12-17 19:26:11 -0800283
284 BaseResource.setServiceDirectory(testDirectory);
285 }
286
287 /**
288 * Cleans up and verifies the mocks.
Ray Milkey4f5de002014-12-17 19:26:11 -0800289 */
290 @After
Ray Milkeyed0b1662015-02-05 09:34:29 -0800291 public void tearDownTest() {
Ray Milkey4f5de002014-12-17 19:26:11 -0800292 verify(mockFlowService);
Ray Milkeyd43fe452015-05-29 09:35:12 -0700293 verify(mockCoreService);
Ray Milkey4f5de002014-12-17 19:26:11 -0800294 }
295
296 /**
297 * Hamcrest matcher to check that a flow representation in JSON matches
298 * the actual flow entry.
299 */
300 public static class FlowJsonMatcher extends TypeSafeMatcher<JsonObject> {
301 private final FlowEntry flow;
302 private String reason = "";
303
304 public FlowJsonMatcher(FlowEntry flowValue) {
305 flow = flowValue;
306 }
307
308 @Override
309 public boolean matchesSafely(JsonObject jsonFlow) {
310 // check id
311 final String jsonId = jsonFlow.get("id").asString();
312 final String flowId = Long.toString(flow.id().value());
313 if (!jsonId.equals(flowId)) {
314 reason = "id " + flow.id().toString();
315 return false;
316 }
317
318 // check application id
319 final int jsonAppId = jsonFlow.get("appId").asInt();
320 if (jsonAppId != flow.appId()) {
321 reason = "appId " + Short.toString(flow.appId());
322 return false;
323 }
324
325 // check device id
326 final String jsonDeviceId = jsonFlow.get("deviceId").asString();
327 if (!jsonDeviceId.equals(flow.deviceId().toString())) {
328 reason = "deviceId " + flow.deviceId();
329 return false;
330 }
331
332 // check treatment and instructions array
333 if (flow.treatment() != null) {
334 final JsonObject jsonTreatment = jsonFlow.get("treatment").asObject();
335 final JsonArray jsonInstructions = jsonTreatment.get("instructions").asArray();
Ray Milkey42507352015-03-20 15:16:10 -0700336 if (flow.treatment().immediate().size() != jsonInstructions.size()) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800337 reason = "instructions array size of " +
Ray Milkey42507352015-03-20 15:16:10 -0700338 Integer.toString(flow.treatment().immediate().size());
Ray Milkey4f5de002014-12-17 19:26:11 -0800339 return false;
340 }
Ray Milkey42507352015-03-20 15:16:10 -0700341 for (final Instruction instruction : flow.treatment().immediate()) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800342 boolean instructionFound = false;
Ray Milkey4f5de002014-12-17 19:26:11 -0800343 for (int instructionIndex = 0; instructionIndex < jsonInstructions.size(); instructionIndex++) {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800344 final String jsonType =
345 jsonInstructions.get(instructionIndex)
346 .asObject().get("type").asString();
347 final String instructionType = instruction.type().name();
348 if (jsonType.equals(instructionType)) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800349 instructionFound = true;
350 }
351 }
352 if (!instructionFound) {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800353 reason = "instruction " + instruction.toString();
Ray Milkey4f5de002014-12-17 19:26:11 -0800354 return false;
355 }
356 }
357 }
358
359 // check selector and criteria array
360 if (flow.selector() != null) {
361 final JsonObject jsonTreatment = jsonFlow.get("selector").asObject();
362 final JsonArray jsonCriteria = jsonTreatment.get("criteria").asArray();
363 if (flow.selector().criteria().size() != jsonCriteria.size()) {
364 reason = "criteria array size of " +
365 Integer.toString(flow.selector().criteria().size());
366 return false;
367 }
368 for (final Criterion criterion : flow.selector().criteria()) {
369 boolean criterionFound = false;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800370
Ray Milkey4f5de002014-12-17 19:26:11 -0800371 for (int criterionIndex = 0; criterionIndex < jsonCriteria.size(); criterionIndex++) {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800372 final String jsonType =
373 jsonCriteria.get(criterionIndex)
374 .asObject().get("type").asString();
375 final String criterionType = criterion.type().name();
376 if (jsonType.equals(criterionType)) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800377 criterionFound = true;
378 }
379 }
380 if (!criterionFound) {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800381 reason = "criterion " + criterion.toString();
Ray Milkey4f5de002014-12-17 19:26:11 -0800382 return false;
383 }
384 }
385 }
386
387 return true;
388 }
389
390 @Override
391 public void describeTo(Description description) {
392 description.appendText(reason);
393 }
394 }
395
396 /**
397 * Factory to allocate a flow matcher.
398 *
399 * @param flow flow object we are looking for
400 * @return matcher
401 */
402 private static FlowJsonMatcher matchesFlow(FlowEntry flow) {
403 return new FlowJsonMatcher(flow);
404 }
405
406 /**
407 * Hamcrest matcher to check that a flow is represented properly in a JSON
408 * array of flows.
409 */
410 public static class FlowJsonArrayMatcher extends TypeSafeMatcher<JsonArray> {
411 private final FlowEntry flow;
412 private String reason = "";
413
414 public FlowJsonArrayMatcher(FlowEntry flowValue) {
415 flow = flowValue;
416 }
417
418 @Override
419 public boolean matchesSafely(JsonArray json) {
420 boolean flowFound = false;
421
422 for (int jsonFlowIndex = 0; jsonFlowIndex < json.size();
423 jsonFlowIndex++) {
424
425 final JsonObject jsonFlow = json.get(jsonFlowIndex).asObject();
426
427 final String flowId = Long.toString(flow.id().value());
428 final String jsonFlowId = jsonFlow.get("id").asString();
429 if (jsonFlowId.equals(flowId)) {
430 flowFound = true;
431
432 // We found the correct flow, check attribute values
433 assertThat(jsonFlow, matchesFlow(flow));
434 }
435 }
436 if (!flowFound) {
437 reason = "Flow with id " + flow.id().toString() + " not found";
438 return false;
439 } else {
440 return true;
441 }
442 }
443
444 @Override
445 public void describeTo(Description description) {
446 description.appendText(reason);
447 }
448 }
449
450 /**
451 * Factory to allocate a flow array matcher.
452 *
453 * @param flow flow object we are looking for
454 * @return matcher
455 */
456 private static FlowJsonArrayMatcher hasFlow(FlowEntry flow) {
457 return new FlowJsonArrayMatcher(flow);
458 }
459
460 /**
461 * Tests the result of the rest api GET when there are no flows.
462 */
463 @Test
464 public void testFlowsEmptyArray() {
465 expect(mockFlowService.getFlowEntries(deviceId1))
466 .andReturn(null).anyTimes();
467 expect(mockFlowService.getFlowEntries(deviceId2))
468 .andReturn(null).anyTimes();
469 replay(mockFlowService);
470 replay(mockDeviceService);
471 final WebResource rs = resource();
472 final String response = rs.path("flows").get(String.class);
473 assertThat(response, is("{\"flows\":[]}"));
474 }
475
476 /**
477 * Tests the result of the rest api GET when there are active flows.
478 */
479 @Test
480 public void testFlowsPopulatedArray() {
481 setupMockFlows();
482 replay(mockFlowService);
483 replay(mockDeviceService);
484 final WebResource rs = resource();
485 final String response = rs.path("flows").get(String.class);
486 final JsonObject result = JsonObject.readFrom(response);
487 assertThat(result, notNullValue());
488
489 assertThat(result.names(), hasSize(1));
490 assertThat(result.names().get(0), is("flows"));
491 final JsonArray jsonFlows = result.get("flows").asArray();
492 assertThat(jsonFlows, notNullValue());
493 assertThat(jsonFlows, hasFlow(flow1));
494 assertThat(jsonFlows, hasFlow(flow2));
495 assertThat(jsonFlows, hasFlow(flow3));
496 assertThat(jsonFlows, hasFlow(flow4));
497 }
498
499 /**
500 * Tests the result of a rest api GET for a device.
501 */
502 @Test
503 public void testFlowsSingleDevice() {
504 setupMockFlows();
505 final Set<FlowEntry> flows = new HashSet<>();
506 flows.add(flow5);
507 flows.add(flow6);
508 expect(mockFlowService.getFlowEntries(anyObject()))
509 .andReturn(flows).anyTimes();
510 replay(mockFlowService);
511 replay(mockDeviceService);
512 final WebResource rs = resource();
513 final String response = rs.path("flows/" + deviceId3).get(String.class);
514 final JsonObject result = JsonObject.readFrom(response);
515 assertThat(result, notNullValue());
516
517 assertThat(result.names(), hasSize(1));
518 assertThat(result.names().get(0), is("flows"));
519 final JsonArray jsonFlows = result.get("flows").asArray();
520 assertThat(jsonFlows, notNullValue());
521 assertThat(jsonFlows, hasFlow(flow5));
522 assertThat(jsonFlows, hasFlow(flow6));
523 }
524
525 /**
526 * Tests the result of a rest api GET for a device.
527 */
528 @Test
529 public void testFlowsSingleDeviceWithFlowId() {
530 setupMockFlows();
531 final Set<FlowEntry> flows = new HashSet<>();
532 flows.add(flow5);
533 flows.add(flow6);
534 expect(mockFlowService.getFlowEntries(anyObject()))
535 .andReturn(flows).anyTimes();
536 replay(mockFlowService);
537 replay(mockDeviceService);
538 final WebResource rs = resource();
539 final String response = rs.path("flows/" + deviceId3 + "/"
540 + Long.toString(flow5.id().value())).get(String.class);
541 final JsonObject result = JsonObject.readFrom(response);
542 assertThat(result, notNullValue());
543
544 assertThat(result.names(), hasSize(1));
545 assertThat(result.names().get(0), is("flows"));
546 final JsonArray jsonFlows = result.get("flows").asArray();
547 assertThat(jsonFlows, notNullValue());
548 assertThat(jsonFlows, hasFlow(flow5));
549 assertThat(jsonFlows, not(hasFlow(flow6)));
550 }
551
552 /**
553 * Tests that a fetch of a non-existent device object throws an exception.
554 */
555 @Test
556 public void testBadGet() {
557 expect(mockFlowService.getFlowEntries(deviceId1))
558 .andReturn(null).anyTimes();
559 expect(mockFlowService.getFlowEntries(deviceId2))
560 .andReturn(null).anyTimes();
561 replay(mockFlowService);
562 replay(mockDeviceService);
563
564 WebResource rs = resource();
565 try {
566 rs.path("flows/0").get(String.class);
567 fail("Fetch of non-existent device did not throw an exception");
568 } catch (UniformInterfaceException ex) {
569 assertThat(ex.getMessage(),
570 containsString("returned a response status of"));
571 }
572 }
Ray Milkeyd43fe452015-05-29 09:35:12 -0700573
574 /**
575 * Tests creating a flow with POST.
576 */
577 @Test
578 public void testPost() {
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700579
Ray Milkeyd43fe452015-05-29 09:35:12 -0700580
581 mockFlowService.applyFlowRules(anyObject());
582 expectLastCall();
583 replay(mockFlowService);
584
585 WebResource rs = resource();
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700586 InputStream jsonStream = IntentsResourceTest.class
587 .getResourceAsStream("post-flow.json");
Ray Milkeyd43fe452015-05-29 09:35:12 -0700588
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700589 ClientResponse response = rs.path("flows/of:0000000000000001")
Ray Milkeyd43fe452015-05-29 09:35:12 -0700590 .type(MediaType.APPLICATION_JSON_TYPE)
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700591 .post(ClientResponse.class, jsonStream);
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700592 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED));
593 String location = response.getLocation().getPath();
594 assertThat(location, Matchers.startsWith("/flows/of:0000000000000001/"));
595 }
596
597 /**
598 * Tests deleting a flow.
599 */
600 @Test
601 public void testDelete() {
602 setupMockFlows();
603 mockFlowService.removeFlowRules(anyObject());
604 expectLastCall();
605 replay(mockFlowService);
606
607 WebResource rs = resource();
608
609 String location = "/flows/1/155";
610
611 ClientResponse deleteResponse = rs.path(location)
612 .type(MediaType.APPLICATION_JSON_TYPE)
613 .delete(ClientResponse.class);
614 assertThat(deleteResponse.getStatus(),
615 is(HttpURLConnection.HTTP_NO_CONTENT));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700616 }
Ray Milkey4f5de002014-12-17 19:26:11 -0800617}