Skip to content

Commit

Permalink
MET-5805 add condition to hasEmbeddableMedia
Browse files Browse the repository at this point in the history
  • Loading branch information
jeortizquan committed Aug 7, 2024
1 parent b5c7b9c commit a1a0723
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -69,6 +70,9 @@ final class EmbeddableMedia {
.get()
.collect(Collectors.toList());

private static final String OEMBED_XML = "application/xml+oembed";
private static final String OEMBED_JSON = "application/json+oembed";

// Create patterns from the urls, quote url, wildcards are allowed in the pattern, so we do not quote those,
// and we also add a wildcard at the end of each url
private static final Collection<Pattern> PATTERNS = URL_MATCHING_LIST.stream()
Expand Down Expand Up @@ -97,7 +101,29 @@ private EmbeddableMedia() {
*/
static boolean hasEmbeddableMedia(RdfWrapper entity) {
return entity.getUrlsOfTypes(EnumSet.of(WebResourceLinkType.IS_SHOWN_BY)).stream()
.anyMatch(EmbeddableMedia::isEmbeddableMedia);
.anyMatch(EmbeddableMedia::isEmbeddableMedia)
|| isOEmbeddableMedia(entity);
}

/**
* Is an OEmbeddable media .
*
* @param entity the entity
* @return true, if the mimetype has application/json+oembed or application/xml+oembed
*/
static boolean isOEmbeddableMedia(RdfWrapper entity) {
return entity.getWebResources()
.stream()
.filter(Objects::nonNull)
.anyMatch(webResourceType ->
webResourceType.getHasMimeType()
.getHasMimeType()
.startsWith(OEMBED_XML)
|| webResourceType.getHasMimeType()
.getHasMimeType()
.startsWith(OEMBED_JSON)
);

}

private static boolean isEmbeddableMedia(String url) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import static org.mockito.Mockito.when;

import eu.europeana.indexing.utils.RdfWrapper;
import eu.europeana.metis.schema.jibx.HasMimeType;
import eu.europeana.metis.schema.jibx.WebResourceType;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -17,51 +20,73 @@
*/
class EmbeddableMediaTest {

private static WebResourceType getResource(String mimeType) {
WebResourceType webResourceType = new WebResourceType();
HasMimeType hasMimeTypeXml = new HasMimeType();
hasMimeTypeXml.setHasMimeType(mimeType);
webResourceType.setHasMimeType(hasMimeTypeXml);

return webResourceType;
}

private static Stream<Arguments> embeddableMedia() {
return Stream.of(
Arguments.of("http://sounds.bl.uk/embed/", true, List.of()),

Arguments.of("http://eusounds.ait.co.at/player/", true, List.of()),
Arguments.of("http://www.dismarc.org/player/", true, List.of()),

Arguments.of("http://api.picturepipe.net/api/html/widgets/public/playout_cloudfront?token=", true, List.of()),
Arguments.of("http://archives.crem-cnrs.fr/archives/items/", true, List.of()),
Arguments.of("http://www.ccma.cat/tv3/alacarta/programa/titol/video/", true, List.of()),
Arguments.of("http://www.ina.fr/*/video/", true, List.of()),
Arguments.of("http://www.ina.fr/video/", true, List.of()),
Arguments.of("http://www.theeuropeanlibrary.org/tel4/newspapers/issue/fullscreen/", true, List.of()),
Arguments.of("https://api.picturepipe.net/api/html/widgets/public/playout_cloudfront?token=", true, List.of()),

Arguments.of("http://www.euscreen.eu/item.html", true, List.of()),
Arguments.of("https://www.euscreen.eu/item.html*", true, List.of()),

Arguments.of("https://sketchfab.com/3d-models", true, List.of()),
Arguments.of("https://sketchfab.com/models/", true, List.of()),
Arguments.of("https://skfb.ly/", true, List.of()),

Arguments.of("http://soundcloud.com/", true, List.of()),
Arguments.of("https://soundcloud.com/", true, List.of()),

Arguments.of("http://player.vimeo.com/video/", true, List.of()),
Arguments.of("http://vimeo.com/", true, List.of()),
Arguments.of("https://player.vimeo.com/video/", true, List.of()),
Arguments.of("https://vimeo.com/", true, List.of()),

Arguments.of("https://*.youtube.com/v/", true, List.of()),
Arguments.of("https://*.youtube.com/watch", true, List.of()),
Arguments.of("https://youtu.be/", true, List.of()),
Arguments.of("https://www.google.com", false, List.of()),
Arguments.of("https://get.webgl.org/", false, List.of()),
Arguments.of("https://getemoji.com/", false, List.of()),
Arguments.of("https://www.cssfontstack.com/", false, List.of()),
Arguments.of("https://api64.ipify.org/?format=json", false, List.of()),

Arguments.of("https://oembed.com/api/oembed.xml?url=https%3A%2F%2Fvimeo.com%2F24416915", true,
List.of(getResource("application/xml+oembed"))),
Arguments.of("https://oembed.com/api/oembed.json?url=https%3A%2F%2Fvimeo.com%2F24416915", true,
List.of(getResource("application/json+oembed"))),
Arguments.of("https://oembed.com/api/oembed?url=https%3A%2F%2Fvimeo.com%2F24416915", false,
List.of(getResource("image/jpeg"))),
Arguments.of("https://oembed.com/api/oembed?url=https%3A%2F%2Fvimeo.com%2F24416915", false,
List.of(getResource("video/mp4")))
);
}

@ParameterizedTest
@MethodSource("embeddableMedia")
void hasEmbeddableMedia(String url, boolean expectedEmbeddable) {
void hasEmbeddableMedia(String url, boolean expectedEmbeddable, List<WebResourceType> resourceTypeList) {

final RdfWrapper entity = mock(RdfWrapper.class);
when(entity.getUrlsOfTypes(any())).thenReturn(Set.of(url));
assertEquals(expectedEmbeddable, EmbeddableMedia.hasEmbeddableMedia(entity));
}
when(entity.getWebResources()).thenReturn(resourceTypeList);

private static Stream<Arguments> embeddableMedia() {
return Stream.of(Arguments.of("http://sounds.bl.uk/embed/", true),

Arguments.of("http://eusounds.ait.co.at/player/", true),
Arguments.of("http://www.dismarc.org/player/", true),

Arguments.of("http://api.picturepipe.net/api/html/widgets/public/playout_cloudfront?token=", true),
Arguments.of("http://archives.crem-cnrs.fr/archives/items/", true),
Arguments.of("http://www.ccma.cat/tv3/alacarta/programa/titol/video/", true),
Arguments.of("http://www.ina.fr/*/video/", true),
Arguments.of("http://www.ina.fr/video/", true),
Arguments.of("http://www.theeuropeanlibrary.org/tel4/newspapers/issue/fullscreen/", true),
Arguments.of("https://api.picturepipe.net/api/html/widgets/public/playout_cloudfront?token=", true),

Arguments.of("http://www.euscreen.eu/item.html", true),
Arguments.of("https://www.euscreen.eu/item.html*", true),

Arguments.of("https://sketchfab.com/3d-models", true),
Arguments.of("https://sketchfab.com/models/", true),
Arguments.of("https://skfb.ly/", true),

Arguments.of("http://soundcloud.com/", true),
Arguments.of("https://soundcloud.com/", true),

Arguments.of("http://player.vimeo.com/video/", true),
Arguments.of("http://vimeo.com/", true),
Arguments.of("https://player.vimeo.com/video/", true),
Arguments.of("https://vimeo.com/", true),

Arguments.of("https://*.youtube.com/v/", true),
Arguments.of("https://*.youtube.com/watch", true),
Arguments.of("https://youtu.be/", true),

Arguments.of("https://www.google.com", false),
Arguments.of("https://get.webgl.org/", false),
Arguments.of("https://getemoji.com/", false),
Arguments.of("https://www.cssfontstack.com/", false),
Arguments.of("https://api64.ipify.org/?format=json", false));
assertEquals(expectedEmbeddable, EmbeddableMedia.hasEmbeddableMedia(entity));
}
}

0 comments on commit a1a0723

Please sign in to comment.