blob: 4fb582155d059161ba1652b5427497a9736cec85 [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;
Ray Milkeybe539db2015-09-04 11:00:43 -0700302 private final String expectedAppId;
Ray Milkey4f5de002014-12-17 19:26:11 -0800303 private String reason = "";
304
Ray Milkeybe539db2015-09-04 11:00:43 -0700305 public FlowJsonMatcher(FlowEntry flowValue, String expectedAppIdValue) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800306 flow = flowValue;
Ray Milkeybe539db2015-09-04 11:00:43 -0700307 expectedAppId = expectedAppIdValue;
Ray Milkey4f5de002014-12-17 19:26:11 -0800308 }
309
310 @Override
311 public boolean matchesSafely(JsonObject jsonFlow) {
312 // check id
313 final String jsonId = jsonFlow.get("id").asString();
314 final String flowId = Long.toString(flow.id().value());
315 if (!jsonId.equals(flowId)) {
316 reason = "id " + flow.id().toString();
317 return false;
318 }
319
320 // check application id
Ray Milkeybe539db2015-09-04 11:00:43 -0700321 final String jsonAppId = jsonFlow.get("appId").asString();
322 if (!jsonAppId.equals(expectedAppId)) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800323 reason = "appId " + Short.toString(flow.appId());
324 return false;
325 }
326
327 // check device id
328 final String jsonDeviceId = jsonFlow.get("deviceId").asString();
329 if (!jsonDeviceId.equals(flow.deviceId().toString())) {
330 reason = "deviceId " + flow.deviceId();
331 return false;
332 }
333
334 // check treatment and instructions array
335 if (flow.treatment() != null) {
336 final JsonObject jsonTreatment = jsonFlow.get("treatment").asObject();
337 final JsonArray jsonInstructions = jsonTreatment.get("instructions").asArray();
Ray Milkey42507352015-03-20 15:16:10 -0700338 if (flow.treatment().immediate().size() != jsonInstructions.size()) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800339 reason = "instructions array size of " +
Ray Milkey42507352015-03-20 15:16:10 -0700340 Integer.toString(flow.treatment().immediate().size());
Ray Milkey4f5de002014-12-17 19:26:11 -0800341 return false;
342 }
Ray Milkey42507352015-03-20 15:16:10 -0700343 for (final Instruction instruction : flow.treatment().immediate()) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800344 boolean instructionFound = false;
Ray Milkey4f5de002014-12-17 19:26:11 -0800345 for (int instructionIndex = 0; instructionIndex < jsonInstructions.size(); instructionIndex++) {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800346 final String jsonType =
347 jsonInstructions.get(instructionIndex)
348 .asObject().get("type").asString();
349 final String instructionType = instruction.type().name();
350 if (jsonType.equals(instructionType)) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800351 instructionFound = true;
352 }
353 }
354 if (!instructionFound) {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800355 reason = "instruction " + instruction.toString();
Ray Milkey4f5de002014-12-17 19:26:11 -0800356 return false;
357 }
358 }
359 }
360
361 // check selector and criteria array
362 if (flow.selector() != null) {
363 final JsonObject jsonTreatment = jsonFlow.get("selector").asObject();
364 final JsonArray jsonCriteria = jsonTreatment.get("criteria").asArray();
365 if (flow.selector().criteria().size() != jsonCriteria.size()) {
366 reason = "criteria array size of " +
367 Integer.toString(flow.selector().criteria().size());
368 return false;
369 }
370 for (final Criterion criterion : flow.selector().criteria()) {
371 boolean criterionFound = false;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800372
Ray Milkey4f5de002014-12-17 19:26:11 -0800373 for (int criterionIndex = 0; criterionIndex < jsonCriteria.size(); criterionIndex++) {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800374 final String jsonType =
375 jsonCriteria.get(criterionIndex)
376 .asObject().get("type").asString();
377 final String criterionType = criterion.type().name();
378 if (jsonType.equals(criterionType)) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800379 criterionFound = true;
380 }
381 }
382 if (!criterionFound) {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800383 reason = "criterion " + criterion.toString();
Ray Milkey4f5de002014-12-17 19:26:11 -0800384 return false;
385 }
386 }
387 }
388
389 return true;
390 }
391
392 @Override
393 public void describeTo(Description description) {
394 description.appendText(reason);
395 }
396 }
397
398 /**
399 * Factory to allocate a flow matcher.
400 *
401 * @param flow flow object we are looking for
402 * @return matcher
403 */
Ray Milkeybe539db2015-09-04 11:00:43 -0700404 private static FlowJsonMatcher matchesFlow(FlowEntry flow, String expectedAppName) {
405 return new FlowJsonMatcher(flow, expectedAppName);
Ray Milkey4f5de002014-12-17 19:26:11 -0800406 }
407
408 /**
409 * Hamcrest matcher to check that a flow is represented properly in a JSON
410 * array of flows.
411 */
412 public static class FlowJsonArrayMatcher extends TypeSafeMatcher<JsonArray> {
413 private final FlowEntry flow;
414 private String reason = "";
415
416 public FlowJsonArrayMatcher(FlowEntry flowValue) {
417 flow = flowValue;
418 }
419
420 @Override
421 public boolean matchesSafely(JsonArray json) {
422 boolean flowFound = false;
423
424 for (int jsonFlowIndex = 0; jsonFlowIndex < json.size();
425 jsonFlowIndex++) {
426
427 final JsonObject jsonFlow = json.get(jsonFlowIndex).asObject();
428
429 final String flowId = Long.toString(flow.id().value());
430 final String jsonFlowId = jsonFlow.get("id").asString();
431 if (jsonFlowId.equals(flowId)) {
432 flowFound = true;
433
434 // We found the correct flow, check attribute values
Ray Milkeybe539db2015-09-04 11:00:43 -0700435 assertThat(jsonFlow, matchesFlow(flow, APP_ID.name()));
Ray Milkey4f5de002014-12-17 19:26:11 -0800436 }
437 }
438 if (!flowFound) {
439 reason = "Flow with id " + flow.id().toString() + " not found";
440 return false;
441 } else {
442 return true;
443 }
444 }
445
446 @Override
447 public void describeTo(Description description) {
448 description.appendText(reason);
449 }
450 }
451
452 /**
453 * Factory to allocate a flow array matcher.
454 *
455 * @param flow flow object we are looking for
456 * @return matcher
457 */
458 private static FlowJsonArrayMatcher hasFlow(FlowEntry flow) {
459 return new FlowJsonArrayMatcher(flow);
460 }
461
462 /**
463 * Tests the result of the rest api GET when there are no flows.
464 */
465 @Test
466 public void testFlowsEmptyArray() {
467 expect(mockFlowService.getFlowEntries(deviceId1))
468 .andReturn(null).anyTimes();
469 expect(mockFlowService.getFlowEntries(deviceId2))
470 .andReturn(null).anyTimes();
471 replay(mockFlowService);
472 replay(mockDeviceService);
473 final WebResource rs = resource();
474 final String response = rs.path("flows").get(String.class);
475 assertThat(response, is("{\"flows\":[]}"));
476 }
477
478 /**
479 * Tests the result of the rest api GET when there are active flows.
480 */
481 @Test
482 public void testFlowsPopulatedArray() {
483 setupMockFlows();
484 replay(mockFlowService);
485 replay(mockDeviceService);
486 final WebResource rs = resource();
487 final String response = rs.path("flows").get(String.class);
488 final JsonObject result = JsonObject.readFrom(response);
489 assertThat(result, notNullValue());
490
491 assertThat(result.names(), hasSize(1));
492 assertThat(result.names().get(0), is("flows"));
493 final JsonArray jsonFlows = result.get("flows").asArray();
494 assertThat(jsonFlows, notNullValue());
495 assertThat(jsonFlows, hasFlow(flow1));
496 assertThat(jsonFlows, hasFlow(flow2));
497 assertThat(jsonFlows, hasFlow(flow3));
498 assertThat(jsonFlows, hasFlow(flow4));
499 }
500
501 /**
502 * Tests the result of a rest api GET for a device.
503 */
504 @Test
505 public void testFlowsSingleDevice() {
506 setupMockFlows();
507 final Set<FlowEntry> flows = new HashSet<>();
508 flows.add(flow5);
509 flows.add(flow6);
510 expect(mockFlowService.getFlowEntries(anyObject()))
511 .andReturn(flows).anyTimes();
512 replay(mockFlowService);
513 replay(mockDeviceService);
514 final WebResource rs = resource();
515 final String response = rs.path("flows/" + deviceId3).get(String.class);
516 final JsonObject result = JsonObject.readFrom(response);
517 assertThat(result, notNullValue());
518
519 assertThat(result.names(), hasSize(1));
520 assertThat(result.names().get(0), is("flows"));
521 final JsonArray jsonFlows = result.get("flows").asArray();
522 assertThat(jsonFlows, notNullValue());
523 assertThat(jsonFlows, hasFlow(flow5));
524 assertThat(jsonFlows, hasFlow(flow6));
525 }
526
527 /**
528 * Tests the result of a rest api GET for a device.
529 */
530 @Test
531 public void testFlowsSingleDeviceWithFlowId() {
532 setupMockFlows();
533 final Set<FlowEntry> flows = new HashSet<>();
534 flows.add(flow5);
535 flows.add(flow6);
536 expect(mockFlowService.getFlowEntries(anyObject()))
537 .andReturn(flows).anyTimes();
538 replay(mockFlowService);
539 replay(mockDeviceService);
540 final WebResource rs = resource();
541 final String response = rs.path("flows/" + deviceId3 + "/"
542 + Long.toString(flow5.id().value())).get(String.class);
543 final JsonObject result = JsonObject.readFrom(response);
544 assertThat(result, notNullValue());
545
546 assertThat(result.names(), hasSize(1));
547 assertThat(result.names().get(0), is("flows"));
548 final JsonArray jsonFlows = result.get("flows").asArray();
549 assertThat(jsonFlows, notNullValue());
550 assertThat(jsonFlows, hasFlow(flow5));
551 assertThat(jsonFlows, not(hasFlow(flow6)));
552 }
553
554 /**
555 * Tests that a fetch of a non-existent device object throws an exception.
556 */
557 @Test
558 public void testBadGet() {
559 expect(mockFlowService.getFlowEntries(deviceId1))
560 .andReturn(null).anyTimes();
561 expect(mockFlowService.getFlowEntries(deviceId2))
562 .andReturn(null).anyTimes();
563 replay(mockFlowService);
564 replay(mockDeviceService);
565
566 WebResource rs = resource();
567 try {
568 rs.path("flows/0").get(String.class);
569 fail("Fetch of non-existent device did not throw an exception");
570 } catch (UniformInterfaceException ex) {
571 assertThat(ex.getMessage(),
572 containsString("returned a response status of"));
573 }
574 }
Ray Milkeyd43fe452015-05-29 09:35:12 -0700575
576 /**
577 * Tests creating a flow with POST.
578 */
579 @Test
580 public void testPost() {
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700581
Ray Milkeyd43fe452015-05-29 09:35:12 -0700582
583 mockFlowService.applyFlowRules(anyObject());
584 expectLastCall();
585 replay(mockFlowService);
586
587 WebResource rs = resource();
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700588 InputStream jsonStream = IntentsResourceTest.class
589 .getResourceAsStream("post-flow.json");
Ray Milkeyd43fe452015-05-29 09:35:12 -0700590
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700591 ClientResponse response = rs.path("flows/of:0000000000000001")
Ray Milkeyd43fe452015-05-29 09:35:12 -0700592 .type(MediaType.APPLICATION_JSON_TYPE)
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700593 .post(ClientResponse.class, jsonStream);
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700594 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED));
595 String location = response.getLocation().getPath();
596 assertThat(location, Matchers.startsWith("/flows/of:0000000000000001/"));
597 }
598
599 /**
600 * Tests deleting a flow.
601 */
602 @Test
603 public void testDelete() {
604 setupMockFlows();
605 mockFlowService.removeFlowRules(anyObject());
606 expectLastCall();
607 replay(mockFlowService);
608
609 WebResource rs = resource();
610
611 String location = "/flows/1/155";
612
613 ClientResponse deleteResponse = rs.path(location)
614 .type(MediaType.APPLICATION_JSON_TYPE)
615 .delete(ClientResponse.class);
616 assertThat(deleteResponse.getStatus(),
617 is(HttpURLConnection.HTTP_NO_CONTENT));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700618 }
Ray Milkey4f5de002014-12-17 19:26:11 -0800619}