From a41e3aed6d73182702a60d93a11d7a7350e630a0 Mon Sep 17 00:00:00 2001 From: carycheng Date: Wed, 8 Aug 2018 15:21:39 -0700 Subject: [PATCH 1/3] patch for transfer content to another user --- doc/users.md | 9 +-- src/main/java/com/box/sdk/BoxUser.java | 30 ++++++++ .../BoxFolder/PutTransferFolder200.json | 71 +++++++++++++++++++ src/test/java/com/box/sdk/BoxUserTest.java | 30 ++++++++ 4 files changed, 136 insertions(+), 4 deletions(-) create mode 100644 src/test/Fixtures/BoxFolder/PutTransferFolder200.json diff --git a/doc/users.md b/doc/users.md index 42c29de9c..a794e1917 100644 --- a/doc/users.md +++ b/doc/users.md @@ -210,11 +210,12 @@ Move User's Folder ------------------ To move all of a user's content to another user, call the -[`moveFolderToUser(String destinationUserID)`][move-folder-to-user] method. +[`transferContent(String destinationUserID)`][transfer-folder-to-new-user] method. ```java -BoxUser user = new BoxUser(api, "0"); -BoxFolder.Info folderInfo = user.moveFolderToUser("1"); +String destinationUserID = "12345"; +BoxUser sourceUser = new BoxUser(api, "0"); +BoxFolder.Info transferredFolderInfo = sourceUser.transferContent(destinationUserID); ``` -[move-folder-to-user]: http://opensource.box.com/box-java-sdk/javadoc/com/box/sdk/BoxUser.html#moveFolderToUser-java.lang.String- +[transfer-folder-to-new-user]: http://opensource.box.com/box-java-sdk/javadoc/com/box/sdk/BoxUser.html#transferFolderToNewUser-java.lang.String- diff --git a/src/main/java/com/box/sdk/BoxUser.java b/src/main/java/com/box/sdk/BoxUser.java index 7c283b5bd..b5b2ec5a9 100644 --- a/src/main/java/com/box/sdk/BoxUser.java +++ b/src/main/java/com/box/sdk/BoxUser.java @@ -438,6 +438,9 @@ public void updateInfo(BoxUser.Info info) { } /** + * @deprecated As of release 2.22.0, replaced by {@link #transferContent(String)} ()} + * + * * Moves all of the owned content from within one user’s folder into a new folder in another user's account. * You can move folders across users as long as the you have administrative permissions and the 'source' * user owns the folders. Per the documentation at the link below, this will move everything from the root @@ -448,6 +451,7 @@ public void updateInfo(BoxUser.Info info) { * @param sourceUserID the user id of the user whose files will be the source for this operation * @return info for the newly created folder */ + @Deprecated public BoxFolder.Info moveFolderToUser(String sourceUserID) { // Currently the API only supports moving of the root folder (0), hence the hard coded "0" URL url = MOVE_FOLDER_TO_USER_TEMPLATE.build(this.getAPI().getBaseURL(), sourceUserID, "0"); @@ -464,6 +468,32 @@ public BoxFolder.Info moveFolderToUser(String sourceUserID) { return movedFolder.new Info(responseJSON); } + /** + * Moves all of the owned content from within one user’s folder into a new folder in another user's account. + * You can move folders across users as long as the you have administrative permissions and the 'source' + * user owns the folders. Per the documentation at the link below, this will move everything from the root + * folder, as this is currently the only mode of operation supported. + * + * See also https://box-content.readme.io/reference#move-folder-into-another-users-folder + * + * @param destinationUserID the user id of the user that you wish to transfer content to. + * @return info for the newly created folder. + */ + public BoxFolder.Info transferContent(String destinationUserID) { + URL url = MOVE_FOLDER_TO_USER_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID(), "0"); + BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "PUT"); + JsonObject destinationUser = new JsonObject(); + destinationUser.add("id", destinationUserID); + JsonObject ownedBy = new JsonObject(); + ownedBy.add("owned_by", destinationUser); + request.setBody(ownedBy.toString()); + BoxJSONResponse response = (BoxJSONResponse) request.send(); + JsonObject responseJSON = JsonObject.readFrom(response.getJSON()); + BoxFolder movedFolder = new BoxFolder(this.getAPI(), responseJSON.get("id").asString()); + + return movedFolder.new Info(responseJSON); + } + /** * Enumerates the possible roles that a user can have within an enterprise. diff --git a/src/test/Fixtures/BoxFolder/PutTransferFolder200.json b/src/test/Fixtures/BoxFolder/PutTransferFolder200.json new file mode 100644 index 000000000..380ef59f7 --- /dev/null +++ b/src/test/Fixtures/BoxFolder/PutTransferFolder200.json @@ -0,0 +1,71 @@ +{ + "type": "folder", + "id": "0", + "sequence_id": "0", + "etag": "0", + "name": "Example Test Folder", + "created_at": "2018-04-24T12:50:03-07:00", + "modified_at": "2018-04-24T12:50:03-07:00", + "description": "", + "size": 0, + "path_collection": { + "total_count": 1, + "entries": [ + { + "type": "folder", + "id": "0", + "sequence_id": null, + "etag": null, + "name": "All Files" + } + ] + }, + "created_by": { + "type": "user", + "id": "1111", + "name": "Test User", + "login": "test@user.com" + }, + "modified_by": { + "type": "user", + "id": "1111", + "name": "Test User", + "login": "test@user.com" + }, + "trashed_at": null, + "purged_at": null, + "content_created_at": "2018-04-24T12:50:03-07:00", + "content_modified_at": "2018-04-24T12:50:03-07:00", + "owned_by": { + "type": "user", + "id": "1111", + "name": "Test User", + "login": "test@user.com" + }, + "shared_link": null, + "folder_upload_email": null, + "parent": { + "type": "folder", + "id": "0", + "sequence_id": null, + "etag": null, + "name": "All Files" + }, + "item_status": "active", + "item_collection": { + "total_count": 0, + "entries": [], + "offset": 0, + "limit": 100, + "order": [ + { + "by": "type", + "direction": "ASC" + }, + { + "by": "name", + "direction": "ASC" + } + ] + } +} diff --git a/src/test/java/com/box/sdk/BoxUserTest.java b/src/test/java/com/box/sdk/BoxUserTest.java index f45cdf702..8c3fed5a9 100644 --- a/src/test/java/com/box/sdk/BoxUserTest.java +++ b/src/test/java/com/box/sdk/BoxUserTest.java @@ -361,4 +361,34 @@ public void testGetAllEnterpriseUsersSucceeds() throws IOException { Assert.assertEquals(secondUserName, secondUser.getName()); Assert.assertEquals(secondUserLogin, secondUser.getLogin()); } + + @Test + @Category(UnitTest.class) + public void testTransferContent() throws IOException { + String result = ""; + final String sourceUserID = "1111"; + final String destinationUserID = "5678"; + final String createdByLogin = "test@user.com"; + final String transferredFolderName = "Example Test Folder"; + final String transferContentURL = "/users/" + sourceUserID + "/folders/0"; + + JsonObject destinationUser = new JsonObject() + .add("id", destinationUserID); + JsonObject ownedBy = new JsonObject() + .add("owned_by", destinationUser); + + result = TestConfig.getFixture("BoxFolder/PutTransferFolder200"); + + WIRE_MOCK_CLASS_RULE.stubFor(WireMock.put(WireMock.urlPathEqualTo(transferContentURL)) + .withRequestBody(WireMock.equalToJson(ownedBy.toString())) + .willReturn(WireMock.aResponse() + .withHeader("Content-Type", "application/json") + .withBody(result))); + + BoxUser sourceUser = new BoxUser(this.api, sourceUserID); + BoxFolder.Info movedFolder = sourceUser.transferContent(destinationUserID); + + Assert.assertEquals(transferredFolderName, movedFolder.getName()); + Assert.assertEquals(createdByLogin, movedFolder.getCreatedBy().getLogin()); + } } From e48f01511718b87c23b6f5aa99ced651b698cbc7 Mon Sep 17 00:00:00 2001 From: carycheng Date: Wed, 8 Aug 2018 15:29:07 -0700 Subject: [PATCH 2/3] added time for set up for wiremock test --- src/test/java/com/box/sdk/BoxUserTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/box/sdk/BoxUserTest.java b/src/test/java/com/box/sdk/BoxUserTest.java index 8c3fed5a9..a18471919 100644 --- a/src/test/java/com/box/sdk/BoxUserTest.java +++ b/src/test/java/com/box/sdk/BoxUserTest.java @@ -364,7 +364,7 @@ public void testGetAllEnterpriseUsersSucceeds() throws IOException { @Test @Category(UnitTest.class) - public void testTransferContent() throws IOException { + public void testTransferContent() throws IOException, InterruptedException { String result = ""; final String sourceUserID = "1111"; final String destinationUserID = "5678"; @@ -385,6 +385,8 @@ public void testTransferContent() throws IOException { .withHeader("Content-Type", "application/json") .withBody(result))); + Thread.sleep(5000); + BoxUser sourceUser = new BoxUser(this.api, sourceUserID); BoxFolder.Info movedFolder = sourceUser.transferContent(destinationUserID); From b1b6ae2b4c31b7c57d95f5fc2b999c25970a41fa Mon Sep 17 00:00:00 2001 From: carycheng Date: Wed, 8 Aug 2018 16:42:20 -0700 Subject: [PATCH 3/3] updated readme --- doc/users.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/doc/users.md b/doc/users.md index a794e1917..177b1560f 100644 --- a/doc/users.md +++ b/doc/users.md @@ -213,9 +213,10 @@ To move all of a user's content to another user, call the [`transferContent(String destinationUserID)`][transfer-folder-to-new-user] method. ```java -String destinationUserID = "12345"; -BoxUser sourceUser = new BoxUser(api, "0"); +String sourceUserID = "11111"; +String destinationUserID = "22222"; +BoxUser sourceUser = new BoxUser(api, sourceUserID); BoxFolder.Info transferredFolderInfo = sourceUser.transferContent(destinationUserID); ``` -[transfer-folder-to-new-user]: http://opensource.box.com/box-java-sdk/javadoc/com/box/sdk/BoxUser.html#transferFolderToNewUser-java.lang.String- +[transfer-folder-to-new-user]: http://opensource.box.com/box-java-sdk/javadoc/com/box/sdk/BoxUser.html#transferContent-java.lang.String-