blob: 50cc9b1213f7c1add810c4e6a63e566823ceab82 [file] [log] [blame]
Jian Liecb3c0f2015-12-15 10:07:49 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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;
22import com.google.common.collect.ImmutableSet;
Jian Liecb3c0f2015-12-15 10:07:49 -080023import org.hamcrest.Description;
Jian Li9d616492016-03-09 10:52:49 -080024import org.hamcrest.Matchers;
Jian Liecb3c0f2015-12-15 10:07:49 -080025import org.hamcrest.TypeSafeMatcher;
26import org.junit.After;
27import org.junit.Before;
28import org.junit.Test;
29import org.onlab.osgi.ServiceDirectory;
30import org.onlab.osgi.TestServiceDirectory;
31import org.onlab.rest.BaseResource;
32import 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;
51
Jian Li9d616492016-03-09 10:52:49 -080052import javax.ws.rs.client.Entity;
53import javax.ws.rs.client.WebTarget;
Jian Liecb3c0f2015-12-15 10:07:49 -080054import javax.ws.rs.core.MediaType;
Jian Li9d616492016-03-09 10:52:49 -080055import javax.ws.rs.core.Response;
Jian Liecb3c0f2015-12-15 10:07:49 -080056import java.io.InputStream;
57import java.net.HttpURLConnection;
58import java.util.ArrayList;
59import java.util.HashMap;
60import java.util.HashSet;
61import java.util.List;
62import java.util.Set;
63
64import static org.easymock.EasyMock.anyObject;
65import static org.easymock.EasyMock.anyShort;
66import static org.easymock.EasyMock.createMock;
67import static org.easymock.EasyMock.expect;
68import static org.easymock.EasyMock.expectLastCall;
69import static org.easymock.EasyMock.replay;
70import static org.easymock.EasyMock.verify;
71import static org.hamcrest.Matchers.hasSize;
72import static org.hamcrest.Matchers.is;
73import static org.hamcrest.Matchers.notNullValue;
Jian Lia7f86ce2015-12-20 13:42:10 -080074import static org.junit.Assert.assertEquals;
Jian Li9d616492016-03-09 10:52:49 -080075import static org.junit.Assert.assertThat;
Jian Liecb3c0f2015-12-15 10:07:49 -080076import static org.onosproject.net.NetTestTools.APP_ID;
77
78/**
79 * Unit tests for Groups REST APIs.
80 */
81public 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 Sharma1853b3f2016-07-18 14:37:13 +053097 final MockGroup group1 = new MockGroup(deviceId1, 1, "0x111", 1);
98 final MockGroup group2 = new MockGroup(deviceId1, 2, "0x222", 2);
Jian Liecb3c0f2015-12-15 10:07:49 -080099
Varun Sharma1853b3f2016-07-18 14:37:13 +0530100 final MockGroup group3 = new MockGroup(deviceId2, 3, "0x333", 3);
101 final MockGroup group4 = new MockGroup(deviceId2, 4, "0x444", 4);
Jian Liecb3c0f2015-12-15 10:07:49 -0800102
Varun Sharma1853b3f2016-07-18 14:37:13 +0530103 final MockGroup group5 = new MockGroup(deviceId3, 5, "0x555", 5);
104 final MockGroup group6 = new MockGroup(deviceId3, 6, "0x666", 6);
Jian Liecb3c0f2015-12-15 10:07:49 -0800105
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());
rohitc2e77362017-03-27 20:07:25 +0530122 this.baseValue = id * 100L;
Jian Liecb3c0f2015-12-15 10:07:49 -0800123 this.bucketList = new ArrayList<>();
124 this.buckets = new GroupBuckets(bucketList);
125 }
126
127 @Override
128 public GroupId id() {
Yi Tsengfa394de2017-02-01 11:26:40 -0800129 return new GroupId((int) baseValue + 55);
Jian Liecb3c0f2015-12-15 10:07:49 -0800130 }
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
alshabibb0285992016-03-28 23:30:37 -0700158 public int age() {
159 return 0;
160 }
161
162 @Override
Jian Liecb3c0f2015-12-15 10:07:49 -0800163 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 Pereira3ff504c2016-08-30 14:23:43 +0530274 final String groupId = group.id().id().toString();
Jian Liecb3c0f2015-12-15 10:07:49 -0800275 if (!jsonId.equals(groupId)) {
Prince Pereira3ff504c2016-08-30 14:23:43 +0530276 reason = "id " + group.id().id().toString();
Jian Liecb3c0f2015-12-15 10:07:49 -0800277 return false;
278 }
279
280 // check application id
281 final String jsonAppId = jsonGroup.get("appId").asString();
varunsha34b30602016-08-18 10:31:18 -0700282 final String appId = group.appId().name();
Jian Liecb3c0f2015-12-15 10:07:49 -0800283 if (!jsonAppId.equals(appId)) {
varunsha34b30602016-08-18 10:31:18 -0700284 reason = "appId " + group.appId().name();
Jian Liecb3c0f2015-12-15 10:07:49 -0800285 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 Pereira3ff504c2016-08-30 14:23:43 +0530358 final String groupId = group.id().id().toString();
Jian Liecb3c0f2015-12-15 10:07:49 -0800359 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 Pereira3ff504c2016-08-30 14:23:43 +0530368 reason = "Group with id " + group.id().id().toString() + " not found";
Jian Liecb3c0f2015-12-15 10:07:49 -0800369 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 Li9d616492016-03-09 10:52:49 -0800400 final WebTarget wt = target();
401 final String response = wt.path("groups").request().get(String.class);
Jian Liecb3c0f2015-12-15 10:07:49 -0800402 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 Li9d616492016-03-09 10:52:49 -0800413 final WebTarget wt = target();
414 final String response = wt.path("groups").request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800415 final JsonObject result = Json.parse(response).asObject();
Jian Liecb3c0f2015-12-15 10:07:49 -0800416 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 Li9d616492016-03-09 10:52:49 -0800441 final WebTarget wt = target();
442 final String response = wt.path("groups/" + deviceId3).request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800443 final JsonObject result = Json.parse(response).asObject();
Jian Liecb3c0f2015-12-15 10:07:49 -0800444 assertThat(result, notNullValue());
445
446 assertThat(result.names(), hasSize(1));
447 assertThat(result.names().get(0), is("groups"));
Jian Li2e02fab2016-02-25 15:45:59 +0900448 final JsonArray jsonGroups = result.get("groups").asArray();
449 assertThat(jsonGroups, notNullValue());
450 assertThat(jsonGroups, hasGroup(group5));
451 assertThat(jsonGroups, hasGroup(group6));
Jian Liecb3c0f2015-12-15 10:07:49 -0800452 }
453
454 /**
Jian Lia7f86ce2015-12-20 13:42:10 -0800455 * 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 Li9d616492016-03-09 10:52:49 -0800463 final WebTarget wt = target();
Varun Sharma1853b3f2016-07-18 14:37:13 +0530464 final String response = wt.path("groups/" + deviceId3 + "/" + "0x111")
Jian Li9d616492016-03-09 10:52:49 -0800465 .request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800466 final JsonObject result = Json.parse(response).asObject();
Jian Lia7f86ce2015-12-20 13:42:10 -0800467 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 Li9d616492016-03-09 10:52:49 -0800485 final WebTarget wt = target();
Varun Sharma1853b3f2016-07-18 14:37:13 +0530486 final Response response = wt.path("groups/" + deviceId3 + "/" + "0x222").request().get();
Jian Lia7f86ce2015-12-20 13:42:10 -0800487
488 assertEquals(404, response.getStatus());
489 }
490
491 /**
Jian Liecb3c0f2015-12-15 10:07:49 -0800492 * Tests creating a group with POST.
493 */
494 @Test
495 public void testPost() {
496 mockGroupService.addGroup(anyObject());
497 expectLastCall();
498 replay(mockGroupService);
499
Jian Li9d616492016-03-09 10:52:49 -0800500 WebTarget wt = target();
Jian Liecb3c0f2015-12-15 10:07:49 -0800501 InputStream jsonStream = GroupsResourceTest.class
502 .getResourceAsStream("post-group.json");
503
Jian Li9d616492016-03-09 10:52:49 -0800504 Response response = wt.path("groups/of:0000000000000001")
505 .request(MediaType.APPLICATION_JSON_TYPE)
506 .post(Entity.json(jsonStream));
Jian Liecb3c0f2015-12-15 10:07:49 -0800507 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED));
Jian Li9d616492016-03-09 10:52:49 -0800508 String location = response.getLocation().getPath();
509 assertThat(location, Matchers.startsWith("/groups/of:0000000000000001/"));
Jian Liecb3c0f2015-12-15 10:07:49 -0800510 }
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 Li9d616492016-03-09 10:52:49 -0800522 WebTarget wt = target();
Jian Liecb3c0f2015-12-15 10:07:49 -0800523
Varun Sharma1853b3f2016-07-18 14:37:13 +0530524 String location = "/groups/1/0x111";
Jian Liecb3c0f2015-12-15 10:07:49 -0800525
Jian Li9d616492016-03-09 10:52:49 -0800526 Response deleteResponse = wt.path(location)
527 .request(MediaType.APPLICATION_JSON_TYPE)
528 .delete();
Jian Liecb3c0f2015-12-15 10:07:49 -0800529 assertThat(deleteResponse.getStatus(),
530 is(HttpURLConnection.HTTP_NO_CONTENT));
531 }
532}