diff --git a/src/main/java/com/box/sdk/BoxNotificationEmail.java b/src/main/java/com/box/sdk/BoxNotificationEmail.java new file mode 100644 index 000000000..b73534f73 --- /dev/null +++ b/src/main/java/com/box/sdk/BoxNotificationEmail.java @@ -0,0 +1,63 @@ +package com.box.sdk; + +import com.eclipsesource.json.JsonObject; +import com.eclipsesource.json.JsonValue; + +/** + * Represents a notification email object + */ +public class BoxNotificationEmail extends BoxJSONObject { + private boolean isConfirmed; + private String email; + + /** + * Constructs a BoxNotificationEmail from a JSON string. + * + * @param json the json encoded email alias. + */ + public BoxNotificationEmail(String json) { + super(json); + } + + /** + * Constructs a BoxNotificationEmail using an already parsed JSON object. + * + * @param jsonObject the parsed JSON object. + */ + BoxNotificationEmail(JsonObject jsonObject) { + super(jsonObject); + } + + /** + * Gets whether or not the email address has been confirmed. + * + * @return true if this email address has been confirmed; otherwise false. + */ + public boolean getIsConfirmed() { + return this.isConfirmed; + } + + /** + * Gets the email address to send notifications to. + * + * @return The email address to send the notifications to. + */ + public String getEmail() { + return this.email; + } + + @Override + void parseJSONMember(JsonObject.Member member) { + JsonValue value = member.getValue(); + String memberName = member.getName(); + try { + if (memberName.equals("is_confirmed")) { + this.isConfirmed = value.asBoolean(); + } else if (memberName.equals("email")) { + this.email = value.asString(); + } + } catch (Exception e) { + throw new BoxDeserializationException(memberName, value.toString(), e); + } + } +} diff --git a/src/main/java/com/box/sdk/BoxUser.java b/src/main/java/com/box/sdk/BoxUser.java index b106c7a31..7351b4d57 100644 --- a/src/main/java/com/box/sdk/BoxUser.java +++ b/src/main/java/com/box/sdk/BoxUser.java @@ -34,7 +34,7 @@ public class BoxUser extends BoxCollaborator { "language", "timezone", "space_amount", "space_used", "max_upload_size", "tracking_codes", "can_see_managed_users", "is_sync_enabled", "is_external_collab_restricted", "status", "job_title", "phone", "address", "avatar_url", "is_exempt_from_device_limits", "is_exempt_from_login_verification", "enterprise", - "my_tags", "hostname", "is_platform_access_only", "external_app_user_id"}; + "my_tags", "hostname", "is_platform_access_only", "external_app_user_id", "notification_email"}; /** * User URL Template. @@ -690,8 +690,8 @@ public AvatarUploadResponse uploadAvatar(File file) { } /** - * Upload avatar image to user account. Supported formats are JPG and PNG. - * The image size cannot exceed 1024 * 1024 pixels or 1MB. + * Upload avatar image to user account. Supported formats are JPG and PNG. + * The image size cannot exceed 1024 * 1024 pixels or 1MB. * * @param file {@link File} containg avatar image. * @param progressListener {@link ProgressListener} set if you want to track upload progress @@ -718,8 +718,8 @@ public AvatarUploadResponse uploadAvatar(InputStream content, String fileName) { } /** - * Upload avatar image to user account. Supported formats are JPG and PNG. - * The image size cannot exceed 1024 * 1024 pixels or 1MB. + * Upload avatar image to user account. Supported formats are JPG and PNG. + * The image size cannot exceed 1024 * 1024 pixels or 1MB. * * @param content {@link InputStream} containing image data * @param fileName file name with extention what will be used to determine content type @@ -852,6 +852,7 @@ public class Info extends BoxCollaborator.Info { private String phone; private String address; private String avatarURL; + private BoxNotificationEmail notificationEmail; private boolean isExemptFromDeviceLimits; private boolean isExemptFromLoginVerification; private boolean isPasswordResetRequired; @@ -1086,6 +1087,24 @@ public String getAvatarURL() { return this.avatarURL; } + /** + * Gets the user's alternate notification email address to which email notifications are sent. + * + * @return the user's notification email address. + */ + public BoxNotificationEmail getNotificationEmail() { + return this.notificationEmail; + } + + /** + * Sets the user's notification email address. + * + * @param notificationEmail the user's new notification email address. + */ + public void setNotificationEmail(BoxNotificationEmail notificationEmail) { + this.notificationEmail = notificationEmail; + } + /** * Gets the enterprise that the user belongs to. * @@ -1325,6 +1344,12 @@ protected void parseJSONMember(JsonObject.Member member) { this.address = value.asString(); } else if (memberName.equals("avatar_url")) { this.avatarURL = value.asString(); + } else if (memberName.equals("notification_email")) { + if (value.isObject()) { + this.notificationEmail = new BoxNotificationEmail(value.asObject()); + } else { + this.notificationEmail = null; + } } else if (memberName.equals("can_see_managed_users")) { this.canSeeManagedUsers = value.asBoolean(); } else if (memberName.equals("is_sync_enabled")) { diff --git a/src/test/Fixtures/BoxUser/GetCurrentUserInfo200.json b/src/test/Fixtures/BoxUser/GetCurrentUserInfo200.json index 8b503d2b0..5a64158f4 100644 --- a/src/test/Fixtures/BoxUser/GetCurrentUserInfo200.json +++ b/src/test/Fixtures/BoxUser/GetCurrentUserInfo200.json @@ -14,5 +14,9 @@ "job_title": "", "phone": "1111111111", "address": "", - "avatar_url": "https://test.app.box.com/api/avatar/large/12345" + "avatar_url": "https://test.app.box.com/api/avatar/large/12345", + "notification_email": { + "email": "notifications@example.com", + "is_confirmed": true + } } diff --git a/src/test/Fixtures/BoxUser/GetCurrentUserInfoWithNoNotifcationEmail200.json b/src/test/Fixtures/BoxUser/GetCurrentUserInfoWithNoNotifcationEmail200.json new file mode 100644 index 000000000..d6e8983d4 --- /dev/null +++ b/src/test/Fixtures/BoxUser/GetCurrentUserInfoWithNoNotifcationEmail200.json @@ -0,0 +1,19 @@ +{ + "type": "user", + "id": "12345", + "name": "Test User", + "login": "test@user.com", + "created_at": "2015-04-09T12:32:40-07:00", + "modified_at": "2018-04-25T11:00:20-07:00", + "language": "en", + "timezone": "America/Los_Angeles", + "space_amount": 1000000000000000, + "space_used": 22392578035, + "max_upload_size": 16106127360, + "status": "active", + "job_title": "", + "phone": "1111111111", + "address": "", + "avatar_url": "https://test.app.box.com/api/avatar/large/12345", + "notification_email": [] +} diff --git a/src/test/java/com/box/sdk/BoxUserTest.java b/src/test/java/com/box/sdk/BoxUserTest.java index b6f20a2ca..682fde14a 100644 --- a/src/test/java/com/box/sdk/BoxUserTest.java +++ b/src/test/java/com/box/sdk/BoxUserTest.java @@ -3,6 +3,10 @@ import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; import static java.lang.String.format; import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import com.eclipsesource.json.JsonArray; import com.eclipsesource.json.JsonObject; @@ -26,7 +30,6 @@ import java.util.Iterator; import java.util.Map; import org.hamcrest.CoreMatchers; -import org.junit.Assert; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -73,7 +76,7 @@ public void testGetAvatar() throws IOException { throw new BoxAPIException("Couldn't connect to the Box API due to a network error.", e); } - Assert.assertArrayEquals(fileByteArray, output.toByteArray()); + assertArrayEquals(fileByteArray, output.toByteArray()); } @Test @@ -99,9 +102,9 @@ public void testGetCurrentUserInfoSucceeds() throws IOException { BoxUser user = BoxUser.getCurrentUser(this.api); BoxUser.Info info = user.getInfo(); - Assert.assertEquals(userName, info.getName()); - Assert.assertEquals(userLogin, info.getLogin()); - Assert.assertEquals(userphoneNumber, info.getPhone()); + assertEquals(userName, info.getName()); + assertEquals(userLogin, info.getLogin()); + assertEquals(userphoneNumber, info.getPhone()); } @Test @@ -111,6 +114,7 @@ public void testGetUserInfoByIDSucceeds() throws IOException { final String userName = "Test User"; final String userLogin = "test@user.com"; final String userPhoneNumber = "1111111111"; + final String notificationEmail = "notifications@example.com"; String result = TestUtils.getFixture("BoxUser/GetCurrentUserInfo200"); @@ -122,10 +126,37 @@ public void testGetUserInfoByIDSucceeds() throws IOException { BoxUser user = new BoxUser(this.api, userID); BoxUser.Info userInfo = user.getInfo(); - Assert.assertEquals(userID, userInfo.getID()); - Assert.assertEquals(userName, userInfo.getName()); - Assert.assertEquals(userLogin, userInfo.getLogin()); - Assert.assertEquals(userPhoneNumber, userInfo.getPhone()); + assertEquals(userID, userInfo.getID()); + assertEquals(userName, userInfo.getName()); + assertEquals(userLogin, userInfo.getLogin()); + assertEquals(userPhoneNumber, userInfo.getPhone()); + assertEquals(notificationEmail, userInfo.getNotificationEmail().getEmail()); + assertTrue(userInfo.getNotificationEmail().getIsConfirmed()); + } + + @Test + public void testGetUserInfoWithNoNotificationEmailByIDSucceeds() throws IOException { + final String userID = "12345"; + final String userURL = "/2.0/users/" + userID; + final String userName = "Test User"; + final String userLogin = "test@user.com"; + final String userPhoneNumber = "1111111111"; + + String result = TestUtils.getFixture("BoxUser/GetCurrentUserInfoWithNoNotifcationEmail200"); + + wireMockRule.stubFor(WireMock.get(WireMock.urlPathEqualTo(userURL)) + .willReturn(WireMock.aResponse() + .withHeader("Content-Type", "application/json") + .withBody(result))); + + BoxUser user = new BoxUser(this.api, userID); + BoxUser.Info userInfo = user.getInfo(); + + assertEquals(userID, userInfo.getID()); + assertEquals(userName, userInfo.getName()); + assertEquals(userLogin, userInfo.getLogin()); + assertEquals(userPhoneNumber, userInfo.getPhone()); + assertNull(userInfo.getNotificationEmail()); } @Test @@ -144,9 +175,9 @@ public void testCreateAppUserSucceeds() throws IOException { BoxUser.Info createdUserInfo = BoxUser.createAppUser(this.api, userName); - Assert.assertEquals(userID, createdUserInfo.getID()); - Assert.assertEquals(userName, createdUserInfo.getName()); - Assert.assertEquals(userLogin, createdUserInfo.getLogin()); + assertEquals(userID, createdUserInfo.getID()); + assertEquals(userName, createdUserInfo.getName()); + assertEquals(userLogin, createdUserInfo.getLogin()); } @Test @@ -166,10 +197,10 @@ public void testCreateManagedUserSucceeds() throws IOException { BoxUser.Info createdUserInfo = BoxUser.createEnterpriseUser(this.api, userLogin, userName); - Assert.assertEquals(userID, createdUserInfo.getID()); - Assert.assertEquals(userName, createdUserInfo.getName()); - Assert.assertEquals(userLogin, createdUserInfo.getLogin()); - Assert.assertEquals(userTimeZone, createdUserInfo.getTimezone()); + assertEquals(userID, createdUserInfo.getID()); + assertEquals(userName, createdUserInfo.getName()); + assertEquals(userLogin, createdUserInfo.getLogin()); + assertEquals(userTimeZone, createdUserInfo.getTimezone()); } @Test @@ -203,11 +234,11 @@ public void testUpdateUserInfoSucceedsAndSendsCorrectJson() throws IOException { info.setPhone(userPhone); user.updateInfo(info); - Assert.assertEquals(userID, info.getID()); - Assert.assertEquals(userName, info.getName()); - Assert.assertEquals(userLogin, info.getLogin()); - Assert.assertEquals(userJob, info.getJobTitle()); - Assert.assertEquals(userPhone, info.getPhone()); + assertEquals(userID, info.getID()); + assertEquals(userName, info.getName()); + assertEquals(userLogin, info.getLogin()); + assertEquals(userJob, info.getJobTitle()); + assertEquals(userPhone, info.getPhone()); } @Test @@ -243,9 +274,9 @@ public void testCreateEmailAliasSucceeds() throws IOException { BoxUser user = new BoxUser(this.api, userID); EmailAlias alias = user.addEmailAlias(emailAlias); - Assert.assertEquals(userID, alias.getID()); - Assert.assertTrue(alias.getIsConfirmed()); - Assert.assertEquals(emailAlias, alias.getEmail()); + assertEquals(userID, alias.getID()); + assertTrue(alias.getIsConfirmed()); + assertEquals(emailAlias, alias.getEmail()); } @Test @@ -264,8 +295,8 @@ public void testGetEmailAliasSucceeds() throws IOException { BoxUser user = new BoxUser(this.api, userID); Collection emailAliases = user.getEmailAliases(); - Assert.assertEquals(userID, emailAliases.iterator().next().getID()); - Assert.assertEquals(userEmail, emailAliases.iterator().next().getEmail()); + assertEquals(userID, emailAliases.iterator().next().getID()); + assertEquals(userEmail, emailAliases.iterator().next().getEmail()); } @Test @@ -303,15 +334,15 @@ public void testGetAllEnterpriseUsersSucceeds() throws IOException { Iterator users = BoxUser.getAllEnterpriseUsers(this.api).iterator(); BoxUser.Info firstUser = users.next(); - Assert.assertEquals(firstUserID, firstUser.getID()); - Assert.assertEquals(firstUserName, firstUser.getName()); - Assert.assertEquals(firstUserLogin, firstUser.getLogin()); + assertEquals(firstUserID, firstUser.getID()); + assertEquals(firstUserName, firstUser.getName()); + assertEquals(firstUserLogin, firstUser.getLogin()); BoxUser.Info secondUser = users.next(); - Assert.assertEquals(secondUserID, secondUser.getID()); - Assert.assertEquals(secondUserName, secondUser.getName()); - Assert.assertEquals(secondUserLogin, secondUser.getLogin()); + assertEquals(secondUserID, secondUser.getID()); + assertEquals(secondUserName, secondUser.getName()); + assertEquals(secondUserLogin, secondUser.getLogin()); } @Test @@ -336,15 +367,15 @@ public void testGetAllEnterpriseUsersMarkerPaginationSucceeds() throws IOExcepti Iterator users = BoxUser.getAllEnterpriseUsers(this.api, true, null).iterator(); BoxUser.Info firstUser = users.next(); - Assert.assertEquals(firstUserID, firstUser.getID()); - Assert.assertEquals(firstUserName, firstUser.getName()); - Assert.assertEquals(firstUserLogin, firstUser.getLogin()); + assertEquals(firstUserID, firstUser.getID()); + assertEquals(firstUserName, firstUser.getName()); + assertEquals(firstUserLogin, firstUser.getLogin()); BoxUser.Info secondUser = users.next(); - Assert.assertEquals(secondUserID, secondUser.getID()); - Assert.assertEquals(secondUserName, secondUser.getName()); - Assert.assertEquals(secondUserLogin, secondUser.getLogin()); + assertEquals(secondUserID, secondUser.getID()); + assertEquals(secondUserName, secondUser.getName()); + assertEquals(secondUserLogin, secondUser.getLogin()); } @Test @@ -371,8 +402,8 @@ public void testTransferContent() throws IOException { BoxUser sourceUser = new BoxUser(this.api, sourceUserID); BoxFolder.Info movedFolder = sourceUser.transferContent(destinationUserID); - Assert.assertEquals(transferredFolderName, movedFolder.getName()); - Assert.assertEquals(createdByLogin, movedFolder.getCreatedBy().getLogin()); + assertEquals(transferredFolderName, movedFolder.getName()); + assertEquals(createdByLogin, movedFolder.getCreatedBy().getLogin()); } @Test(expected = BoxDeserializationException.class) @@ -388,7 +419,7 @@ public void testDeserializationException() throws IOException { .withBody(result))); BoxUser.Info userInfo = new BoxUser(this.api, userID).getInfo(); - Assert.assertEquals("12345", userInfo.getID()); + assertEquals("12345", userInfo.getID()); } @Test @@ -455,7 +486,7 @@ public void testCreateReadAddTrackingCodesSucceeds() throws IOException { user = new BoxUser(this.api, userID); info = user.getInfo("tracking_codes"); Map receivedTrackingCodes = info.getTrackingCodes(); - Assert.assertEquals(createTrackingCodes, receivedTrackingCodes); + assertEquals(createTrackingCodes, receivedTrackingCodes); // Add one more tracking code info.appendTrackingCodes("Company ID", companyID); @@ -465,7 +496,7 @@ public void testCreateReadAddTrackingCodesSucceeds() throws IOException { user = new BoxUser(this.api, userID); info = user.getInfo("tracking_codes"); receivedTrackingCodes = info.getTrackingCodes(); - Assert.assertEquals(appendTrackingCodes, receivedTrackingCodes); + assertEquals(appendTrackingCodes, receivedTrackingCodes); } @Test