From 7e76056c6b64e0232a352740505ddfaea3d2d238 Mon Sep 17 00:00:00 2001 From: Stefan Seifert Date: Tue, 20 Aug 2024 10:20:55 +0200 Subject: [PATCH] implement addRendition with map in MockAsset and check only for mime type property (which is mandatory) delegate implementation of Granite Asset.setRendition to this method --- .../wcm/testing/mock/aem/dam/MockAsset.java | 13 ++++++--- .../mock/aem/dam/MockGraniteAssetWrapper.java | 20 +++++-------- .../testing/mock/aem/dam/MockAssetTest.java | 29 +++++++++++++++++-- .../aem/dam/MockGraniteAssetWrapperTest.java | 12 ++++++-- 4 files changed, 52 insertions(+), 22 deletions(-) diff --git a/core/src/main/java/io/wcm/testing/mock/aem/dam/MockAsset.java b/core/src/main/java/io/wcm/testing/mock/aem/dam/MockAsset.java index 010583bf..27196963 100644 --- a/core/src/main/java/io/wcm/testing/mock/aem/dam/MockAsset.java +++ b/core/src/main/java/io/wcm/testing/mock/aem/dam/MockAsset.java @@ -43,6 +43,7 @@ import org.osgi.framework.BundleContext; import org.osgi.service.event.EventAdmin; +import com.adobe.granite.asset.api.RenditionHandler; import com.day.cq.commons.jcr.JcrConstants; import com.day.cq.dam.api.Asset; import com.day.cq.dam.api.DamConstants; @@ -246,14 +247,18 @@ public String getID() { return resource.getValueMap().get(JcrConstants.JCR_UUID, ""); } - - // --- unsupported operations --- - @Override public Rendition addRendition(String name, InputStream is, Map map) { - throw new UnsupportedOperationException(); + Object mimeTypeObject = map.get(RenditionHandler.PROPERTY_RENDITION_MIME_TYPE); + if (mimeTypeObject instanceof String) { + return addRendition(name, is, mimeTypeObject.toString()); + } + throw new UnsupportedOperationException("Mime type property missing in map: " + RenditionHandler.PROPERTY_RENDITION_MIME_TYPE); } + + // --- unsupported operations --- + @Override public Rendition getCurrentOriginal() { throw new UnsupportedOperationException(); diff --git a/core/src/main/java/io/wcm/testing/mock/aem/dam/MockGraniteAssetWrapper.java b/core/src/main/java/io/wcm/testing/mock/aem/dam/MockGraniteAssetWrapper.java index 930440c6..72ef99d9 100644 --- a/core/src/main/java/io/wcm/testing/mock/aem/dam/MockGraniteAssetWrapper.java +++ b/core/src/main/java/io/wcm/testing/mock/aem/dam/MockGraniteAssetWrapper.java @@ -82,19 +82,8 @@ public String getIdentifier() { } @Override - public Rendition setRendition(String s, InputStream inputStream, Map map) { - if (map.size() == 1) { - Object val = map.values().iterator().next(); - if (val != null) { - return (Rendition)asset.addRendition(s, inputStream, val.toString()); - } - } - return (Rendition)asset.addRendition(s, inputStream, map); - } - - @Override - public Rendition setRendition(String s, Binary binary, Map map) { - return (Rendition)asset.addRendition(s, binary, map); + public Rendition setRendition(String name, InputStream inputStream, Map map) { + return (Rendition)asset.addRendition(name, inputStream, map); } @Override @@ -133,6 +122,11 @@ public AssetMetadata getAssetMetadata() { throw new UnsupportedOperationException(); } + @Override + public Rendition setRendition(String s, Binary binary, Map map) { + throw new UnsupportedOperationException(); + } + @Override public AssetRelation addRelation(String s, String s1, Map map) { throw new UnsupportedOperationException(); diff --git a/core/src/test/java/io/wcm/testing/mock/aem/dam/MockAssetTest.java b/core/src/test/java/io/wcm/testing/mock/aem/dam/MockAssetTest.java index dd4a71d8..50a5299e 100644 --- a/core/src/test/java/io/wcm/testing/mock/aem/dam/MockAssetTest.java +++ b/core/src/test/java/io/wcm/testing/mock/aem/dam/MockAssetTest.java @@ -19,9 +19,19 @@ */ package io.wcm.testing.mock.aem.dam; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.List; +import java.util.Map; import java.util.Optional; import org.apache.commons.lang3.StringUtils; @@ -32,6 +42,7 @@ import org.junit.Test; import org.osgi.service.event.EventHandler; +import com.adobe.granite.asset.api.RenditionHandler; import com.day.cq.dam.api.Asset; import com.day.cq.dam.api.DamConstants; import com.day.cq.dam.api.DamEvent; @@ -43,8 +54,6 @@ import io.wcm.testing.mock.aem.dam.MockAssetManagerTest.DamEventHandler; import io.wcm.testing.mock.aem.junit.AemContext; -import static org.junit.Assert.*; - @SuppressWarnings("null") public class MockAssetTest { @@ -145,6 +154,22 @@ public void testAddRemoveRendition() { doTestAddRemoveRendition("test.bin"); } + @Test + public void testAddRenditionWithMap() { + InputStream is = new ByteArrayInputStream(BINARY_DATA); + Rendition rendition = asset.addRendition("rendition1", is, Map.of(RenditionHandler.PROPERTY_RENDITION_MIME_TYPE, "application/octet-stream")); + assertNotNull(rendition); + } + + @Test + public void testAddRenditionWithMapWithoutMimetype() { + InputStream is = new ByteArrayInputStream(BINARY_DATA); + Map emptyMap = Map.of(); + assertThrows(UnsupportedOperationException.class, () -> { + asset.addRendition("rendition1", is, emptyMap); + }); + } + @Test public void testRenditionListMutable() { // make sure rendition list is modifiable by calling getBestfitRendition which does a sort on it diff --git a/core/src/test/java/io/wcm/testing/mock/aem/dam/MockGraniteAssetWrapperTest.java b/core/src/test/java/io/wcm/testing/mock/aem/dam/MockGraniteAssetWrapperTest.java index 2a2ab09d..5437a578 100644 --- a/core/src/test/java/io/wcm/testing/mock/aem/dam/MockGraniteAssetWrapperTest.java +++ b/core/src/test/java/io/wcm/testing/mock/aem/dam/MockGraniteAssetWrapperTest.java @@ -19,6 +19,13 @@ */ package io.wcm.testing.mock.aem.dam; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.List; @@ -36,13 +43,12 @@ import com.adobe.granite.asset.api.Asset; import com.adobe.granite.asset.api.Rendition; +import com.adobe.granite.asset.api.RenditionHandler; import com.day.cq.dam.api.DamEvent; import io.wcm.testing.mock.aem.context.TestAemContext; import io.wcm.testing.mock.aem.junit.AemContext; -import static org.junit.Assert.*; - public class MockGraniteAssetWrapperTest { private static final byte[] BINARY_DATA = new byte[] { @@ -119,7 +125,7 @@ public void testEquals() { private void doTestAddRemoveRendition(final String renditionName) { InputStream is = new ByteArrayInputStream(BINARY_DATA); - Rendition rendition = asset.setRendition(renditionName, is, Map.of("jcr:mimeType", "application/octet-stream")); + Rendition rendition = asset.setRendition(renditionName, is, Map.of(RenditionHandler.PROPERTY_RENDITION_MIME_TYPE, "application/octet-stream")); assertNotNull(rendition); assertNotNull(asset.getRendition(renditionName));