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; |
| 22 | import com.google.common.collect.ImmutableSet; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 23 | import org.hamcrest.Description; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 24 | import org.hamcrest.Matchers; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 25 | import org.hamcrest.TypeSafeMatcher; |
| 26 | import org.junit.After; |
| 27 | import org.junit.Before; |
| 28 | import org.junit.Test; |
| 29 | import org.onlab.osgi.ServiceDirectory; |
| 30 | import org.onlab.osgi.TestServiceDirectory; |
| 31 | import org.onlab.rest.BaseResource; |
| 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; |
| 51 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 52 | import javax.ws.rs.client.Entity; |
| 53 | import javax.ws.rs.client.WebTarget; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 54 | import javax.ws.rs.core.MediaType; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 55 | import javax.ws.rs.core.Response; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 56 | import java.io.InputStream; |
| 57 | import java.net.HttpURLConnection; |
| 58 | import java.util.ArrayList; |
| 59 | import java.util.HashMap; |
| 60 | import java.util.HashSet; |
| 61 | import java.util.List; |
| 62 | import java.util.Set; |
| 63 | |
| 64 | import static org.easymock.EasyMock.anyObject; |
| 65 | import static org.easymock.EasyMock.anyShort; |
| 66 | import static org.easymock.EasyMock.createMock; |
| 67 | import static org.easymock.EasyMock.expect; |
| 68 | import static org.easymock.EasyMock.expectLastCall; |
| 69 | import static org.easymock.EasyMock.replay; |
| 70 | import static org.easymock.EasyMock.verify; |
| 71 | import static org.hamcrest.Matchers.hasSize; |
| 72 | import static org.hamcrest.Matchers.is; |
| 73 | import static org.hamcrest.Matchers.notNullValue; |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 74 | import static org.junit.Assert.assertEquals; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 75 | import static org.junit.Assert.assertThat; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 76 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 77 | |
| 78 | /** |
| 79 | * Unit tests for Groups REST APIs. |
| 80 | */ |
| 81 | public class GroupsResourceTest extends ResourceTest { |
| 82 | final GroupService mockGroupService = createMock(GroupService.class); |
| 83 | CoreService mockCoreService = createMock(CoreService.class); |
| 84 | final DeviceService mockDeviceService = createMock(DeviceService.class); |
| 85 | |
| 86 | final HashMap<DeviceId, Set<Group>> groups = new HashMap<>(); |
| 87 | |
| 88 | |
| 89 | final DeviceId deviceId1 = DeviceId.deviceId("1"); |
| 90 | final DeviceId deviceId2 = DeviceId.deviceId("2"); |
| 91 | final DeviceId deviceId3 = DeviceId.deviceId("3"); |
| 92 | final Device device1 = new DefaultDevice(null, deviceId1, Device.Type.OTHER, |
| 93 | "", "", "", "", null); |
| 94 | final Device device2 = new DefaultDevice(null, deviceId2, Device.Type.OTHER, |
| 95 | "", "", "", "", null); |
| 96 | |
Varun Sharma | 1853b3f | 2016-07-18 14:37:13 +0530 | [diff] [blame] | 97 | final MockGroup group1 = new MockGroup(deviceId1, 1, "0x111", 1); |
| 98 | final MockGroup group2 = new MockGroup(deviceId1, 2, "0x222", 2); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 99 | |
Varun Sharma | 1853b3f | 2016-07-18 14:37:13 +0530 | [diff] [blame] | 100 | final MockGroup group3 = new MockGroup(deviceId2, 3, "0x333", 3); |
| 101 | final MockGroup group4 = new MockGroup(deviceId2, 4, "0x444", 4); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 102 | |
Varun Sharma | 1853b3f | 2016-07-18 14:37:13 +0530 | [diff] [blame] | 103 | final MockGroup group5 = new MockGroup(deviceId3, 5, "0x555", 5); |
| 104 | final MockGroup group6 = new MockGroup(deviceId3, 6, "0x666", 6); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * Mock class for a group. |
| 108 | */ |
| 109 | private static class MockGroup implements Group { |
| 110 | |
| 111 | final DeviceId deviceId; |
| 112 | final ApplicationId appId; |
| 113 | final GroupKey appCookie; |
| 114 | final long baseValue; |
| 115 | final List<GroupBucket> bucketList; |
| 116 | GroupBuckets buckets; |
| 117 | |
| 118 | public MockGroup(DeviceId deviceId, int appId, String appCookie, int id) { |
| 119 | this.deviceId = deviceId; |
| 120 | this.appId = new DefaultApplicationId(appId, String.valueOf(appId)); |
| 121 | this.appCookie = new DefaultGroupKey(appCookie.getBytes()); |
rohit | c2e7736 | 2017-03-27 20:07:25 +0530 | [diff] [blame] | 122 | this.baseValue = id * 100L; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 123 | this.bucketList = new ArrayList<>(); |
| 124 | this.buckets = new GroupBuckets(bucketList); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public GroupId id() { |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 129 | return new GroupId((int) baseValue + 55); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public GroupState state() { |
| 134 | return GroupState.ADDED; |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public long life() { |
| 139 | return baseValue + 11; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public long packets() { |
| 144 | return baseValue + 22; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public long bytes() { |
| 149 | return baseValue + 33; |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public long referenceCount() { |
| 154 | return baseValue + 44; |
| 155 | } |
| 156 | |
| 157 | @Override |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 158 | public int age() { |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | @Override |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 163 | public Type type() { |
| 164 | return GroupDescription.Type.ALL; |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public DeviceId deviceId() { |
| 169 | return this.deviceId; |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public ApplicationId appId() { |
| 174 | return this.appId; |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public GroupKey appCookie() { |
| 179 | return this.appCookie; |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public Integer givenGroupId() { |
| 184 | return (int) baseValue + 55; |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public GroupBuckets buckets() { |
| 189 | return this.buckets; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Populates some groups used as testing data. |
| 195 | */ |
| 196 | private void setupMockGroups() { |
| 197 | final Set<Group> groups1 = new HashSet<>(); |
| 198 | groups1.add(group1); |
| 199 | groups1.add(group2); |
| 200 | |
| 201 | final Set<Group> groups2 = new HashSet<>(); |
| 202 | groups2.add(group3); |
| 203 | groups2.add(group4); |
| 204 | |
| 205 | groups.put(deviceId1, groups1); |
| 206 | groups.put(deviceId2, groups2); |
| 207 | |
| 208 | expect(mockGroupService.getGroups(deviceId1)) |
| 209 | .andReturn(groups.get(deviceId1)).anyTimes(); |
| 210 | expect(mockGroupService.getGroups(deviceId2)) |
| 211 | .andReturn(groups.get(deviceId2)).anyTimes(); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Sets up the global values for all the tests. |
| 216 | */ |
| 217 | @Before |
| 218 | public void setUpTest() { |
| 219 | // Mock device service |
| 220 | expect(mockDeviceService.getDevice(deviceId1)) |
| 221 | .andReturn(device1); |
| 222 | expect(mockDeviceService.getDevice(deviceId2)) |
| 223 | .andReturn(device2); |
| 224 | expect(mockDeviceService.getDevices()) |
| 225 | .andReturn(ImmutableSet.of(device1, device2)); |
| 226 | |
| 227 | // Mock Core Service |
| 228 | expect(mockCoreService.getAppId(anyShort())) |
| 229 | .andReturn(NetTestTools.APP_ID).anyTimes(); |
| 230 | expect(mockCoreService.registerApplication(GroupCodec.REST_APP_ID)) |
| 231 | .andReturn(APP_ID).anyTimes(); |
| 232 | replay(mockCoreService); |
| 233 | |
| 234 | // Register the services needed for the test |
| 235 | final CodecManager codecService = new CodecManager(); |
| 236 | codecService.activate(); |
| 237 | ServiceDirectory testDirectory = |
| 238 | new TestServiceDirectory() |
| 239 | .add(GroupService.class, mockGroupService) |
| 240 | .add(DeviceService.class, mockDeviceService) |
| 241 | .add(CodecService.class, codecService) |
| 242 | .add(CoreService.class, mockCoreService); |
| 243 | |
| 244 | BaseResource.setServiceDirectory(testDirectory); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Cleans up and verifies the mocks. |
| 249 | */ |
| 250 | @After |
| 251 | public void tearDownTest() { |
| 252 | verify(mockGroupService); |
| 253 | verify(mockCoreService); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Hamcrest matcher to check that a group representation in JSON matches |
| 258 | * the actual group. |
| 259 | */ |
| 260 | public static class GroupJsonMatcher extends TypeSafeMatcher<JsonObject> { |
| 261 | private final Group group; |
| 262 | private final String expectedAppId; |
| 263 | private String reason = ""; |
| 264 | |
| 265 | public GroupJsonMatcher(Group groupValue, String expectedAppIdValue) { |
| 266 | group = groupValue; |
| 267 | expectedAppId = expectedAppIdValue; |
| 268 | } |
| 269 | |
| 270 | @Override |
| 271 | public boolean matchesSafely(JsonObject jsonGroup) { |
| 272 | // check id |
| 273 | final String jsonId = jsonGroup.get("id").asString(); |
Prince Pereira | 3ff504c | 2016-08-30 14:23:43 +0530 | [diff] [blame] | 274 | final String groupId = group.id().id().toString(); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 275 | if (!jsonId.equals(groupId)) { |
Prince Pereira | 3ff504c | 2016-08-30 14:23:43 +0530 | [diff] [blame] | 276 | reason = "id " + group.id().id().toString(); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 277 | return false; |
| 278 | } |
| 279 | |
| 280 | // check application id |
| 281 | final String jsonAppId = jsonGroup.get("appId").asString(); |
varunsha | 34b3060 | 2016-08-18 10:31:18 -0700 | [diff] [blame] | 282 | final String appId = group.appId().name(); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 283 | if (!jsonAppId.equals(appId)) { |
varunsha | 34b3060 | 2016-08-18 10:31:18 -0700 | [diff] [blame] | 284 | reason = "appId " + group.appId().name(); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 285 | return false; |
| 286 | } |
| 287 | |
| 288 | // check device id |
| 289 | final String jsonDeviceId = jsonGroup.get("deviceId").asString(); |
| 290 | if (!jsonDeviceId.equals(group.deviceId().toString())) { |
| 291 | reason = "deviceId " + group.deviceId(); |
| 292 | return false; |
| 293 | } |
| 294 | |
| 295 | // check bucket array |
| 296 | if (group.buckets().buckets() != null) { |
| 297 | final JsonArray jsonBuckets = jsonGroup.get("buckets").asArray(); |
| 298 | if (group.buckets().buckets().size() != jsonBuckets.size()) { |
| 299 | reason = "buckets array size of " + |
| 300 | Integer.toString(group.buckets().buckets().size()); |
| 301 | return false; |
| 302 | } |
| 303 | for (final GroupBucket groupBucket : group.buckets().buckets()) { |
| 304 | boolean groupBucketFound = false; |
| 305 | for (int groupBucketIndex = 0; groupBucketIndex < jsonBuckets.size(); groupBucketIndex++) { |
| 306 | final String jsonType = jsonBuckets.get(groupBucketIndex).asObject().get("type").asString(); |
| 307 | final String bucketType = groupBucket.type().name(); |
| 308 | if (jsonType.equals(bucketType)) { |
| 309 | groupBucketFound = true; |
| 310 | } |
| 311 | } |
| 312 | if (!groupBucketFound) { |
| 313 | reason = "group bucket " + groupBucket.toString(); |
| 314 | return false; |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | return true; |
| 320 | } |
| 321 | |
| 322 | @Override |
| 323 | public void describeTo(Description description) { |
| 324 | description.appendText(reason); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Factory to allocate a group matcher. |
| 330 | * |
| 331 | * @param group group object we are looking for |
| 332 | * @return matcher |
| 333 | */ |
| 334 | private static GroupJsonMatcher matchesGroup(Group group, String expectedAppName) { |
| 335 | return new GroupJsonMatcher(group, expectedAppName); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Hamcrest matcher to check that a group is represented properly in a JSON |
| 340 | * array of flows. |
| 341 | */ |
| 342 | public static class GroupJsonArrayMatcher extends TypeSafeMatcher<JsonArray> { |
| 343 | private final Group group; |
| 344 | private String reason = ""; |
| 345 | |
| 346 | public GroupJsonArrayMatcher(Group groupValue) { |
| 347 | group = groupValue; |
| 348 | } |
| 349 | |
| 350 | @Override |
| 351 | public boolean matchesSafely(JsonArray json) { |
| 352 | boolean groupFound = false; |
| 353 | for (int jsonGroupIndex = 0; jsonGroupIndex < json.size(); |
| 354 | jsonGroupIndex++) { |
| 355 | |
| 356 | final JsonObject jsonGroup = json.get(jsonGroupIndex).asObject(); |
| 357 | |
Prince Pereira | 3ff504c | 2016-08-30 14:23:43 +0530 | [diff] [blame] | 358 | final String groupId = group.id().id().toString(); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 359 | final String jsonGroupId = jsonGroup.get("id").asString(); |
| 360 | if (jsonGroupId.equals(groupId)) { |
| 361 | groupFound = true; |
| 362 | |
| 363 | // We found the correct group, check attribute values |
| 364 | assertThat(jsonGroup, matchesGroup(group, APP_ID.name())); |
| 365 | } |
| 366 | } |
| 367 | if (!groupFound) { |
Prince Pereira | 3ff504c | 2016-08-30 14:23:43 +0530 | [diff] [blame] | 368 | reason = "Group with id " + group.id().id().toString() + " not found"; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 369 | return false; |
| 370 | } else { |
| 371 | return true; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | @Override |
| 376 | public void describeTo(Description description) { |
| 377 | description.appendText(reason); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * Factory to allocate a group array matcher. |
| 383 | * |
| 384 | * @param group group object we are looking for |
| 385 | * @return matcher |
| 386 | */ |
| 387 | private static GroupJsonArrayMatcher hasGroup(Group group) { |
| 388 | return new GroupJsonArrayMatcher(group); |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * Tests the result of the rest api GET when there are no groups. |
| 393 | */ |
| 394 | @Test |
| 395 | public void testGroupsEmptyArray() { |
| 396 | expect(mockGroupService.getGroups(deviceId1)).andReturn(null).anyTimes(); |
| 397 | expect(mockGroupService.getGroups(deviceId2)).andReturn(null).anyTimes(); |
| 398 | replay(mockGroupService); |
| 399 | replay(mockDeviceService); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 400 | final WebTarget wt = target(); |
| 401 | final String response = wt.path("groups").request().get(String.class); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 402 | assertThat(response, is("{\"groups\":[]}")); |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * Tests the result of the rest api GET when there are active groups. |
| 407 | */ |
| 408 | @Test |
| 409 | public void testGroupsPopulatedArray() { |
| 410 | setupMockGroups(); |
| 411 | replay(mockGroupService); |
| 412 | replay(mockDeviceService); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 413 | final WebTarget wt = target(); |
| 414 | final String response = wt.path("groups").request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 415 | final JsonObject result = Json.parse(response).asObject(); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 416 | assertThat(result, notNullValue()); |
| 417 | |
| 418 | assertThat(result.names(), hasSize(1)); |
| 419 | assertThat(result.names().get(0), is("groups")); |
| 420 | final JsonArray jsonGroups = result.get("groups").asArray(); |
| 421 | assertThat(jsonGroups, notNullValue()); |
| 422 | assertThat(jsonGroups, hasGroup(group1)); |
| 423 | assertThat(jsonGroups, hasGroup(group2)); |
| 424 | assertThat(jsonGroups, hasGroup(group3)); |
| 425 | assertThat(jsonGroups, hasGroup(group4)); |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * Tests the result of a rest api GET for a device. |
| 430 | */ |
| 431 | @Test |
| 432 | public void testGroupsSingleDevice() { |
| 433 | setupMockGroups(); |
| 434 | final Set<Group> groups = new HashSet<>(); |
| 435 | groups.add(group5); |
| 436 | groups.add(group6); |
| 437 | expect(mockGroupService.getGroups(anyObject())) |
| 438 | .andReturn(groups).anyTimes(); |
| 439 | replay(mockGroupService); |
| 440 | replay(mockDeviceService); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 441 | final WebTarget wt = target(); |
| 442 | final String response = wt.path("groups/" + deviceId3).request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 443 | final JsonObject result = Json.parse(response).asObject(); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 444 | assertThat(result, notNullValue()); |
| 445 | |
| 446 | assertThat(result.names(), hasSize(1)); |
| 447 | assertThat(result.names().get(0), is("groups")); |
Jian Li | 2e02fab | 2016-02-25 15:45:59 +0900 | [diff] [blame] | 448 | final JsonArray jsonGroups = result.get("groups").asArray(); |
| 449 | assertThat(jsonGroups, notNullValue()); |
| 450 | assertThat(jsonGroups, hasGroup(group5)); |
| 451 | assertThat(jsonGroups, hasGroup(group6)); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /** |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 455 | * Test the result of a rest api GET with specifying device id and appcookie. |
| 456 | */ |
| 457 | @Test |
| 458 | public void testGroupByDeviceIdAndAppCookie() { |
| 459 | setupMockGroups(); |
| 460 | expect(mockGroupService.getGroup(anyObject(), anyObject())) |
| 461 | .andReturn(group5).anyTimes(); |
| 462 | replay(mockGroupService); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 463 | final WebTarget wt = target(); |
Varun Sharma | 1853b3f | 2016-07-18 14:37:13 +0530 | [diff] [blame] | 464 | final String response = wt.path("groups/" + deviceId3 + "/" + "0x111") |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 465 | .request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 466 | final JsonObject result = Json.parse(response).asObject(); |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 467 | assertThat(result, notNullValue()); |
| 468 | |
| 469 | assertThat(result.names(), hasSize(1)); |
| 470 | assertThat(result.names().get(0), is("groups")); |
| 471 | final JsonArray jsonFlows = result.get("groups").asArray(); |
| 472 | assertThat(jsonFlows, notNullValue()); |
| 473 | assertThat(jsonFlows, hasGroup(group5)); |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * Test whether the REST API returns 404 if no entry has been found. |
| 478 | */ |
| 479 | @Test |
| 480 | public void testGroupByDeviceIdAndAppCookieNull() { |
| 481 | setupMockGroups(); |
| 482 | expect(mockGroupService.getGroup(anyObject(), anyObject())) |
| 483 | .andReturn(null).anyTimes(); |
| 484 | replay(mockGroupService); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 485 | final WebTarget wt = target(); |
Varun Sharma | 1853b3f | 2016-07-18 14:37:13 +0530 | [diff] [blame] | 486 | final Response response = wt.path("groups/" + deviceId3 + "/" + "0x222").request().get(); |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 487 | |
| 488 | assertEquals(404, response.getStatus()); |
| 489 | } |
| 490 | |
| 491 | /** |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 492 | * Tests creating a group with POST. |
| 493 | */ |
| 494 | @Test |
| 495 | public void testPost() { |
| 496 | mockGroupService.addGroup(anyObject()); |
| 497 | expectLastCall(); |
| 498 | replay(mockGroupService); |
| 499 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 500 | WebTarget wt = target(); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 501 | InputStream jsonStream = GroupsResourceTest.class |
| 502 | .getResourceAsStream("post-group.json"); |
| 503 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 504 | Response response = wt.path("groups/of:0000000000000001") |
| 505 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 506 | .post(Entity.json(jsonStream)); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 507 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED)); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 508 | String location = response.getLocation().getPath(); |
| 509 | assertThat(location, Matchers.startsWith("/groups/of:0000000000000001/")); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | /** |
| 513 | * Tests deleting a group. |
| 514 | */ |
| 515 | @Test |
| 516 | public void testDelete() { |
| 517 | setupMockGroups(); |
| 518 | mockGroupService.removeGroup(anyObject(), anyObject(), anyObject()); |
| 519 | expectLastCall(); |
| 520 | replay(mockGroupService); |
| 521 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 522 | WebTarget wt = target(); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 523 | |
Varun Sharma | 1853b3f | 2016-07-18 14:37:13 +0530 | [diff] [blame] | 524 | String location = "/groups/1/0x111"; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 525 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 526 | Response deleteResponse = wt.path(location) |
| 527 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 528 | .delete(); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 529 | assertThat(deleteResponse.getStatus(), |
| 530 | is(HttpURLConnection.HTTP_NO_CONTENT)); |
| 531 | } |
| 532 | } |