Skip to content

Commit

Permalink
Fix collaboration notify parameter (#545)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Matt Willer authored Feb 1, 2018
1 parent f34ad26 commit ebcef04
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 68 deletions.
42 changes: 42 additions & 0 deletions src/main/java/com/box/sdk/BoxCollaboration.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 1 addition & 22 deletions src/main/java/com/box/sdk/BoxFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
49 changes: 3 additions & 46 deletions src/main/java/com/box/sdk/BoxFolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/com/box/sdk/BoxFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,42 @@ public void setCollectionsWithInfoSucceeds() {
uploadedFile.delete();
}

@Test
@Category(UnitTest.class)
public void collaborateWithOptionalParamsSendsCorrectRequest() {

final String fileID = "983745";
final String collaboratorLogin = "[email protected]";
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 {
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/com/box/sdk/BoxFolderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,42 @@ public void addCollaborationsWithAttributesSucceeds() {
folder.delete(false);
}

@Test
@Category(UnitTest.class)
public void collaborateWithOptionalParamsSendsCorrectRequest() {

final String folderID = "983745";
final String collaboratorLogin = "[email protected]";
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() {
Expand Down

0 comments on commit ebcef04

Please sign in to comment.