diff --git a/changes.xml b/changes.xml index 546a477e..405816bb 100644 --- a/changes.xml +++ b/changes.xml @@ -24,6 +24,9 @@ + + Add support for Rendition.getBinary() method. + Update to latest OSGi Mock. diff --git a/core/src/main/java/io/wcm/testing/mock/aem/dam/MockRendition.java b/core/src/main/java/io/wcm/testing/mock/aem/dam/MockRendition.java index e357ba12..9168aa36 100644 --- a/core/src/main/java/io/wcm/testing/mock/aem/dam/MockRendition.java +++ b/core/src/main/java/io/wcm/testing/mock/aem/dam/MockRendition.java @@ -21,16 +21,22 @@ import java.io.IOException; import java.io.InputStream; +import java.net.URI; +import java.net.URISyntaxException; import javax.jcr.Binary; +import javax.jcr.RepositoryException; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.jackrabbit.api.binary.BinaryDownload; +import org.apache.jackrabbit.api.binary.BinaryDownloadOptions; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceUtil; import org.apache.sling.api.resource.ResourceWrapper; import org.apache.sling.api.resource.ValueMap; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import com.day.cq.commons.jcr.JcrConstants; import com.day.cq.dam.api.Asset; @@ -123,12 +129,54 @@ public boolean equals(Object obj) { return StringUtils.equals(getPath(), ((MockRendition)obj).getPath()); } - - // --- unsupported operations --- - @Override public Binary getBinary() { - throw new UnsupportedOperationException(); + return new MockBinary(this); + } + + + private static class MockBinary implements BinaryDownload { + + private Rendition rendition; + + MockBinary(Rendition rendition) { + this.rendition = rendition; + } + + + @Override + public InputStream getStream() throws RepositoryException { + return rendition.getStream(); + } + + @Override + public int read(byte[] b, long position) throws IOException, RepositoryException { + throw new UnsupportedOperationException(); + } + + @Override + public long getSize() throws RepositoryException { + return rendition.getSize(); + } + + @Override + public void dispose() { + // nothing to do + } + + @Override + public @Nullable URI getURI(BinaryDownloadOptions downloadOptions) throws RepositoryException { + final String path = "https://blostore.local/blostore/" + rendition.getPath(); + try { + return new URI(path); + } + catch (URISyntaxException e) { + // nothing + } + return null; + } + } + } diff --git a/core/src/test/java/io/wcm/testing/mock/aem/dam/MockRenditionTest.java b/core/src/test/java/io/wcm/testing/mock/aem/dam/MockRenditionTest.java index 82f712c3..1bddb77e 100644 --- a/core/src/test/java/io/wcm/testing/mock/aem/dam/MockRenditionTest.java +++ b/core/src/test/java/io/wcm/testing/mock/aem/dam/MockRenditionTest.java @@ -23,7 +23,12 @@ import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import javax.jcr.Binary; +import javax.jcr.RepositoryException; + +import org.apache.jackrabbit.api.binary.BinaryDownload; import org.apache.sling.api.resource.Resource; import org.junit.Before; import org.junit.Rule; @@ -95,4 +100,18 @@ public void testAdaptTo() { assertSame(rendition, rendition.adaptTo(com.adobe.granite.asset.api.Rendition.class)); } + @Test + public void testBinaryDownload() throws RepositoryException { + Binary binary = rendition.getBinary(); + assertNotNull(binary); + assertEquals(0L, binary.getSize()); + assertNotNull(binary.getStream()); + + assertTrue(binary instanceof BinaryDownload); + assertEquals("https://blostore.local/blostore//content/dam/sample/portraits/scott_reynolds.jpg/jcr:content/renditions/original", + ((BinaryDownload)binary).getURI(null).toString()); + + binary.dispose(); + } + }