Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for setting tags on items #554

Merged
merged 1 commit into from
Feb 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/main/java/com/box/sdk/BoxItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,19 @@ public List<String> getTags() {
return this.tags;
}

/**
* Sets the tags for an item.
* @param tags The new tags for the item.
*/
public void setTags(List<String> tags) {
this.tags = tags;
JsonArray tagsJSON = new JsonArray();
for (String tag : tags) {
tagsJSON.add(tag);
}
this.addPendingChange("tags", tagsJSON);
}

/**
* Gets info about the parent folder of the item.
* @return info about the parent folder of the item.
Expand Down
43 changes: 43 additions & 0 deletions src/test/java/com/box/sdk/BoxFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import com.eclipsesource.json.JsonArray;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;
Expand Down Expand Up @@ -738,6 +739,48 @@ public void updateFileInfoSucceeds() {
uploadedFile.delete();
}

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

JsonObject fakeResponse = new JsonObject();
fakeResponse.add("type", "file");
fakeResponse.add("id", "1234");
JsonArray tagsJSON = new JsonArray();
tagsJSON.add("foo");
tagsJSON.add("bar");
fakeResponse.add("tags", tagsJSON);

BoxAPIConnection api = new BoxAPIConnection("");
api.setRequestInterceptor(JSONRequestInterceptor.respondWith(fakeResponse));

BoxFile file = new BoxFile(api, "1234");
BoxFile.Info info = file.getInfo();
List<String> tags = info.getTags();
Assert.assertEquals("foo", tags.get(0));
Assert.assertEquals("bar", tags.get(1));

tags.add("baz");
info.setTags(tags);

api.setRequestInterceptor(new JSONRequestInterceptor() {
@Override
protected BoxAPIResponse onJSONRequest(BoxJSONRequest request, JsonObject json) {
Assert.assertEquals("foo", json.get("tags").asArray().get(0).asString());
Assert.assertEquals("bar", json.get("tags").asArray().get(1).asString());
Assert.assertEquals("baz", json.get("tags").asArray().get(2).asString());
return new BoxJSONResponse() {
@Override
public String getJSON() {
return "{}";
}
};
}
});

file.updateInfo(info);
}

@Test
@Category(IntegrationTest.class)
public void deleteVersionSucceeds() {
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/com/box/sdk/BoxFolderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,48 @@ public void updateFolderInfoSucceeds() {
assertThat(rootFolder, not(hasItem(Matchers.<BoxItem.Info>hasProperty("ID", equalTo(childFolder.getID())))));
}

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

JsonObject fakeResponse = new JsonObject();
fakeResponse.add("type", "file");
fakeResponse.add("id", "1234");
JsonArray tagsJSON = new JsonArray();
tagsJSON.add("foo");
tagsJSON.add("bar");
fakeResponse.add("tags", tagsJSON);

BoxAPIConnection api = new BoxAPIConnection("");
api.setRequestInterceptor(JSONRequestInterceptor.respondWith(fakeResponse));

BoxFolder folder = new BoxFolder(api, "1234");
BoxFolder.Info info = folder.getInfo();
List<String> tags = info.getTags();
Assert.assertEquals("foo", tags.get(0));
Assert.assertEquals("bar", tags.get(1));

tags.add("quux");
info.setTags(tags);

api.setRequestInterceptor(new JSONRequestInterceptor() {
@Override
protected BoxAPIResponse onJSONRequest(BoxJSONRequest request, JsonObject json) {
Assert.assertEquals("foo", json.get("tags").asArray().get(0).asString());
Assert.assertEquals("bar", json.get("tags").asArray().get(1).asString());
Assert.assertEquals("quux", json.get("tags").asArray().get(2).asString());
return new BoxJSONResponse() {
@Override
public String getJSON() {
return "{}";
}
};
}
});

folder.updateInfo(info);
}

@Test
@Category(IntegrationTest.class)
public void copyFolderToSameDestinationWithNewNameSucceeds() {
Expand Down