blob: 192c417b4d5557b2e85ddea211024d81f9e02c8c [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
Ray Milkeyb82c42b2015-06-30 09:42:20 -070018import java.io.InputStream;
Ray Milkeyd43fe452015-05-29 09:35:12 -070019import java.net.HttpURLConnection;
Ray Milkey4f5de002014-12-17 19:26:11 -080020import java.util.HashMap;
21import java.util.HashSet;
22import java.util.Set;
23
Ray Milkeyd43fe452015-05-29 09:35:12 -070024import javax.ws.rs.core.MediaType;
25
Ray Milkey4f5de002014-12-17 19:26:11 -080026import org.hamcrest.Description;
Ray Milkeyeb5c7172015-06-23 14:59:27 -070027import org.hamcrest.Matchers;
Ray Milkey4f5de002014-12-17 19:26:11 -080028import org.hamcrest.TypeSafeMatcher;
29import org.junit.After;
30import org.junit.Before;
31import org.junit.Test;
32import org.onlab.osgi.ServiceDirectory;
33import org.onlab.osgi.TestServiceDirectory;
34import org.onlab.packet.MacAddress;
35import org.onlab.rest.BaseResource;
36import org.onosproject.codec.CodecService;
37import org.onosproject.codec.impl.CodecManager;
Ray Milkeyeb5c7172015-06-23 14:59:27 -070038import org.onosproject.codec.impl.FlowRuleCodec;
Ray Milkeyd43fe452015-05-29 09:35:12 -070039import org.onosproject.core.CoreService;
Ray Milkey4f5de002014-12-17 19:26:11 -080040import org.onosproject.core.DefaultGroupId;
41import org.onosproject.core.GroupId;
42import org.onosproject.net.DefaultDevice;
43import org.onosproject.net.Device;
44import org.onosproject.net.DeviceId;
Ray Milkeyeb5c7172015-06-23 14:59:27 -070045import org.onosproject.net.IndexedLambda;
Ray Milkeyd43fe452015-05-29 09:35:12 -070046import org.onosproject.net.NetTestTools;
Ray Milkey4f5de002014-12-17 19:26:11 -080047import org.onosproject.net.device.DeviceService;
48import org.onosproject.net.flow.DefaultTrafficSelector;
49import org.onosproject.net.flow.DefaultTrafficTreatment;
50import org.onosproject.net.flow.FlowEntry;
51import org.onosproject.net.flow.FlowId;
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;
jcc3d4e14a2015-04-21 11:32:05 +080059
Ray Milkey4f5de002014-12-17 19:26:11 -080060import com.eclipsesource.json.JsonArray;
61import com.eclipsesource.json.JsonObject;
Ray Milkey4f5de002014-12-17 19:26:11 -080062import com.google.common.collect.ImmutableSet;
Ray Milkeyd43fe452015-05-29 09:35:12 -070063import com.sun.jersey.api.client.ClientResponse;
Ray Milkey4f5de002014-12-17 19:26:11 -080064import com.sun.jersey.api.client.UniformInterfaceException;
65import com.sun.jersey.api.client.WebResource;
Ray Milkey4f5de002014-12-17 19:26:11 -080066
67import static org.easymock.EasyMock.anyObject;
Ray Milkeyd43fe452015-05-29 09:35:12 -070068import static org.easymock.EasyMock.anyShort;
Ray Milkey4f5de002014-12-17 19:26:11 -080069import static org.easymock.EasyMock.createMock;
70import static org.easymock.EasyMock.expect;
Ray Milkeyd43fe452015-05-29 09:35:12 -070071import static org.easymock.EasyMock.expectLastCall;
Ray Milkey4f5de002014-12-17 19:26:11 -080072import static org.easymock.EasyMock.replay;
73import static org.easymock.EasyMock.verify;
74import static org.hamcrest.Matchers.containsString;
75import static org.hamcrest.Matchers.hasSize;
76import static org.hamcrest.Matchers.is;
77import static org.hamcrest.Matchers.not;
78import static org.hamcrest.Matchers.notNullValue;
79import static org.junit.Assert.assertThat;
80import static org.junit.Assert.fail;
Ray Milkeyeb5c7172015-06-23 14:59:27 -070081import static org.onosproject.net.NetTestTools.APP_ID;
Ray Milkey4f5de002014-12-17 19:26:11 -080082
83/**
84 * Unit tests for Flows REST APIs.
85 */
Ray Milkey9c3d3362015-01-28 10:39:56 -080086public class FlowsResourceTest extends ResourceTest {
Ray Milkey4f5de002014-12-17 19:26:11 -080087 final FlowRuleService mockFlowService = createMock(FlowRuleService.class);
Ray Milkeyd43fe452015-05-29 09:35:12 -070088 CoreService mockCoreService = createMock(CoreService.class);
89
Ray Milkey4f5de002014-12-17 19:26:11 -080090 final HashMap<DeviceId, Set<FlowEntry>> rules = new HashMap<>();
91
92 final DeviceService mockDeviceService = createMock(DeviceService.class);
93
94 final DeviceId deviceId1 = DeviceId.deviceId("1");
95 final DeviceId deviceId2 = DeviceId.deviceId("2");
96 final DeviceId deviceId3 = DeviceId.deviceId("3");
97 final Device device1 = new DefaultDevice(null, deviceId1, Device.Type.OTHER,
98 "", "", "", "", null);
99 final Device device2 = new DefaultDevice(null, deviceId2, Device.Type.OTHER,
100 "", "", "", "", null);
101
102 final MockFlowEntry flow1 = new MockFlowEntry(deviceId1, 1);
103 final MockFlowEntry flow2 = new MockFlowEntry(deviceId1, 2);
104
105 final MockFlowEntry flow3 = new MockFlowEntry(deviceId2, 3);
106 final MockFlowEntry flow4 = new MockFlowEntry(deviceId2, 4);
107
108 final MockFlowEntry flow5 = new MockFlowEntry(deviceId2, 5);
109 final MockFlowEntry flow6 = new MockFlowEntry(deviceId2, 6);
110
111 /**
112 * Mock class for a flow entry.
113 */
114 private static class MockFlowEntry implements FlowEntry {
115 final DeviceId deviceId;
116 final long baseValue;
117 TrafficTreatment treatment;
118 TrafficSelector selector;
119
120 public MockFlowEntry(DeviceId deviceId, long id) {
121 this.deviceId = deviceId;
122 this.baseValue = id * 100;
123 }
124
125 @Override
126 public FlowEntryState state() {
127 return FlowEntryState.ADDED;
128 }
129
130 @Override
131 public long life() {
132 return baseValue + 11;
133 }
134
135 @Override
136 public long packets() {
137 return baseValue + 22;
138 }
139
140 @Override
141 public long bytes() {
142 return baseValue + 33;
143 }
144
145 @Override
146 public long lastSeen() {
147 return baseValue + 44;
148 }
149
150 @Override
151 public int errType() {
152 return 0;
153 }
154
155 @Override
156 public int errCode() {
157 return 0;
158 }
159
160 @Override
161 public FlowId id() {
162 final long id = baseValue + 55;
163 return FlowId.valueOf(id);
164 }
165
166 @Override
alshabib08d98982015-04-21 16:25:50 -0700167 public GroupId groupId() {
168 return new DefaultGroupId(3);
Ray Milkey4f5de002014-12-17 19:26:11 -0800169 }
170
171 @Override
alshabib08d98982015-04-21 16:25:50 -0700172 public short appId() {
173 return 2;
Ray Milkey4f5de002014-12-17 19:26:11 -0800174 }
175
176 @Override
177 public int priority() {
178 return (int) (baseValue + 66);
179 }
180
181 @Override
182 public DeviceId deviceId() {
183 return deviceId;
184 }
185
186 @Override
187 public TrafficSelector selector() {
188 return selector;
189 }
190
191 @Override
192 public TrafficTreatment treatment() {
193 return treatment;
194 }
195
196 @Override
197 public int timeout() {
198 return (int) (baseValue + 77);
199 }
200
201 @Override
202 public boolean isPermanent() {
203 return false;
204 }
sangho11c30ac2015-01-22 14:30:55 -0800205
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800206 @Override
alshabibdb774072015-04-20 13:13:51 -0700207 public int tableId() {
208 return 0;
209 }
jcc3d4e14a2015-04-21 11:32:05 +0800210
211 @Override
212 public FlowRuleExtPayLoad payLoad() {
213 return null;
214 }
Ray Milkey4f5de002014-12-17 19:26:11 -0800215 }
216
Ray Milkey4f5de002014-12-17 19:26:11 -0800217 /**
218 * Populates some flows used as testing data.
219 */
220 private void setupMockFlows() {
221 flow2.treatment = DefaultTrafficTreatment.builder()
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700222 .add(Instructions.modL0Lambda(new IndexedLambda((short) 4)))
223 .add(Instructions.modL0Lambda(new IndexedLambda((short) 5)))
Ray Milkey4f5de002014-12-17 19:26:11 -0800224 .setEthDst(MacAddress.BROADCAST)
225 .build();
226 flow2.selector = DefaultTrafficSelector.builder()
227 .matchEthType((short) 3)
228 .matchIPProtocol((byte) 9)
229 .build();
230 flow4.treatment = DefaultTrafficTreatment.builder()
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700231 .add(Instructions.modL0Lambda(new IndexedLambda((short) 6)))
Ray Milkey4f5de002014-12-17 19:26:11 -0800232 .build();
233 final Set<FlowEntry> flows1 = new HashSet<>();
234 flows1.add(flow1);
235 flows1.add(flow2);
236
237 final Set<FlowEntry> flows2 = new HashSet<>();
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700238 flows2.add(flow3);
239 flows2.add(flow4);
Ray Milkey4f5de002014-12-17 19:26:11 -0800240
241 rules.put(deviceId1, flows1);
242 rules.put(deviceId2, flows2);
243
244 expect(mockFlowService.getFlowEntries(deviceId1))
245 .andReturn(rules.get(deviceId1)).anyTimes();
246 expect(mockFlowService.getFlowEntries(deviceId2))
247 .andReturn(rules.get(deviceId2)).anyTimes();
248 }
249
250 /**
251 * Sets up the global values for all the tests.
252 */
253 @Before
Ray Milkeyed0b1662015-02-05 09:34:29 -0800254 public void setUpTest() {
Ray Milkey4f5de002014-12-17 19:26:11 -0800255 // Mock device service
256 expect(mockDeviceService.getDevice(deviceId1))
257 .andReturn(device1);
258 expect(mockDeviceService.getDevice(deviceId2))
259 .andReturn(device2);
260 expect(mockDeviceService.getDevices())
261 .andReturn(ImmutableSet.of(device1, device2));
262
Ray Milkeyd43fe452015-05-29 09:35:12 -0700263 // Mock Core Service
264 expect(mockCoreService.getAppId(anyShort()))
265 .andReturn(NetTestTools.APP_ID).anyTimes();
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700266 expect(mockCoreService.registerApplication(FlowRuleCodec.REST_APP_ID))
267 .andReturn(APP_ID).anyTimes();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700268 replay(mockCoreService);
269
Ray Milkey4f5de002014-12-17 19:26:11 -0800270 // Register the services needed for the test
271 final CodecManager codecService = new CodecManager();
272 codecService.activate();
273 ServiceDirectory testDirectory =
274 new TestServiceDirectory()
275 .add(FlowRuleService.class, mockFlowService)
276 .add(DeviceService.class, mockDeviceService)
Ray Milkeyd43fe452015-05-29 09:35:12 -0700277 .add(CodecService.class, codecService)
278 .add(CoreService.class, mockCoreService);
Ray Milkey4f5de002014-12-17 19:26:11 -0800279
280 BaseResource.setServiceDirectory(testDirectory);
281 }
282
283 /**
284 * Cleans up and verifies the mocks.
Ray Milkey4f5de002014-12-17 19:26:11 -0800285 */
286 @After
Ray Milkeyed0b1662015-02-05 09:34:29 -0800287 public void tearDownTest() {
Ray Milkey4f5de002014-12-17 19:26:11 -0800288 verify(mockFlowService);
Ray Milkeyd43fe452015-05-29 09:35:12 -0700289 verify(mockCoreService);
Ray Milkey4f5de002014-12-17 19:26:11 -0800290 }
291
292 /**
293 * Hamcrest matcher to check that a flow representation in JSON matches
294 * the actual flow entry.
295 */
296 public static class FlowJsonMatcher extends TypeSafeMatcher<JsonObject> {
297 private final FlowEntry flow;
298 private String reason = "";
299
300 public FlowJsonMatcher(FlowEntry flowValue) {
301 flow = flowValue;
302 }
303
304 @Override
305 public boolean matchesSafely(JsonObject jsonFlow) {
306 // check id
307 final String jsonId = jsonFlow.get("id").asString();
308 final String flowId = Long.toString(flow.id().value());
309 if (!jsonId.equals(flowId)) {
310 reason = "id " + flow.id().toString();
311 return false;
312 }
313
314 // check application id
315 final int jsonAppId = jsonFlow.get("appId").asInt();
316 if (jsonAppId != flow.appId()) {
317 reason = "appId " + Short.toString(flow.appId());
318 return false;
319 }
320
321 // check device id
322 final String jsonDeviceId = jsonFlow.get("deviceId").asString();
323 if (!jsonDeviceId.equals(flow.deviceId().toString())) {
324 reason = "deviceId " + flow.deviceId();
325 return false;
326 }
327
328 // check treatment and instructions array
329 if (flow.treatment() != null) {
330 final JsonObject jsonTreatment = jsonFlow.get("treatment").asObject();
331 final JsonArray jsonInstructions = jsonTreatment.get("instructions").asArray();
Ray Milkey42507352015-03-20 15:16:10 -0700332 if (flow.treatment().immediate().size() != jsonInstructions.size()) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800333 reason = "instructions array size of " +
Ray Milkey42507352015-03-20 15:16:10 -0700334 Integer.toString(flow.treatment().immediate().size());
Ray Milkey4f5de002014-12-17 19:26:11 -0800335 return false;
336 }
Ray Milkey42507352015-03-20 15:16:10 -0700337 for (final Instruction instruction : flow.treatment().immediate()) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800338 boolean instructionFound = false;
Ray Milkey4f5de002014-12-17 19:26:11 -0800339 for (int instructionIndex = 0; instructionIndex < jsonInstructions.size(); instructionIndex++) {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800340 final String jsonType =
341 jsonInstructions.get(instructionIndex)
342 .asObject().get("type").asString();
343 final String instructionType = instruction.type().name();
344 if (jsonType.equals(instructionType)) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800345 instructionFound = true;
346 }
347 }
348 if (!instructionFound) {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800349 reason = "instruction " + instruction.toString();
Ray Milkey4f5de002014-12-17 19:26:11 -0800350 return false;
351 }
352 }
353 }
354
355 // check selector and criteria array
356 if (flow.selector() != null) {
357 final JsonObject jsonTreatment = jsonFlow.get("selector").asObject();
358 final JsonArray jsonCriteria = jsonTreatment.get("criteria").asArray();
359 if (flow.selector().criteria().size() != jsonCriteria.size()) {
360 reason = "criteria array size of " +
361 Integer.toString(flow.selector().criteria().size());
362 return false;
363 }
364 for (final Criterion criterion : flow.selector().criteria()) {
365 boolean criterionFound = false;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800366
Ray Milkey4f5de002014-12-17 19:26:11 -0800367 for (int criterionIndex = 0; criterionIndex < jsonCriteria.size(); criterionIndex++) {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800368 final String jsonType =
369 jsonCriteria.get(criterionIndex)
370 .asObject().get("type").asString();
371 final String criterionType = criterion.type().name();
372 if (jsonType.equals(criterionType)) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800373 criterionFound = true;
374 }
375 }
376 if (!criterionFound) {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800377 reason = "criterion " + criterion.toString();
Ray Milkey4f5de002014-12-17 19:26:11 -0800378 return false;
379 }
380 }
381 }
382
383 return true;
384 }
385
386 @Override
387 public void describeTo(Description description) {
388 description.appendText(reason);
389 }
390 }
391
392 /**
393 * Factory to allocate a flow matcher.
394 *
395 * @param flow flow object we are looking for
396 * @return matcher
397 */
398 private static FlowJsonMatcher matchesFlow(FlowEntry flow) {
399 return new FlowJsonMatcher(flow);
400 }
401
402 /**
403 * Hamcrest matcher to check that a flow is represented properly in a JSON
404 * array of flows.
405 */
406 public static class FlowJsonArrayMatcher extends TypeSafeMatcher<JsonArray> {
407 private final FlowEntry flow;
408 private String reason = "";
409
410 public FlowJsonArrayMatcher(FlowEntry flowValue) {
411 flow = flowValue;
412 }
413
414 @Override
415 public boolean matchesSafely(JsonArray json) {
416 boolean flowFound = false;
417
418 for (int jsonFlowIndex = 0; jsonFlowIndex < json.size();
419 jsonFlowIndex++) {
420
421 final JsonObject jsonFlow = json.get(jsonFlowIndex).asObject();
422
423 final String flowId = Long.toString(flow.id().value());
424 final String jsonFlowId = jsonFlow.get("id").asString();
425 if (jsonFlowId.equals(flowId)) {
426 flowFound = true;
427
428 // We found the correct flow, check attribute values
429 assertThat(jsonFlow, matchesFlow(flow));
430 }
431 }
432 if (!flowFound) {
433 reason = "Flow with id " + flow.id().toString() + " not found";
434 return false;
435 } else {
436 return true;
437 }
438 }
439
440 @Override
441 public void describeTo(Description description) {
442 description.appendText(reason);
443 }
444 }
445
446 /**
447 * Factory to allocate a flow array matcher.
448 *
449 * @param flow flow object we are looking for
450 * @return matcher
451 */
452 private static FlowJsonArrayMatcher hasFlow(FlowEntry flow) {
453 return new FlowJsonArrayMatcher(flow);
454 }
455
456 /**
457 * Tests the result of the rest api GET when there are no flows.
458 */
459 @Test
460 public void testFlowsEmptyArray() {
461 expect(mockFlowService.getFlowEntries(deviceId1))
462 .andReturn(null).anyTimes();
463 expect(mockFlowService.getFlowEntries(deviceId2))
464 .andReturn(null).anyTimes();
465 replay(mockFlowService);
466 replay(mockDeviceService);
467 final WebResource rs = resource();
468 final String response = rs.path("flows").get(String.class);
469 assertThat(response, is("{\"flows\":[]}"));
470 }
471
472 /**
473 * Tests the result of the rest api GET when there are active flows.
474 */
475 @Test
476 public void testFlowsPopulatedArray() {
477 setupMockFlows();
478 replay(mockFlowService);
479 replay(mockDeviceService);
480 final WebResource rs = resource();
481 final String response = rs.path("flows").get(String.class);
482 final JsonObject result = JsonObject.readFrom(response);
483 assertThat(result, notNullValue());
484
485 assertThat(result.names(), hasSize(1));
486 assertThat(result.names().get(0), is("flows"));
487 final JsonArray jsonFlows = result.get("flows").asArray();
488 assertThat(jsonFlows, notNullValue());
489 assertThat(jsonFlows, hasFlow(flow1));
490 assertThat(jsonFlows, hasFlow(flow2));
491 assertThat(jsonFlows, hasFlow(flow3));
492 assertThat(jsonFlows, hasFlow(flow4));
493 }
494
495 /**
496 * Tests the result of a rest api GET for a device.
497 */
498 @Test
499 public void testFlowsSingleDevice() {
500 setupMockFlows();
501 final Set<FlowEntry> flows = new HashSet<>();
502 flows.add(flow5);
503 flows.add(flow6);
504 expect(mockFlowService.getFlowEntries(anyObject()))
505 .andReturn(flows).anyTimes();
506 replay(mockFlowService);
507 replay(mockDeviceService);
508 final WebResource rs = resource();
509 final String response = rs.path("flows/" + deviceId3).get(String.class);
510 final JsonObject result = JsonObject.readFrom(response);
511 assertThat(result, notNullValue());
512
513 assertThat(result.names(), hasSize(1));
514 assertThat(result.names().get(0), is("flows"));
515 final JsonArray jsonFlows = result.get("flows").asArray();
516 assertThat(jsonFlows, notNullValue());
517 assertThat(jsonFlows, hasFlow(flow5));
518 assertThat(jsonFlows, hasFlow(flow6));
519 }
520
521 /**
522 * Tests the result of a rest api GET for a device.
523 */
524 @Test
525 public void testFlowsSingleDeviceWithFlowId() {
526 setupMockFlows();
527 final Set<FlowEntry> flows = new HashSet<>();
528 flows.add(flow5);
529 flows.add(flow6);
530 expect(mockFlowService.getFlowEntries(anyObject()))
531 .andReturn(flows).anyTimes();
532 replay(mockFlowService);
533 replay(mockDeviceService);
534 final WebResource rs = resource();
535 final String response = rs.path("flows/" + deviceId3 + "/"
536 + Long.toString(flow5.id().value())).get(String.class);
537 final JsonObject result = JsonObject.readFrom(response);
538 assertThat(result, notNullValue());
539
540 assertThat(result.names(), hasSize(1));
541 assertThat(result.names().get(0), is("flows"));
542 final JsonArray jsonFlows = result.get("flows").asArray();
543 assertThat(jsonFlows, notNullValue());
544 assertThat(jsonFlows, hasFlow(flow5));
545 assertThat(jsonFlows, not(hasFlow(flow6)));
546 }
547
548 /**
549 * Tests that a fetch of a non-existent device object throws an exception.
550 */
551 @Test
552 public void testBadGet() {
553 expect(mockFlowService.getFlowEntries(deviceId1))
554 .andReturn(null).anyTimes();
555 expect(mockFlowService.getFlowEntries(deviceId2))
556 .andReturn(null).anyTimes();
557 replay(mockFlowService);
558 replay(mockDeviceService);
559
560 WebResource rs = resource();
561 try {
562 rs.path("flows/0").get(String.class);
563 fail("Fetch of non-existent device did not throw an exception");
564 } catch (UniformInterfaceException ex) {
565 assertThat(ex.getMessage(),
566 containsString("returned a response status of"));
567 }
568 }
Ray Milkeyd43fe452015-05-29 09:35:12 -0700569
570 /**
571 * Tests creating a flow with POST.
572 */
573 @Test
574 public void testPost() {
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700575
Ray Milkeyd43fe452015-05-29 09:35:12 -0700576
577 mockFlowService.applyFlowRules(anyObject());
578 expectLastCall();
579 replay(mockFlowService);
580
581 WebResource rs = resource();
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700582 InputStream jsonStream = IntentsResourceTest.class
583 .getResourceAsStream("post-flow.json");
Ray Milkeyd43fe452015-05-29 09:35:12 -0700584
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700585 ClientResponse response = rs.path("flows/of:0000000000000001")
Ray Milkeyd43fe452015-05-29 09:35:12 -0700586 .type(MediaType.APPLICATION_JSON_TYPE)
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700587 .post(ClientResponse.class, jsonStream);
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700588 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED));
589 String location = response.getLocation().getPath();
590 assertThat(location, Matchers.startsWith("/flows/of:0000000000000001/"));
591 }
592
593 /**
594 * Tests deleting a flow.
595 */
596 @Test
597 public void testDelete() {
598 setupMockFlows();
599 mockFlowService.removeFlowRules(anyObject());
600 expectLastCall();
601 replay(mockFlowService);
602
603 WebResource rs = resource();
604
605 String location = "/flows/1/155";
606
607 ClientResponse deleteResponse = rs.path(location)
608 .type(MediaType.APPLICATION_JSON_TYPE)
609 .delete(ClientResponse.class);
610 assertThat(deleteResponse.getStatus(),
611 is(HttpURLConnection.HTTP_NO_CONTENT));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700612 }
Ray Milkey4f5de002014-12-17 19:26:11 -0800613}