Skip to content

Commit 6ea70f7

Browse files
authored
feat: Expose getVersionByID method on BoxFile (#1268)
1 parent 1b6384d commit 6ea70f7

File tree

4 files changed

+107
-22
lines changed

4 files changed

+107
-22
lines changed

src/intTest/java/com/box/sdk/BoxFileIT.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,27 @@ public void uploadNewVersionSucceeds() {
432432
}
433433
}
434434

435+
@Test
436+
public void getVersionSucceeds() {
437+
BoxAPIConnection api = jwtApiForServiceAccount();
438+
String fileName = "[getVersionByIdSucceeds] Multi-version File.txt";
439+
BoxFile uploadedFile = null;
440+
try {
441+
uploadedFile = uploadFileToUniqueFolder(api, fileName, "Test file");
442+
uploadedFile.uploadNewVersion(this.getFileContent("Version 2"));
443+
444+
Collection<BoxFileVersion> versions = uploadedFile.getVersions();
445+
BoxFileVersion versionFromGetVersions = versions.iterator().next();
446+
447+
BoxFileVersion versionFromGetById = uploadedFile.getVersionByID(versionFromGetVersions.getID());
448+
449+
assertThat(versionFromGetVersions.getVersionID(), equalTo(versionFromGetById.getVersionID()));
450+
assertThat(versionFromGetVersions.getFileID(), equalTo(versionFromGetById.getFileID()));
451+
} finally {
452+
deleteFile(uploadedFile);
453+
}
454+
}
455+
435456
@Test
436457
public void deleteVersionSucceeds() {
437458
BoxAPIConnection api = jwtApiForServiceAccount();

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

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ public void downloadRange(OutputStream output, long rangeStart, long rangeEnd, P
354354

355355
/**
356356
* Can be used to override the URL used for file download.
357+
*
357358
* @return URL for file downalod
358359
*/
359360
protected URL getDownloadUrl() {
@@ -631,6 +632,32 @@ public void updateInfo(BoxFile.Info info) {
631632
}
632633
}
633634

635+
/**
636+
* Retrieve a specific file version.
637+
*
638+
* @param fileVersionID the ID of the file version to retrieve.
639+
* @param fields the optional fields to retrieve.
640+
* @return a specific file version.
641+
*/
642+
public BoxFileVersion getVersionByID(String fileVersionID, String... fields) {
643+
URL url = BoxFileVersion.VERSION_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID(), fileVersionID);
644+
if (fields.length > 0) {
645+
String queryString = new QueryStringBuilder().appendParam("fields", fields).toString();
646+
url = BoxFileVersion.VERSION_URL_TEMPLATE.buildWithQuery(
647+
this.getAPI().getBaseURL(),
648+
queryString,
649+
this.getID(),
650+
fileVersionID
651+
);
652+
}
653+
654+
BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "GET");
655+
try (BoxJSONResponse response = request.send()) {
656+
JsonObject jsonObject = Json.parse(response.getJSON()).asObject();
657+
return new BoxFileVersion(this.getAPI(), jsonObject, this.getID());
658+
}
659+
}
660+
634661
/**
635662
* Gets up to 1000 versions of this file. Note that only users with premium accounts will be able to retrieve
636663
* previous versions of their files. `fields` parameter is optional, if specified only requested fields will
@@ -661,8 +688,8 @@ public Collection<BoxFileVersion> getVersions(String... fields) {
661688
*/
662689
public PartialCollection<BoxFileVersion> getVersionsRange(long offset, long limit, String... fields) {
663690
QueryStringBuilder builder = new QueryStringBuilder()
664-
.appendParam("limit", limit)
665-
.appendParam("offset", offset);
691+
.appendParam("limit", limit)
692+
.appendParam("offset", offset);
666693

667694
if (fields.length > 0) {
668695
builder.appendParam("fields", fields);
@@ -1439,7 +1466,7 @@ private BoxCollaboration.Info collaborate(JsonObject accessibleByField, BoxColla
14391466
itemField.add("type", "file");
14401467

14411468
return BoxCollaboration.create(this.getAPI(), accessibleByField, itemField, role, notify, canViewPath,
1442-
expiresAt, isAccessOnly);
1469+
expiresAt, isAccessOnly);
14431470
}
14441471

14451472
/**
@@ -1487,10 +1514,10 @@ public BoxCollaboration.Info collaborate(BoxCollaborator collaborator, BoxCollab
14871514
* Adds a collaborator to this folder. An email will be sent to the collaborator if they don't already have a Box
14881515
* account.
14891516
*
1490-
* @param email the email address of the collaborator to add.
1491-
* @param role the role of the collaborator.
1492-
* @param notify determines if the user (or all the users in the group) will receive email notifications.
1493-
* @param canViewPath whether view path collaboration feature is enabled or not.
1517+
* @param email the email address of the collaborator to add.
1518+
* @param role the role of the collaborator.
1519+
* @param notify determines if the user (or all the users in the group) will receive email notifications.
1520+
* @param canViewPath whether view path collaboration feature is enabled or not.
14941521
* @param expiresAt when the collaboration should expire.
14951522
* @param isAccessOnly whether the collaboration is access only or not.
14961523
* @return info about the new collaboration.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"type": "file_version",
3+
"id": "12345",
4+
"sha1": "a783fb33dcc129625d36c1b401fd315222590b5d",
5+
"name": "Example.jpg",
6+
"size": 37921,
7+
"created_at": "2018-05-02T13:50:57-07:00",
8+
"modified_at": "2018-05-02T13:50:59-07:00",
9+
"modified_by": {
10+
"type": "user",
11+
"id": "1111",
12+
"name": "Test User",
13+
"login": "[email protected]"
14+
},
15+
"trashed_at": null,
16+
"purged_at": null
17+
}

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

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ public void testGetThumbnailSucceeds() {
420420
);
421421
wireMockRule.stubFor(
422422
WireMock.get(WireMock.urlPathEqualTo(
423-
"/2.0/internal_files/12345/versions/1116420931563/representations/jpg_thumb_32x32")
423+
"/2.0/internal_files/12345/versions/1116420931563/representations/jpg_thumb_32x32")
424424
)
425425
.willReturn(WireMock.aResponse()
426426
.withHeader("Content-Type", APPLICATION_JSON)
@@ -429,7 +429,7 @@ public void testGetThumbnailSucceeds() {
429429
);
430430
wireMockRule.stubFor(
431431
WireMock.get(WireMock.urlPathEqualTo(
432-
"/2.0/internal_files/1030335435441/versions/1116437417841/representations/jpg_thumb_32x32/content/"
432+
"/2.0/internal_files/1030335435441/versions/1116437417841/representations/jpg_thumb_32x32/content/"
433433
))
434434
.willReturn(WireMock.aResponse()
435435
.withHeader("Content-Type", "image/jpg")
@@ -527,6 +527,26 @@ public void testGetRepresentationContentSuccess() {
527527
assertThat(output.toString(), equalTo("This is a JPG"));
528528
}
529529

530+
@Test
531+
public void testGetFileVersionByIdSucceeds() {
532+
final String versionID = "12345";
533+
final String fileID = "1111";
534+
final String versionURL = "/2.0/files/" + fileID + "/versions/" + versionID;
535+
536+
String versionResult = getFixture("BoxFile/GetFileVersion200");
537+
538+
wireMockRule.stubFor(WireMock.get(WireMock.urlPathEqualTo(versionURL))
539+
.willReturn(WireMock.aResponse()
540+
.withHeader("Content-Type", APPLICATION_JSON)
541+
.withBody(versionResult)));
542+
543+
BoxFile file = new BoxFile(this.api, fileID);
544+
BoxFileVersion version = file.getVersionByID(versionID);
545+
546+
assertEquals(versionID, version.getVersionID());
547+
assertEquals(fileID, version.getFileID());
548+
}
549+
530550
@Test
531551
public void testDeletePreviousFileVersionSucceeds() {
532552
final String versionID = "12345";
@@ -1213,20 +1233,20 @@ public void getVersionsWithLimitAndOffset() {
12131233

12141234
// then
12151235
api.setRequestInterceptor(
1216-
request -> {
1217-
try {
1218-
String query = URLDecoder.decode(request.getUrl().getQuery(), "UTF-8");
1219-
assertThat(query, containsString("limit=10&offset=0&fields=name,version_number"));
1220-
return new BoxJSONResponse() {
1221-
@Override
1222-
public String getJSON() {
1223-
return "{\"entries\": [], \"total_count\": 100}";
1224-
}
1225-
};
1226-
} catch (UnsupportedEncodingException e) {
1227-
throw new RuntimeException(e);
1228-
}
1236+
request -> {
1237+
try {
1238+
String query = URLDecoder.decode(request.getUrl().getQuery(), "UTF-8");
1239+
assertThat(query, containsString("limit=10&offset=0&fields=name,version_number"));
1240+
return new BoxJSONResponse() {
1241+
@Override
1242+
public String getJSON() {
1243+
return "{\"entries\": [], \"total_count\": 100}";
1244+
}
1245+
};
1246+
} catch (UnsupportedEncodingException e) {
1247+
throw new RuntimeException(e);
12291248
}
1249+
}
12301250
);
12311251

12321252
// when

0 commit comments

Comments
 (0)