From ebcef043ba81b232526defe26d7cd361ec8340d1 Mon Sep 17 00:00:00 2001 From: Matt Willer Date: Thu, 1 Feb 2018 11:59:04 -0800 Subject: [PATCH] Fix collaboration notify parameter (#545) Fixed a bug where the `notify` parameter when creating a collab was placed in a header instead of the query string. Also did some opportunistic refactoring to consolidate collab creation API call logic in one place in the BoxCollaboration class to avoid duplicate code. Fixes #544 --- .../java/com/box/sdk/BoxCollaboration.java | 42 ++++++++++++++++ src/main/java/com/box/sdk/BoxFile.java | 23 +-------- src/main/java/com/box/sdk/BoxFolder.java | 49 ++----------------- src/test/java/com/box/sdk/BoxFileTest.java | 36 ++++++++++++++ src/test/java/com/box/sdk/BoxFolderTest.java | 36 ++++++++++++++ 5 files changed, 118 insertions(+), 68 deletions(-) diff --git a/src/main/java/com/box/sdk/BoxCollaboration.java b/src/main/java/com/box/sdk/BoxCollaboration.java index 1f008d1fc..cf1f64083 100644 --- a/src/main/java/com/box/sdk/BoxCollaboration.java +++ b/src/main/java/com/box/sdk/BoxCollaboration.java @@ -47,6 +47,48 @@ public BoxCollaboration(BoxAPIConnection api, String id) { super(api, id); } + /** + * Create a new collaboration object. + * @param api the API connection used to make the request. + * @param accessibleBy the JSON object describing who should be collaborated. + * @param item the JSON object describing which item to collaborate. + * @param role the role to give the collaborators. + * @param notify the user/group should receive email notification of the collaboration or not. + * @param canViewPath the view path collaboration feature is enabled or not. + * @return info about the new collaboration. + */ + protected static BoxCollaboration.Info create(BoxAPIConnection api, JsonObject accessibleBy, JsonObject item, + BoxCollaboration.Role role, Boolean notify, Boolean canViewPath) { + + + String queryString = ""; + if (notify != null) { + queryString = new QueryStringBuilder().appendParam("notify", notify.toString()).toString(); + } + URL url; + if (queryString.length() > 0) { + url = COLLABORATIONS_URL_TEMPLATE.buildWithQuery(api.getBaseURL(), queryString); + } else { + url = COLLABORATIONS_URL_TEMPLATE.build(api.getBaseURL()); + } + + JsonObject requestJSON = new JsonObject(); + requestJSON.add("item", item); + requestJSON.add("accessible_by", accessibleBy); + requestJSON.add("role", role.toJSONString()); + if (canViewPath != null) { + requestJSON.add("can_view_path", canViewPath.booleanValue()); + } + + BoxJSONRequest request = new BoxJSONRequest(api, url, "POST"); + + request.setBody(requestJSON.toString()); + BoxJSONResponse response = (BoxJSONResponse) request.send(); + JsonObject responseJSON = JsonObject.readFrom(response.getJSON()); + + BoxCollaboration newCollaboration = new BoxCollaboration(api, responseJSON.get("id").asString()); + return newCollaboration.new Info(responseJSON); + } /** * Gets all pending collaboration invites for the current user. diff --git a/src/main/java/com/box/sdk/BoxFile.java b/src/main/java/com/box/sdk/BoxFile.java index 11d59c5ad..3c5bde1bc 100644 --- a/src/main/java/com/box/sdk/BoxFile.java +++ b/src/main/java/com/box/sdk/BoxFile.java @@ -1440,33 +1440,12 @@ String toJSONValue() { private BoxCollaboration.Info collaborate(JsonObject accessibleByField, BoxCollaboration.Role role, Boolean notify, Boolean canViewPath) { - BoxAPIConnection api = this.getAPI(); - URL url = ADD_COLLABORATION_URL.build(api.getBaseURL()); JsonObject itemField = new JsonObject(); itemField.add("id", this.getID()); itemField.add("type", "file"); - JsonObject requestJSON = new JsonObject(); - requestJSON.add("item", itemField); - requestJSON.add("accessible_by", accessibleByField); - requestJSON.add("role", role.toJSONString()); - if (canViewPath != null) { - requestJSON.add("can_view_path", canViewPath.booleanValue()); - } - - BoxJSONRequest request = new BoxJSONRequest(api, url, "POST"); - if (notify != null) { - request.addHeader("notify", notify.toString()); - } - - request.setBody(requestJSON.toString()); - BoxJSONResponse response = (BoxJSONResponse) request.send(); - JsonObject responseJSON = JsonObject.readFrom(response.getJSON()); - - BoxCollaboration newCollaboration = new BoxCollaboration(api, responseJSON.get("id").asString()); - BoxCollaboration.Info info = newCollaboration.new Info(responseJSON); - return info; + return BoxCollaboration.create(this.getAPI(), accessibleByField, itemField, role, notify, canViewPath); } /** diff --git a/src/main/java/com/box/sdk/BoxFolder.java b/src/main/java/com/box/sdk/BoxFolder.java index 76133f4bd..38f3f4f09 100644 --- a/src/main/java/com/box/sdk/BoxFolder.java +++ b/src/main/java/com/box/sdk/BoxFolder.java @@ -128,7 +128,7 @@ public BoxCollaboration.Info collaborate(BoxCollaborator collaborator, BoxCollab throw new IllegalArgumentException("The given collaborator is of an unknown type."); } - return this.collaborate(accessibleByField, role); + return this.collaborate(accessibleByField, role, null, null); } /** @@ -143,31 +143,9 @@ public BoxCollaboration.Info collaborate(String email, BoxCollaboration.Role rol accessibleByField.add("login", email); accessibleByField.add("type", "user"); - return this.collaborate(accessibleByField, role); + return this.collaborate(accessibleByField, role, null, null); } - private BoxCollaboration.Info collaborate(JsonObject accessibleByField, BoxCollaboration.Role role) { - BoxAPIConnection api = this.getAPI(); - URL url = ADD_COLLABORATION_URL.build(api.getBaseURL()); - - JsonObject itemField = new JsonObject(); - itemField.add("id", this.getID()); - itemField.add("type", "folder"); - - JsonObject requestJSON = new JsonObject(); - requestJSON.add("item", itemField); - requestJSON.add("accessible_by", accessibleByField); - requestJSON.add("role", role.toJSONString()); - - BoxJSONRequest request = new BoxJSONRequest(api, url, "POST"); - request.setBody(requestJSON.toString()); - BoxJSONResponse response = (BoxJSONResponse) request.send(); - JsonObject responseJSON = JsonObject.readFrom(response.getJSON()); - - BoxCollaboration newCollaboration = new BoxCollaboration(api, responseJSON.get("id").asString()); - BoxCollaboration.Info info = newCollaboration.new Info(responseJSON); - return info; - } /** * Adds a collaborator to this folder. * @param collaborator the collaborator to add. @@ -216,33 +194,12 @@ public BoxCollaboration.Info collaborate(String email, BoxCollaboration.Role rol private BoxCollaboration.Info collaborate(JsonObject accessibleByField, BoxCollaboration.Role role, Boolean notify, Boolean canViewPath) { - BoxAPIConnection api = this.getAPI(); - URL url = ADD_COLLABORATION_URL.build(api.getBaseURL()); JsonObject itemField = new JsonObject(); itemField.add("id", this.getID()); itemField.add("type", "folder"); - JsonObject requestJSON = new JsonObject(); - requestJSON.add("item", itemField); - requestJSON.add("accessible_by", accessibleByField); - requestJSON.add("role", role.toJSONString()); - if (canViewPath != null) { - requestJSON.add("can_view_path", canViewPath.booleanValue()); - } - - BoxJSONRequest request = new BoxJSONRequest(api, url, "POST"); - if (notify != null) { - request.addHeader("notify", notify.toString()); - } - - request.setBody(requestJSON.toString()); - BoxJSONResponse response = (BoxJSONResponse) request.send(); - JsonObject responseJSON = JsonObject.readFrom(response.getJSON()); - - BoxCollaboration newCollaboration = new BoxCollaboration(api, responseJSON.get("id").asString()); - BoxCollaboration.Info info = newCollaboration.new Info(responseJSON); - return info; + return BoxCollaboration.create(this.getAPI(), accessibleByField, itemField, role, notify, canViewPath); } @Override diff --git a/src/test/java/com/box/sdk/BoxFileTest.java b/src/test/java/com/box/sdk/BoxFileTest.java index fe51ccb38..1a820c3a2 100644 --- a/src/test/java/com/box/sdk/BoxFileTest.java +++ b/src/test/java/com/box/sdk/BoxFileTest.java @@ -1107,6 +1107,42 @@ public void setCollectionsWithInfoSucceeds() { uploadedFile.delete(); } + @Test + @Category(UnitTest.class) + public void collaborateWithOptionalParamsSendsCorrectRequest() { + + final String fileID = "983745"; + final String collaboratorLogin = "boxer@example.com"; + final BoxCollaboration.Role collaboratorRole = BoxCollaboration.Role.VIEWER; + + BoxAPIConnection api = new BoxAPIConnection(""); + api.setRequestInterceptor(new JSONRequestInterceptor() { + @Override + public BoxJSONResponse onJSONRequest(BoxJSONRequest request, JsonObject body) { + Assert.assertEquals( + "https://api.box.com/2.0/collaborations?notify=true", + request.getUrl().toString()); + Assert.assertEquals("POST", request.getMethod()); + + Assert.assertEquals(fileID, body.get("item").asObject().get("id").asString()); + Assert.assertEquals("file", body.get("item").asObject().get("type").asString()); + Assert.assertEquals(collaboratorLogin, body.get("accessible_by").asObject().get("login").asString()); + Assert.assertEquals("user", body.get("accessible_by").asObject().get("type").asString()); + Assert.assertEquals(collaboratorRole.toJSONString(), body.get("role").asString()); + + return new BoxJSONResponse() { + @Override + public String getJSON() { + return "{\"type\":\"collaboration\",\"id\":\"98763245\"}"; + } + }; + } + }); + + BoxFile file = new BoxFile(api, fileID); + BoxCollaboration.Info collabInfo = file.collaborate(collaboratorLogin, collaboratorRole, true, true); + } + @Test @Category(IntegrationTest.class) public void uploadSessionCommitFlowSuccess() throws Exception { diff --git a/src/test/java/com/box/sdk/BoxFolderTest.java b/src/test/java/com/box/sdk/BoxFolderTest.java index a393622ad..b2049f653 100644 --- a/src/test/java/com/box/sdk/BoxFolderTest.java +++ b/src/test/java/com/box/sdk/BoxFolderTest.java @@ -934,6 +934,42 @@ public void addCollaborationsWithAttributesSucceeds() { folder.delete(false); } + @Test + @Category(UnitTest.class) + public void collaborateWithOptionalParamsSendsCorrectRequest() { + + final String folderID = "983745"; + final String collaboratorLogin = "boxer@example.com"; + final BoxCollaboration.Role collaboratorRole = BoxCollaboration.Role.VIEWER; + + BoxAPIConnection api = new BoxAPIConnection(""); + api.setRequestInterceptor(new JSONRequestInterceptor() { + @Override + public BoxJSONResponse onJSONRequest(BoxJSONRequest request, JsonObject body) { + Assert.assertEquals( + "https://api.box.com/2.0/collaborations?notify=true", + request.getUrl().toString()); + Assert.assertEquals("POST", request.getMethod()); + + Assert.assertEquals(folderID, body.get("item").asObject().get("id").asString()); + Assert.assertEquals("folder", body.get("item").asObject().get("type").asString()); + Assert.assertEquals(collaboratorLogin, body.get("accessible_by").asObject().get("login").asString()); + Assert.assertEquals("user", body.get("accessible_by").asObject().get("type").asString()); + Assert.assertEquals(collaboratorRole.toJSONString(), body.get("role").asString()); + + return new BoxJSONResponse() { + @Override + public String getJSON() { + return "{\"type\":\"collaboration\",\"id\":\"98763245\"}"; + } + }; + } + }); + + BoxFolder folder = new BoxFolder(api, folderID); + BoxCollaboration.Info collabInfo = folder.collaborate(collaboratorLogin, collaboratorRole, true, true); + } + @Test @Category(IntegrationTest.class) public void getCollaborationsHasCorrectCollaborations() {