From 9457b6b6e225495a18f2c7bdf17a485bab03d4f3 Mon Sep 17 00:00:00 2001 From: lsocha Date: Mon, 1 Jul 2024 17:53:19 +0200 Subject: [PATCH] fix: Fix deserializing permissions for `BoxFile` and `BoxFolder` --- src/intTest/java/com/box/sdk/BoxFileIT.java | 1 + src/main/java/com/box/sdk/BoxFile.java | 30 +++---------------- src/main/java/com/box/sdk/BoxFolder.java | 28 +++-------------- src/test/Fixtures/BoxFile/GetFileInfo200.json | 16 +++++++++- .../Fixtures/BoxFolder/GetFolderInfo200.json | 11 ++++++- src/test/java/com/box/sdk/BoxFileTest.java | 2 ++ src/test/java/com/box/sdk/BoxFolderTest.java | 2 ++ 7 files changed, 38 insertions(+), 52 deletions(-) diff --git a/src/intTest/java/com/box/sdk/BoxFileIT.java b/src/intTest/java/com/box/sdk/BoxFileIT.java index 255ef9d8f..831701a62 100644 --- a/src/intTest/java/com/box/sdk/BoxFileIT.java +++ b/src/intTest/java/com/box/sdk/BoxFileIT.java @@ -291,6 +291,7 @@ public void getInfoWithAllFields() { assertThat(uploadedFileInfo.getItemStatus(), is(equalTo("active"))); assertThat(uploadedFileInfo.getVersion(), not(nullValue())); assertThat(uploadedFileInfo.getVersion().getVersionID(), not(nullValue())); + assertThat(uploadedFileInfo.getPermissions(), hasItem(BoxFile.Permission.CAN_INVITE_COLLABORATOR)); } finally { deleteFile(uploadedFile); } diff --git a/src/main/java/com/box/sdk/BoxFile.java b/src/main/java/com/box/sdk/BoxFile.java index 0334abfd2..4d4c8f836 100644 --- a/src/main/java/com/box/sdk/BoxFile.java +++ b/src/main/java/com/box/sdk/BoxFile.java @@ -1964,32 +1964,10 @@ private EnumSet parsePermissions(JsonObject jsonObject) { if (value.isNull() || !value.asBoolean()) { continue; } - - switch (member.getName()) { - case "can_download": - permissions.add(Permission.CAN_DOWNLOAD); - break; - case "can_upload": - permissions.add(Permission.CAN_UPLOAD); - break; - case "can_rename": - permissions.add(Permission.CAN_RENAME); - break; - case "can_delete": - permissions.add(Permission.CAN_DELETE); - break; - case "can_share": - permissions.add(Permission.CAN_SHARE); - break; - case "can_set_share_access": - permissions.add(Permission.CAN_SET_SHARE_ACCESS); - break; - case "can_preview": - permissions.add(Permission.CAN_PREVIEW); - break; - case "can_comment": - permissions.add(Permission.CAN_COMMENT); - break; + try { + permissions.add(Permission.fromJSONValue(member.getName())); + } catch (IllegalArgumentException ignored) { + // If the permission is not recognized, we ignore it. } } diff --git a/src/main/java/com/box/sdk/BoxFolder.java b/src/main/java/com/box/sdk/BoxFolder.java index 0420a6957..799fbbff9 100644 --- a/src/main/java/com/box/sdk/BoxFolder.java +++ b/src/main/java/com/box/sdk/BoxFolder.java @@ -1745,30 +1745,10 @@ private EnumSet parsePermissions(JsonObject jsonObject) { continue; } - switch (member.getName()) { - case "can_download": - permissions.add(Permission.CAN_DOWNLOAD); - break; - case "can_upload": - permissions.add(Permission.CAN_UPLOAD); - break; - case "can_rename": - permissions.add(Permission.CAN_RENAME); - break; - case "can_delete": - permissions.add(Permission.CAN_DELETE); - break; - case "can_share": - permissions.add(Permission.CAN_SHARE); - break; - case "can_invite_collaborator": - permissions.add(Permission.CAN_INVITE_COLLABORATOR); - break; - case "can_set_share_access": - permissions.add(Permission.CAN_SET_SHARE_ACCESS); - break; - default: - break; + try { + permissions.add(BoxFolder.Permission.fromJSONValue(member.getName())); + } catch (IllegalArgumentException ignored) { + // If the permission is not recognized, we ignore it. } } diff --git a/src/test/Fixtures/BoxFile/GetFileInfo200.json b/src/test/Fixtures/BoxFile/GetFileInfo200.json index 7230bd1b2..72373dcef 100644 --- a/src/test/Fixtures/BoxFile/GetFileInfo200.json +++ b/src/test/Fixtures/BoxFile/GetFileInfo200.json @@ -76,5 +76,19 @@ "name": "Top Secret" }, "disposition_at": "2012-12-12T18:53:43Z", - "is_accessible_via_shared_link": true + "is_accessible_via_shared_link": true, + "permissions": { + "can_annotate": true, + "can_comment": true, + "can_delete": true, + "can_download": true, + "can_invite_collaborator": true, + "can_preview": true, + "can_rename": true, + "can_set_share_access": true, + "can_share": true, + "can_upload": true, + "can_view_annotations_all": true, + "can_view_annotations_self": true + } } diff --git a/src/test/Fixtures/BoxFolder/GetFolderInfo200.json b/src/test/Fixtures/BoxFolder/GetFolderInfo200.json index afb88939d..4a633a99a 100644 --- a/src/test/Fixtures/BoxFolder/GetFolderInfo200.json +++ b/src/test/Fixtures/BoxFolder/GetFolderInfo200.json @@ -107,5 +107,14 @@ "definition": "Content that should not be shared outside the company.", "name": "Top Secret" }, - "is_accessible_via_shared_link": true + "is_accessible_via_shared_link": true, + "permissions": { + "can_delete": true, + "can_download": true, + "can_invite_collaborator": true, + "can_rename": true, + "can_set_share_access": true, + "can_share": true, + "can_upload": true + } } diff --git a/src/test/java/com/box/sdk/BoxFileTest.java b/src/test/java/com/box/sdk/BoxFileTest.java index 0d17c09b0..b2e586348 100644 --- a/src/test/java/com/box/sdk/BoxFileTest.java +++ b/src/test/java/com/box/sdk/BoxFileTest.java @@ -34,6 +34,7 @@ import java.text.ParseException; import java.util.ArrayList; import java.util.Collection; +import java.util.EnumSet; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -179,6 +180,7 @@ public void testGetFileInfoSucceeds() throws ParseException { assertTrue(info.getHasCollaborations()); assertEquals(info.getDispositionAt(), BoxDateFormat.parse("2012-12-12T18:53:43Z")); assertTrue(info.getIsAccessibleViaSharedLink()); + assertEquals(info.getPermissions(), EnumSet.allOf(BoxFile.Permission.class)); } @Test(expected = BoxDeserializationException.class) diff --git a/src/test/java/com/box/sdk/BoxFolderTest.java b/src/test/java/com/box/sdk/BoxFolderTest.java index 5c1d0e780..6242065a4 100644 --- a/src/test/java/com/box/sdk/BoxFolderTest.java +++ b/src/test/java/com/box/sdk/BoxFolderTest.java @@ -31,6 +31,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Date; +import java.util.EnumSet; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -488,6 +489,7 @@ public void testGetAllRootFolderItemsSucceeds() { assertEquals(ownedByUserLogin, rootFolderInfo.getOwnedBy().getLogin()); assertEquals(modifiedByLogin, rootFolderInfo.getModifiedBy().getLogin()); assertEquals(modifiedByName, rootFolderInfo.getModifiedBy().getName()); + assertEquals(rootFolderInfo.getPermissions(), EnumSet.allOf(BoxFolder.Permission.class)); } @Test