From 90708fe600856c01e0d0d7f4fd9352657274736e Mon Sep 17 00:00:00 2001 From: Jonathan Zacsh Date: Mon, 18 Apr 2022 15:17:44 -0500 Subject: [PATCH] fixes #1058: add MediaContainerResourceTest & more (#1059) backfills missing unit tests of MedaiContainerResourceTest and fixes a bug in the process This change forks test code (`{Photos,Media}ContainerResourceTest`) in line with MediaContainerResource's existing creation, then deletes some transmogrification stuff that we're not doing (per issue #1000) and adds a TODO(#1060) atop the new test to finish the tests more fully for video logic. --- .../models/media/MediaContainerResource.java | 19 +- .../media/MediaContainerResourceTest.java | 307 ++++++++++++++++++ 2 files changed, 318 insertions(+), 8 deletions(-) create mode 100644 portability-types-common/src/test/java/org/datatransferproject/types/common/models/media/MediaContainerResourceTest.java diff --git a/portability-types-common/src/main/java/org/datatransferproject/types/common/models/media/MediaContainerResource.java b/portability-types-common/src/main/java/org/datatransferproject/types/common/models/media/MediaContainerResource.java index 9386efc38..41cec2d1f 100644 --- a/portability-types-common/src/main/java/org/datatransferproject/types/common/models/media/MediaContainerResource.java +++ b/portability-types-common/src/main/java/org/datatransferproject/types/common/models/media/MediaContainerResource.java @@ -7,8 +7,10 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import java.util.Collection; +import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.stream.Collectors; import org.datatransferproject.types.common.models.ContainerResource; import org.datatransferproject.types.common.models.TransmogrificationConfig; import org.datatransferproject.types.common.models.photos.PhotoModel; @@ -22,7 +24,7 @@ public class MediaContainerResource extends ContainerResource { private static final String ROOT_ALBUM = "Transferred Photos"; private final Collection photos; private final Collection videos; - private final Collection albums; + private Collection albums; @JsonCreator public MediaContainerResource( @@ -66,7 +68,9 @@ public boolean equals(Object o) { } public void transmogrify(TransmogrificationConfig config) { - ensureRootAlbum(config.getAlbumAllowRootPhotos()); + if (!config.getAlbumAllowRootPhotos()) { + ensureRootAlbum(); + } transmogrifyTitles(config); // TODO(#1000): This splitting code isn't entirely correct since it assumes all the album items @@ -103,11 +107,8 @@ private void transmogrifyTitles(TransmogrificationConfig config) { } // Ensures that the model obeys the restrictions of the destination service, grouping all - // un-nested photos into their own root album if allowRootPhotos is true, noop otherwise - void ensureRootAlbum(boolean allowRootPhotos) { - if (allowRootPhotos) { - return; - } + // un-nested photos into their own root album + private void ensureRootAlbum() { MediaAlbum rootAlbum = new MediaAlbum( ROOT_ALBUM, ROOT_ALBUM, "A copy of your transferred media that were not in any album"); @@ -128,7 +129,9 @@ void ensureRootAlbum(boolean allowRootPhotos) { } if (usedRootAlbum) { - albums.add(rootAlbum); + List tempMutableAlbums = this.albums.stream().collect(Collectors.toList()); + tempMutableAlbums.add(rootAlbum); + this.albums = ImmutableList.copyOf(tempMutableAlbums); } } diff --git a/portability-types-common/src/test/java/org/datatransferproject/types/common/models/media/MediaContainerResourceTest.java b/portability-types-common/src/test/java/org/datatransferproject/types/common/models/media/MediaContainerResourceTest.java new file mode 100644 index 000000000..89b1ba819 --- /dev/null +++ b/portability-types-common/src/test/java/org/datatransferproject/types/common/models/media/MediaContainerResourceTest.java @@ -0,0 +1,307 @@ +package org.datatransferproject.types.common.models.media; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterables; +import com.google.common.truth.Truth; +import java.util.List; +import java.util.stream.Collectors; +import org.datatransferproject.types.common.models.ContainerResource; +import org.datatransferproject.types.common.models.TransmogrificationConfig; +import org.datatransferproject.types.common.models.photos.PhotoModel; +import org.junit.Ignore; +import org.junit.Test; + +// TODO(#1060) this code was ported over without unit tests; below is a mostly 1:1-port +// backfill but the new video handling logic in MediaContainerResource should have test coverage +// too. +public class MediaContainerResourceTest { + @Test + public void verifySerializeDeserialize() throws Exception { + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.registerSubtypes(MediaContainerResource.class); + + List albums = + ImmutableList.of(new MediaAlbum("id1", "albumb1", "This is a fake albumb")); + + List photos = ImmutableList.of( + new PhotoModel("Pic1", "http://fake.com/1.jpg", "A pic", "image/jpg", "p1", "id1", false), + new PhotoModel( + "Pic2", "https://fake.com/pic.png", "fine art", "image/png", "p2", "id1", false)); + + ContainerResource data = new MediaContainerResource(albums, photos, null /*video*/); + + String serialized = objectMapper.writeValueAsString(data); + + ContainerResource deserializedModel = + objectMapper.readValue(serialized, ContainerResource.class); + + Truth.assertThat(deserializedModel).isNotNull(); + Truth.assertThat(deserializedModel).isInstanceOf(MediaContainerResource.class); + MediaContainerResource deserialized = (MediaContainerResource) deserializedModel; + Truth.assertThat(deserialized.getAlbums()).hasSize(1); + Truth.assertThat(deserialized.getPhotos()).hasSize(2); + Truth.assertThat(deserialized).isEqualTo(data); + } + + @Test + public void verifyTransmogrifyAlbums_nullName() throws Exception { + TransmogrificationConfig config = new TransmogrificationConfig() { + public int getAlbumMaxSize() { + return 2; + } + }; + List albums = ImmutableList.of(new MediaAlbum("id1", null, "This is a fake album")); + + List photos = ImmutableList.of( + new PhotoModel("Pic1", "http://fake.com/1.jpg", "A pic", "image/jpg", "p1", "id1", false)); + + MediaContainerResource data = new MediaContainerResource(albums, photos, null /*video*/); + data.transmogrify(config); + Truth.assertThat(Iterables.get(data.getAlbums(), 0).getName()).isEqualTo(null); + } + + @Test + public void verifyTransmogrifyAlbums_NoLimit() throws Exception { + TransmogrificationConfig config = new TransmogrificationConfig(); + List albums = + ImmutableList.of(new MediaAlbum("id1", "albumb1", "This is a fake album")); + + List photos = ImmutableList.of( + new PhotoModel("Pic1", "http://fake.com/1.jpg", "A pic", "image/jpg", "p1", "id1", false), + new PhotoModel("Pic3", "http://fake.com/2.jpg", "A pic", "image/jpg", "p3", "id1", false), + new PhotoModel( + "Pic2", "https://fake.com/pic.png", "fine art", "image/png", "p2", "id1", false)); + + MediaContainerResource data = new MediaContainerResource(albums, photos, null /*video*/); + data.transmogrify(config); + Truth.assertThat(data.getAlbums()).hasSize(1); + Truth.assertThat(data.getPhotos()).hasSize(3); + } + + @Test + public void verifyTransmogrifyAlbums_NoRootPhotos() throws Exception { + TransmogrificationConfig config = new TransmogrificationConfig() { + public boolean getAlbumAllowRootPhotos() { + return false; + } + }; + List albums = + ImmutableList.of(new MediaAlbum("id1", "albumb1", "This is a fake album")); + + List photos = ImmutableList.of( + new PhotoModel("Pic1", "http://fake.com/1.jpg", "A pic", "image/jpg", "p1", "id1", false), + new PhotoModel("Pic3", "http://fake.com/2.jpg", "A pic", "image/jpg", "p3", "id1", false), + new PhotoModel( + "Pic2", "https://fake.com/pic.png", "fine art", "image/png", "p2", null, false)); + + MediaContainerResource data = new MediaContainerResource(albums, photos, null /*video*/); + data.transmogrify(config); + Truth.assertThat(data.getAlbums()).hasSize(2); + Truth.assertThat(data.getPhotos()).hasSize(3); + } + + @Test + public void verifyTransmogrifyAlbums_NameForbiddenCharacters() throws Exception { + TransmogrificationConfig config = new TransmogrificationConfig() { + public String getAlbumNameForbiddenCharacters() { + return ":!"; + } + public char getAlbumNameReplacementCharacter() { + return '?'; + } + }; + List albums = + ImmutableList.of(new MediaAlbum("id1", "This:a fake album!", "This:a fake album!")); + List photos = ImmutableList.of( + new PhotoModel("Pic1", "http://fake.com/1.jpg", "A pic", "image/jpg", "p1", "id1", false), + new PhotoModel("Pic3", "http://fake.com/2.jpg", "A pic", "image/jpg", "p3", "id1", false), + new PhotoModel( + "Pic2", "https://fake.com/pic2.png", "fine art", "image/png", "p2", null, false), + new PhotoModel( + "Pic5", "https://fake.com/pic5.png", "fine art", "image/png", "p5", null, false), + new PhotoModel( + "Pic6", "https://fake.com/pic6.png", "fine art", "image/png", "p6", null, false)); + + MediaContainerResource data = new MediaContainerResource(albums, photos, null /*video*/); + data.transmogrify(config); + Truth.assertThat(Iterables.get(data.getAlbums(), 0).getName()).isEqualTo("This?a fake album?"); + } + + @Test + public void verifyTransmogrifyAlbums_NameNoForbiddenCharacters() throws Exception { + TransmogrificationConfig config = new TransmogrificationConfig(); + List albums = + ImmutableList.of(new MediaAlbum("id1", "This:a fake album!", "This:a fake album!")); + + List photos = ImmutableList.of( + new PhotoModel("Pic1", "http://fake.com/1.jpg", "A pic", "image/jpg", "p1", "id1", false), + new PhotoModel("Pic3", "http://fake.com/2.jpg", "A pic", "image/jpg", "p3", "id1", false), + new PhotoModel( + "Pic2", "https://fake.com/pic.png", "fine art", "image/png", "p2", null, false)); + + MediaContainerResource data = new MediaContainerResource(albums, photos, null /*video*/); + data.transmogrify(config); + Truth.assertThat(Iterables.get(data.getAlbums(), 0).getName()).isEqualTo("This:a fake album!"); + } + + @Test + public void verifyTransmogrifyAlbums_stripName() throws Exception { + TransmogrificationConfig config = new TransmogrificationConfig(); + List albums = + ImmutableList.of(new MediaAlbum("id1", "This:a fake album! ", "This:a fake album!")); + + List photos = ImmutableList.of( + new PhotoModel("Pic1", "http://fake.com/1.jpg", "A pic", "image/jpg", "p1", "id1", false), + new PhotoModel("Pic3", "http://fake.com/2.jpg", "A pic", "image/jpg", "p3", "id1", false), + new PhotoModel( + "Pic2", "https://fake.com/pic.png", "fine art", "image/png", "p2", null, false)); + + MediaContainerResource data = new MediaContainerResource(albums, photos, null /*video*/); + data.transmogrify(config); + Truth.assertThat(Iterables.get(data.getAlbums(), 0).getName()).isEqualTo("This:a fake album!"); + } + + @Test + public void verifyTransmogrifyAlbums_NameTooLong() throws Exception { + TransmogrificationConfig config = new TransmogrificationConfig() { + public int getAlbumNameMaxLength() { + return 5; + } + }; + List albums = + ImmutableList.of(new MediaAlbum("id1", "This:a fake album!", "This:a fake album!")); + + List photos = ImmutableList.of( + new PhotoModel("Pic1", "http://fake.com/1.jpg", "A pic", "image/jpg", "p1", "id1", false), + new PhotoModel("Pic3", "http://fake.com/2.jpg", "A pic", "image/jpg", "p3", "id1", false), + new PhotoModel( + "Pic2", "https://fake.com/pic.png", "fine art", "image/png", "p2", null, false)); + + MediaContainerResource data = new MediaContainerResource(albums, photos, null /*video*/); + data.transmogrify(config); + Truth.assertThat(Iterables.get(data.getAlbums(), 0).getName()).hasLength(5); + } + + @Test + public void verifyTransmogrifyAlbums_NameNoLengthLimit() throws Exception { + TransmogrificationConfig config = new TransmogrificationConfig(); + List albums = + ImmutableList.of(new MediaAlbum("id1", "albumb1", "This:a fake album!")); + + List photos = ImmutableList.of( + new PhotoModel("Pic1", "http://fake.com/1.jpg", "A pic", "image/jpg", "p1", "id1", false), + new PhotoModel("Pic3", "http://fake.com/2.jpg", "A pic", "image/jpg", "p3", "id1", false), + new PhotoModel( + "Pic2", "https://fake.com/pic.png", "fine art", "image/png", "p2", null, false)); + + MediaContainerResource data = new MediaContainerResource(albums, photos, null /*video*/); + data.transmogrify(config); + Truth.assertThat(Iterables.get(data.getAlbums(), 0).getName()).hasLength(7); + } + + @Test + public void verifyTransmogrifyPhotos_TitleForbiddenCharacters() throws Exception { + TransmogrificationConfig config = new TransmogrificationConfig() { + public String getPhotoTitleForbiddenCharacters() { + return ":!"; + } + + public char getPhotoTitleReplacementCharacter() { + return '?'; + } + }; + List albums = + ImmutableList.of(new MediaAlbum("id1", "albumb1", "This:a fake album!")); + + List photos = ImmutableList.of( + new PhotoModel("Pic1!", "http://fake.com/1.jpg", "A pic", "image/jpg", "p1", "id1", false), + new PhotoModel("Pic:3", "http://fake.com/2.jpg", "A pic", "image/jpg", "p3", "id1", false), + new PhotoModel( + "Pic2", "https://fake.com/pic.png", "fine art", "image/png", "p2", null, false)); + + MediaContainerResource data = new MediaContainerResource(albums, photos, null /*video*/); + data.transmogrify(config); + Truth.assertThat(Iterables.get(data.getPhotos(), 0).getTitle()).isEqualTo("Pic1?"); + Truth.assertThat(Iterables.get(data.getPhotos(), 1).getTitle()).isEqualTo("Pic?3"); + Truth.assertThat(Iterables.get(data.getPhotos(), 2).getTitle()).isEqualTo("Pic2"); + } + + @Test + public void verifyTransmogrifyPhotos_TitleNoForbiddenCharacters() throws Exception { + TransmogrificationConfig config = new TransmogrificationConfig(); + List albums = + ImmutableList.of(new MediaAlbum("id1", "albumb1", "This:a fake album!")); + + List photos = ImmutableList.of( + new PhotoModel("Pic?1", "http://fake.com/1.jpg", "A pic", "image/jpg", "p1", "id1", false), + new PhotoModel("Pic:3", "http://fake.com/2.jpg", "A pic", "image/jpg", "p3", "id1", false), + new PhotoModel( + "Pic2", "https://fake.com/pic.png", "fine art", "image/png", "p2", null, false)); + + MediaContainerResource data = new MediaContainerResource(albums, photos, null /*video*/); + data.transmogrify(config); + Truth.assertThat(Iterables.get(data.getPhotos(), 0).getTitle()).isEqualTo("Pic?1"); + Truth.assertThat(Iterables.get(data.getPhotos(), 1).getTitle()).isEqualTo("Pic:3"); + Truth.assertThat(Iterables.get(data.getPhotos(), 2).getTitle()).isEqualTo("Pic2"); + } + + @Test + public void verifyTransmogrifyPhotos_TitleTooLong() throws Exception { + TransmogrificationConfig config = new TransmogrificationConfig() { + public int getPhotoTitleMaxLength() { + return 3; + } + }; + List albums = + ImmutableList.of(new MediaAlbum("id1", "albumb1", "This:a fake album!")); + + List photos = ImmutableList.of( + new PhotoModel("Pic1", "http://fake.com/1.jpg", "A pic", "image/jpg", "p1", "id1", false), + new PhotoModel("Pic3", "http://fake.com/2.jpg", "A pic", "image/jpg", "p3", "id1", false), + new PhotoModel( + "P2", "https://fake.com/pic.png", "fine art", "image/png", "p2", null, false)); + + MediaContainerResource data = new MediaContainerResource(albums, photos, null /*video*/); + data.transmogrify(config); + Truth.assertThat(Iterables.get(data.getPhotos(), 0).getTitle()).hasLength(3); + Truth.assertThat(Iterables.get(data.getPhotos(), 1).getTitle()).hasLength(3); + Truth.assertThat(Iterables.get(data.getPhotos(), 2).getTitle()).isEqualTo("P2"); + } + + @Test + public void verifyTransmogrifyPhotos_TitleNoLengthLimit() throws Exception { + TransmogrificationConfig config = new TransmogrificationConfig(); + List albums = + ImmutableList.of(new MediaAlbum("id1", "albumb1", "This:a fake album!")); + + List photos = ImmutableList.of( + new PhotoModel("Pic1", "http://fake.com/1.jpg", "A pic", "image/jpg", "p1", "id1", false), + new PhotoModel("Pic3", "http://fake.com/2.jpg", "A pic", "image/jpg", "p3", "id1", false), + new PhotoModel( + "Pic2", "https://fake.com/pic.png", "fine art", "image/png", "p2", null, false)); + + MediaContainerResource data = new MediaContainerResource(albums, photos, null /*video*/); + data.transmogrify(config); + Truth.assertThat(Iterables.get(data.getPhotos(), 0).getTitle()).hasLength(4); + Truth.assertThat(Iterables.get(data.getPhotos(), 1).getTitle()).hasLength(4); + Truth.assertThat(Iterables.get(data.getPhotos(), 2).getTitle()).hasLength(4); + } + + @Test + public void verifyTransmogrifyPhotos_stripTitle() throws Exception { + TransmogrificationConfig config = new TransmogrificationConfig(); + List albums = + ImmutableList.of(new MediaAlbum("id1", "albumb1", "This:a fake album!")); + + List photos = ImmutableList.of( + new PhotoModel("Pic1 ", "http://fake.com/1.jpg", "A pic", "image/jpg", "p1", "id1", false), + new PhotoModel("Pic3 ", "http://fake.com/2.jpg", "A pic", "image/jpg", "p3", "id1", false)); + + MediaContainerResource data = new MediaContainerResource(albums, photos, null /*video*/); + data.transmogrify(config); + Truth.assertThat(Iterables.get(data.getPhotos(), 0).getTitle()).isEqualTo("Pic1"); + Truth.assertThat(Iterables.get(data.getPhotos(), 1).getTitle()).isEqualTo("Pic3"); + + } +}