blob: e13784d35ecf2aae521ec4869d4aa3d61aba321c [file] [log] [blame]
Jian Liecb3c0f2015-12-15 10:07:49 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jian Liecb3c0f2015-12-15 10:07:49 -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 */
16
Jian Li8ae91202016-03-24 14:36:16 -070017package org.onosproject.rest.resources;
Jian Liecb3c0f2015-12-15 10:07:49 -080018
Jian Li80cfe452016-01-14 16:04:58 -080019import com.eclipsesource.json.Json;
Jian Liecb3c0f2015-12-15 10:07:49 -080020import com.eclipsesource.json.JsonArray;
21import com.eclipsesource.json.JsonObject;
Ray Milkey957bb372018-04-03 15:39:46 -070022import com.google.common.collect.ImmutableList;
Jian Liecb3c0f2015-12-15 10:07:49 -080023import com.google.common.collect.ImmutableSet;
Jian Liecb3c0f2015-12-15 10:07:49 -080024import org.hamcrest.Description;
Jian Li9d616492016-03-09 10:52:49 -080025import org.hamcrest.Matchers;
Jian Liecb3c0f2015-12-15 10:07:49 -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;
Jian Liecb3c0f2015-12-15 10:07:49 -080032import org.onosproject.codec.CodecService;
33import org.onosproject.codec.impl.CodecManager;
34import org.onosproject.codec.impl.GroupCodec;
35import org.onosproject.core.ApplicationId;
36import org.onosproject.core.CoreService;
37import org.onosproject.core.DefaultApplicationId;
Jian Liecb3c0f2015-12-15 10:07:49 -080038import org.onosproject.core.GroupId;
39import org.onosproject.net.DefaultDevice;
40import org.onosproject.net.Device;
41import org.onosproject.net.DeviceId;
42import org.onosproject.net.NetTestTools;
43import org.onosproject.net.device.DeviceService;
44import org.onosproject.net.group.DefaultGroupKey;
45import org.onosproject.net.group.Group;
46import org.onosproject.net.group.GroupBucket;
47import org.onosproject.net.group.GroupBuckets;
48import org.onosproject.net.group.GroupDescription;
49import org.onosproject.net.group.GroupKey;
50import org.onosproject.net.group.GroupService;
Ray Milkey957bb372018-04-03 15:39:46 -070051import org.onosproject.net.group.GroupServiceAdapter;
Jian Liecb3c0f2015-12-15 10:07:49 -080052
Jian Li9d616492016-03-09 10:52:49 -080053import javax.ws.rs.client.Entity;
54import javax.ws.rs.client.WebTarget;
Jian Liecb3c0f2015-12-15 10:07:49 -080055import javax.ws.rs.core.MediaType;
Jian Li9d616492016-03-09 10:52:49 -080056import javax.ws.rs.core.Response;
Jian Liecb3c0f2015-12-15 10:07:49 -080057import java.io.InputStream;
58import java.net.HttpURLConnection;
59import java.util.ArrayList;
60import java.util.HashMap;
61import java.util.HashSet;
62import java.util.List;
63import java.util.Set;
64
65import static org.easymock.EasyMock.anyObject;
66import static org.easymock.EasyMock.anyShort;
67import static org.easymock.EasyMock.createMock;
68import static org.easymock.EasyMock.expect;
69import static org.easymock.EasyMock.expectLastCall;
70import static org.easymock.EasyMock.replay;
71import static org.easymock.EasyMock.verify;
72import static org.hamcrest.Matchers.hasSize;
73import static org.hamcrest.Matchers.is;
74import static org.hamcrest.Matchers.notNullValue;
Jian Lia7f86ce2015-12-20 13:42:10 -080075import static org.junit.Assert.assertEquals;
Jian Li9d616492016-03-09 10:52:49 -080076import static org.junit.Assert.assertThat;
Jian Liecb3c0f2015-12-15 10:07:49 -080077import static org.onosproject.net.NetTestTools.APP_ID;
78
79/**
80 * Unit tests for Groups REST APIs.
81 */
82public class GroupsResourceTest extends ResourceTest {
83 final GroupService mockGroupService = createMock(GroupService.class);
84 CoreService mockCoreService = createMock(CoreService.class);
85 final DeviceService mockDeviceService = createMock(DeviceService.class);
86
87 final HashMap<DeviceId, Set<Group>> groups = new HashMap<>();
88
89
90 final DeviceId deviceId1 = DeviceId.deviceId("1");
91 final DeviceId deviceId2 = DeviceId.deviceId("2");
92 final DeviceId deviceId3 = DeviceId.deviceId("3");
93 final Device device1 = new DefaultDevice(null, deviceId1, Device.Type.OTHER,
94 "", "", "", "", null);
95 final Device device2 = new DefaultDevice(null, deviceId2, Device.Type.OTHER,
96 "", "", "", "", null);
97
Varun Sharma1853b3f2016-07-18 14:37:13 +053098 final MockGroup group1 = new MockGroup(deviceId1, 1, "0x111", 1);
99 final MockGroup group2 = new MockGroup(deviceId1, 2, "0x222", 2);
Jian Liecb3c0f2015-12-15 10:07:49 -0800100
Varun Sharma1853b3f2016-07-18 14:37:13 +0530101 final MockGroup group3 = new MockGroup(deviceId2, 3, "0x333", 3);
102 final MockGroup group4 = new MockGroup(deviceId2, 4, "0x444", 4);
Jian Liecb3c0f2015-12-15 10:07:49 -0800103
Varun Sharma1853b3f2016-07-18 14:37:13 +0530104 final MockGroup group5 = new MockGroup(deviceId3, 5, "0x555", 5);
105 final MockGroup group6 = new MockGroup(deviceId3, 6, "0x666", 6);
Jian Liecb3c0f2015-12-15 10:07:49 -0800106
107 /**
108 * Mock class for a group.
109 */
110 private static class MockGroup implements Group {
111
112 final DeviceId deviceId;
113 final ApplicationId appId;
114 final GroupKey appCookie;
115 final long baseValue;
116 final List<GroupBucket> bucketList;
117 GroupBuckets buckets;
118
119 public MockGroup(DeviceId deviceId, int appId, String appCookie, int id) {
120 this.deviceId = deviceId;
121 this.appId = new DefaultApplicationId(appId, String.valueOf(appId));
122 this.appCookie = new DefaultGroupKey(appCookie.getBytes());
rohitc2e77362017-03-27 20:07:25 +0530123 this.baseValue = id * 100L;
Jian Liecb3c0f2015-12-15 10:07:49 -0800124 this.bucketList = new ArrayList<>();
125 this.buckets = new GroupBuckets(bucketList);
126 }
127
128 @Override
129 public GroupId id() {
Yi Tsengfa394de2017-02-01 11:26:40 -0800130 return new GroupId((int) baseValue + 55);
Jian Liecb3c0f2015-12-15 10:07:49 -0800131 }
132
133 @Override
134 public GroupState state() {
135 return GroupState.ADDED;
136 }
137
138 @Override
139 public long life() {
140 return baseValue + 11;
141 }
142
143 @Override
144 public long packets() {
145 return baseValue + 22;
146 }
147
148 @Override
149 public long bytes() {
150 return baseValue + 33;
151 }
152
153 @Override
154 public long referenceCount() {
155 return baseValue + 44;
156 }
157
158 @Override
alshabibb0285992016-03-28 23:30:37 -0700159 public int age() {
160 return 0;
161 }
162
163 @Override
Jian Liecb3c0f2015-12-15 10:07:49 -0800164 public Type type() {
165 return GroupDescription.Type.ALL;
166 }
167
168 @Override
169 public DeviceId deviceId() {
170 return this.deviceId;
171 }
172
173 @Override
174 public ApplicationId appId() {
175 return this.appId;
176 }
177
178 @Override
179 public GroupKey appCookie() {
180 return this.appCookie;
181 }
182
183 @Override
184 public Integer givenGroupId() {
185 return (int) baseValue + 55;
186 }
187
188 @Override
189 public GroupBuckets buckets() {
190 return this.buckets;
191 }
Saurav Das137f27f2018-06-11 17:02:31 -0700192
193 @Override
194 public int failedRetryCount() {
195 return 0;
196 }
Jian Liecb3c0f2015-12-15 10:07:49 -0800197 }
198
199 /**
200 * Populates some groups used as testing data.
201 */
202 private void setupMockGroups() {
203 final Set<Group> groups1 = new HashSet<>();
204 groups1.add(group1);
205 groups1.add(group2);
206
207 final Set<Group> groups2 = new HashSet<>();
208 groups2.add(group3);
209 groups2.add(group4);
210
211 groups.put(deviceId1, groups1);
212 groups.put(deviceId2, groups2);
213
214 expect(mockGroupService.getGroups(deviceId1))
215 .andReturn(groups.get(deviceId1)).anyTimes();
216 expect(mockGroupService.getGroups(deviceId2))
217 .andReturn(groups.get(deviceId2)).anyTimes();
218 }
219
220 /**
221 * Sets up the global values for all the tests.
222 */
223 @Before
224 public void setUpTest() {
225 // Mock device service
226 expect(mockDeviceService.getDevice(deviceId1))
227 .andReturn(device1);
228 expect(mockDeviceService.getDevice(deviceId2))
229 .andReturn(device2);
230 expect(mockDeviceService.getDevices())
231 .andReturn(ImmutableSet.of(device1, device2));
232
233 // Mock Core Service
234 expect(mockCoreService.getAppId(anyShort()))
235 .andReturn(NetTestTools.APP_ID).anyTimes();
236 expect(mockCoreService.registerApplication(GroupCodec.REST_APP_ID))
237 .andReturn(APP_ID).anyTimes();
238 replay(mockCoreService);
239
240 // Register the services needed for the test
241 final CodecManager codecService = new CodecManager();
242 codecService.activate();
243 ServiceDirectory testDirectory =
244 new TestServiceDirectory()
245 .add(GroupService.class, mockGroupService)
246 .add(DeviceService.class, mockDeviceService)
247 .add(CodecService.class, codecService)
248 .add(CoreService.class, mockCoreService);
249
Ray Milkey094a1352018-01-22 14:03:54 -0800250 setServiceDirectory(testDirectory);
Jian Liecb3c0f2015-12-15 10:07:49 -0800251 }
252
253 /**
254 * Cleans up and verifies the mocks.
255 */
256 @After
257 public void tearDownTest() {
258 verify(mockGroupService);
259 verify(mockCoreService);
260 }
261
262 /**
263 * Hamcrest matcher to check that a group representation in JSON matches
264 * the actual group.
265 */
266 public static class GroupJsonMatcher extends TypeSafeMatcher<JsonObject> {
267 private final Group group;
268 private final String expectedAppId;
269 private String reason = "";
270
271 public GroupJsonMatcher(Group groupValue, String expectedAppIdValue) {
272 group = groupValue;
273 expectedAppId = expectedAppIdValue;
274 }
275
276 @Override
277 public boolean matchesSafely(JsonObject jsonGroup) {
278 // check id
279 final String jsonId = jsonGroup.get("id").asString();
Prince Pereira3ff504c2016-08-30 14:23:43 +0530280 final String groupId = group.id().id().toString();
Jian Liecb3c0f2015-12-15 10:07:49 -0800281 if (!jsonId.equals(groupId)) {
Prince Pereira3ff504c2016-08-30 14:23:43 +0530282 reason = "id " + group.id().id().toString();
Jian Liecb3c0f2015-12-15 10:07:49 -0800283 return false;
284 }
285
286 // check application id
287 final String jsonAppId = jsonGroup.get("appId").asString();
varunsha34b30602016-08-18 10:31:18 -0700288 final String appId = group.appId().name();
Jian Liecb3c0f2015-12-15 10:07:49 -0800289 if (!jsonAppId.equals(appId)) {
varunsha34b30602016-08-18 10:31:18 -0700290 reason = "appId " + group.appId().name();
Jian Liecb3c0f2015-12-15 10:07:49 -0800291 return false;
292 }
293
294 // check device id
295 final String jsonDeviceId = jsonGroup.get("deviceId").asString();
296 if (!jsonDeviceId.equals(group.deviceId().toString())) {
297 reason = "deviceId " + group.deviceId();
298 return false;
299 }
300
301 // check bucket array
302 if (group.buckets().buckets() != null) {
303 final JsonArray jsonBuckets = jsonGroup.get("buckets").asArray();
304 if (group.buckets().buckets().size() != jsonBuckets.size()) {
305 reason = "buckets array size of " +
306 Integer.toString(group.buckets().buckets().size());
307 return false;
308 }
309 for (final GroupBucket groupBucket : group.buckets().buckets()) {
310 boolean groupBucketFound = false;
311 for (int groupBucketIndex = 0; groupBucketIndex < jsonBuckets.size(); groupBucketIndex++) {
312 final String jsonType = jsonBuckets.get(groupBucketIndex).asObject().get("type").asString();
313 final String bucketType = groupBucket.type().name();
314 if (jsonType.equals(bucketType)) {
315 groupBucketFound = true;
316 }
317 }
318 if (!groupBucketFound) {
319 reason = "group bucket " + groupBucket.toString();
320 return false;
321 }
322 }
323 }
324
325 return true;
326 }
327
328 @Override
329 public void describeTo(Description description) {
330 description.appendText(reason);
331 }
332 }
333
334 /**
335 * Factory to allocate a group matcher.
336 *
337 * @param group group object we are looking for
338 * @return matcher
339 */
340 private static GroupJsonMatcher matchesGroup(Group group, String expectedAppName) {
341 return new GroupJsonMatcher(group, expectedAppName);
342 }
343
344 /**
345 * Hamcrest matcher to check that a group is represented properly in a JSON
346 * array of flows.
347 */
348 public static class GroupJsonArrayMatcher extends TypeSafeMatcher<JsonArray> {
349 private final Group group;
350 private String reason = "";
351
352 public GroupJsonArrayMatcher(Group groupValue) {
353 group = groupValue;
354 }
355
356 @Override
357 public boolean matchesSafely(JsonArray json) {
358 boolean groupFound = false;
359 for (int jsonGroupIndex = 0; jsonGroupIndex < json.size();
360 jsonGroupIndex++) {
361
362 final JsonObject jsonGroup = json.get(jsonGroupIndex).asObject();
363
Prince Pereira3ff504c2016-08-30 14:23:43 +0530364 final String groupId = group.id().id().toString();
Jian Liecb3c0f2015-12-15 10:07:49 -0800365 final String jsonGroupId = jsonGroup.get("id").asString();
366 if (jsonGroupId.equals(groupId)) {
367 groupFound = true;
368
369 // We found the correct group, check attribute values
370 assertThat(jsonGroup, matchesGroup(group, APP_ID.name()));
371 }
372 }
373 if (!groupFound) {
Prince Pereira3ff504c2016-08-30 14:23:43 +0530374 reason = "Group with id " + group.id().id().toString() + " not found";
Jian Liecb3c0f2015-12-15 10:07:49 -0800375 return false;
376 } else {
377 return true;
378 }
379 }
380
381 @Override
382 public void describeTo(Description description) {
383 description.appendText(reason);
384 }
385 }
386
387 /**
388 * Factory to allocate a group array matcher.
389 *
390 * @param group group object we are looking for
391 * @return matcher
392 */
393 private static GroupJsonArrayMatcher hasGroup(Group group) {
394 return new GroupJsonArrayMatcher(group);
395 }
396
397 /**
398 * Tests the result of the rest api GET when there are no groups.
399 */
400 @Test
401 public void testGroupsEmptyArray() {
402 expect(mockGroupService.getGroups(deviceId1)).andReturn(null).anyTimes();
403 expect(mockGroupService.getGroups(deviceId2)).andReturn(null).anyTimes();
404 replay(mockGroupService);
405 replay(mockDeviceService);
Jian Li9d616492016-03-09 10:52:49 -0800406 final WebTarget wt = target();
407 final String response = wt.path("groups").request().get(String.class);
Jian Liecb3c0f2015-12-15 10:07:49 -0800408 assertThat(response, is("{\"groups\":[]}"));
409 }
410
411 /**
412 * Tests the result of the rest api GET when there are active groups.
413 */
414 @Test
415 public void testGroupsPopulatedArray() {
416 setupMockGroups();
417 replay(mockGroupService);
418 replay(mockDeviceService);
Jian Li9d616492016-03-09 10:52:49 -0800419 final WebTarget wt = target();
420 final String response = wt.path("groups").request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800421 final JsonObject result = Json.parse(response).asObject();
Jian Liecb3c0f2015-12-15 10:07:49 -0800422 assertThat(result, notNullValue());
423
424 assertThat(result.names(), hasSize(1));
425 assertThat(result.names().get(0), is("groups"));
426 final JsonArray jsonGroups = result.get("groups").asArray();
427 assertThat(jsonGroups, notNullValue());
428 assertThat(jsonGroups, hasGroup(group1));
429 assertThat(jsonGroups, hasGroup(group2));
430 assertThat(jsonGroups, hasGroup(group3));
431 assertThat(jsonGroups, hasGroup(group4));
432 }
433
434 /**
435 * Tests the result of a rest api GET for a device.
436 */
437 @Test
438 public void testGroupsSingleDevice() {
439 setupMockGroups();
440 final Set<Group> groups = new HashSet<>();
441 groups.add(group5);
442 groups.add(group6);
443 expect(mockGroupService.getGroups(anyObject()))
444 .andReturn(groups).anyTimes();
445 replay(mockGroupService);
446 replay(mockDeviceService);
Jian Li9d616492016-03-09 10:52:49 -0800447 final WebTarget wt = target();
448 final String response = wt.path("groups/" + deviceId3).request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800449 final JsonObject result = Json.parse(response).asObject();
Jian Liecb3c0f2015-12-15 10:07:49 -0800450 assertThat(result, notNullValue());
451
452 assertThat(result.names(), hasSize(1));
453 assertThat(result.names().get(0), is("groups"));
Jian Li2e02fab2016-02-25 15:45:59 +0900454 final JsonArray jsonGroups = result.get("groups").asArray();
455 assertThat(jsonGroups, notNullValue());
456 assertThat(jsonGroups, hasGroup(group5));
457 assertThat(jsonGroups, hasGroup(group6));
Jian Liecb3c0f2015-12-15 10:07:49 -0800458 }
459
460 /**
Jian Lia7f86ce2015-12-20 13:42:10 -0800461 * Test the result of a rest api GET with specifying device id and appcookie.
462 */
463 @Test
464 public void testGroupByDeviceIdAndAppCookie() {
465 setupMockGroups();
466 expect(mockGroupService.getGroup(anyObject(), anyObject()))
467 .andReturn(group5).anyTimes();
468 replay(mockGroupService);
Jian Li9d616492016-03-09 10:52:49 -0800469 final WebTarget wt = target();
Varun Sharma1853b3f2016-07-18 14:37:13 +0530470 final String response = wt.path("groups/" + deviceId3 + "/" + "0x111")
Jian Li9d616492016-03-09 10:52:49 -0800471 .request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800472 final JsonObject result = Json.parse(response).asObject();
Jian Lia7f86ce2015-12-20 13:42:10 -0800473 assertThat(result, notNullValue());
474
475 assertThat(result.names(), hasSize(1));
476 assertThat(result.names().get(0), is("groups"));
477 final JsonArray jsonFlows = result.get("groups").asArray();
478 assertThat(jsonFlows, notNullValue());
479 assertThat(jsonFlows, hasGroup(group5));
480 }
481
482 /**
483 * Test whether the REST API returns 404 if no entry has been found.
484 */
485 @Test
486 public void testGroupByDeviceIdAndAppCookieNull() {
487 setupMockGroups();
488 expect(mockGroupService.getGroup(anyObject(), anyObject()))
489 .andReturn(null).anyTimes();
490 replay(mockGroupService);
Jian Li9d616492016-03-09 10:52:49 -0800491 final WebTarget wt = target();
Varun Sharma1853b3f2016-07-18 14:37:13 +0530492 final Response response = wt.path("groups/" + deviceId3 + "/" + "0x222").request().get();
Jian Lia7f86ce2015-12-20 13:42:10 -0800493
494 assertEquals(404, response.getStatus());
495 }
496
497 /**
Jian Liecb3c0f2015-12-15 10:07:49 -0800498 * Tests creating a group with POST.
499 */
500 @Test
501 public void testPost() {
502 mockGroupService.addGroup(anyObject());
503 expectLastCall();
504 replay(mockGroupService);
505
Jian Li9d616492016-03-09 10:52:49 -0800506 WebTarget wt = target();
Jian Liecb3c0f2015-12-15 10:07:49 -0800507 InputStream jsonStream = GroupsResourceTest.class
508 .getResourceAsStream("post-group.json");
509
Jian Li9d616492016-03-09 10:52:49 -0800510 Response response = wt.path("groups/of:0000000000000001")
511 .request(MediaType.APPLICATION_JSON_TYPE)
512 .post(Entity.json(jsonStream));
Jian Liecb3c0f2015-12-15 10:07:49 -0800513 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED));
Jian Li9d616492016-03-09 10:52:49 -0800514 String location = response.getLocation().getPath();
515 assertThat(location, Matchers.startsWith("/groups/of:0000000000000001/"));
Jian Liecb3c0f2015-12-15 10:07:49 -0800516 }
517
518 /**
519 * Tests deleting a group.
520 */
521 @Test
522 public void testDelete() {
523 setupMockGroups();
524 mockGroupService.removeGroup(anyObject(), anyObject(), anyObject());
525 expectLastCall();
526 replay(mockGroupService);
527
Jian Li9d616492016-03-09 10:52:49 -0800528 WebTarget wt = target();
Jian Liecb3c0f2015-12-15 10:07:49 -0800529
Varun Sharma1853b3f2016-07-18 14:37:13 +0530530 String location = "/groups/1/0x111";
Jian Liecb3c0f2015-12-15 10:07:49 -0800531
Jian Li9d616492016-03-09 10:52:49 -0800532 Response deleteResponse = wt.path(location)
533 .request(MediaType.APPLICATION_JSON_TYPE)
534 .delete();
Jian Liecb3c0f2015-12-15 10:07:49 -0800535 assertThat(deleteResponse.getStatus(),
536 is(HttpURLConnection.HTTP_NO_CONTENT));
537 }
Ray Milkey957bb372018-04-03 15:39:46 -0700538
539 private JsonArray fetchAndCheckBuckets(DeviceId deviceId, int expectedBucketCount) {
540 WebTarget wt = target();
541 final String fetchGroupPath = wt.path("groups/" + deviceId).request().get(String.class);
542 final JsonObject result = Json.parse(fetchGroupPath).asObject();
543 assertThat(result, notNullValue());
544 assertThat(result.names(), hasSize(1));
545 assertThat(result.names().get(0), is("groups"));
546 JsonArray jsonGroups = result.get("groups").asArray();
547 assertThat(jsonGroups, notNullValue());
548 JsonObject group = jsonGroups.get(0).asObject();
549 JsonArray buckets = group.get("buckets").asArray();
550 assertThat(buckets.size(), is(expectedBucketCount));
551 return buckets;
552 }
553
554 private void removeBucketsViaRest(String endpointPath) {
555 WebTarget wt = target();
556 Response removeResponse = wt.path(endpointPath)
557 .request(MediaType.APPLICATION_JSON_TYPE)
558 .delete();
559 assertThat(removeResponse.getStatus(), is(HttpURLConnection.HTTP_NO_CONTENT));
560 }
561
562
563 class TestGroupService extends GroupServiceAdapter {
564 @Override
565 public void addBucketsToGroup(DeviceId deviceId, GroupKey oldCookie, GroupBuckets buckets,
566 GroupKey newCookie, ApplicationId appId) {
567 group1.buckets = buckets;
568 }
569
570 @Override
571 public void removeBucketsFromGroup(DeviceId deviceId, GroupKey oldCookie, GroupBuckets buckets,
572 GroupKey newCookie, ApplicationId appId) {
573 if (!group1.buckets.buckets().isEmpty()) {
574 ArrayList<GroupBucket> newList = new ArrayList<>(group1.buckets.buckets());
575 for (GroupBucket bucketToRemove : buckets.buckets()) {
576 for (GroupBucket bucketToCheck : group1.buckets.buckets()) {
577 if (bucketToCheck.equals(bucketToRemove)) {
578 newList.remove(bucketToCheck);
579 }
580 }
581 }
582 group1.buckets = new GroupBuckets(newList);
583 }
584 }
585
586 @Override
587 public Group getGroup(DeviceId deviceId, GroupKey appCookie) {
588 return group1;
589 }
590
591 @Override
592 public Iterable<Group> getGroups(DeviceId deviceId) {
593 return ImmutableList.of(group1);
594 }
595 }
596
597 /**
598 * Tests adding and removing buckets.
599 */
600 @Test
601 public void testAddRemoveBucket() {
602 String deviceIdBasePath = "groups/%s/";
603 String endpointBasePath = deviceIdBasePath + "%s/buckets/";
604 TestGroupService groupService = new TestGroupService();
605 expect(mockGroupService.getGroup(anyObject(), anyObject()))
606 .andDelegateTo(groupService).anyTimes();
607 expect(mockGroupService.getGroups(anyObject()))
608 .andDelegateTo(groupService).anyTimes();
609 mockGroupService.addBucketsToGroup(anyObject(), anyObject(), anyObject(), anyObject(), anyObject());
610 expectLastCall().andDelegateTo(groupService).anyTimes();
611 mockGroupService.removeBucketsFromGroup(anyObject(), anyObject(), anyObject(), anyObject(), anyObject());
612 expectLastCall().andDelegateTo(groupService).anyTimes();
613 replay(mockGroupService);
614
615 WebTarget wt = target();
616
617 // Add buckets
618 String addEndpointPath = String.format(endpointBasePath, group1.deviceId(), group1.appCookie());
619 InputStream addJsonStream = GroupsResourceTest.class
620 .getResourceAsStream("post-group-add-buckets.json");
621
622 Response addResponse = wt.path(addEndpointPath)
623 .request(MediaType.APPLICATION_JSON_TYPE)
624 .post(Entity.json(addJsonStream));
625 assertThat(addResponse.getStatus(), is(HttpURLConnection.HTTP_NO_CONTENT));
626
627 // Check that buckets are there
628 JsonArray bucketsAfterAdd = fetchAndCheckBuckets(deviceId1, 4);
629
630 String bucketId1 = Long.toString(bucketsAfterAdd.get(0).asObject().get("bucketId").asLong());
631 String bucketId2 = Long.toString(bucketsAfterAdd.get(1).asObject().get("bucketId").asLong());
632 String bucketId3 = Long.toString(bucketsAfterAdd.get(2).asObject().get("bucketId").asLong());
633 String bucketId4 = Long.toString(bucketsAfterAdd.get(3).asObject().get("bucketId").asLong());
634
635 // Remove one bucket
636 String removeEndpointPath = addEndpointPath + bucketId1;
637 removeBucketsViaRest(removeEndpointPath);
638 fetchAndCheckBuckets(deviceId1, 3);
639
640 // Remove two buckets
641 String removeTwoEndpointPath = addEndpointPath + bucketId2 + ',' + bucketId3;
642 removeBucketsViaRest(removeTwoEndpointPath);
643 fetchAndCheckBuckets(deviceId1, 1);
644
645 // Remove nothing - non-existent bucket id
646 String removeNothingEndpointPath = addEndpointPath + "no-such-bucket";
647 removeBucketsViaRest(removeNothingEndpointPath);
648 fetchAndCheckBuckets(deviceId1, 1);
649
650 // Remove last bucket - bucket list should be empty
651 String lastOneEndpointPath = addEndpointPath + bucketId4;
652 removeBucketsViaRest(lastOneEndpointPath);
653 fetchAndCheckBuckets(deviceId1, 0);
654 }
655
Jian Liecb3c0f2015-12-15 10:07:49 -0800656}