blob: 63e96444b812bcecab20daeec4aaf1758f4e7e36 [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 Milkeyd43fe452015-05-29 09:35:12 -070018import java.net.HttpURLConnection;
Ray Milkey4f5de002014-12-17 19:26:11 -080019import java.util.HashMap;
20import java.util.HashSet;
21import java.util.Set;
22
Ray Milkeyd43fe452015-05-29 09:35:12 -070023import javax.ws.rs.core.MediaType;
24
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;
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
Ray Milkey4f5de002014-12-17 19:26:11 -080059import com.eclipsesource.json.JsonArray;
60import com.eclipsesource.json.JsonObject;
Ray Milkey4f5de002014-12-17 19:26:11 -080061import com.google.common.collect.ImmutableSet;
Ray Milkeyd43fe452015-05-29 09:35:12 -070062import com.sun.jersey.api.client.ClientResponse;
Ray Milkey4f5de002014-12-17 19:26:11 -080063import com.sun.jersey.api.client.UniformInterfaceException;
64import com.sun.jersey.api.client.WebResource;
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
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700110 private static final String FLOW_JSON = "{\"priority\":1,\"isPermanent\":true,"
111 + "\"treatment\":{\"instructions\":[ {\"type\":\"OUTPUT\",\"port\":2}]},"
112 + "\"selector\":{\"criteria\":[ {\"type\":\"ETH_TYPE\",\"ethType\":2054}]}}";
113
Ray Milkey4f5de002014-12-17 19:26:11 -0800114 /**
115 * Mock class for a flow entry.
116 */
117 private static class MockFlowEntry implements FlowEntry {
118 final DeviceId deviceId;
119 final long baseValue;
120 TrafficTreatment treatment;
121 TrafficSelector selector;
122
123 public MockFlowEntry(DeviceId deviceId, long id) {
124 this.deviceId = deviceId;
125 this.baseValue = id * 100;
126 }
127
128 @Override
129 public FlowEntryState state() {
130 return FlowEntryState.ADDED;
131 }
132
133 @Override
134 public long life() {
135 return baseValue + 11;
136 }
137
138 @Override
139 public long packets() {
140 return baseValue + 22;
141 }
142
143 @Override
144 public long bytes() {
145 return baseValue + 33;
146 }
147
148 @Override
149 public long lastSeen() {
150 return baseValue + 44;
151 }
152
153 @Override
154 public int errType() {
155 return 0;
156 }
157
158 @Override
159 public int errCode() {
160 return 0;
161 }
162
163 @Override
164 public FlowId id() {
165 final long id = baseValue + 55;
166 return FlowId.valueOf(id);
167 }
168
169 @Override
alshabib08d98982015-04-21 16:25:50 -0700170 public GroupId groupId() {
171 return new DefaultGroupId(3);
Ray Milkey4f5de002014-12-17 19:26:11 -0800172 }
173
174 @Override
alshabib08d98982015-04-21 16:25:50 -0700175 public short appId() {
176 return 2;
Ray Milkey4f5de002014-12-17 19:26:11 -0800177 }
178
179 @Override
180 public int priority() {
181 return (int) (baseValue + 66);
182 }
183
184 @Override
185 public DeviceId deviceId() {
186 return deviceId;
187 }
188
189 @Override
190 public TrafficSelector selector() {
191 return selector;
192 }
193
194 @Override
195 public TrafficTreatment treatment() {
196 return treatment;
197 }
198
199 @Override
200 public int timeout() {
201 return (int) (baseValue + 77);
202 }
203
204 @Override
205 public boolean isPermanent() {
206 return false;
207 }
sangho11c30ac2015-01-22 14:30:55 -0800208
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800209 @Override
alshabibdb774072015-04-20 13:13:51 -0700210 public int tableId() {
211 return 0;
212 }
jcc3d4e14a2015-04-21 11:32:05 +0800213
214 @Override
215 public FlowRuleExtPayLoad payLoad() {
216 return null;
217 }
Ray Milkey4f5de002014-12-17 19:26:11 -0800218 }
219
Ray Milkey4f5de002014-12-17 19:26:11 -0800220 /**
221 * Populates some flows used as testing data.
222 */
223 private void setupMockFlows() {
224 flow2.treatment = DefaultTrafficTreatment.builder()
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700225 .add(Instructions.modL0Lambda(new IndexedLambda((short) 4)))
226 .add(Instructions.modL0Lambda(new IndexedLambda((short) 5)))
Ray Milkey4f5de002014-12-17 19:26:11 -0800227 .setEthDst(MacAddress.BROADCAST)
228 .build();
229 flow2.selector = DefaultTrafficSelector.builder()
230 .matchEthType((short) 3)
231 .matchIPProtocol((byte) 9)
232 .build();
233 flow4.treatment = DefaultTrafficTreatment.builder()
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700234 .add(Instructions.modL0Lambda(new IndexedLambda((short) 6)))
Ray Milkey4f5de002014-12-17 19:26:11 -0800235 .build();
236 final Set<FlowEntry> flows1 = new HashSet<>();
237 flows1.add(flow1);
238 flows1.add(flow2);
239
240 final Set<FlowEntry> flows2 = new HashSet<>();
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700241 flows2.add(flow3);
242 flows2.add(flow4);
Ray Milkey4f5de002014-12-17 19:26:11 -0800243
244 rules.put(deviceId1, flows1);
245 rules.put(deviceId2, flows2);
246
247 expect(mockFlowService.getFlowEntries(deviceId1))
248 .andReturn(rules.get(deviceId1)).anyTimes();
249 expect(mockFlowService.getFlowEntries(deviceId2))
250 .andReturn(rules.get(deviceId2)).anyTimes();
251 }
252
253 /**
254 * Sets up the global values for all the tests.
255 */
256 @Before
Ray Milkeyed0b1662015-02-05 09:34:29 -0800257 public void setUpTest() {
Ray Milkey4f5de002014-12-17 19:26:11 -0800258 // Mock device service
259 expect(mockDeviceService.getDevice(deviceId1))
260 .andReturn(device1);
261 expect(mockDeviceService.getDevice(deviceId2))
262 .andReturn(device2);
263 expect(mockDeviceService.getDevices())
264 .andReturn(ImmutableSet.of(device1, device2));
265
Ray Milkeyd43fe452015-05-29 09:35:12 -0700266 // Mock Core Service
267 expect(mockCoreService.getAppId(anyShort()))
268 .andReturn(NetTestTools.APP_ID).anyTimes();
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700269 expect(mockCoreService.registerApplication(FlowRuleCodec.REST_APP_ID))
270 .andReturn(APP_ID).anyTimes();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700271 replay(mockCoreService);
272
Ray Milkey4f5de002014-12-17 19:26:11 -0800273 // Register the services needed for the test
274 final CodecManager codecService = new CodecManager();
275 codecService.activate();
276 ServiceDirectory testDirectory =
277 new TestServiceDirectory()
278 .add(FlowRuleService.class, mockFlowService)
279 .add(DeviceService.class, mockDeviceService)
Ray Milkeyd43fe452015-05-29 09:35:12 -0700280 .add(CodecService.class, codecService)
281 .add(CoreService.class, mockCoreService);
Ray Milkey4f5de002014-12-17 19:26:11 -0800282
283 BaseResource.setServiceDirectory(testDirectory);
284 }
285
286 /**
287 * Cleans up and verifies the mocks.
Ray Milkey4f5de002014-12-17 19:26:11 -0800288 */
289 @After
Ray Milkeyed0b1662015-02-05 09:34:29 -0800290 public void tearDownTest() {
Ray Milkey4f5de002014-12-17 19:26:11 -0800291 verify(mockFlowService);
Ray Milkeyd43fe452015-05-29 09:35:12 -0700292 verify(mockCoreService);
Ray Milkey4f5de002014-12-17 19:26:11 -0800293 }
294
295 /**
296 * Hamcrest matcher to check that a flow representation in JSON matches
297 * the actual flow entry.
298 */
299 public static class FlowJsonMatcher extends TypeSafeMatcher<JsonObject> {
300 private final FlowEntry flow;
301 private String reason = "";
302
303 public FlowJsonMatcher(FlowEntry flowValue) {
304 flow = flowValue;
305 }
306
307 @Override
308 public boolean matchesSafely(JsonObject jsonFlow) {
309 // check id
310 final String jsonId = jsonFlow.get("id").asString();
311 final String flowId = Long.toString(flow.id().value());
312 if (!jsonId.equals(flowId)) {
313 reason = "id " + flow.id().toString();
314 return false;
315 }
316
317 // check application id
318 final int jsonAppId = jsonFlow.get("appId").asInt();
319 if (jsonAppId != flow.appId()) {
320 reason = "appId " + Short.toString(flow.appId());
321 return false;
322 }
323
324 // check device id
325 final String jsonDeviceId = jsonFlow.get("deviceId").asString();
326 if (!jsonDeviceId.equals(flow.deviceId().toString())) {
327 reason = "deviceId " + flow.deviceId();
328 return false;
329 }
330
331 // check treatment and instructions array
332 if (flow.treatment() != null) {
333 final JsonObject jsonTreatment = jsonFlow.get("treatment").asObject();
334 final JsonArray jsonInstructions = jsonTreatment.get("instructions").asArray();
Ray Milkey42507352015-03-20 15:16:10 -0700335 if (flow.treatment().immediate().size() != jsonInstructions.size()) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800336 reason = "instructions array size of " +
Ray Milkey42507352015-03-20 15:16:10 -0700337 Integer.toString(flow.treatment().immediate().size());
Ray Milkey4f5de002014-12-17 19:26:11 -0800338 return false;
339 }
Ray Milkey42507352015-03-20 15:16:10 -0700340 for (final Instruction instruction : flow.treatment().immediate()) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800341 boolean instructionFound = false;
Ray Milkey4f5de002014-12-17 19:26:11 -0800342 for (int instructionIndex = 0; instructionIndex < jsonInstructions.size(); instructionIndex++) {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800343 final String jsonType =
344 jsonInstructions.get(instructionIndex)
345 .asObject().get("type").asString();
346 final String instructionType = instruction.type().name();
347 if (jsonType.equals(instructionType)) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800348 instructionFound = true;
349 }
350 }
351 if (!instructionFound) {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800352 reason = "instruction " + instruction.toString();
Ray Milkey4f5de002014-12-17 19:26:11 -0800353 return false;
354 }
355 }
356 }
357
358 // check selector and criteria array
359 if (flow.selector() != null) {
360 final JsonObject jsonTreatment = jsonFlow.get("selector").asObject();
361 final JsonArray jsonCriteria = jsonTreatment.get("criteria").asArray();
362 if (flow.selector().criteria().size() != jsonCriteria.size()) {
363 reason = "criteria array size of " +
364 Integer.toString(flow.selector().criteria().size());
365 return false;
366 }
367 for (final Criterion criterion : flow.selector().criteria()) {
368 boolean criterionFound = false;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800369
Ray Milkey4f5de002014-12-17 19:26:11 -0800370 for (int criterionIndex = 0; criterionIndex < jsonCriteria.size(); criterionIndex++) {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800371 final String jsonType =
372 jsonCriteria.get(criterionIndex)
373 .asObject().get("type").asString();
374 final String criterionType = criterion.type().name();
375 if (jsonType.equals(criterionType)) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800376 criterionFound = true;
377 }
378 }
379 if (!criterionFound) {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800380 reason = "criterion " + criterion.toString();
Ray Milkey4f5de002014-12-17 19:26:11 -0800381 return false;
382 }
383 }
384 }
385
386 return true;
387 }
388
389 @Override
390 public void describeTo(Description description) {
391 description.appendText(reason);
392 }
393 }
394
395 /**
396 * Factory to allocate a flow matcher.
397 *
398 * @param flow flow object we are looking for
399 * @return matcher
400 */
401 private static FlowJsonMatcher matchesFlow(FlowEntry flow) {
402 return new FlowJsonMatcher(flow);
403 }
404
405 /**
406 * Hamcrest matcher to check that a flow is represented properly in a JSON
407 * array of flows.
408 */
409 public static class FlowJsonArrayMatcher extends TypeSafeMatcher<JsonArray> {
410 private final FlowEntry flow;
411 private String reason = "";
412
413 public FlowJsonArrayMatcher(FlowEntry flowValue) {
414 flow = flowValue;
415 }
416
417 @Override
418 public boolean matchesSafely(JsonArray json) {
419 boolean flowFound = false;
420
421 for (int jsonFlowIndex = 0; jsonFlowIndex < json.size();
422 jsonFlowIndex++) {
423
424 final JsonObject jsonFlow = json.get(jsonFlowIndex).asObject();
425
426 final String flowId = Long.toString(flow.id().value());
427 final String jsonFlowId = jsonFlow.get("id").asString();
428 if (jsonFlowId.equals(flowId)) {
429 flowFound = true;
430
431 // We found the correct flow, check attribute values
432 assertThat(jsonFlow, matchesFlow(flow));
433 }
434 }
435 if (!flowFound) {
436 reason = "Flow with id " + flow.id().toString() + " not found";
437 return false;
438 } else {
439 return true;
440 }
441 }
442
443 @Override
444 public void describeTo(Description description) {
445 description.appendText(reason);
446 }
447 }
448
449 /**
450 * Factory to allocate a flow array matcher.
451 *
452 * @param flow flow object we are looking for
453 * @return matcher
454 */
455 private static FlowJsonArrayMatcher hasFlow(FlowEntry flow) {
456 return new FlowJsonArrayMatcher(flow);
457 }
458
459 /**
460 * Tests the result of the rest api GET when there are no flows.
461 */
462 @Test
463 public void testFlowsEmptyArray() {
464 expect(mockFlowService.getFlowEntries(deviceId1))
465 .andReturn(null).anyTimes();
466 expect(mockFlowService.getFlowEntries(deviceId2))
467 .andReturn(null).anyTimes();
468 replay(mockFlowService);
469 replay(mockDeviceService);
470 final WebResource rs = resource();
471 final String response = rs.path("flows").get(String.class);
472 assertThat(response, is("{\"flows\":[]}"));
473 }
474
475 /**
476 * Tests the result of the rest api GET when there are active flows.
477 */
478 @Test
479 public void testFlowsPopulatedArray() {
480 setupMockFlows();
481 replay(mockFlowService);
482 replay(mockDeviceService);
483 final WebResource rs = resource();
484 final String response = rs.path("flows").get(String.class);
485 final JsonObject result = JsonObject.readFrom(response);
486 assertThat(result, notNullValue());
487
488 assertThat(result.names(), hasSize(1));
489 assertThat(result.names().get(0), is("flows"));
490 final JsonArray jsonFlows = result.get("flows").asArray();
491 assertThat(jsonFlows, notNullValue());
492 assertThat(jsonFlows, hasFlow(flow1));
493 assertThat(jsonFlows, hasFlow(flow2));
494 assertThat(jsonFlows, hasFlow(flow3));
495 assertThat(jsonFlows, hasFlow(flow4));
496 }
497
498 /**
499 * Tests the result of a rest api GET for a device.
500 */
501 @Test
502 public void testFlowsSingleDevice() {
503 setupMockFlows();
504 final Set<FlowEntry> flows = new HashSet<>();
505 flows.add(flow5);
506 flows.add(flow6);
507 expect(mockFlowService.getFlowEntries(anyObject()))
508 .andReturn(flows).anyTimes();
509 replay(mockFlowService);
510 replay(mockDeviceService);
511 final WebResource rs = resource();
512 final String response = rs.path("flows/" + deviceId3).get(String.class);
513 final JsonObject result = JsonObject.readFrom(response);
514 assertThat(result, notNullValue());
515
516 assertThat(result.names(), hasSize(1));
517 assertThat(result.names().get(0), is("flows"));
518 final JsonArray jsonFlows = result.get("flows").asArray();
519 assertThat(jsonFlows, notNullValue());
520 assertThat(jsonFlows, hasFlow(flow5));
521 assertThat(jsonFlows, hasFlow(flow6));
522 }
523
524 /**
525 * Tests the result of a rest api GET for a device.
526 */
527 @Test
528 public void testFlowsSingleDeviceWithFlowId() {
529 setupMockFlows();
530 final Set<FlowEntry> flows = new HashSet<>();
531 flows.add(flow5);
532 flows.add(flow6);
533 expect(mockFlowService.getFlowEntries(anyObject()))
534 .andReturn(flows).anyTimes();
535 replay(mockFlowService);
536 replay(mockDeviceService);
537 final WebResource rs = resource();
538 final String response = rs.path("flows/" + deviceId3 + "/"
539 + Long.toString(flow5.id().value())).get(String.class);
540 final JsonObject result = JsonObject.readFrom(response);
541 assertThat(result, notNullValue());
542
543 assertThat(result.names(), hasSize(1));
544 assertThat(result.names().get(0), is("flows"));
545 final JsonArray jsonFlows = result.get("flows").asArray();
546 assertThat(jsonFlows, notNullValue());
547 assertThat(jsonFlows, hasFlow(flow5));
548 assertThat(jsonFlows, not(hasFlow(flow6)));
549 }
550
551 /**
552 * Tests that a fetch of a non-existent device object throws an exception.
553 */
554 @Test
555 public void testBadGet() {
556 expect(mockFlowService.getFlowEntries(deviceId1))
557 .andReturn(null).anyTimes();
558 expect(mockFlowService.getFlowEntries(deviceId2))
559 .andReturn(null).anyTimes();
560 replay(mockFlowService);
561 replay(mockDeviceService);
562
563 WebResource rs = resource();
564 try {
565 rs.path("flows/0").get(String.class);
566 fail("Fetch of non-existent device did not throw an exception");
567 } catch (UniformInterfaceException ex) {
568 assertThat(ex.getMessage(),
569 containsString("returned a response status of"));
570 }
571 }
Ray Milkeyd43fe452015-05-29 09:35:12 -0700572
573 /**
574 * Tests creating a flow with POST.
575 */
576 @Test
577 public void testPost() {
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700578
Ray Milkeyd43fe452015-05-29 09:35:12 -0700579
580 mockFlowService.applyFlowRules(anyObject());
581 expectLastCall();
582 replay(mockFlowService);
583
584 WebResource rs = resource();
585
586
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700587 ClientResponse response = rs.path("flows/of:0000000000000001")
Ray Milkeyd43fe452015-05-29 09:35:12 -0700588 .type(MediaType.APPLICATION_JSON_TYPE)
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700589 .post(ClientResponse.class, FLOW_JSON);
590 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED));
591 String location = response.getLocation().getPath();
592 assertThat(location, Matchers.startsWith("/flows/of:0000000000000001/"));
593 }
594
595 /**
596 * Tests deleting a flow.
597 */
598 @Test
599 public void testDelete() {
600 setupMockFlows();
601 mockFlowService.removeFlowRules(anyObject());
602 expectLastCall();
603 replay(mockFlowService);
604
605 WebResource rs = resource();
606
607 String location = "/flows/1/155";
608
609 ClientResponse deleteResponse = rs.path(location)
610 .type(MediaType.APPLICATION_JSON_TYPE)
611 .delete(ClientResponse.class);
612 assertThat(deleteResponse.getStatus(),
613 is(HttpURLConnection.HTTP_NO_CONTENT));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700614 }
Ray Milkey4f5de002014-12-17 19:26:11 -0800615}