Skip to content

Commit

Permalink
MET-6219 Fix embeddable web resources to properly populate technical …
Browse files Browse the repository at this point in the history
…metadata
  • Loading branch information
stzanakis committed Oct 18, 2024
1 parent c214957 commit 0ed4720
Show file tree
Hide file tree
Showing 2 changed files with 241 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<String> 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<WebResourceType, WebResourceMetaInfoImpl> metaDataCreator =
switch (mediaType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 0ed4720

Please sign in to comment.