Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 3 | * |
| 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 Li | 8ae9120 | 2016-03-24 14:36:16 -0700 | [diff] [blame] | 17 | package org.onosproject.rest.resources; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 18 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 19 | import com.eclipsesource.json.Json; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 20 | import com.eclipsesource.json.JsonArray; |
| 21 | import com.eclipsesource.json.JsonObject; |
Ray Milkey | 957bb37 | 2018-04-03 15:39:46 -0700 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableList; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 23 | import com.google.common.collect.ImmutableSet; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 24 | import org.hamcrest.Description; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 25 | import org.hamcrest.Matchers; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 26 | import org.hamcrest.TypeSafeMatcher; |
| 27 | import org.junit.After; |
| 28 | import org.junit.Before; |
| 29 | import org.junit.Test; |
| 30 | import org.onlab.osgi.ServiceDirectory; |
| 31 | import org.onlab.osgi.TestServiceDirectory; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 32 | import org.onosproject.codec.CodecService; |
| 33 | import org.onosproject.codec.impl.CodecManager; |
| 34 | import org.onosproject.codec.impl.GroupCodec; |
| 35 | import org.onosproject.core.ApplicationId; |
| 36 | import org.onosproject.core.CoreService; |
| 37 | import org.onosproject.core.DefaultApplicationId; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 38 | import org.onosproject.core.GroupId; |
| 39 | import org.onosproject.net.DefaultDevice; |
| 40 | import org.onosproject.net.Device; |
| 41 | import org.onosproject.net.DeviceId; |
| 42 | import org.onosproject.net.NetTestTools; |
| 43 | import org.onosproject.net.device.DeviceService; |
| 44 | import org.onosproject.net.group.DefaultGroupKey; |
| 45 | import org.onosproject.net.group.Group; |
| 46 | import org.onosproject.net.group.GroupBucket; |
| 47 | import org.onosproject.net.group.GroupBuckets; |
| 48 | import org.onosproject.net.group.GroupDescription; |
| 49 | import org.onosproject.net.group.GroupKey; |
| 50 | import org.onosproject.net.group.GroupService; |
Ray Milkey | 957bb37 | 2018-04-03 15:39:46 -0700 | [diff] [blame] | 51 | import org.onosproject.net.group.GroupServiceAdapter; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 52 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 53 | import javax.ws.rs.client.Entity; |
| 54 | import javax.ws.rs.client.WebTarget; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 55 | import javax.ws.rs.core.MediaType; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 56 | import javax.ws.rs.core.Response; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 57 | import java.io.InputStream; |
| 58 | import java.net.HttpURLConnection; |
| 59 | import java.util.ArrayList; |
| 60 | import java.util.HashMap; |
| 61 | import java.util.HashSet; |
| 62 | import java.util.List; |
| 63 | import java.util.Set; |
| 64 | |
| 65 | import static org.easymock.EasyMock.anyObject; |
| 66 | import static org.easymock.EasyMock.anyShort; |
| 67 | import static org.easymock.EasyMock.createMock; |
| 68 | import static org.easymock.EasyMock.expect; |
| 69 | import static org.easymock.EasyMock.expectLastCall; |
| 70 | import static org.easymock.EasyMock.replay; |
| 71 | import static org.easymock.EasyMock.verify; |
| 72 | import static org.hamcrest.Matchers.hasSize; |
| 73 | import static org.hamcrest.Matchers.is; |
| 74 | import static org.hamcrest.Matchers.notNullValue; |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 75 | import static org.junit.Assert.assertEquals; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 76 | import static org.junit.Assert.assertThat; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 77 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 78 | |
| 79 | /** |
| 80 | * Unit tests for Groups REST APIs. |
| 81 | */ |
| 82 | public 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 Sharma | 1853b3f | 2016-07-18 14:37:13 +0530 | [diff] [blame] | 98 | final MockGroup group1 = new MockGroup(deviceId1, 1, "0x111", 1); |
| 99 | final MockGroup group2 = new MockGroup(deviceId1, 2, "0x222", 2); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 100 | |
Varun Sharma | 1853b3f | 2016-07-18 14:37:13 +0530 | [diff] [blame] | 101 | final MockGroup group3 = new MockGroup(deviceId2, 3, "0x333", 3); |
| 102 | final MockGroup group4 = new MockGroup(deviceId2, 4, "0x444", 4); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 103 | |
Varun Sharma | 1853b3f | 2016-07-18 14:37:13 +0530 | [diff] [blame] | 104 | final MockGroup group5 = new MockGroup(deviceId3, 5, "0x555", 5); |
| 105 | final MockGroup group6 = new MockGroup(deviceId3, 6, "0x666", 6); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 106 | |
| 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()); |
rohit | c2e7736 | 2017-03-27 20:07:25 +0530 | [diff] [blame] | 123 | this.baseValue = id * 100L; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 124 | this.bucketList = new ArrayList<>(); |
| 125 | this.buckets = new GroupBuckets(bucketList); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public GroupId id() { |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 130 | return new GroupId((int) baseValue + 55); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 131 | } |
| 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 |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 159 | public int age() { |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | @Override |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 164 | 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 Das | 137f27f | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 192 | |
| 193 | @Override |
| 194 | public int failedRetryCount() { |
| 195 | return 0; |
| 196 | } |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 197 | } |
| 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 Milkey | 094a135 | 2018-01-22 14:03:54 -0800 | [diff] [blame] | 250 | setServiceDirectory(testDirectory); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 251 | } |
| 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 |
Seyeon Jeong | 11c148b | 2020-03-03 12:49:48 -0800 | [diff] [blame] | 279 | final Long jsonId = jsonGroup.get("id").asLong(); |
Prince Pereira | 3ff504c | 2016-08-30 14:23:43 +0530 | [diff] [blame] | 280 | final String groupId = group.id().id().toString(); |
Seyeon Jeong | 11c148b | 2020-03-03 12:49:48 -0800 | [diff] [blame] | 281 | if (!jsonId.equals(Long.valueOf(groupId))) { |
Prince Pereira | 3ff504c | 2016-08-30 14:23:43 +0530 | [diff] [blame] | 282 | reason = "id " + group.id().id().toString(); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 283 | return false; |
| 284 | } |
| 285 | |
| 286 | // check application id |
| 287 | final String jsonAppId = jsonGroup.get("appId").asString(); |
varunsha | 34b3060 | 2016-08-18 10:31:18 -0700 | [diff] [blame] | 288 | final String appId = group.appId().name(); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 289 | if (!jsonAppId.equals(appId)) { |
varunsha | 34b3060 | 2016-08-18 10:31:18 -0700 | [diff] [blame] | 290 | reason = "appId " + group.appId().name(); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 291 | 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 Pereira | 3ff504c | 2016-08-30 14:23:43 +0530 | [diff] [blame] | 364 | final String groupId = group.id().id().toString(); |
Seyeon Jeong | 11c148b | 2020-03-03 12:49:48 -0800 | [diff] [blame] | 365 | final Long jsonGroupId = jsonGroup.get("id").asLong(); |
| 366 | if (jsonGroupId.equals(Long.valueOf(groupId))) { |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 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 Pereira | 3ff504c | 2016-08-30 14:23:43 +0530 | [diff] [blame] | 374 | reason = "Group with id " + group.id().id().toString() + " not found"; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 375 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 406 | final WebTarget wt = target(); |
| 407 | final String response = wt.path("groups").request().get(String.class); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 408 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 419 | final WebTarget wt = target(); |
| 420 | final String response = wt.path("groups").request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 421 | final JsonObject result = Json.parse(response).asObject(); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 422 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 447 | final WebTarget wt = target(); |
| 448 | final String response = wt.path("groups/" + deviceId3).request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 449 | final JsonObject result = Json.parse(response).asObject(); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 450 | assertThat(result, notNullValue()); |
| 451 | |
| 452 | assertThat(result.names(), hasSize(1)); |
| 453 | assertThat(result.names().get(0), is("groups")); |
Jian Li | 2e02fab | 2016-02-25 15:45:59 +0900 | [diff] [blame] | 454 | final JsonArray jsonGroups = result.get("groups").asArray(); |
| 455 | assertThat(jsonGroups, notNullValue()); |
| 456 | assertThat(jsonGroups, hasGroup(group5)); |
| 457 | assertThat(jsonGroups, hasGroup(group6)); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | /** |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 461 | * 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 469 | final WebTarget wt = target(); |
Varun Sharma | 1853b3f | 2016-07-18 14:37:13 +0530 | [diff] [blame] | 470 | final String response = wt.path("groups/" + deviceId3 + "/" + "0x111") |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 471 | .request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 472 | final JsonObject result = Json.parse(response).asObject(); |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 473 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 491 | final WebTarget wt = target(); |
Varun Sharma | 1853b3f | 2016-07-18 14:37:13 +0530 | [diff] [blame] | 492 | final Response response = wt.path("groups/" + deviceId3 + "/" + "0x222").request().get(); |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 493 | |
| 494 | assertEquals(404, response.getStatus()); |
| 495 | } |
| 496 | |
| 497 | /** |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 498 | * Tests creating a group with POST. |
| 499 | */ |
| 500 | @Test |
| 501 | public void testPost() { |
| 502 | mockGroupService.addGroup(anyObject()); |
| 503 | expectLastCall(); |
| 504 | replay(mockGroupService); |
| 505 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 506 | WebTarget wt = target(); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 507 | InputStream jsonStream = GroupsResourceTest.class |
| 508 | .getResourceAsStream("post-group.json"); |
| 509 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 510 | Response response = wt.path("groups/of:0000000000000001") |
| 511 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 512 | .post(Entity.json(jsonStream)); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 513 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED)); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 514 | String location = response.getLocation().getPath(); |
| 515 | assertThat(location, Matchers.startsWith("/groups/of:0000000000000001/")); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 516 | } |
| 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 528 | WebTarget wt = target(); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 529 | |
Varun Sharma | 1853b3f | 2016-07-18 14:37:13 +0530 | [diff] [blame] | 530 | String location = "/groups/1/0x111"; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 531 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 532 | Response deleteResponse = wt.path(location) |
| 533 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 534 | .delete(); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 535 | assertThat(deleteResponse.getStatus(), |
| 536 | is(HttpURLConnection.HTTP_NO_CONTENT)); |
| 537 | } |
Ray Milkey | 957bb37 | 2018-04-03 15:39:46 -0700 | [diff] [blame] | 538 | |
| 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 Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 656 | } |