Skip to content

Commit

Permalink
implement addRendition with map in MockAsset and check only for mime …
Browse files Browse the repository at this point in the history
…type property (which is mandatory)

delegate implementation of Granite Asset.setRendition to this method
  • Loading branch information
stefanseifert committed Aug 20, 2024
1 parent fc4172f commit 7e76056
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 22 deletions.
13 changes: 9 additions & 4 deletions core/src/main/java/io/wcm/testing/mock/aem/dam/MockAsset.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Object> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,8 @@ public String getIdentifier() {
}

@Override
public Rendition setRendition(String s, InputStream inputStream, Map<String, Object> 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<String, Object> map) {
return (Rendition)asset.addRendition(s, binary, map);
public Rendition setRendition(String name, InputStream inputStream, Map<String, Object> map) {
return (Rendition)asset.addRendition(name, inputStream, map);
}

@Override
Expand Down Expand Up @@ -133,6 +122,11 @@ public AssetMetadata getAssetMetadata() {
throw new UnsupportedOperationException();
}

@Override
public Rendition setRendition(String s, Binary binary, Map<String, Object> map) {
throw new UnsupportedOperationException();
}

@Override
public AssetRelation addRelation(String s, String s1, Map<String, Object> map) {
throw new UnsupportedOperationException();
Expand Down
29 changes: 27 additions & 2 deletions core/src/test/java/io/wcm/testing/mock/aem/dam/MockAssetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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<String, Object> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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[] {
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 7e76056

Please sign in to comment.