Skip to content

Commit

Permalink
Add iterator support for group collaborations (#813)
Browse files Browse the repository at this point in the history
  • Loading branch information
sujaygarlanka authored Jun 5, 2020
1 parent d2408b1 commit fb0d552
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Fix issue for `getIsExternallyOwned()` for Files and Folders ([#808](https://github.com/box/box-java-sdk/pull/808))
- Add support for the classification field for Files and Folders ([#809](https://github.com/box/box-java-sdk/pull/809))
- Add ability to set the filename when uploading a new version of a file ([#810](https://github.com/box/box-java-sdk/pull/810))
- Add iterator support for group collaborations ([#813](https://github.com/box/box-java-sdk/pull/813))

## 2.47.0 [2020-04-23]
- Add support for the uploader display name field for Files and File Versions ([#791](https://github.com/box/box-java-sdk/pull/791))
Expand Down
22 changes: 18 additions & 4 deletions doc/groups.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ Groups are sets of users that can be used in collaborations.
- [Get Information About a Group](#get-information-about-a-group)
- [Update a Group](#update-a-group)
- [Delete a Group](#delete-a-group)
- [Get a Groups collaborations](#get-a-groups-collaborations)
- [Get first page of a Group's Collaborations](#get-a-groups-collaborations)
- [Get all of a Group's Collaborations](#get-all-a-groups-collaborations)
- [Create Membership](#create-membership)
- [Get Membership](#get-membership)
- [Update Membership](#update-membership)
Expand Down Expand Up @@ -98,19 +99,32 @@ group.delete();

[delete]: http://opensource.box.com/box-java-sdk/javadoc/com/box/sdk/BoxGroup.html#delete--

Get a Groups collaborations
Get first page of a Group's Collaborations
---------------------------

A groups collaborations can be retrieved by calling the [`getCollaborations()`][get-collaborations] method.
The first page of a group's collaborations can be retrieved by calling the [`getCollaborations()`][get-collaborations] method.

<!-- sample get_groups_id_collaborations -->
```java
BoxGroup group = new BoxGroup(api, "id");
group.getCollaborations();
Collection<BoxCollaboration.Info> collaborations = group.getCollaborations();
```

[get-collaborations]: http://opensource.box.com/box-java-sdk/javadoc/com/box/sdk/BoxGroup.html#getCollaborations--

Get all of a Group's Collaborations
---------------------------

All of a group's collaborations can be retrieved by calling the [`getAllCollaborations(String... fields)`][get-all-collaborations] method.
An iterable is returned to allow a user to iterate until the last collaboration.

```java
BoxGroup group = new BoxGroup(api, "id");
Iterable<BoxCollaboration.Info> collaborations = group.getAllCollaborations();
```

[get-all-collaborations]: http://opensource.box.com/box-java-sdk/javadoc/com/box/sdk/BoxGroup.html#getAllCollaborations--

Create Membership
---------------

Expand Down
35 changes: 35 additions & 0 deletions src/main/java/com/box/sdk/BoxCollaborationIterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.box.sdk;

import java.net.URL;
import java.util.Iterator;

import com.eclipsesource.json.JsonObject;

class BoxCollaborationIterator implements Iterator<BoxCollaboration.Info> {
private static final long LIMIT = 100;
private final BoxAPIConnection api;
private final JSONIterator jsonIterator;

BoxCollaborationIterator(BoxAPIConnection api, URL url) {
this.api = api;
this.jsonIterator = new JSONIterator(api, url, LIMIT);
}

@Override
public boolean hasNext() {
return this.jsonIterator.hasNext();
}

@Override
public BoxCollaboration.Info next() {
JsonObject nextJSONObject = this.jsonIterator.next();
String id = nextJSONObject.get("id").asString();

BoxCollaboration collaboration = new BoxCollaboration(this.api, id);
return collaboration.new Info(nextJSONObject);
}

public void remove() {
throw new UnsupportedOperationException();
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/box/sdk/BoxGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,26 @@ public Collection<BoxCollaboration.Info> getCollaborations() {
return collaborations;
}

/**
* Gets information about all of the collaborations for this group.
* @param fields the optional fields to retrieve.
* @return An iterable of BoxCollaboration.Info instances associated with the item.
*/
public Iterable<BoxCollaboration.Info> getAllCollaborations(String... fields) {
final BoxAPIConnection api = this.getAPI();
final QueryStringBuilder builder = new QueryStringBuilder();
if (fields.length > 0) {
builder.appendParam("fields", fields);
}
return new Iterable<BoxCollaboration.Info>() {
public Iterator<BoxCollaboration.Info> iterator() {
URL url = COLLABORATIONS_URL_TEMPLATE.buildWithQuery(api.getBaseURL(), builder.toString(),
BoxGroup.this.getID());
return new BoxCollaborationIterator(api, url);
}
};
}

/**
* Deletes this group.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"total_count": 1,
"total_count": 200,
"entries": [
{
"type": "collaboration",
Expand Down
31 changes: 31 additions & 0 deletions src/test/Fixtures/BoxGroup/GetAGroupsCollaborations2ndPage200.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"total_count": 200,
"entries": [
{
"type": "collaboration",
"id": "23647",
"created_by": null,
"created_at": "2018-04-20T14:57:25-07:00",
"modified_at": "2018-04-20T14:57:25-07:00",
"expires_at": null,
"status": "accepted",
"accessible_by": {
"type": "group",
"id": "1111",
"name": "New Group Name",
"group_type": "managed_group"
},
"role": "editor",
"acknowledged_at": "2018-04-20T14:57:25-07:00",
"item": {
"type": "file",
"id": "12342",
"sequence_id": "2",
"etag": "2",
"name": "Ball Valve Diagram"
}
}
],
"offset": 1,
"limit": 100
}
46 changes: 44 additions & 2 deletions src/test/java/com/box/sdk/BoxGroupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public void testCreateGroupMembershipSucceedsAndSendsCorrectJson() throws IOExce

@Test
@Category(UnitTest.class)
public void testGetAllGroupsCollaborationsSucceeds() throws IOException {
public void testGetGroupsCollaborationsSucceeds() throws IOException {
String result = "";
final String groupID = "12345";
final String groupCollaborationURL = "/groups/" + groupID + "/collaborations";
Expand All @@ -319,7 +319,7 @@ public void testGetAllGroupsCollaborationsSucceeds() throws IOException {
final String itemID = "2222";
final String itemName = "Ball Valve Diagram";

result = TestConfig.getFixture("BoxGroup/GetAGroupsCollaborations200");
result = TestConfig.getFixture("BoxGroup/GetAGroupsCollaborations1stPage200");

WIRE_MOCK_CLASS_RULE.stubFor(WireMock.get(WireMock.urlPathEqualTo(groupCollaborationURL))
.willReturn(WireMock.aResponse()
Expand All @@ -337,6 +337,48 @@ public void testGetAllGroupsCollaborationsSucceeds() throws IOException {
Assert.assertEquals(itemName, info.getItem().getName());
}

@Test
@Category(UnitTest.class)
public void testGetAllGroupsCollaborationsSucceeds() throws IOException {
final String groupID = "12345";
final String groupCollaborationURL = "/groups/" + groupID + "/collaborations";
String result1 = TestConfig.getFixture("BoxGroup/GetAGroupsCollaborations1stPage200");
String result2 = TestConfig.getFixture("BoxGroup/GetAGroupsCollaborations2ndPage200");

// First request will return a page of results with one item
WIRE_MOCK_CLASS_RULE.stubFor(WireMock.get(WireMock.urlPathEqualTo(groupCollaborationURL))
.withQueryParam("offset", WireMock.containing("0"))
.withQueryParam("limit", WireMock.containing("100"))
.willReturn(WireMock.aResponse()
.withHeader("Content-Type", "application/json")
.withBody(result1)));

// Second request will return a page of results with remaining one item
WIRE_MOCK_CLASS_RULE.stubFor(WireMock.get(WireMock.urlPathEqualTo(groupCollaborationURL))
.withQueryParam("offset", WireMock.containing("1"))
.withQueryParam("limit", WireMock.containing("100"))
.willReturn(WireMock.aResponse()
.withHeader("Content-Type", "application/json")
.withBody(result2)));

BoxGroup group = new BoxGroup(this.api, groupID);
Iterator<BoxCollaboration.Info> collaborations = group.getAllCollaborations().iterator();

// First item on the first page of results
BoxCollaboration.Info currCollaboration = collaborations.next();
Assert.assertEquals("12345", currCollaboration.getID());
Assert.assertEquals("New Group Name", currCollaboration.getAccessibleBy().getName());
Assert.assertEquals("2222", currCollaboration.getItem().getID());
Assert.assertEquals("folder", currCollaboration.getItem().getType());

// First item on the second page of results (this next call makes the second request to get the second page)
currCollaboration = collaborations.next();
Assert.assertEquals("23647", currCollaboration.getID());
Assert.assertEquals("New Group Name", currCollaboration.getAccessibleBy().getName());
Assert.assertEquals("12342", currCollaboration.getItem().getID());
Assert.assertEquals("file", currCollaboration.getItem().getType());
}

@Test
@Category(UnitTest.class)
public void testDeleteAGroupSucceedsAndSendsCorrectJson() throws IOException {
Expand Down

0 comments on commit fb0d552

Please sign in to comment.