Skip to content

Commit

Permalink
add support for Rendition.getBinary() (#50)
Browse files Browse the repository at this point in the history
Co-authored-by: Stefan Seifert <[email protected]>
  • Loading branch information
joerghoh and stefanseifert authored Sep 9, 2024
1 parent 7af2317 commit fc23802
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
3 changes: 3 additions & 0 deletions changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<body>

<release version="5.6.2" date="not released">
<action type="update" dev="joerghoh" issue="50">
Add support for Rendition.getBinary() method.
</action>
<action type="update" dev="sseifert">
Update to latest OSGi Mock.
</action>
Expand Down
56 changes: 52 additions & 4 deletions core/src/main/java/io/wcm/testing/mock/aem/dam/MockRendition.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

}

0 comments on commit fc23802

Please sign in to comment.