From 0ed4720923430cc9dec641e2c4942ff52b341c23 Mon Sep 17 00:00:00 2001 From: Simon Tzanakis Date: Fri, 18 Oct 2024 14:32:27 +0200 Subject: [PATCH] MET-6219 Fix embeddable web resources to properly populate technical metadata --- .../fullbean/WebResourceFieldInput.java | 26 ++- .../fullbean/WebResourceFieldInputTest.java | 219 ++++++++++++++++++ 2 files changed, 241 insertions(+), 4 deletions(-) create mode 100644 metis-indexing/src/test/java/eu/europeana/indexing/fullbean/WebResourceFieldInputTest.java diff --git a/metis-indexing/src/main/java/eu/europeana/indexing/fullbean/WebResourceFieldInput.java b/metis-indexing/src/main/java/eu/europeana/indexing/fullbean/WebResourceFieldInput.java index 2f4eeca4a..47a9a7976 100644 --- a/metis-indexing/src/main/java/eu/europeana/indexing/fullbean/WebResourceFieldInput.java +++ b/metis-indexing/src/main/java/eu/europeana/indexing/fullbean/WebResourceFieldInput.java @@ -12,6 +12,7 @@ import eu.europeana.metis.schema.jibx.ColorSpaceType; import eu.europeana.metis.schema.jibx.DoubleType; import eu.europeana.metis.schema.jibx.Duration; +import eu.europeana.metis.schema.jibx.EdmType; import eu.europeana.metis.schema.jibx.HasColorSpace; import eu.europeana.metis.schema.jibx.HasMimeType; import eu.europeana.metis.schema.jibx.HexBinaryType; @@ -20,6 +21,7 @@ import eu.europeana.metis.schema.jibx.NonNegativeIntegerType; import eu.europeana.metis.schema.jibx.OrientationType; import eu.europeana.metis.schema.jibx.Type1; +import eu.europeana.metis.schema.jibx.Type2; import eu.europeana.metis.schema.jibx.WebResourceType; import eu.europeana.metis.schema.model.MediaType; import eu.europeana.metis.schema.model.Orientation; @@ -131,16 +133,32 @@ public WebResourceImpl apply(WebResourceType wResourceType) { webResource.setEdmPreview(wResourceType.getPreview().getResource()); } - webResource.setWebResourceMetaInfo(createWebresourceMetaInfo(wResourceType)); + webResource.setWebResourceMetaInfo(createWebResourceMetaInfo(wResourceType)); return webResource; } - private WebResourceMetaInfoImpl createWebresourceMetaInfo(WebResourceType webResource) { + private WebResourceMetaInfoImpl createWebResourceMetaInfo(WebResourceType webResource) { // Get the media type and determine meta data creator - final MediaType mediaType = Optional.ofNullable(webResource.getHasMimeType()) - .map(HasMimeType::getHasMimeType).map(MediaType::getMediaType).orElse(MediaType.OTHER); + final Optional optionalHasMimeType = Optional.ofNullable(webResource.getHasMimeType()) + .map(HasMimeType::getHasMimeType); + + final MediaType mediaType = optionalHasMimeType.map(hasMimeType -> { + MediaType adaptedMediaType = MediaType.getMediaType(hasMimeType); + final EdmType edmType = Optional.ofNullable(webResource.getType1()).map(Type2::getType).orElse(null); + final boolean isOembedMimeType = hasMimeType.startsWith("application/xml+oembed") || hasMimeType.startsWith("application/json+oembed"); + final boolean isPossibleOembedMediaType = adaptedMediaType == MediaType.TEXT || adaptedMediaType == MediaType.OTHER; + if (isPossibleOembedMediaType && edmType != null && isOembedMimeType) { + if (edmType == EdmType.IMAGE) { + adaptedMediaType = MediaType.IMAGE; + } else if (edmType == EdmType.VIDEO) { + adaptedMediaType = MediaType.VIDEO; + } + } + + return adaptedMediaType; + }).orElse(MediaType.OTHER); final BiConsumer metaDataCreator = switch (mediaType) { diff --git a/metis-indexing/src/test/java/eu/europeana/indexing/fullbean/WebResourceFieldInputTest.java b/metis-indexing/src/test/java/eu/europeana/indexing/fullbean/WebResourceFieldInputTest.java new file mode 100644 index 000000000..4eea72284 --- /dev/null +++ b/metis-indexing/src/test/java/eu/europeana/indexing/fullbean/WebResourceFieldInputTest.java @@ -0,0 +1,219 @@ +package eu.europeana.indexing.fullbean; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +import eu.europeana.corelib.solr.entity.WebResourceImpl; +import eu.europeana.metis.schema.jibx.BitRate; +import eu.europeana.metis.schema.jibx.CodecName; +import eu.europeana.metis.schema.jibx.ColorSpaceType; +import eu.europeana.metis.schema.jibx.DoubleType; +import eu.europeana.metis.schema.jibx.Duration; +import eu.europeana.metis.schema.jibx.EdmType; +import eu.europeana.metis.schema.jibx.HasColorSpace; +import eu.europeana.metis.schema.jibx.HasMimeType; +import eu.europeana.metis.schema.jibx.Height; +import eu.europeana.metis.schema.jibx.HexBinaryType; +import eu.europeana.metis.schema.jibx.LongType; +import eu.europeana.metis.schema.jibx.OrientationType; +import eu.europeana.metis.schema.jibx.Type2; +import eu.europeana.metis.schema.jibx.WebResourceType; +import eu.europeana.metis.schema.jibx.Width; +import eu.europeana.metis.schema.model.Orientation; +import java.math.BigInteger; +import java.util.List; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.Test; + +class WebResourceFieldInputTest { + + @Test + void testImageWebResource() { + final WebResourceType webResourceType = getWebResourceImage(); + WebResourceImpl webResourceImpl = new WebResourceFieldInput().apply(webResourceType); + + assertImageWebResourceImpl(webResourceType, webResourceImpl); + } + + @Test + void testOEmbedJsonImageWebResource() { + final WebResourceType webResourceType = getWebResourceImage(); + final HasMimeType hasMimeType = new HasMimeType(); + hasMimeType.setHasMimeType("application/json+oembed"); + webResourceType.setHasMimeType(hasMimeType); + WebResourceImpl webResourceImpl = new WebResourceFieldInput().apply(webResourceType); + + assertImageWebResourceImpl(webResourceType, webResourceImpl); + } + + @Test + void testOEmbedXmlImageWebResource() { + final WebResourceType webResourceType = getWebResourceImage(); + final HasMimeType hasMimeType = new HasMimeType(); + hasMimeType.setHasMimeType("application/xml+oembed"); + webResourceType.setHasMimeType(hasMimeType); + WebResourceImpl webResourceImpl = new WebResourceFieldInput().apply(webResourceType); + + assertImageWebResourceImpl(webResourceType, webResourceImpl); + } + + @Test + void testOEmbedJsonImageWebResource_Null_WebResourceMetaInfo() { + final WebResourceType webResourceType = getWebResourceImage(); + final HasMimeType hasMimeType = new HasMimeType(); + hasMimeType.setHasMimeType("application/json"); + webResourceType.setHasMimeType(hasMimeType); + WebResourceImpl webResourceImpl = new WebResourceFieldInput().apply(webResourceType); + + assertNull(webResourceImpl.getWebResourceMetaInfo()); + } + + @Test + void testOEmbedXmlImageWebResource_NotNull_WebResourceMetaInfo() { + final WebResourceType webResourceType = getWebResourceImage(); + final HasMimeType hasMimeType = new HasMimeType(); + hasMimeType.setHasMimeType("application/xml"); + webResourceType.setHasMimeType(hasMimeType); + WebResourceImpl webResourceImpl = new WebResourceFieldInput().apply(webResourceType); + + assertNotNull(webResourceImpl.getWebResourceMetaInfo().getTextMetaInfo()); + } + + @Test + void testVideoWebResource() { + final WebResourceType webResourceType = getWebResourceVideo(); + WebResourceImpl webResourceImpl = new WebResourceFieldInput().apply(webResourceType); + + assertVideoWebResourceImpl(webResourceType, webResourceImpl); + } + + @Test + void testOEmbedJsonVideoVideoWebResource() { + final WebResourceType webResourceType = getWebResourceVideo(); + final HasMimeType hasMimeType = new HasMimeType(); + hasMimeType.setHasMimeType("application/json+oembed"); + webResourceType.setHasMimeType(hasMimeType); + WebResourceImpl webResourceImpl = new WebResourceFieldInput().apply(webResourceType); + + assertVideoWebResourceImpl(webResourceType, webResourceImpl); + } + + @Test + void testOEmbedXmlVideoVideoWebResource() { + final WebResourceType webResourceType = getWebResourceVideo(); + final HasMimeType hasMimeType = new HasMimeType(); + hasMimeType.setHasMimeType("application/xml+oembed"); + webResourceType.setHasMimeType(hasMimeType); + WebResourceImpl webResourceImpl = new WebResourceFieldInput().apply(webResourceType); + + assertVideoWebResourceImpl(webResourceType, webResourceImpl); + } + + @Test + void testOEmbedJsonVideoWebResource_Null_WebResourceMetaInfo() { + final WebResourceType webResourceType = getWebResourceVideo(); + final HasMimeType hasMimeType = new HasMimeType(); + hasMimeType.setHasMimeType("application/json"); + webResourceType.setHasMimeType(hasMimeType); + WebResourceImpl webResourceImpl = new WebResourceFieldInput().apply(webResourceType); + + assertNull(webResourceImpl.getWebResourceMetaInfo()); + } + + @Test + void testOEmbedXmlVideoWebResource_NotNull_WebResourceMetaInfo() { + final WebResourceType webResourceType = getWebResourceVideo(); + final HasMimeType hasMimeType = new HasMimeType(); + hasMimeType.setHasMimeType("application/xml"); + webResourceType.setHasMimeType(hasMimeType); + WebResourceImpl webResourceImpl = new WebResourceFieldInput().apply(webResourceType); + + assertNotNull(webResourceImpl.getWebResourceMetaInfo().getTextMetaInfo()); + } + + private static void assertImageWebResourceImpl(WebResourceType webResourceType, WebResourceImpl webResourceImpl) { + assertEquals(webResourceType.getHasMimeType().getHasMimeType(), webResourceImpl.getWebResourceMetaInfo().getImageMetaInfo().getMimeType()); + assertEquals(webResourceType.getFileByteSize().getLong(), webResourceImpl.getWebResourceMetaInfo().getImageMetaInfo().getFileSize()); + assertEquals(Long.valueOf(webResourceType.getHeight().getLong()).intValue(), webResourceImpl.getWebResourceMetaInfo().getImageMetaInfo().getHeight()); + assertEquals(Long.valueOf(webResourceType.getWidth().getLong()).intValue(), webResourceImpl.getWebResourceMetaInfo().getImageMetaInfo().getWidth()); + assertEquals(webResourceType.getHasColorSpace().getHasColorSpace().xmlValue(), webResourceImpl.getWebResourceMetaInfo().getImageMetaInfo().getColorSpace()); + assertEquals(webResourceType.getOrientation().getString(), webResourceImpl.getWebResourceMetaInfo().getImageMetaInfo().getOrientation().name()); + assertArrayEquals(webResourceType.getComponentColorList().stream().map(HexBinaryType::getString).toList().toArray(), webResourceImpl.getWebResourceMetaInfo().getImageMetaInfo().getColorPalette()); + } + + private static void assertVideoWebResourceImpl(WebResourceType webResourceType, WebResourceImpl webResourceImpl) { + assertEquals(webResourceType.getHasMimeType().getHasMimeType(), webResourceImpl.getWebResourceMetaInfo().getVideoMetaInfo().getMimeType()); + assertEquals(webResourceType.getFileByteSize().getLong(), webResourceImpl.getWebResourceMetaInfo().getVideoMetaInfo().getFileSize()); + assertEquals(Long.valueOf(webResourceType.getHeight().getLong()).intValue(), webResourceImpl.getWebResourceMetaInfo().getVideoMetaInfo().getHeight()); + assertEquals(Long.valueOf(webResourceType.getWidth().getLong()).intValue(), webResourceImpl.getWebResourceMetaInfo().getVideoMetaInfo().getWidth()); + assertEquals(webResourceType.getBitRate().getInteger().intValue(), webResourceImpl.getWebResourceMetaInfo().getVideoMetaInfo().getBitRate()); + assertEquals(webResourceType.getCodecName().getCodecName(), webResourceImpl.getWebResourceMetaInfo().getVideoMetaInfo().getCodec()); + assertEquals(webResourceType.getFrameRate().getDouble(), webResourceImpl.getWebResourceMetaInfo().getVideoMetaInfo().getFrameRate()); + assertEquals(Long.valueOf(webResourceType.getDuration().getDuration()), webResourceImpl.getWebResourceMetaInfo().getVideoMetaInfo().getDuration()); + } + + private static @NotNull WebResourceType getWebResourceImage() { + WebResourceType webResourceType = new WebResourceType(); + final Type2 type2 = new Type2(); + type2.setType(EdmType.IMAGE); + webResourceType.setType1(type2); + final LongType longType = new LongType(); + longType.setLong(1000L); + webResourceType.setFileByteSize(longType); + final Height height = new Height(); + height.setLong(500L); + webResourceType.setHeight(height); + final Width width = new Width(); + width.setLong(500L); + webResourceType.setWidth(width); + final HasColorSpace hasColorSpace = new HasColorSpace(); + hasColorSpace.setHasColorSpace(ColorSpaceType.CMY); + webResourceType.setHasColorSpace(hasColorSpace); + final OrientationType orientationType = new OrientationType(); + orientationType.setString(Orientation.LANDSCAPE.name()); + webResourceType.setOrientation(orientationType); + final HexBinaryType hexBinaryType = new HexBinaryType(); + hexBinaryType.setString("#8FBC8F"); + webResourceType.setComponentColorList(List.of(hexBinaryType)); + + final HasMimeType hasMimeType = new HasMimeType(); + hasMimeType.setHasMimeType("image/jpeg"); + webResourceType.setHasMimeType(hasMimeType); + return webResourceType; + } + + private static @NotNull WebResourceType getWebResourceVideo() { + WebResourceType webResourceType = new WebResourceType(); + final Type2 type2 = new Type2(); + type2.setType(EdmType.VIDEO); + webResourceType.setType1(type2); + final LongType longType = new LongType(); + longType.setLong(1000L); + webResourceType.setFileByteSize(longType); + final Height height = new Height(); + height.setLong(500L); + webResourceType.setHeight(height); + final Width width = new Width(); + width.setLong(500L); + webResourceType.setWidth(width); + final BitRate bitRate = new BitRate(); + bitRate.setInteger(new BigInteger("1000")); + webResourceType.setBitRate(bitRate); + final CodecName codecName = new CodecName(); + codecName.setCodecName("codec"); + webResourceType.setCodecName(codecName); + final DoubleType doubleType = new DoubleType(); + doubleType.setDouble((double) 1000L); + webResourceType.setFrameRate(doubleType); + final Duration duration = new Duration(); + duration.setDuration("1000"); + webResourceType.setDuration(duration); + + final HasMimeType hasMimeType = new HasMimeType(); + hasMimeType.setHasMimeType("video/mpeg"); + webResourceType.setHasMimeType(hasMimeType); + return webResourceType; + } +}