diff --git a/src/main/java/com/box/sdk/BoxItem.java b/src/main/java/com/box/sdk/BoxItem.java index dad8de9b8..a89e2588b 100644 --- a/src/main/java/com/box/sdk/BoxItem.java +++ b/src/main/java/com/box/sdk/BoxItem.java @@ -413,6 +413,19 @@ public List getTags() { return this.tags; } + /** + * Sets the tags for an item. + * @param tags The new tags for the item. + */ + public void setTags(List 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. diff --git a/src/test/java/com/box/sdk/BoxFileTest.java b/src/test/java/com/box/sdk/BoxFileTest.java index 1aacef0cc..2bb62c92f 100644 --- a/src/test/java/com/box/sdk/BoxFileTest.java +++ b/src/test/java/com/box/sdk/BoxFileTest.java @@ -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; @@ -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 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() { diff --git a/src/test/java/com/box/sdk/BoxFolderTest.java b/src/test/java/com/box/sdk/BoxFolderTest.java index 9c6224e2d..7edaa05d0 100644 --- a/src/test/java/com/box/sdk/BoxFolderTest.java +++ b/src/test/java/com/box/sdk/BoxFolderTest.java @@ -823,6 +823,48 @@ public void updateFolderInfoSucceeds() { assertThat(rootFolder, not(hasItem(Matchers.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 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() {