blob: da192bd07319331f7ae79508af2f6ce20dd9dd9f [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;
Andrea Campanella5df35952015-12-08 15:46:49 -080058import org.onosproject.rest.resources.CoreWebApplication;
jcc3d4e14a2015-04-21 11:32:05 +080059
Jonathan Hartf44e42c2015-08-04 09:58:46 -070060import javax.ws.rs.core.MediaType;
61import java.io.InputStream;
62import java.net.HttpURLConnection;
63import java.util.HashMap;
64import java.util.HashSet;
65import java.util.Set;
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
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700212 public boolean exactMatch(FlowRule rule) {
213 return false;
214 }
215
216 @Override
jcc3d4e14a2015-04-21 11:32:05 +0800217 public FlowRuleExtPayLoad payLoad() {
218 return null;
219 }
Ray Milkey4f5de002014-12-17 19:26:11 -0800220 }
221
Ray Milkey4f5de002014-12-17 19:26:11 -0800222 /**
223 * Populates some flows used as testing data.
224 */
225 private void setupMockFlows() {
226 flow2.treatment = DefaultTrafficTreatment.builder()
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700227 .add(Instructions.modL0Lambda(new IndexedLambda((short) 4)))
228 .add(Instructions.modL0Lambda(new IndexedLambda((short) 5)))
Ray Milkey4f5de002014-12-17 19:26:11 -0800229 .setEthDst(MacAddress.BROADCAST)
230 .build();
231 flow2.selector = DefaultTrafficSelector.builder()
232 .matchEthType((short) 3)
233 .matchIPProtocol((byte) 9)
234 .build();
235 flow4.treatment = DefaultTrafficTreatment.builder()
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700236 .add(Instructions.modL0Lambda(new IndexedLambda((short) 6)))
Ray Milkey4f5de002014-12-17 19:26:11 -0800237 .build();
238 final Set<FlowEntry> flows1 = new HashSet<>();
239 flows1.add(flow1);
240 flows1.add(flow2);
241
242 final Set<FlowEntry> flows2 = new HashSet<>();
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700243 flows2.add(flow3);
244 flows2.add(flow4);
Ray Milkey4f5de002014-12-17 19:26:11 -0800245
246 rules.put(deviceId1, flows1);
247 rules.put(deviceId2, flows2);
248
249 expect(mockFlowService.getFlowEntries(deviceId1))
250 .andReturn(rules.get(deviceId1)).anyTimes();
251 expect(mockFlowService.getFlowEntries(deviceId2))
252 .andReturn(rules.get(deviceId2)).anyTimes();
253 }
254
Andrea Campanella5df35952015-12-08 15:46:49 -0800255 public FlowsResourceTest() {
256 super(CoreWebApplication.class);
257 }
258
Ray Milkey4f5de002014-12-17 19:26:11 -0800259 /**
260 * Sets up the global values for all the tests.
261 */
262 @Before
Ray Milkeyed0b1662015-02-05 09:34:29 -0800263 public void setUpTest() {
Ray Milkey4f5de002014-12-17 19:26:11 -0800264 // Mock device service
265 expect(mockDeviceService.getDevice(deviceId1))
266 .andReturn(device1);
267 expect(mockDeviceService.getDevice(deviceId2))
268 .andReturn(device2);
269 expect(mockDeviceService.getDevices())
270 .andReturn(ImmutableSet.of(device1, device2));
271
Ray Milkeyd43fe452015-05-29 09:35:12 -0700272 // Mock Core Service
273 expect(mockCoreService.getAppId(anyShort()))
274 .andReturn(NetTestTools.APP_ID).anyTimes();
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700275 expect(mockCoreService.registerApplication(FlowRuleCodec.REST_APP_ID))
276 .andReturn(APP_ID).anyTimes();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700277 replay(mockCoreService);
278
Ray Milkey4f5de002014-12-17 19:26:11 -0800279 // Register the services needed for the test
280 final CodecManager codecService = new CodecManager();
281 codecService.activate();
282 ServiceDirectory testDirectory =
283 new TestServiceDirectory()
284 .add(FlowRuleService.class, mockFlowService)
285 .add(DeviceService.class, mockDeviceService)
Ray Milkeyd43fe452015-05-29 09:35:12 -0700286 .add(CodecService.class, codecService)
287 .add(CoreService.class, mockCoreService);
Ray Milkey4f5de002014-12-17 19:26:11 -0800288
289 BaseResource.setServiceDirectory(testDirectory);
290 }
291
292 /**
293 * Cleans up and verifies the mocks.
Ray Milkey4f5de002014-12-17 19:26:11 -0800294 */
295 @After
Ray Milkeyed0b1662015-02-05 09:34:29 -0800296 public void tearDownTest() {
Ray Milkey4f5de002014-12-17 19:26:11 -0800297 verify(mockFlowService);
Ray Milkeyd43fe452015-05-29 09:35:12 -0700298 verify(mockCoreService);
Ray Milkey4f5de002014-12-17 19:26:11 -0800299 }
300
301 /**
302 * Hamcrest matcher to check that a flow representation in JSON matches
303 * the actual flow entry.
304 */
305 public static class FlowJsonMatcher extends TypeSafeMatcher<JsonObject> {
306 private final FlowEntry flow;
Ray Milkeybe539db2015-09-04 11:00:43 -0700307 private final String expectedAppId;
Ray Milkey4f5de002014-12-17 19:26:11 -0800308 private String reason = "";
309
Ray Milkeybe539db2015-09-04 11:00:43 -0700310 public FlowJsonMatcher(FlowEntry flowValue, String expectedAppIdValue) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800311 flow = flowValue;
Ray Milkeybe539db2015-09-04 11:00:43 -0700312 expectedAppId = expectedAppIdValue;
Ray Milkey4f5de002014-12-17 19:26:11 -0800313 }
314
315 @Override
316 public boolean matchesSafely(JsonObject jsonFlow) {
317 // check id
318 final String jsonId = jsonFlow.get("id").asString();
319 final String flowId = Long.toString(flow.id().value());
320 if (!jsonId.equals(flowId)) {
321 reason = "id " + flow.id().toString();
322 return false;
323 }
324
325 // check application id
Ray Milkeybe539db2015-09-04 11:00:43 -0700326 final String jsonAppId = jsonFlow.get("appId").asString();
327 if (!jsonAppId.equals(expectedAppId)) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800328 reason = "appId " + Short.toString(flow.appId());
329 return false;
330 }
331
332 // check device id
333 final String jsonDeviceId = jsonFlow.get("deviceId").asString();
334 if (!jsonDeviceId.equals(flow.deviceId().toString())) {
335 reason = "deviceId " + flow.deviceId();
336 return false;
337 }
338
339 // check treatment and instructions array
340 if (flow.treatment() != null) {
341 final JsonObject jsonTreatment = jsonFlow.get("treatment").asObject();
342 final JsonArray jsonInstructions = jsonTreatment.get("instructions").asArray();
Ray Milkey42507352015-03-20 15:16:10 -0700343 if (flow.treatment().immediate().size() != jsonInstructions.size()) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800344 reason = "instructions array size of " +
Ray Milkey42507352015-03-20 15:16:10 -0700345 Integer.toString(flow.treatment().immediate().size());
Ray Milkey4f5de002014-12-17 19:26:11 -0800346 return false;
347 }
Ray Milkey42507352015-03-20 15:16:10 -0700348 for (final Instruction instruction : flow.treatment().immediate()) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800349 boolean instructionFound = false;
Ray Milkey4f5de002014-12-17 19:26:11 -0800350 for (int instructionIndex = 0; instructionIndex < jsonInstructions.size(); instructionIndex++) {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800351 final String jsonType =
352 jsonInstructions.get(instructionIndex)
353 .asObject().get("type").asString();
354 final String instructionType = instruction.type().name();
355 if (jsonType.equals(instructionType)) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800356 instructionFound = true;
357 }
358 }
359 if (!instructionFound) {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800360 reason = "instruction " + instruction.toString();
Ray Milkey4f5de002014-12-17 19:26:11 -0800361 return false;
362 }
363 }
364 }
365
366 // check selector and criteria array
367 if (flow.selector() != null) {
368 final JsonObject jsonTreatment = jsonFlow.get("selector").asObject();
369 final JsonArray jsonCriteria = jsonTreatment.get("criteria").asArray();
370 if (flow.selector().criteria().size() != jsonCriteria.size()) {
371 reason = "criteria array size of " +
372 Integer.toString(flow.selector().criteria().size());
373 return false;
374 }
375 for (final Criterion criterion : flow.selector().criteria()) {
376 boolean criterionFound = false;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800377
Ray Milkey4f5de002014-12-17 19:26:11 -0800378 for (int criterionIndex = 0; criterionIndex < jsonCriteria.size(); criterionIndex++) {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800379 final String jsonType =
380 jsonCriteria.get(criterionIndex)
381 .asObject().get("type").asString();
382 final String criterionType = criterion.type().name();
383 if (jsonType.equals(criterionType)) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800384 criterionFound = true;
385 }
386 }
387 if (!criterionFound) {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800388 reason = "criterion " + criterion.toString();
Ray Milkey4f5de002014-12-17 19:26:11 -0800389 return false;
390 }
391 }
392 }
393
394 return true;
395 }
396
397 @Override
398 public void describeTo(Description description) {
399 description.appendText(reason);
400 }
401 }
402
403 /**
404 * Factory to allocate a flow matcher.
405 *
406 * @param flow flow object we are looking for
407 * @return matcher
408 */
Ray Milkeybe539db2015-09-04 11:00:43 -0700409 private static FlowJsonMatcher matchesFlow(FlowEntry flow, String expectedAppName) {
410 return new FlowJsonMatcher(flow, expectedAppName);
Ray Milkey4f5de002014-12-17 19:26:11 -0800411 }
412
413 /**
414 * Hamcrest matcher to check that a flow is represented properly in a JSON
415 * array of flows.
416 */
417 public static class FlowJsonArrayMatcher extends TypeSafeMatcher<JsonArray> {
418 private final FlowEntry flow;
419 private String reason = "";
420
421 public FlowJsonArrayMatcher(FlowEntry flowValue) {
422 flow = flowValue;
423 }
424
425 @Override
426 public boolean matchesSafely(JsonArray json) {
427 boolean flowFound = false;
428
429 for (int jsonFlowIndex = 0; jsonFlowIndex < json.size();
430 jsonFlowIndex++) {
431
432 final JsonObject jsonFlow = json.get(jsonFlowIndex).asObject();
433
434 final String flowId = Long.toString(flow.id().value());
435 final String jsonFlowId = jsonFlow.get("id").asString();
436 if (jsonFlowId.equals(flowId)) {
437 flowFound = true;
438
439 // We found the correct flow, check attribute values
Ray Milkeybe539db2015-09-04 11:00:43 -0700440 assertThat(jsonFlow, matchesFlow(flow, APP_ID.name()));
Ray Milkey4f5de002014-12-17 19:26:11 -0800441 }
442 }
443 if (!flowFound) {
444 reason = "Flow with id " + flow.id().toString() + " not found";
445 return false;
446 } else {
447 return true;
448 }
449 }
450
451 @Override
452 public void describeTo(Description description) {
453 description.appendText(reason);
454 }
455 }
456
457 /**
458 * Factory to allocate a flow array matcher.
459 *
460 * @param flow flow object we are looking for
461 * @return matcher
462 */
463 private static FlowJsonArrayMatcher hasFlow(FlowEntry flow) {
464 return new FlowJsonArrayMatcher(flow);
465 }
466
467 /**
468 * Tests the result of the rest api GET when there are no flows.
469 */
470 @Test
471 public void testFlowsEmptyArray() {
472 expect(mockFlowService.getFlowEntries(deviceId1))
473 .andReturn(null).anyTimes();
474 expect(mockFlowService.getFlowEntries(deviceId2))
475 .andReturn(null).anyTimes();
476 replay(mockFlowService);
477 replay(mockDeviceService);
478 final WebResource rs = resource();
479 final String response = rs.path("flows").get(String.class);
480 assertThat(response, is("{\"flows\":[]}"));
481 }
482
483 /**
484 * Tests the result of the rest api GET when there are active flows.
485 */
486 @Test
487 public void testFlowsPopulatedArray() {
488 setupMockFlows();
489 replay(mockFlowService);
490 replay(mockDeviceService);
491 final WebResource rs = resource();
492 final String response = rs.path("flows").get(String.class);
493 final JsonObject result = JsonObject.readFrom(response);
494 assertThat(result, notNullValue());
495
496 assertThat(result.names(), hasSize(1));
497 assertThat(result.names().get(0), is("flows"));
498 final JsonArray jsonFlows = result.get("flows").asArray();
499 assertThat(jsonFlows, notNullValue());
500 assertThat(jsonFlows, hasFlow(flow1));
501 assertThat(jsonFlows, hasFlow(flow2));
502 assertThat(jsonFlows, hasFlow(flow3));
503 assertThat(jsonFlows, hasFlow(flow4));
504 }
505
506 /**
507 * Tests the result of a rest api GET for a device.
508 */
509 @Test
510 public void testFlowsSingleDevice() {
511 setupMockFlows();
512 final Set<FlowEntry> flows = new HashSet<>();
513 flows.add(flow5);
514 flows.add(flow6);
515 expect(mockFlowService.getFlowEntries(anyObject()))
516 .andReturn(flows).anyTimes();
517 replay(mockFlowService);
518 replay(mockDeviceService);
519 final WebResource rs = resource();
520 final String response = rs.path("flows/" + deviceId3).get(String.class);
521 final JsonObject result = JsonObject.readFrom(response);
522 assertThat(result, notNullValue());
523
524 assertThat(result.names(), hasSize(1));
525 assertThat(result.names().get(0), is("flows"));
526 final JsonArray jsonFlows = result.get("flows").asArray();
527 assertThat(jsonFlows, notNullValue());
528 assertThat(jsonFlows, hasFlow(flow5));
529 assertThat(jsonFlows, hasFlow(flow6));
530 }
531
532 /**
533 * Tests the result of a rest api GET for a device.
534 */
535 @Test
536 public void testFlowsSingleDeviceWithFlowId() {
537 setupMockFlows();
538 final Set<FlowEntry> flows = new HashSet<>();
539 flows.add(flow5);
540 flows.add(flow6);
541 expect(mockFlowService.getFlowEntries(anyObject()))
542 .andReturn(flows).anyTimes();
543 replay(mockFlowService);
544 replay(mockDeviceService);
545 final WebResource rs = resource();
546 final String response = rs.path("flows/" + deviceId3 + "/"
547 + Long.toString(flow5.id().value())).get(String.class);
548 final JsonObject result = JsonObject.readFrom(response);
549 assertThat(result, notNullValue());
550
551 assertThat(result.names(), hasSize(1));
552 assertThat(result.names().get(0), is("flows"));
553 final JsonArray jsonFlows = result.get("flows").asArray();
554 assertThat(jsonFlows, notNullValue());
555 assertThat(jsonFlows, hasFlow(flow5));
556 assertThat(jsonFlows, not(hasFlow(flow6)));
557 }
558
559 /**
560 * Tests that a fetch of a non-existent device object throws an exception.
561 */
562 @Test
563 public void testBadGet() {
564 expect(mockFlowService.getFlowEntries(deviceId1))
565 .andReturn(null).anyTimes();
566 expect(mockFlowService.getFlowEntries(deviceId2))
567 .andReturn(null).anyTimes();
568 replay(mockFlowService);
569 replay(mockDeviceService);
570
571 WebResource rs = resource();
572 try {
573 rs.path("flows/0").get(String.class);
574 fail("Fetch of non-existent device did not throw an exception");
575 } catch (UniformInterfaceException ex) {
576 assertThat(ex.getMessage(),
577 containsString("returned a response status of"));
578 }
579 }
Ray Milkeyd43fe452015-05-29 09:35:12 -0700580
581 /**
582 * Tests creating a flow with POST.
583 */
584 @Test
585 public void testPost() {
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700586
Ray Milkeyd43fe452015-05-29 09:35:12 -0700587
588 mockFlowService.applyFlowRules(anyObject());
589 expectLastCall();
590 replay(mockFlowService);
591
592 WebResource rs = resource();
Phaneendra Manda2be2f882015-11-11 15:23:40 +0530593 InputStream jsonStream = FlowsResourceTest.class
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700594 .getResourceAsStream("post-flow.json");
Ray Milkeyd43fe452015-05-29 09:35:12 -0700595
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700596 ClientResponse response = rs.path("flows/of:0000000000000001")
Ray Milkeyd43fe452015-05-29 09:35:12 -0700597 .type(MediaType.APPLICATION_JSON_TYPE)
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700598 .post(ClientResponse.class, jsonStream);
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700599 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED));
600 String location = response.getLocation().getPath();
601 assertThat(location, Matchers.startsWith("/flows/of:0000000000000001/"));
602 }
603
604 /**
605 * Tests deleting a flow.
606 */
607 @Test
608 public void testDelete() {
609 setupMockFlows();
610 mockFlowService.removeFlowRules(anyObject());
611 expectLastCall();
612 replay(mockFlowService);
613
614 WebResource rs = resource();
615
616 String location = "/flows/1/155";
617
618 ClientResponse deleteResponse = rs.path(location)
619 .type(MediaType.APPLICATION_JSON_TYPE)
620 .delete(ClientResponse.class);
621 assertThat(deleteResponse.getStatus(),
622 is(HttpURLConnection.HTTP_NO_CONTENT));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700623 }
Ray Milkey4f5de002014-12-17 19:26:11 -0800624}