blob: 4aede1583b02bf503216f882969b46bfd773d459 [file] [log] [blame]
Ray Milkey4f5de002014-12-17 19:26:11 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present 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 */
Jian Li8ae91202016-03-24 14:36:16 -070016package org.onosproject.rest.resources;
Ray Milkey4f5de002014-12-17 19:26:11 -080017
Jian Li80cfe452016-01-14 16:04:58 -080018import com.eclipsesource.json.Json;
Jonathan Hartf44e42c2015-08-04 09:58:46 -070019import com.eclipsesource.json.JsonArray;
20import com.eclipsesource.json.JsonObject;
21import com.google.common.collect.ImmutableSet;
Ray Milkey4f5de002014-12-17 19:26:11 -080022import org.hamcrest.Description;
Ray Milkeyeb5c7172015-06-23 14:59:27 -070023import org.hamcrest.Matchers;
Ray Milkey4f5de002014-12-17 19:26:11 -080024import org.hamcrest.TypeSafeMatcher;
25import org.junit.After;
26import org.junit.Before;
27import org.junit.Test;
28import org.onlab.osgi.ServiceDirectory;
29import org.onlab.osgi.TestServiceDirectory;
30import org.onlab.packet.MacAddress;
31import org.onlab.rest.BaseResource;
32import org.onosproject.codec.CodecService;
33import org.onosproject.codec.impl.CodecManager;
Ray Milkeyeb5c7172015-06-23 14:59:27 -070034import org.onosproject.codec.impl.FlowRuleCodec;
Ray Milkeyd43fe452015-05-29 09:35:12 -070035import org.onosproject.core.CoreService;
Ray Milkey4f5de002014-12-17 19:26:11 -080036import org.onosproject.core.DefaultGroupId;
37import org.onosproject.core.GroupId;
38import org.onosproject.net.DefaultDevice;
39import org.onosproject.net.Device;
40import org.onosproject.net.DeviceId;
Ray Milkeyd43fe452015-05-29 09:35:12 -070041import org.onosproject.net.NetTestTools;
Ray Milkey4f5de002014-12-17 19:26:11 -080042import org.onosproject.net.device.DeviceService;
43import org.onosproject.net.flow.DefaultTrafficSelector;
44import org.onosproject.net.flow.DefaultTrafficTreatment;
45import org.onosproject.net.flow.FlowEntry;
46import org.onosproject.net.flow.FlowId;
Jonathan Hartf44e42c2015-08-04 09:58:46 -070047import org.onosproject.net.flow.FlowRule;
jcc3d4e14a2015-04-21 11:32:05 +080048import org.onosproject.net.flow.FlowRuleExtPayLoad;
Ray Milkey4f5de002014-12-17 19:26:11 -080049import org.onosproject.net.flow.FlowRuleService;
50import org.onosproject.net.flow.TrafficSelector;
51import org.onosproject.net.flow.TrafficTreatment;
52import org.onosproject.net.flow.criteria.Criterion;
53import org.onosproject.net.flow.instructions.Instruction;
jcc3d4e14a2015-04-21 11:32:05 +080054
Jian Li9d616492016-03-09 10:52:49 -080055import javax.ws.rs.NotFoundException;
56import javax.ws.rs.client.Entity;
57import javax.ws.rs.client.WebTarget;
Jonathan Hartf44e42c2015-08-04 09:58:46 -070058import javax.ws.rs.core.MediaType;
Jian Li9d616492016-03-09 10:52:49 -080059import javax.ws.rs.core.Response;
Jonathan Hartf44e42c2015-08-04 09:58:46 -070060import 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 Milkey4f5de002014-12-17 19:26:11 -0800226 .setEthDst(MacAddress.BROADCAST)
227 .build();
228 flow2.selector = DefaultTrafficSelector.builder()
229 .matchEthType((short) 3)
230 .matchIPProtocol((byte) 9)
231 .build();
232 flow4.treatment = DefaultTrafficTreatment.builder()
Ray Milkey4f5de002014-12-17 19:26:11 -0800233 .build();
234 final Set<FlowEntry> flows1 = new HashSet<>();
235 flows1.add(flow1);
236 flows1.add(flow2);
237
238 final Set<FlowEntry> flows2 = new HashSet<>();
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700239 flows2.add(flow3);
240 flows2.add(flow4);
Ray Milkey4f5de002014-12-17 19:26:11 -0800241
242 rules.put(deviceId1, flows1);
243 rules.put(deviceId2, flows2);
244
245 expect(mockFlowService.getFlowEntries(deviceId1))
246 .andReturn(rules.get(deviceId1)).anyTimes();
247 expect(mockFlowService.getFlowEntries(deviceId2))
248 .andReturn(rules.get(deviceId2)).anyTimes();
249 }
250
251 /**
252 * Sets up the global values for all the tests.
253 */
254 @Before
Ray Milkeyed0b1662015-02-05 09:34:29 -0800255 public void setUpTest() {
Ray Milkey4f5de002014-12-17 19:26:11 -0800256 // Mock device service
257 expect(mockDeviceService.getDevice(deviceId1))
258 .andReturn(device1);
259 expect(mockDeviceService.getDevice(deviceId2))
260 .andReturn(device2);
261 expect(mockDeviceService.getDevices())
262 .andReturn(ImmutableSet.of(device1, device2));
263
Ray Milkeyd43fe452015-05-29 09:35:12 -0700264 // Mock Core Service
265 expect(mockCoreService.getAppId(anyShort()))
266 .andReturn(NetTestTools.APP_ID).anyTimes();
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700267 expect(mockCoreService.registerApplication(FlowRuleCodec.REST_APP_ID))
268 .andReturn(APP_ID).anyTimes();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700269 replay(mockCoreService);
270
Ray Milkey4f5de002014-12-17 19:26:11 -0800271 // Register the services needed for the test
272 final CodecManager codecService = new CodecManager();
273 codecService.activate();
274 ServiceDirectory testDirectory =
275 new TestServiceDirectory()
276 .add(FlowRuleService.class, mockFlowService)
277 .add(DeviceService.class, mockDeviceService)
Ray Milkeyd43fe452015-05-29 09:35:12 -0700278 .add(CodecService.class, codecService)
279 .add(CoreService.class, mockCoreService);
Ray Milkey4f5de002014-12-17 19:26:11 -0800280
281 BaseResource.setServiceDirectory(testDirectory);
282 }
283
284 /**
285 * Cleans up and verifies the mocks.
Ray Milkey4f5de002014-12-17 19:26:11 -0800286 */
287 @After
Ray Milkeyed0b1662015-02-05 09:34:29 -0800288 public void tearDownTest() {
Ray Milkey4f5de002014-12-17 19:26:11 -0800289 verify(mockFlowService);
Ray Milkeyd43fe452015-05-29 09:35:12 -0700290 verify(mockCoreService);
Ray Milkey4f5de002014-12-17 19:26:11 -0800291 }
292
293 /**
294 * Hamcrest matcher to check that a flow representation in JSON matches
295 * the actual flow entry.
296 */
297 public static class FlowJsonMatcher extends TypeSafeMatcher<JsonObject> {
298 private final FlowEntry flow;
Ray Milkeybe539db2015-09-04 11:00:43 -0700299 private final String expectedAppId;
Ray Milkey4f5de002014-12-17 19:26:11 -0800300 private String reason = "";
301
Ray Milkeybe539db2015-09-04 11:00:43 -0700302 public FlowJsonMatcher(FlowEntry flowValue, String expectedAppIdValue) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800303 flow = flowValue;
Ray Milkeybe539db2015-09-04 11:00:43 -0700304 expectedAppId = expectedAppIdValue;
Ray Milkey4f5de002014-12-17 19:26:11 -0800305 }
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
Ray Milkeybe539db2015-09-04 11:00:43 -0700318 final String jsonAppId = jsonFlow.get("appId").asString();
319 if (!jsonAppId.equals(expectedAppId)) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800320 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 */
Ray Milkeybe539db2015-09-04 11:00:43 -0700401 private static FlowJsonMatcher matchesFlow(FlowEntry flow, String expectedAppName) {
402 return new FlowJsonMatcher(flow, expectedAppName);
Ray Milkey4f5de002014-12-17 19:26:11 -0800403 }
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
Ray Milkeybe539db2015-09-04 11:00:43 -0700432 assertThat(jsonFlow, matchesFlow(flow, APP_ID.name()));
Ray Milkey4f5de002014-12-17 19:26:11 -0800433 }
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);
Jian Li9d616492016-03-09 10:52:49 -0800470 final WebTarget wt = target();
471 final String response = wt.path("flows").request().get(String.class);
Ray Milkey4f5de002014-12-17 19:26:11 -0800472 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);
Jian Li9d616492016-03-09 10:52:49 -0800483 final WebTarget wt = target();
484 final String response = wt.path("flows").request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800485 final JsonObject result = Json.parse(response).asObject();
Ray Milkey4f5de002014-12-17 19:26:11 -0800486 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);
Jian Li9d616492016-03-09 10:52:49 -0800511 final WebTarget wt = target();
512 final String response = wt.path("flows/" + deviceId3).request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800513 final JsonObject result = Json.parse(response).asObject();
Ray Milkey4f5de002014-12-17 19:26:11 -0800514 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);
Jian Li9d616492016-03-09 10:52:49 -0800537 final WebTarget wt = target();
538 final String response = wt.path("flows/" + deviceId3 + "/"
539 + Long.toString(flow5.id().value())).request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800540 final JsonObject result = Json.parse(response).asObject();
Ray Milkey4f5de002014-12-17 19:26:11 -0800541 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() {
Jian Li9d616492016-03-09 10:52:49 -0800556 expect(mockFlowService.getFlowEntries(anyObject()))
Ray Milkey4f5de002014-12-17 19:26:11 -0800557 .andReturn(null).anyTimes();
558 replay(mockFlowService);
559 replay(mockDeviceService);
560
Jian Li9d616492016-03-09 10:52:49 -0800561 WebTarget wt = target();
Ray Milkey4f5de002014-12-17 19:26:11 -0800562 try {
Jian Li9d616492016-03-09 10:52:49 -0800563 wt.path("flows/0").request().get(String.class);
Ray Milkey4f5de002014-12-17 19:26:11 -0800564 fail("Fetch of non-existent device did not throw an exception");
Jian Li9d616492016-03-09 10:52:49 -0800565 } catch (NotFoundException ex) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800566 assertThat(ex.getMessage(),
Jian Li9d616492016-03-09 10:52:49 -0800567 containsString("HTTP 404 Not Found"));
Ray Milkey4f5de002014-12-17 19:26:11 -0800568 }
569 }
Ray Milkeyd43fe452015-05-29 09:35:12 -0700570
571 /**
572 * Tests creating a flow with POST.
573 */
574 @Test
575 public void testPost() {
Ray Milkeyd43fe452015-05-29 09:35:12 -0700576 mockFlowService.applyFlowRules(anyObject());
577 expectLastCall();
578 replay(mockFlowService);
579
Jian Li9d616492016-03-09 10:52:49 -0800580 WebTarget wt = target();
Phaneendra Manda2be2f882015-11-11 15:23:40 +0530581 InputStream jsonStream = FlowsResourceTest.class
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700582 .getResourceAsStream("post-flow.json");
Ray Milkeyd43fe452015-05-29 09:35:12 -0700583
Jian Li9d616492016-03-09 10:52:49 -0800584 Response response = wt.path("flows/of:0000000000000001")
585 .request(MediaType.APPLICATION_JSON_TYPE)
586 .post(Entity.json(jsonStream));
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700587 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED));
588 String location = response.getLocation().getPath();
589 assertThat(location, Matchers.startsWith("/flows/of:0000000000000001/"));
590 }
591
592 /**
593 * Tests deleting a flow.
594 */
595 @Test
596 public void testDelete() {
597 setupMockFlows();
598 mockFlowService.removeFlowRules(anyObject());
599 expectLastCall();
600 replay(mockFlowService);
601
Jian Li9d616492016-03-09 10:52:49 -0800602 WebTarget wt = target();
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700603
604 String location = "/flows/1/155";
605
Jian Li9d616492016-03-09 10:52:49 -0800606 Response deleteResponse = wt.path(location)
607 .request(MediaType.APPLICATION_JSON_TYPE)
608 .delete();
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700609 assertThat(deleteResponse.getStatus(),
610 is(HttpURLConnection.HTTP_NO_CONTENT));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700611 }
Ray Milkey4f5de002014-12-17 19:26:11 -0800612}