From d2cf2c5a7439376aff260efe01ab0e7222b343ba Mon Sep 17 00:00:00 2001 From: Stefan Seifert Date: Fri, 16 Aug 2024 17:09:36 +0200 Subject: [PATCH] refactor unit tests --- .../media/testcontext/DummyMediaFormats.java | 5 + .../ngdm/NextGenDynamicMediaTest.java | 369 ------------------ ...namicMedia_LocalAssetWithMetadataTest.java | 147 +++++++ ...micMedia_RemoteAssetWithMetadataTest.java} | 106 ++++- ...cMedia_RemoteAssetWithoutMetadataTest.java | 165 ++++++++ 5 files changed, 418 insertions(+), 374 deletions(-) delete mode 100644 src/test/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMediaTest.java create mode 100644 src/test/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMedia_LocalAssetWithMetadataTest.java rename src/test/java/io/wcm/handler/mediasource/ngdm/{NextGenDynamicMediaWithMetadataTest.java => NextGenDynamicMedia_RemoteAssetWithMetadataTest.java} (66%) create mode 100644 src/test/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMedia_RemoteAssetWithoutMetadataTest.java diff --git a/src/test/java/io/wcm/handler/media/testcontext/DummyMediaFormats.java b/src/test/java/io/wcm/handler/media/testcontext/DummyMediaFormats.java index a92576ed..39b49461 100644 --- a/src/test/java/io/wcm/handler/media/testcontext/DummyMediaFormats.java +++ b/src/test/java/io/wcm/handler/media/testcontext/DummyMediaFormats.java @@ -503,6 +503,11 @@ private DummyMediaFormats() { .ratio(16, 10) .extensions("gif", "jpg", "png") .build(); + public static final MediaFormat RATIO_16_9 = create("ratio_16_9") + .label("Ratio (16:9)") + .ratio(16, 9) + .extensions("gif", "jpg", "png") + .build(); public static final MediaFormat RATIO_4_3 = create("ratio_4_3") .label("Ratio (4:3)") .ratio(4, 3) diff --git a/src/test/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMediaTest.java b/src/test/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMediaTest.java deleted file mode 100644 index e6480d7c..00000000 --- a/src/test/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMediaTest.java +++ /dev/null @@ -1,369 +0,0 @@ -/* - * #%L - * wcm.io - * %% - * Copyright (C) 2024 wcm.io - * %% - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * #L% - */ -package io.wcm.handler.mediasource.ngdm; - -import static com.day.cq.dam.api.DamConstants.ASSET_STATUS_APPROVED; -import static com.day.cq.dam.api.DamConstants.ASSET_STATUS_PROPERTY; -import static io.wcm.handler.mediasource.ngdm.impl.NextGenDynamicMediaReferenceSample.SAMPLE_ASSET_ID; -import static io.wcm.handler.mediasource.ngdm.impl.NextGenDynamicMediaReferenceSample.SAMPLE_FILENAME; -import static io.wcm.handler.mediasource.ngdm.impl.NextGenDynamicMediaReferenceSample.SAMPLE_REFERENCE; -import static io.wcm.handler.mediasource.ngdm.impl.NextGenDynamicMediaReferenceSample.SAMPLE_UUID; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import org.apache.sling.api.resource.ModifiableValueMap; -import org.apache.sling.api.resource.Resource; -import org.apache.sling.api.resource.ValueMap; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; - -import com.day.cq.commons.jcr.JcrConstants; - -import io.wcm.handler.media.Asset; -import io.wcm.handler.media.Media; -import io.wcm.handler.media.MediaArgs; -import io.wcm.handler.media.MediaHandler; -import io.wcm.handler.media.MediaInvalidReason; -import io.wcm.handler.media.MediaNameConstants; -import io.wcm.handler.media.Rendition; -import io.wcm.handler.media.UriTemplate; -import io.wcm.handler.media.UriTemplateType; -import io.wcm.handler.media.testcontext.AppAemContext; -import io.wcm.handler.media.testcontext.DummyMediaFormats; -import io.wcm.handler.mediasource.ngdm.impl.NextGenDynamicMediaConfigServiceImpl; -import io.wcm.sling.commons.adapter.AdaptTo; -import io.wcm.testing.mock.aem.dam.ngdm.MockNextGenDynamicMediaConfig; -import io.wcm.testing.mock.aem.junit5.AemContext; -import io.wcm.testing.mock.aem.junit5.AemContextExtension; -import io.wcm.wcm.commons.contenttype.ContentType; - -@ExtendWith(AemContextExtension.class) -class NextGenDynamicMediaTest { - - private final AemContext context = AppAemContext.newAemContext(); - - private Resource resource; - - @BeforeEach - @SuppressWarnings("null") - void setUp() { - resource = context.create().resource(context.currentPage(), "test", - MediaNameConstants.PN_MEDIA_REF, SAMPLE_REFERENCE); - } - - @Test - void testAsset() { - setupNGDM(false); - MediaHandler mediaHandler = AdaptTo.notNull(context.request(), MediaHandler.class); - - Media media = mediaHandler.get(resource) - .build(); - assertTrue(media.isValid()); - assertUrl(media, "preferwebp=true&quality=85", "jpg"); - - Asset asset = media.getAsset(); - assertNotNull(asset); - assertEquals(SAMPLE_FILENAME, asset.getTitle()); - assertNull(asset.getAltText()); - assertNull(asset.getDescription()); - assertEquals(SAMPLE_REFERENCE, asset.getPath()); - assertEquals(ValueMap.EMPTY, asset.getProperties()); - assertNull(asset.adaptTo(Resource.class)); - - assertUrl(asset.getDefaultRendition(), "preferwebp=true&quality=85", "jpg"); - - // default rendition - Rendition defaultRendition = asset.getDefaultRendition(); - assertNotNull(defaultRendition); - assertEquals(ContentType.JPEG, defaultRendition.getMimeType()); - assertEquals(0, defaultRendition.getWidth()); - assertEquals(0, defaultRendition.getHeight()); - assertUrl(defaultRendition, "preferwebp=true&quality=85", "jpg"); - - // fixed rendition - Rendition fixedRendition = asset.getRendition(new MediaArgs().fixedDimension(100, 50)); - assertNotNull(fixedRendition); - assertEquals(ContentType.JPEG, fixedRendition.getMimeType()); - assertUrl(fixedRendition, "crop=100%3A50%2Csmart&preferwebp=true&quality=85&width=100", "jpg"); - - assertNotNull(asset.getImageRendition(new MediaArgs())); - assertNull(asset.getDownloadRendition(new MediaArgs().download(true))); - - UriTemplate uriTemplate = asset.getUriTemplate(UriTemplateType.SCALE_WIDTH); - assertUriTemplate(uriTemplate, "preferwebp=true&quality=85&width={width}", "jpg"); - } - - @Test - void testRendition_16_10() { - setupNGDM(false); - MediaHandler mediaHandler = AdaptTo.notNull(context.request(), MediaHandler.class); - - Media media = mediaHandler.get(resource) - .mediaFormat(DummyMediaFormats.RATIO_16_10) - .fixedWidth(2048) - .build(); - assertTrue(media.isValid()); - - Rendition rendition = media.getRendition(); - assertNotNull(rendition); - assertUrl(rendition, "crop=16%3A10%2Csmart&preferwebp=true&quality=85&width=2048", "jpg"); - - assertNull(rendition.getPath()); - assertEquals(SAMPLE_FILENAME, rendition.getFileName()); - assertEquals("jpg", rendition.getFileExtension()); - assertEquals(-1, rendition.getFileSize()); - assertEquals(ContentType.JPEG, rendition.getMimeType()); - assertEquals(DummyMediaFormats.RATIO_16_10, rendition.getMediaFormat()); - assertEquals(ValueMap.EMPTY, rendition.getProperties()); - assertTrue(rendition.isImage()); - assertTrue(rendition.isBrowserImage()); - assertFalse(rendition.isVectorImage()); - assertFalse(rendition.isDownload()); - assertEquals(2048, rendition.getWidth()); - assertEquals(1280, rendition.getHeight()); - assertNull(rendition.getModificationDate()); - assertFalse(rendition.isFallback()); - assertNull(rendition.adaptTo(Resource.class)); - assertNotNull(rendition.toString()); - - UriTemplate uriTemplate = rendition.getUriTemplate(UriTemplateType.SCALE_WIDTH); - assertUriTemplate(uriTemplate, "crop=16%3A10%2Csmart&preferwebp=true&quality=85&width={width}", "jpg"); - } - - @Test - void testRendition_16_10_PNG() { - setupNGDM(false); - MediaHandler mediaHandler = AdaptTo.notNull(context.request(), MediaHandler.class); - - Media media = mediaHandler.get(resource) - .mediaFormat(DummyMediaFormats.RATIO_16_10) - .enforceOutputFileExtension("png") - .build(); - assertTrue(media.isValid()); - - Rendition rendition = media.getRendition(); - assertNotNull(rendition); - assertUrl(rendition, "crop=16%3A10%2Csmart&preferwebp=true&quality=85", "png"); - - assertEquals("png", rendition.getFileExtension()); - } - - @Test - void testRendition_FixedDimension() { - setupNGDM(false); - MediaHandler mediaHandler = AdaptTo.notNull(context.request(), MediaHandler.class); - - Media media = mediaHandler.get(resource) - .fixedDimension(100, 50) - .build(); - assertTrue(media.isValid()); - - Rendition rendition = media.getRendition(); - assertNotNull(rendition); - assertUrl(rendition, "crop=100%3A50%2Csmart&preferwebp=true&quality=85&width=100", "jpg"); - } - - @Test - void testRendition_FixedMediaFormat() { - setupNGDM(false); - MediaHandler mediaHandler = AdaptTo.notNull(context.request(), MediaHandler.class); - - Media media = mediaHandler.get(resource) - .mediaFormat(DummyMediaFormats.EDITORIAL_1COL) - .build(); - assertTrue(media.isValid()); - - Rendition rendition = media.getRendition(); - assertNotNull(rendition); - assertUrl(rendition, "crop=210784%3A100000%2Csmart&preferwebp=true&quality=85&width=215", "jpg"); - } - - @Test - void testRendition_NonFixedSmallMediaFormat() { - setupNGDM(false); - MediaHandler mediaHandler = AdaptTo.notNull(context.request(), MediaHandler.class); - - Media media = mediaHandler.get(resource) - .mediaFormat(DummyMediaFormats.NONFIXED_SMALL) - .build(); - assertTrue(media.isValid()); - - Rendition rendition = media.getRendition(); - assertNotNull(rendition); - assertUrl(rendition, "preferwebp=true&quality=85&width=164", "jpg"); - } - - @Test - void testRendition_NonFixedMinWidthHeightMediaFormat() { - setupNGDM(false); - MediaHandler mediaHandler = AdaptTo.notNull(context.request(), MediaHandler.class); - - Media media = mediaHandler.get(resource) - .mediaFormat(DummyMediaFormats.NONFIXED_MINWIDTHHEIGHT) - .build(); - assertTrue(media.isValid()); - - Rendition rendition = media.getRendition(); - assertNotNull(rendition); - assertUrl(rendition, "preferwebp=true&quality=85", "jpg"); - } - - @Test - @SuppressWarnings("null") - void testPDFDownload() { - setupNGDM(false); - MediaHandler mediaHandler = AdaptTo.notNull(context.request(), MediaHandler.class); - - Resource downloadResource = context.create().resource(context.currentPage(), "download", - MediaNameConstants.PN_MEDIA_REF, "/" + SAMPLE_ASSET_ID + "/myfile.pdf"); - - Media media = mediaHandler.get(downloadResource) - .args(new MediaArgs().download(true)) - .build(); - assertTrue(media.isValid()); - - Rendition rendition = media.getRendition(); - assertNotNull(rendition); - assertEquals(ContentType.PDF, rendition.getMimeType()); - assertEquals("https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/original/as/myfile.pdf", rendition.getUrl()); - } - - @Test - void testImageDownload() { - setupNGDM(false); - MediaHandler mediaHandler = AdaptTo.notNull(context.request(), MediaHandler.class); - - Media media = mediaHandler.get(resource) - .args(new MediaArgs().download(true)) - .build(); - assertTrue(media.isValid()); - - Rendition rendition = media.getRendition(); - assertNotNull(rendition); - assertEquals(ContentType.JPEG, rendition.getMimeType()); - assertEquals("https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/original/as/my-image.jpg", rendition.getUrl()); - } - - @Test - @SuppressWarnings("null") - void testLocalAsset() { - setupNGDM(true); - MediaHandler mediaHandler = AdaptTo.notNull(context.request(), MediaHandler.class); - - com.day.cq.dam.api.Asset asset = context.create().asset("/content/dam/my-image.jpg", 20, 10, ContentType.JPEG, - ASSET_STATUS_PROPERTY, ASSET_STATUS_APPROVED); - ModifiableValueMap props = AdaptTo.notNull(asset, ModifiableValueMap.class); - props.put(JcrConstants.JCR_UUID, SAMPLE_UUID); - - resource = context.create().resource(context.currentPage(), "local-asset", - MediaNameConstants.PN_MEDIA_REF, asset.getPath()); - - Media media = mediaHandler.get(resource) - .build(); - assertTrue(media.isValid()); - assertUrl(media, "preferwebp=true&quality=85", "jpg"); - - // validate URI template - Rendition rendition = media.getRendition(); - UriTemplate uriTemplate = rendition.getUriTemplate(UriTemplateType.SCALE_WIDTH); - assertEquals("https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/my-image.jpg?preferwebp=true&quality=85&width={width}", - uriTemplate.getUriTemplate()); - assertEquals(UriTemplateType.SCALE_WIDTH, uriTemplate.getType()); - assertEquals(20, uriTemplate.getMaxWidth()); - assertEquals(10, uriTemplate.getMaxHeight()); - } - - @Test - @SuppressWarnings("null") - void testLocalAsset_NotApproved() { - setupNGDM(true); - MediaHandler mediaHandler = AdaptTo.notNull(context.request(), MediaHandler.class); - - com.day.cq.dam.api.Asset asset = context.create().asset("/content/dam/my-image.jpg", 10, 10, ContentType.JPEG); - ModifiableValueMap props = AdaptTo.notNull(asset, ModifiableValueMap.class); - props.put(JcrConstants.JCR_UUID, SAMPLE_UUID); - - resource = context.create().resource(context.currentPage(), "local-asset", - MediaNameConstants.PN_MEDIA_REF, asset.getPath()); - - Media media = mediaHandler.get(resource) - .build(); - assertFalse(media.isValid()); - assertEquals(MediaInvalidReason.NOT_APPROVED, media.getMediaInvalidReason()); - } - - @Test - @SuppressWarnings("null") - void testLocalAsset_NoUUID() { - setupNGDM(true); - MediaHandler mediaHandler = AdaptTo.notNull(context.request(), MediaHandler.class); - - com.day.cq.dam.api.Asset asset = context.create().asset("/content/dam/my-image.jpg", 10, 10, ContentType.JPEG, - ASSET_STATUS_PROPERTY, ASSET_STATUS_APPROVED); - - resource = context.create().resource(context.currentPage(), "local-asset", - MediaNameConstants.PN_MEDIA_REF, asset.getPath()); - - Media media = mediaHandler.get(resource) - .build(); - assertFalse(media.isValid()); - assertEquals(MediaInvalidReason.MEDIA_REFERENCE_INVALID, media.getMediaInvalidReason()); - } - - private static void assertUrl(Media media, String urlParams, String extension) { - assertEquals(buildUrl(urlParams, extension), media.getUrl()); - } - - private static void assertUrl(Rendition rendition, String urlParams, String extension) { - assertEquals(buildUrl(urlParams, extension), rendition.getUrl()); - } - - private static void assertUriTemplate(UriTemplate uriTemplate, String urlParams, String extension) { - assertEquals(buildUrl(urlParams, extension), uriTemplate.getUriTemplate()); - assertEquals(UriTemplateType.SCALE_WIDTH, uriTemplate.getType()); - assertEquals(0, uriTemplate.getMaxWidth()); - assertEquals(0, uriTemplate.getMaxHeight()); - } - - private static String buildUrl(String urlParams, String extension) { - return "https://repo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/my-image." - + extension + "?" + urlParams; - } - - private void setupNGDM(boolean localAssets) { - MockNextGenDynamicMediaConfig nextGenDynamicMediaConfig = context.registerInjectActivateService(MockNextGenDynamicMediaConfig.class); - nextGenDynamicMediaConfig.setEnabled(true); - nextGenDynamicMediaConfig.setRepositoryId("repo1"); - - if (localAssets) { - context.registerInjectActivateService(NextGenDynamicMediaConfigServiceImpl.class, - "enabledLocalAssets", true, - "localAssetsRepositoryId", "repo1"); - } - else { - context.registerInjectActivateService(NextGenDynamicMediaConfigServiceImpl.class); - } - } - -} diff --git a/src/test/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMedia_LocalAssetWithMetadataTest.java b/src/test/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMedia_LocalAssetWithMetadataTest.java new file mode 100644 index 00000000..592cc327 --- /dev/null +++ b/src/test/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMedia_LocalAssetWithMetadataTest.java @@ -0,0 +1,147 @@ +/* + * #%L + * wcm.io + * %% + * Copyright (C) 2024 wcm.io + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package io.wcm.handler.mediasource.ngdm; + +import static com.day.cq.dam.api.DamConstants.ASSET_STATUS_APPROVED; +import static com.day.cq.dam.api.DamConstants.ASSET_STATUS_PROPERTY; +import static io.wcm.handler.mediasource.ngdm.impl.NextGenDynamicMediaReferenceSample.SAMPLE_ASSET_ID; +import static io.wcm.handler.mediasource.ngdm.impl.NextGenDynamicMediaReferenceSample.SAMPLE_REFERENCE; +import static io.wcm.handler.mediasource.ngdm.impl.NextGenDynamicMediaReferenceSample.SAMPLE_UUID; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.apache.sling.api.resource.ModifiableValueMap; +import org.apache.sling.api.resource.Resource; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +import com.day.cq.commons.jcr.JcrConstants; + +import io.wcm.handler.media.Media; +import io.wcm.handler.media.MediaHandler; +import io.wcm.handler.media.MediaInvalidReason; +import io.wcm.handler.media.MediaNameConstants; +import io.wcm.handler.media.Rendition; +import io.wcm.handler.media.UriTemplate; +import io.wcm.handler.media.UriTemplateType; +import io.wcm.handler.media.testcontext.AppAemContext; +import io.wcm.handler.mediasource.ngdm.impl.NextGenDynamicMediaConfigServiceImpl; +import io.wcm.sling.commons.adapter.AdaptTo; +import io.wcm.testing.mock.aem.junit5.AemContext; +import io.wcm.testing.mock.aem.junit5.AemContextExtension; +import io.wcm.wcm.commons.contenttype.ContentType; + +@ExtendWith(AemContextExtension.class) +class NextGenDynamicMedia_LocalAssetWithMetadataTest { + + private final AemContext context = AppAemContext.newAemContext(); + + private MediaHandler mediaHandler; + private Resource resource; + + @BeforeEach + @SuppressWarnings("null") + void setUp() { + context.registerInjectActivateService(NextGenDynamicMediaConfigServiceImpl.class, + "enabledLocalAssets", true, + "localAssetsRepositoryId", "repo1"); + + resource = context.create().resource(context.currentPage(), "test", + MediaNameConstants.PN_MEDIA_REF, SAMPLE_REFERENCE); + mediaHandler = AdaptTo.notNull(context.request(), MediaHandler.class); + } + + @Test + @SuppressWarnings("null") + void testLocalAsset() { + com.day.cq.dam.api.Asset asset = context.create().asset("/content/dam/my-image.jpg", 20, 10, ContentType.JPEG, + ASSET_STATUS_PROPERTY, ASSET_STATUS_APPROVED); + ModifiableValueMap props = AdaptTo.notNull(asset, ModifiableValueMap.class); + props.put(JcrConstants.JCR_UUID, SAMPLE_UUID); + + resource = context.create().resource(context.currentPage(), "local-asset", + MediaNameConstants.PN_MEDIA_REF, asset.getPath()); + + Media media = mediaHandler.get(resource) + .build(); + assertTrue(media.isValid()); + assertUrl(media, "preferwebp=true&quality=85", "jpg"); + + // validate URI template + Rendition rendition = media.getRendition(); + + UriTemplate uriTemplateScaleWidth = rendition.getUriTemplate(UriTemplateType.SCALE_WIDTH); + assertEquals("https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/my-image.jpg?preferwebp=true&quality=85&width={width}", + uriTemplateScaleWidth.getUriTemplate()); + assertEquals(UriTemplateType.SCALE_WIDTH, uriTemplateScaleWidth.getType()); + assertEquals(20, uriTemplateScaleWidth.getMaxWidth()); + assertEquals(10, uriTemplateScaleWidth.getMaxHeight()); + + UriTemplate uriTemplateScaleHeight = rendition.getUriTemplate(UriTemplateType.SCALE_HEIGHT); + assertEquals("https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/my-image.jpg?height={height}&preferwebp=true&quality=85", + uriTemplateScaleHeight.getUriTemplate()); + assertEquals(UriTemplateType.SCALE_HEIGHT, uriTemplateScaleHeight.getType()); + assertEquals(20, uriTemplateScaleHeight.getMaxWidth()); + assertEquals(10, uriTemplateScaleHeight.getMaxHeight()); + } + + @Test + @SuppressWarnings("null") + void testLocalAsset_NotApproved() { + com.day.cq.dam.api.Asset asset = context.create().asset("/content/dam/my-image.jpg", 10, 10, ContentType.JPEG); + ModifiableValueMap props = AdaptTo.notNull(asset, ModifiableValueMap.class); + props.put(JcrConstants.JCR_UUID, SAMPLE_UUID); + + resource = context.create().resource(context.currentPage(), "local-asset", + MediaNameConstants.PN_MEDIA_REF, asset.getPath()); + + Media media = mediaHandler.get(resource) + .build(); + assertFalse(media.isValid()); + assertEquals(MediaInvalidReason.NOT_APPROVED, media.getMediaInvalidReason()); + } + + @Test + @SuppressWarnings("null") + void testLocalAsset_NoUUID() { + com.day.cq.dam.api.Asset asset = context.create().asset("/content/dam/my-image.jpg", 10, 10, ContentType.JPEG, + ASSET_STATUS_PROPERTY, ASSET_STATUS_APPROVED); + + resource = context.create().resource(context.currentPage(), "local-asset", + MediaNameConstants.PN_MEDIA_REF, asset.getPath()); + + Media media = mediaHandler.get(resource) + .build(); + assertFalse(media.isValid()); + assertEquals(MediaInvalidReason.MEDIA_REFERENCE_INVALID, media.getMediaInvalidReason()); + } + + private static void assertUrl(Media media, String urlParams, String extension) { + assertEquals(buildUrl(urlParams, extension), media.getUrl()); + } + + private static String buildUrl(String urlParams, String extension) { + return "https://repo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/my-image." + + extension + "?" + urlParams; + } + +} diff --git a/src/test/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMediaWithMetadataTest.java b/src/test/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMedia_RemoteAssetWithMetadataTest.java similarity index 66% rename from src/test/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMediaWithMetadataTest.java rename to src/test/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMedia_RemoteAssetWithMetadataTest.java index 55377e6b..24003538 100644 --- a/src/test/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMediaWithMetadataTest.java +++ b/src/test/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMedia_RemoteAssetWithMetadataTest.java @@ -28,6 +28,7 @@ import static io.wcm.handler.mediasource.ngdm.impl.metadata.MetadataSample.METADATA_JSON_IMAGE; import static io.wcm.handler.mediasource.ngdm.impl.metadata.MetadataSample.METADATA_JSON_PDF; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -49,6 +50,7 @@ import io.wcm.handler.media.MediaNameConstants; import io.wcm.handler.media.Rendition; import io.wcm.handler.media.testcontext.AppAemContext; +import io.wcm.handler.media.testcontext.DummyMediaFormats; import io.wcm.handler.mediasource.ngdm.impl.NextGenDynamicMediaConfigServiceImpl; import io.wcm.handler.mediasource.ngdm.impl.metadata.NextGenDynamicMediaMetadataServiceImpl; import io.wcm.sling.commons.adapter.AdaptTo; @@ -59,7 +61,7 @@ @ExtendWith(AemContextExtension.class) @WireMockTest -class NextGenDynamicMediaWithMetadataTest { +class NextGenDynamicMedia_RemoteAssetWithMetadataTest { private final AemContext context = AppAemContext.newAemContext(); @@ -81,16 +83,16 @@ void setUp(WireMockRuntimeInfo wmRuntimeInfo) { MediaNameConstants.PN_MEDIA_REF, SAMPLE_REFERENCE); mediaHandler = AdaptTo.notNull(context.request(), MediaHandler.class); - } - @Test - void testAsset() { stubFor(get("/adobe/assets/" + SAMPLE_ASSET_ID + "/metadata") .willReturn(aResponse() .withStatus(HttpStatus.SC_OK) .withHeader("Content-Type", ContentType.JSON) .withBody(METADATA_JSON_IMAGE))); + } + @Test + void testAsset() { Media media = mediaHandler.get(resource) .build(); assertTrue(media.isValid()); @@ -121,13 +123,107 @@ void testAsset() { assertEquals(ContentType.JPEG, fixedRendition.getMimeType()); assertEquals(100, fixedRendition.getWidth()); assertEquals(50, fixedRendition.getHeight()); - assertUrl(fixedRendition, "crop=100%3A50%2Csmart&preferwebp=true&quality=85&width=100", "jpg"); + assertUrl(fixedRendition, "crop=0%2C100%2C1200%2C600&preferwebp=true&quality=85&width=100", "jpg"); // avoid upscaling Rendition tooLargeRendition = asset.getRendition(new MediaArgs().fixedDimension(2048, 1024)); assertNull(tooLargeRendition); } + @Test + void testRendition_16_9() { + Media media = mediaHandler.get(resource) + .mediaFormat(DummyMediaFormats.RATIO_16_9) + .fixedWidth(1024) + .build(); + assertTrue(media.isValid()); + + Rendition rendition = media.getRendition(); + assertNotNull(rendition); + assertUrl(rendition, "preferwebp=true&quality=85&smartcrop=Landscape&width=1024", "jpg"); + + assertNull(rendition.getPath()); + assertEquals(SAMPLE_FILENAME, rendition.getFileName()); + assertEquals("jpg", rendition.getFileExtension()); + assertEquals(-1, rendition.getFileSize()); + assertEquals(ContentType.JPEG, rendition.getMimeType()); + assertEquals(DummyMediaFormats.RATIO_16_9, rendition.getMediaFormat()); + assertEquals(ValueMap.EMPTY, rendition.getProperties()); + assertTrue(rendition.isImage()); + assertTrue(rendition.isBrowserImage()); + assertFalse(rendition.isVectorImage()); + assertFalse(rendition.isDownload()); + assertEquals(1024, rendition.getWidth()); + assertEquals(576, rendition.getHeight()); + assertNull(rendition.getModificationDate()); + assertFalse(rendition.isFallback()); + assertNull(rendition.adaptTo(Resource.class)); + assertNotNull(rendition.toString()); + } + + @Test + void testRendition_16_9_PNG() { + Media media = mediaHandler.get(resource) + .mediaFormat(DummyMediaFormats.RATIO_16_9) + .enforceOutputFileExtension("png") + .build(); + assertTrue(media.isValid()); + + Rendition rendition = media.getRendition(); + assertNotNull(rendition); + assertUrl(rendition, "preferwebp=true&quality=85&smartcrop=Landscape&width=2048", "png"); + + assertEquals("png", rendition.getFileExtension()); + } + + @Test + void testRendition_FixedDimension() { + Media media = mediaHandler.get(resource) + .fixedDimension(100, 50) + .build(); + assertTrue(media.isValid()); + + Rendition rendition = media.getRendition(); + assertNotNull(rendition); + assertUrl(rendition, "crop=0%2C100%2C1200%2C600&preferwebp=true&quality=85&width=100", "jpg"); + } + + @Test + void testRendition_FixedMediaFormat() { + Media media = mediaHandler.get(resource) + .mediaFormat(DummyMediaFormats.EDITORIAL_1COL) + .build(); + assertTrue(media.isValid()); + + Rendition rendition = media.getRendition(); + assertNotNull(rendition); + assertUrl(rendition, "crop=0%2C116%2C1200%2C569&preferwebp=true&quality=85&width=215", "jpg"); + } + + @Test + void testRendition_NonFixedSmallMediaFormat() { + Media media = mediaHandler.get(resource) + .mediaFormat(DummyMediaFormats.NONFIXED_SMALL) + .build(); + assertTrue(media.isValid()); + + Rendition rendition = media.getRendition(); + assertNotNull(rendition); + assertUrl(rendition, "height=20&preferwebp=true&quality=85&width=164", "jpg"); + } + + @Test + void testRendition_NonFixedMinWidthHeightMediaFormat() { + Media media = mediaHandler.get(resource) + .mediaFormat(DummyMediaFormats.NONFIXED_MINWIDTHHEIGHT) + .build(); + assertTrue(media.isValid()); + + Rendition rendition = media.getRendition(); + assertNotNull(rendition); + assertUrl(rendition, "preferwebp=true&quality=85", "jpg"); + } + @Test @SuppressWarnings("null") void testPDFDownload() { diff --git a/src/test/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMedia_RemoteAssetWithoutMetadataTest.java b/src/test/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMedia_RemoteAssetWithoutMetadataTest.java new file mode 100644 index 00000000..c8d0e2b2 --- /dev/null +++ b/src/test/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMedia_RemoteAssetWithoutMetadataTest.java @@ -0,0 +1,165 @@ +/* + * #%L + * wcm.io + * %% + * Copyright (C) 2024 wcm.io + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package io.wcm.handler.mediasource.ngdm; + +import static io.wcm.handler.mediasource.ngdm.impl.NextGenDynamicMediaReferenceSample.SAMPLE_ASSET_ID; +import static io.wcm.handler.mediasource.ngdm.impl.NextGenDynamicMediaReferenceSample.SAMPLE_FILENAME; +import static io.wcm.handler.mediasource.ngdm.impl.NextGenDynamicMediaReferenceSample.SAMPLE_REFERENCE; +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 static org.junit.jupiter.api.Assertions.assertTrue; + +import org.apache.sling.api.resource.Resource; +import org.apache.sling.api.resource.ValueMap; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +import io.wcm.handler.media.Asset; +import io.wcm.handler.media.Media; +import io.wcm.handler.media.MediaArgs; +import io.wcm.handler.media.MediaHandler; +import io.wcm.handler.media.MediaNameConstants; +import io.wcm.handler.media.Rendition; +import io.wcm.handler.media.UriTemplate; +import io.wcm.handler.media.UriTemplateType; +import io.wcm.handler.media.testcontext.AppAemContext; +import io.wcm.handler.mediasource.ngdm.impl.NextGenDynamicMediaConfigServiceImpl; +import io.wcm.sling.commons.adapter.AdaptTo; +import io.wcm.testing.mock.aem.dam.ngdm.MockNextGenDynamicMediaConfig; +import io.wcm.testing.mock.aem.junit5.AemContext; +import io.wcm.testing.mock.aem.junit5.AemContextExtension; +import io.wcm.wcm.commons.contenttype.ContentType; + +@ExtendWith(AemContextExtension.class) +class NextGenDynamicMedia_RemoteAssetWithoutMetadataTest { + + private final AemContext context = AppAemContext.newAemContext(); + + private MediaHandler mediaHandler; + private Resource resource; + + @BeforeEach + @SuppressWarnings("null") + void setUp() { + MockNextGenDynamicMediaConfig nextGenDynamicMediaConfig = context.registerInjectActivateService(MockNextGenDynamicMediaConfig.class); + nextGenDynamicMediaConfig.setEnabled(true); + nextGenDynamicMediaConfig.setRepositoryId("repo1"); + + context.registerInjectActivateService(NextGenDynamicMediaConfigServiceImpl.class); + + resource = context.create().resource(context.currentPage(), "test", + MediaNameConstants.PN_MEDIA_REF, SAMPLE_REFERENCE); + mediaHandler = AdaptTo.notNull(context.request(), MediaHandler.class); + } + + @Test + void testAsset() { + Media media = mediaHandler.get(resource) + .build(); + assertTrue(media.isValid()); + assertUrl(media, "preferwebp=true&quality=85", "jpg"); + + Asset asset = media.getAsset(); + assertNotNull(asset); + assertEquals(SAMPLE_FILENAME, asset.getTitle()); + assertNull(asset.getAltText()); + assertNull(asset.getDescription()); + assertEquals(SAMPLE_REFERENCE, asset.getPath()); + assertEquals(ValueMap.EMPTY, asset.getProperties()); + assertNull(asset.adaptTo(Resource.class)); + + assertUrl(asset.getDefaultRendition(), "preferwebp=true&quality=85", "jpg"); + + // default rendition + Rendition defaultRendition = asset.getDefaultRendition(); + assertNotNull(defaultRendition); + assertEquals(ContentType.JPEG, defaultRendition.getMimeType()); + assertEquals(0, defaultRendition.getWidth()); + assertEquals(0, defaultRendition.getHeight()); + assertUrl(defaultRendition, "preferwebp=true&quality=85", "jpg"); + + // fixed rendition + Rendition fixedRendition = asset.getRendition(new MediaArgs().fixedDimension(100, 50)); + assertNotNull(fixedRendition); + assertEquals(ContentType.JPEG, fixedRendition.getMimeType()); + assertUrl(fixedRendition, "height=50&preferwebp=true&quality=85&width=100", "jpg"); + + assertNotNull(asset.getImageRendition(new MediaArgs())); + assertNull(asset.getDownloadRendition(new MediaArgs().download(true))); + + assertUriTemplate(asset.getUriTemplate(UriTemplateType.SCALE_WIDTH), + "preferwebp=true&quality=85&width={width}", "jpg"); + assertUriTemplate(fixedRendition.getUriTemplate(UriTemplateType.SCALE_WIDTH), + "preferwebp=true&quality=85&width={width}", "jpg"); + } + + @Test + @SuppressWarnings("null") + void testPDFDownload() { + Resource downloadResource = context.create().resource(context.currentPage(), "download", + MediaNameConstants.PN_MEDIA_REF, "/" + SAMPLE_ASSET_ID + "/myfile.pdf"); + + Media media = mediaHandler.get(downloadResource) + .args(new MediaArgs().download(true)) + .build(); + assertTrue(media.isValid()); + + Rendition rendition = media.getRendition(); + assertNotNull(rendition); + assertEquals(ContentType.PDF, rendition.getMimeType()); + assertEquals("https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/original/as/myfile.pdf", rendition.getUrl()); + } + + @Test + void testImageDownload() { + Media media = mediaHandler.get(resource) + .args(new MediaArgs().download(true)) + .build(); + assertTrue(media.isValid()); + + Rendition rendition = media.getRendition(); + assertNotNull(rendition); + assertEquals(ContentType.JPEG, rendition.getMimeType()); + assertEquals("https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/original/as/my-image.jpg", rendition.getUrl()); + } + + private static void assertUrl(Media media, String urlParams, String extension) { + assertEquals(buildUrl(urlParams, extension), media.getUrl()); + } + + private static void assertUrl(Rendition rendition, String urlParams, String extension) { + assertEquals(buildUrl(urlParams, extension), rendition.getUrl()); + } + + private static void assertUriTemplate(UriTemplate uriTemplate, String urlParams, String extension) { + assertEquals(buildUrl(urlParams, extension), uriTemplate.getUriTemplate()); + assertEquals(UriTemplateType.SCALE_WIDTH, uriTemplate.getType()); + assertEquals(0, uriTemplate.getMaxWidth()); + assertEquals(0, uriTemplate.getMaxHeight()); + } + + private static String buildUrl(String urlParams, String extension) { + return "https://repo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/my-image." + + extension + "?" + urlParams; + } + +}