Skip to content

Commit 5d26830

Browse files
authored
Support for recent_item API (#458)
1 parent ba9f90c commit 5d26830

File tree

5 files changed

+266
-24
lines changed

5 files changed

+266
-24
lines changed

src/main/java/com/box/sdk/BoxFile.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,7 @@ public Info(String json) {
992992
*
993993
* @param jsonObject the parsed JSON object.
994994
*/
995-
Info(JsonObject jsonObject) {
995+
public Info(JsonObject jsonObject) {
996996
super(jsonObject);
997997
}
998998

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.box.sdk;
2+
3+
import java.net.MalformedURLException;
4+
import java.net.URL;
5+
import java.text.ParseException;
6+
import java.util.Date;
7+
8+
import com.eclipsesource.json.JsonObject;
9+
import com.eclipsesource.json.JsonValue;
10+
11+
/**
12+
* Represents an individual recent item.
13+
*
14+
* <p>Unless otherwise noted, the methods in this class can throw an unchecked {@link BoxAPIException} (unchecked
15+
* meaning that the compiler won't force you to handle it) if an error occurs. If you wish to implement custom error
16+
* handling for errors related to the Box REST API, you should capture this exception explicitly.*
17+
*/
18+
public class BoxRecentItem extends BoxJSONObject {
19+
private String type;
20+
private String interactionType;
21+
private BoxItem.Info item;
22+
private Date interactedAt;
23+
private URL interactionSharedLink;
24+
private BoxAPIConnection api;
25+
26+
/**
27+
* Construct a BoxRecentItem.
28+
* @param jsonObject the parsed JSON object.
29+
* @param api the API connection to be used to fetch interacted item
30+
*/
31+
public BoxRecentItem(JsonObject jsonObject, BoxAPIConnection api) {
32+
super(jsonObject);
33+
this.api = api;
34+
}
35+
36+
@Override
37+
protected void parseJSONMember(JsonObject.Member member) {
38+
super.parseJSONMember(member);
39+
40+
String memberName = member.getName();
41+
JsonValue value = member.getValue();
42+
try {
43+
if (memberName.equals("type")) {
44+
this.type = value.asString();
45+
} else if (memberName.equals("interaction_type")) {
46+
this.interactionType = value.asString();
47+
} else if (memberName.equals("item")) {
48+
String id = value.asObject().get("id").asString();
49+
this.item = new BoxFile(this.api, id).new Info(value.asObject());
50+
} else if (memberName.equals("interacted_at")) {
51+
this.interactedAt = BoxDateFormat.parse(value.asString());
52+
} else if (memberName.equals("interaction_shared_link")) {
53+
this.interactionSharedLink = new URL(value.asString());
54+
}
55+
} catch (ParseException e) {
56+
assert false : "A ParseException indicates a bug in the SDK.";
57+
} catch (MalformedURLException e) {
58+
assert false : "A ParseException indicates a bug in the SDK.";
59+
}
60+
}
61+
62+
/**
63+
* Get item type.
64+
* @return type of item
65+
*/
66+
public String getType() {
67+
return this.type;
68+
}
69+
70+
/**
71+
* Get interaction type.
72+
* @return interaction type
73+
*/
74+
public String getInteractionType() {
75+
return this.interactionType;
76+
}
77+
78+
/**
79+
* Get the item which was interacted with.
80+
* @return box item
81+
*/
82+
public BoxItem.Info getItem() {
83+
return this.item;
84+
}
85+
86+
/**
87+
* Get the interaction date.
88+
* @return interaction date
89+
*/
90+
public Date getInteractedAt() {
91+
return this.interactedAt;
92+
}
93+
94+
/**
95+
* Get the shared link, if the item was accessed through a shared link.
96+
* @return shared link
97+
*/
98+
public URL getInteractionSharedLink() {
99+
return this.interactionSharedLink;
100+
}
101+
102+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.box.sdk;
2+
3+
import com.eclipsesource.json.JsonObject;
4+
5+
/**
6+
* Utility class to retrieve list of recent items.
7+
* @see <a href="http://google.com">https://developer.box.com/reference#get-recent-items</a>
8+
*/
9+
public final class BoxRecents {
10+
11+
private static final URLTemplate RECENTS_URL_TEMPLATE = new URLTemplate("recent_items");
12+
13+
//Constructor is not allowed
14+
private BoxRecents() {
15+
}
16+
17+
/**
18+
* Used to retrieve all collaborations associated with the item.
19+
*
20+
* @see <a href="http://google.com">https://developer.box.com/reference#get-recent-items</a>
21+
*
22+
* @param api BoxAPIConnection from the associated file.
23+
* @param limit limit of items to be retrieved. Default is 100. Maximum is 1000
24+
* @param fields the optional fields to retrieve.
25+
* @return An iterable of BoxCollaboration.Info instances associated with the item.
26+
*/
27+
public static BoxResourceIterable<BoxRecentItem> getRecentItems(final BoxAPIConnection api,
28+
int limit, String... fields) {
29+
QueryStringBuilder builder = new QueryStringBuilder();
30+
if (fields.length > 0) {
31+
builder.appendParam("fields", fields);
32+
}
33+
return new BoxResourceIterable<BoxRecentItem>(
34+
api, RECENTS_URL_TEMPLATE.buildWithQuery(api.getBaseURL(), builder.toString()),
35+
limit) {
36+
37+
@Override
38+
protected BoxRecentItem factory(JsonObject jsonObject) {
39+
return new BoxRecentItem(jsonObject, api);
40+
}
41+
};
42+
}
43+
}

src/test/java/com/box/sdk/BoxFileTest.java

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -457,39 +457,55 @@ public void downloadFileRangeSucceeds() throws IOException {
457457
@Test
458458
@Category(IntegrationTest.class)
459459
public void uploadAndDownloadMultipleVersionsSucceeds() throws UnsupportedEncodingException {
460-
BoxAPIConnection api = new BoxAPIConnection(TestConfig.getAccessToken());
461-
BoxFolder rootFolder = BoxFolder.getRootFolder(api);
460+
BoxFile uploadedFile = null;
462461
String fileName = "[uploadAndDownloadMultipleVersionsSucceeds] Multi-version File.txt";
463462
String version1Content = "Version 1";
464463
String version1Sha = "db3cbc01da600701b9fe4a497fe328e71fa7022f";
465-
byte[] version1Bytes = version1Content.getBytes(StandardCharsets.UTF_8);
466-
long version1Size = version1Bytes.length;
467464
String version2Content = "Version 2";
465+
ProgressListener mockUploadListener = mock(ProgressListener.class);
466+
try {
467+
uploadedFile = BoxFileTest.createAndUpdateFileHelper(fileName, version1Content,
468+
version2Content, mockUploadListener);
469+
Collection<BoxFileVersion> versions = uploadedFile.getVersions();
470+
BoxFileVersion previousVersion = versions.iterator().next();
471+
472+
ByteArrayOutputStream downloadStream = new ByteArrayOutputStream();
473+
ProgressListener mockDownloadListener = mock(ProgressListener.class);
474+
previousVersion.download(downloadStream, mockDownloadListener);
475+
String downloadedContent = downloadStream.toString(StandardCharsets.UTF_8.name());
476+
477+
assertThat(versions, hasSize(1));
478+
assertThat(previousVersion.getSha1(), is(equalTo(version1Sha)));
479+
assertThat(downloadedContent, equalTo(version1Content));
480+
verify(mockDownloadListener, atLeastOnce()).onProgressChanged(anyLong(), anyLong());
481+
long version1Size = version1Content.getBytes(StandardCharsets.UTF_8).length;
482+
verify(mockUploadListener, atLeastOnce()).onProgressChanged(anyLong(),
483+
longThat(is(equalTo(version1Size))));
484+
485+
} finally {
486+
if (uploadedFile != null) {
487+
uploadedFile.delete();
488+
}
489+
}
490+
}
491+
492+
protected static BoxFile createAndUpdateFileHelper(String fileName, String version1Content,
493+
String version2Content, ProgressListener mockUploadListener) {
494+
BoxAPIConnection api = new BoxAPIConnection(TestConfig.getAccessToken());
495+
BoxFolder rootFolder = BoxFolder.getRootFolder(api);
496+
497+
byte[] version1Bytes = version1Content.getBytes(StandardCharsets.UTF_8);
498+
499+
468500
byte[] version2Bytes = version2Content.getBytes(StandardCharsets.UTF_8);
469501
long version2Size = version1Bytes.length;
470502

471503
InputStream uploadStream = new ByteArrayInputStream(version1Bytes);
472504
BoxFile uploadedFile = rootFolder.uploadFile(uploadStream, fileName).getResource();
473505

474506
uploadStream = new ByteArrayInputStream(version2Bytes);
475-
ProgressListener mockUploadListener = mock(ProgressListener.class);
476507
uploadedFile.uploadVersion(uploadStream, null, version2Size, mockUploadListener);
477-
478-
Collection<BoxFileVersion> versions = uploadedFile.getVersions();
479-
BoxFileVersion previousVersion = versions.iterator().next();
480-
481-
ByteArrayOutputStream downloadStream = new ByteArrayOutputStream();
482-
ProgressListener mockDownloadListener = mock(ProgressListener.class);
483-
previousVersion.download(downloadStream, mockDownloadListener);
484-
String downloadedContent = downloadStream.toString(StandardCharsets.UTF_8.name());
485-
486-
assertThat(versions, hasSize(1));
487-
assertThat(previousVersion.getSha1(), is(equalTo(version1Sha)));
488-
assertThat(downloadedContent, equalTo(version1Content));
489-
verify(mockDownloadListener, atLeastOnce()).onProgressChanged(anyLong(), anyLong());
490-
verify(mockUploadListener, atLeastOnce()).onProgressChanged(anyLong(), longThat(is(equalTo(version1Size))));
491-
492-
uploadedFile.delete();
508+
return uploadedFile;
493509
}
494510

495511
@Test
@@ -1221,7 +1237,7 @@ private void abortUploadSession(BoxFileUploadSession session) {
12211237
}
12221238
}
12231239

1224-
private static byte[] readAllBytes(String fileName) throws IOException {
1240+
protected static byte[] readAllBytes(String fileName) throws IOException {
12251241
RandomAccessFile f = new RandomAccessFile(fileName, "r");
12261242
byte[] b = new byte[(int) f.length()];
12271243
f.read(b);
@@ -1254,7 +1270,7 @@ private BoxFile.Info parallelMuliputUpload(File file, BoxFolder folder, String f
12541270
return folder.uploadLargeFile(newStream, BoxFileTest.generateString(), file.length());
12551271
}
12561272

1257-
private static String generateString() {
1273+
protected static String generateString() {
12581274
Random rng = new Random();
12591275
String characters = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
12601276
int length = 10;
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.box.sdk;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.util.Iterator;
6+
7+
import org.junit.Test;
8+
import org.junit.experimental.categories.Category;
9+
10+
/**
11+
* {@link BoxRecents} related tests.
12+
*/
13+
public class BoxRecentsTest {
14+
15+
@Test
16+
@Category(IntegrationTest.class)
17+
public void getRecentsWorkWithoutFields() {
18+
BoxFile uploadedFile = null;
19+
try {
20+
BoxAPIConnection api = new BoxAPIConnection(TestConfig.getAccessToken());
21+
22+
//Create a file to check if it comes up in recents
23+
String fileName = "[recentItemTest] Multi-version File.txt";
24+
String version1Content = "Version 1";
25+
String version1Sha = "db3cbc01da600701b9fe4a497fe328e71fa7022f";
26+
String version2Content = "Version 2";
27+
uploadedFile = BoxFileTest.createAndUpdateFileHelper(fileName, version1Content, version2Content, null);
28+
29+
BoxResourceIterable<BoxRecentItem> recentItems = BoxRecents.getRecentItems(api, 100);
30+
Iterator<BoxRecentItem> recentItemIterator = recentItems.iterator();
31+
if (recentItemIterator.hasNext()) {
32+
BoxRecentItem recentItem = recentItemIterator.next();
33+
assertNotNull("type should be not be null", recentItem.getType());
34+
assertNotNull("interactedAt date should not be null", recentItem.getInteractedAt());
35+
assertNotNull("interactionType should not be null", recentItem.getInteractionType());
36+
assertNotNull("item should not be null", recentItem.getItem());
37+
}
38+
assertNotNull("Should receive a response", recentItems);
39+
} catch (Exception e) {
40+
assertNull("There should have been no exception", e);
41+
} finally {
42+
if (uploadedFile != null) {
43+
uploadedFile.delete();
44+
}
45+
}
46+
}
47+
48+
@Test
49+
@Category(IntegrationTest.class)
50+
public void getRecentsWorkWithFields() {
51+
BoxFile uploadedFile = null;
52+
try {
53+
BoxAPIConnection api = new BoxAPIConnection(TestConfig.getAccessToken());
54+
55+
//Create a file to check if it comes up in recents
56+
String fileName = "[recentItemTest] Multi-version File.txt";
57+
String version1Content = "Version 1";
58+
String version1Sha = "db3cbc01da600701b9fe4a497fe328e71fa7022f";
59+
String version2Content = "Version 2";
60+
uploadedFile = BoxFileTest.createAndUpdateFileHelper(fileName, version1Content, version2Content, null);
61+
62+
BoxResourceIterable<BoxRecentItem> recentItems = BoxRecents.getRecentItems(api, 100, "created_at");
63+
Iterator<BoxRecentItem> recentItemIterator = recentItems.iterator();
64+
if (recentItemIterator.hasNext()) {
65+
BoxRecentItem recentItem = recentItemIterator.next();
66+
assertNotNull("type should be not be null", recentItem.getType());
67+
assertNotNull("interactedAt date should not be null", recentItem.getInteractedAt());
68+
assertNotNull("interactionType should not be null", recentItem.getInteractionType());
69+
assertNotNull("item should not be null", recentItem.getItem());
70+
assertNotNull("item's created_at should not be null", recentItem.getItem().getCreatedAt());
71+
}
72+
assertNotNull("Should receive a response", recentItems);
73+
} catch (Exception e) {
74+
assertNull("There should have been no exception", e);
75+
} finally {
76+
if (uploadedFile != null) {
77+
uploadedFile.delete();
78+
}
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)