Skip to content

Commit

Permalink
fix unittests and impl
Browse files Browse the repository at this point in the history
  • Loading branch information
joerghoh committed Sep 29, 2024
1 parent bfb850d commit fa7189b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ private void streamOrConvert(@NotNull SlingHttpServletResponse response, @NotNul
LOGGER.warn("Found rendition {}/{} has a width of {}px and does not require a resize for requested width of {}px " +
"but the rendition is not of the requested type {}, cannot convert so serving as is",
rendition.getAsset().getPath(), rendition.getName(), dimension != null ? dimension.getWidth() : null, resizeWidth, imageType);
deliverRendition(response, rendition.getRendition(), imageType, imageName);
deliverRendition(response, rendition.getRendition(), rendition.getMimeType(), imageName);
} else {
LOGGER.debug("Found rendition {}/{} has a width of {}px and does not require a resize for requested width of {}px " +
"but the rendition is not of the requested type {}, need to convert",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -31,6 +33,7 @@
import javax.imageio.ImageIO;
import javax.imageio.spi.IIORegistry;
import javax.imageio.spi.ImageReaderSpi;
import javax.jcr.RepositoryException;
import javax.servlet.http.HttpServletResponse;

import ch.qos.logback.classic.Level;
Expand All @@ -39,6 +42,8 @@
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.http.HttpStatus;
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.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
Expand All @@ -49,6 +54,7 @@
import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest;
import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse;
import org.apache.sling.testing.resourceresolver.MockValueMap;
import org.jetbrains.annotations.Nullable;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -234,13 +240,7 @@ void testRequestWithWidthDesignAllowedRectangleLarge_viaRedirect() throws Except
"allowedRenditionWidths", new String[] {"500", "1000", "1500", "3000"},
"jpegQuality", 82);
servlet.doGet(request, response);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(response.getOutput());
BufferedImage image = ImageIO.read(byteArrayInputStream);
// Expects original
Dimension expectedDimension = new Dimension(2500, 2000);
Dimension actualDimension = new Dimension(image.getWidth(), image.getHeight());
Assertions.assertEquals(expectedDimension, actualDimension, "Expected image rendered at requested size.");
Assertions.assertEquals("image/png", response.getContentType(), "Expected a PNG image.");
assertValidRedirectToBlobStore(response, "/content/dam/core/images/Adobe_Systems_logo_and_wordmark_rectangle.png/jcr:content/renditions/original");
}

@Test
Expand Down Expand Up @@ -311,13 +311,7 @@ void testRequestWithWidthDesignAllowedSmallImageWithLargeRenditionLarge_viaRedir
"allowedRenditionWidths", new String[] {"100", "500", "1500"},
"jpegQuality", 82);
servlet.doGet(request, response);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(response.getOutput());
BufferedImage image = ImageIO.read(byteArrayInputStream);
// Expects no upscaling of web rendition
Dimension expectedDimension = new Dimension(192, 192);
Dimension actualDimension = new Dimension(image.getWidth(), image.getHeight());
Assertions.assertEquals(expectedDimension, actualDimension, "Expected image rendered at requested size.");
Assertions.assertEquals("image/png", response.getContentType(), "Expected a PNG image.");
assertValidRedirectToBlobStore(response, "/content/dam/core/images/Adobe_Systems_logo_and_wordmark_small.png/jcr:content/renditions/original");
}

@Test
Expand Down Expand Up @@ -366,12 +360,7 @@ void testRequestNoWidthWithDesign_viaRedirect() throws Exception {
MockSlingHttpServletRequest request = requestResponsePair.getLeft();
MockSlingHttpServletResponse response = spy(requestResponsePair.getRight());
servlet.doGet(request, response);
verify(response).setContentType("image/png");
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(response.getOutput());
BufferedImage image = ImageIO.read(byteArrayInputStream);
Dimension expectedDimension = new Dimension(ADAPTIVE_IMAGE_SERVLET_DEFAULT_RESIZE_WIDTH, ADAPTIVE_IMAGE_SERVLET_DEFAULT_RESIZE_WIDTH);
Dimension actualDimension = new Dimension(image.getWidth(), image.getHeight());
Assertions.assertEquals(expectedDimension, actualDimension, "Expected image rendered with the default resize configuration width.");
assertValidRedirectToBlobStore(response, "/content/dam/core/images/Adobe_Systems_logo_and_wordmark.png/jcr:content/renditions/cq5dam.web.1280.1280.png");
}


Expand Down Expand Up @@ -399,13 +388,7 @@ void testRequestNoWidthNoDesign_viaRedirect() throws Exception {
MockSlingHttpServletRequest request = requestResponsePair.getLeft();
MockSlingHttpServletResponse response = spy(requestResponsePair.getRight());
servlet.doGet(request, response);
verify(response).setContentType("image/png");
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(response.getOutput());
BufferedImage image = ImageIO.read(byteArrayInputStream);
Dimension expectedDimension =
new Dimension(ADAPTIVE_IMAGE_SERVLET_DEFAULT_RESIZE_WIDTH, ADAPTIVE_IMAGE_SERVLET_DEFAULT_RESIZE_WIDTH);
Dimension actualDimension = new Dimension(image.getWidth(), image.getHeight());
Assertions.assertEquals(expectedDimension, actualDimension, "Expected image rendered with the default resize configuration width.");
assertValidRedirectToBlobStore(response, "/content/dam/core/images/Adobe_Systems_logo_and_wordmark.png/jcr:content/renditions/cq5dam.web.1280.1280.png");
}

@Test
Expand Down Expand Up @@ -584,11 +567,7 @@ void testSVGUploadedToDAM_viaRedirect() throws Exception {
MockSlingHttpServletRequest request = requestResponsePair.getLeft();
MockSlingHttpServletResponse response = requestResponsePair.getRight();
servlet.doGet(request, response);
Assertions.assertTrue(response.getHeader(HttpHeaders.CONTENT_DISPOSITION).startsWith("attachment"));
ByteArrayInputStream stream = new ByteArrayInputStream(response.getOutput());
InputStream directStream =
this.getClass().getClassLoader().getResourceAsStream("image/Adobe_Systems_logo_and_wordmark.svg");
Assertions.assertTrue(IOUtils.contentEquals(stream, directStream));
assertValidRedirectToBlobStore(response, "/content/dam/core/images/Adobe_Systems_logo_and_wordmark.svg/jcr:content/renditions/original");
}

@Test
Expand Down Expand Up @@ -626,10 +605,7 @@ void testCorrectScalingPNGAssetWidth_viaRedirect() throws Exception {
MockSlingHttpServletResponse response = requestResponsePair.getRight();
context.contentPolicyMapping(ImageImpl.RESOURCE_TYPE, "allowedRenditionWidths", new String[] {"2000"});
servlet.doGet(request, response);
InputStream directStream = this.getClass().getClassLoader().getResourceAsStream("image/Adobe_Systems_logo_and_wordmark.png");
ByteArrayInputStream stream = new ByteArrayInputStream(response.getOutput());
Assertions.assertTrue(IOUtils.contentEquals(directStream, stream),
"Expected to get the original asset back, since the requested width is equal to the image's width.");
assertValidRedirectToBlobStore(response, "/content/dam/core/images/Adobe_Systems_logo_and_wordmark.png/jcr:content/renditions/original");
}

@Test
Expand All @@ -655,10 +631,7 @@ void testDAMFileUpscaledPNG_viaRedirect() throws Exception {
MockSlingHttpServletResponse response = requestResponsePair.getRight();
context.contentPolicyMapping(ImageImpl.RESOURCE_TYPE, "allowedRenditionWidths", new String[] {"2500"});
servlet.doGet(request, response);
InputStream directStream = this.getClass().getClassLoader().getResourceAsStream("image/Adobe_Systems_logo_and_wordmark.png");
ByteArrayInputStream stream = new ByteArrayInputStream(response.getOutput());
Assertions.assertTrue(IOUtils.contentEquals(directStream, stream),
"Expected to get the original asset back, since the requested width would result in upscaling the image.");
assertValidRedirectToBlobStore(response, "/content/dam/core/images/Adobe_Systems_logo_and_wordmark.png/jcr:content/renditions/original");
}

@Test
Expand Down Expand Up @@ -712,7 +685,7 @@ void testWithNoImageNameFromTemplate_viaRedirect() throws IOException {

requestPathInfo.setSuffix(TEMPLATE_IMAGE_PATH.replace(TEMPLATE_PATH, "") + "/1490005239000.png");
servlet.doGet(request, response);
Assertions.assertEquals(200, response.getStatus(), "Expected a 200 response code.");
assertValidRedirectToBlobStore(response, "/content/dam/core/images/Adobe_Systems_logo_and_wordmark.png/jcr:content/renditions/cq5dam.web.1280.1280.png");
}

@Test
Expand Down Expand Up @@ -839,12 +812,7 @@ void testFileReferencePriority_viaRedirect() throws Exception {
MockSlingHttpServletRequest request = requestResponsePair.getLeft();
MockSlingHttpServletResponse response = requestResponsePair.getRight();
servlet.doGet(request, response);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(response.getOutput());
BufferedImage image = ImageIO.read(byteArrayInputStream);
Dimension expectedDimension = new Dimension(ADAPTIVE_IMAGE_SERVLET_DEFAULT_RESIZE_WIDTH, ADAPTIVE_IMAGE_SERVLET_DEFAULT_RESIZE_WIDTH);
Dimension actualDimension = new Dimension(image.getWidth(), image.getHeight());
Assertions.assertEquals(expectedDimension, actualDimension, "Expected image rendered at requested size.");
Assertions.assertEquals("image/png", response.getContentType(), "Expected a PNG image.");
assertValidRedirectToBlobStore(response, "/content/dam/core/images/Adobe_Systems_logo_and_wordmark.png/jcr:content/renditions/cq5dam.web.1280.1280.png");
}

@Test
Expand Down Expand Up @@ -980,13 +948,7 @@ void testImageFromFeaturedImage_viaRedirect() throws IOException {
MockRequestPathInfo requestPathInfo = (MockRequestPathInfo) request.getRequestPathInfo();
requestPathInfo.setSuffix(FEATURED_IMAGE_PATH);
servlet.doGet(request, response);
Assertions.assertEquals(200, response.getStatus(), "Expected a 200 response code.");
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(response.getOutput());
BufferedImage image = ImageIO.read(byteArrayInputStream);
Dimension expectedDimension = new Dimension(ADAPTIVE_IMAGE_SERVLET_DEFAULT_RESIZE_WIDTH, ADAPTIVE_IMAGE_SERVLET_DEFAULT_RESIZE_WIDTH);
Dimension actualDimension = new Dimension(image.getWidth(), image.getHeight());
Assertions.assertEquals(expectedDimension, actualDimension, "Expected image rendered at requested size.");
Assertions.assertEquals("image/png", response.getContentType(), "Expected a PNG image.");
assertValidRedirectToBlobStore(response, "/content/dam/core/images/Adobe_Systems_logo_and_wordmark.png/jcr:content/renditions/cq5dam.web.1280.1280.png");
}

@Test
Expand Down Expand Up @@ -1231,6 +1193,7 @@ void testTransformAndStreamAssetForTiffRenderedAsJpegWithoutJpegRenditionsNoResi
MockSlingHttpServletResponse response = requestResponsePair.getRight();

Asset mockAsset = mock(Asset.class);
when(mockAsset.getPath()).thenReturn("/content/dam/core/images/funky.tiff");
when(mockAsset.getMimeType()).thenReturn("image/tiff");
when(mockAsset.getRenditions()).thenReturn(new LinkedList<>());
when(mockAsset.getMetadataValue(DamConstants.TIFF_IMAGEWIDTH)).thenReturn("2000");
Expand All @@ -1240,15 +1203,7 @@ void testTransformAndStreamAssetForTiffRenderedAsJpegWithoutJpegRenditionsNoResi
when(mockAsset.getOriginal()).thenReturn(original);

servlet.transformAndStreamAsset(response, new MockValueMap(request.getResource(), new HashMap<>()), 0, 90, mockAsset, "image/jpeg", "test");

Assertions.assertEquals(200, response.getStatus(), "Expected a 200 response code.");
Assertions.assertEquals("image/jpeg", response.getContentType(), "Expected a JPG image.");

ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(response.getOutput());
BufferedImage image = ImageIO.read(byteArrayInputStream);
Dimension expectedDimension = new Dimension(2000, 2000);
Dimension actualDimension = new Dimension(image.getWidth(), image.getHeight());
Assertions.assertEquals(expectedDimension, actualDimension, "Expected image rendered at requested size.");
assertValidRedirectToBlobStore(response, "/content/dam/core/images/funky.tiff/jcr:content/renditions/original");
}

@Test
Expand Down Expand Up @@ -1337,6 +1292,41 @@ private Rendition mockRendition(Asset asset, String name, long size, String mime
when(rendition.getProperties()).thenReturn(mockProperties);
when(rendition.getSize()).thenReturn(size);
when(rendition.getAsset()).thenReturn(asset);
final String path = asset.getPath() + "/jcr:content/renditions/" + name;
when(rendition.getPath()).thenReturn(path);
when(rendition.getBinary()).thenReturn(new BinaryDownload() {

@Override
public int read(byte[] b, long position) throws IOException, RepositoryException {
return 0;
}

@Override
public InputStream getStream() throws RepositoryException {
// TODO Auto-generated method stub
return null;
}

@Override
public long getSize() throws RepositoryException {
return 0;
}

@Override
public void dispose() {
// do nothing
}

@Override
public @Nullable URI getURI(BinaryDownloadOptions downloadOptions) throws RepositoryException {
try {
return new URI (BLOBSTORE_BASE + rendition.getPath());
} catch (URISyntaxException e) {
// ignore
}
return null;
}
});
return rendition;
}

Expand Down

0 comments on commit fa7189b

Please sign in to comment.