Skip to content

Commit

Permalink
refactor unit tests, support ratio only use case
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed Aug 16, 2024
1 parent d2cf2c5 commit fbc713e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ public NextGenDynamicMediaImageUrlBuilder(@NotNull NextGenDynamicMediaContext co
SmartCrop namedSmartCrop = getMatchingNamedSmartCrop(metadata, cropSmartRatio);
if (namedSmartCrop != null) {
urlParamMap.put(PARAM_SMARTCROP, namedSmartCrop.getName());
boolean widthAndOrHeightDefined = applyWidthOrPlaceholder(params, urlParamMap) || applyHeightOrPlaceholder(params, urlParamMap);
if (!widthAndOrHeightDefined) {
// if no width given apply default width/height to not rely on dimensions defined in AEM image profile
boolean widthOrHeightDefined = applyWidthOrPlaceholder(params, urlParamMap) || applyHeightOrPlaceholder(params, urlParamMap);
if (!widthOrHeightDefined) {
// if no width or height given apply default width/height to not rely on dimensions defined in AEM image profile
String imageWidthHeightDefault = Long.toString(context.getNextGenDynamicMediaConfig().getImageWidthHeightDefault());
if (namedSmartCrop.getCropDimension().getWidth() >= namedSmartCrop.getCropDimension().getHeight()) {
urlParamMap.put(PARAM_WIDTH, imageWidthHeightDefault);
Expand All @@ -144,8 +144,22 @@ else if (orginalDimension != null && cropSmartRatio != null && isAutoCroppingReq
}
else {
// No cropping required or insufficient metadata available to detect cropping
applyWidthOrPlaceholder(params, urlParamMap);
applyHeightOrPlaceholder(params, urlParamMap);
boolean widthDefined = applyWidthOrPlaceholder(params, urlParamMap);
boolean heightDefined = applyHeightOrPlaceholder(params, urlParamMap);
if (!(widthDefined || heightDefined) && cropSmartRatio != null) {
// if no width or height given apply default width/height respecting the requested aspect ratio
double ratio = Ratio.get(cropSmartRatio);
long width = context.getNextGenDynamicMediaConfig().getImageWidthHeightDefault();
long height = context.getNextGenDynamicMediaConfig().getImageWidthHeightDefault();
if (ratio > 1) {
height = Math.round(width / ratio);
}
else if (ratio < 1) {
width = Math.round(height * ratio);
}
urlParamMap.put(PARAM_WIDTH, Long.toString(width));
urlParamMap.put(PARAM_HEIGHT, Long.toString(height));
}
}

if (rotation != null && rotation != 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void testAsset_JPEG_Original_ContentDisposition() {
void testAsset_JPEG_Rescale() {
Asset asset = createSampleAsset("/filetype/sample.jpg", ContentType.JPEG);
buildAssertMedia_Rescale(asset, 80, 40,
"https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.jpg?crop=80%3A40%2Csmart&preferwebp=true&quality=85&width=80",
"https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.jpg?height=40&preferwebp=true&quality=85&width=80",
ContentType.JPEG);
}

Expand All @@ -84,7 +84,7 @@ void testAsset_JPEG_Rescale() {
void testAsset_JPEG_AutoCrop() {
Asset asset = createSampleAsset("/filetype/sample.jpg", ContentType.JPEG);
buildAssertMedia_AutoCrop(asset, 50, 50,
"https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.jpg?crop=1%3A1%2Csmart&preferwebp=true&quality=85",
"https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.jpg?crop=25%2C0%2C50%2C50&preferwebp=true&quality=85",
ContentType.JPEG);
}

Expand All @@ -93,7 +93,7 @@ void testAsset_JPEG_AutoCrop() {
void testAsset_JPEG_AutoCrop_ImageQuality() {
Asset asset = createSampleAsset("/filetype/sample.jpg", ContentType.JPEG);
buildAssertMedia_AutoCrop(asset, 50, 50,
"https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.jpg?crop=1%3A1%2Csmart&preferwebp=true&quality=60",
"https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.jpg?crop=25%2C0%2C50%2C50&preferwebp=true&quality=60",
ContentType.JPEG, 0.6d);
}

Expand All @@ -103,7 +103,7 @@ void testAsset_JPEG_CropWithExplicitRendition() {
Asset asset = createSampleAsset("/filetype/sample.jpg", ContentType.JPEG);
context.create().assetRendition(asset, "square.jpg", 50, 50, ContentType.JPEG);
buildAssertMedia_AutoCrop(asset, 50, 50,
"https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.jpg?crop=1%3A1%2Csmart&preferwebp=true&quality=85",
"https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.jpg?crop=25%2C0%2C50%2C50&preferwebp=true&quality=85",
ContentType.JPEG);
}

Expand All @@ -121,7 +121,7 @@ void testAsset_GIF_Original() {
void testAsset_GIF_Rescale() {
Asset asset = createSampleAsset("/filetype/sample.gif", ContentType.GIF);
buildAssertMedia_Rescale(asset, 80, 40,
"https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.gif?crop=80%3A40%2Csmart&preferwebp=true&quality=85&width=80",
"https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.gif?height=40&preferwebp=true&quality=85&width=80",
ContentType.JPEG);
}

Expand All @@ -130,7 +130,7 @@ void testAsset_GIF_Rescale() {
void testAsset_GIF_AutoCrop() {
Asset asset = createSampleAsset("/filetype/sample.gif", ContentType.GIF);
buildAssertMedia_AutoCrop(asset, 50, 50,
"https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.gif?crop=1%3A1%2Csmart&preferwebp=true&quality=85",
"https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.gif?crop=25%2C0%2C50%2C50&preferwebp=true&quality=85",
ContentType.JPEG);
}

Expand All @@ -148,7 +148,7 @@ void testAsset_PNG_Original() {
void testAsset_PNG_Rescale() {
Asset asset = createSampleAsset("/filetype/sample.png", ContentType.PNG);
buildAssertMedia_Rescale(asset, 80, 40,
"https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.png?crop=80%3A40%2Csmart&preferwebp=true&quality=85&width=80",
"https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.png?height=40&preferwebp=true&quality=85&width=80",
ContentType.PNG);
}

Expand All @@ -157,7 +157,7 @@ void testAsset_PNG_Rescale() {
void testAsset_PNG_AutoCrop() {
Asset asset = createSampleAsset("/filetype/sample.png", ContentType.PNG);
buildAssertMedia_AutoCrop(asset, 50, 50,
"https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.png?crop=1%3A1%2Csmart&preferwebp=true&quality=85",
"https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.png?crop=25%2C0%2C50%2C50&preferwebp=true&quality=85",
ContentType.PNG);
}

Expand All @@ -184,7 +184,7 @@ void testAsset_TIFF_Original_ContentDisposition() {
void testAsset_TIFF_Rescale() {
Asset asset = createSampleAsset("/filetype/sample.tif", ContentType.TIFF);
buildAssertMedia_Rescale(asset, 80, 40,
"https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.jpg?crop=80%3A40%2Csmart&preferwebp=true&quality=85&width=80",
"https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.jpg?height=40&preferwebp=true&quality=85&width=80",
ContentType.JPEG);
}

Expand All @@ -193,7 +193,7 @@ void testAsset_TIFF_Rescale() {
void testAsset_TIFF_AutoCrop() {
Asset asset = createSampleAsset("/filetype/sample.tif", ContentType.TIFF);
buildAssertMedia_AutoCrop(asset, 50, 50,
"https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.jpg?crop=1%3A1%2Csmart&preferwebp=true&quality=85",
"https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.jpg?crop=25%2C0%2C50%2C50&preferwebp=true&quality=85",
ContentType.JPEG);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void testAsset_JPEG_Original_ContentDisposition() {
void testAsset_JPEG_Rescale() {
Asset asset = createNextGenDynamicMediaReferenceAsAsset("sample.jpg");
buildAssertMedia_Rescale(asset, 80, 40,
"https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.jpg?crop=80%3A40%2Csmart&preferwebp=true&quality=85&width=80",
"https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.jpg?height=40&preferwebp=true&quality=85&width=80",
ContentType.JPEG);
}

Expand All @@ -87,7 +87,7 @@ void testAsset_JPEG_Rescale() {
void testAsset_JPEG_AutoCrop() {
Asset asset = createNextGenDynamicMediaReferenceAsAsset("sample.jpg");
buildAssertMedia_AutoCrop(asset, 50, 50,
"https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.jpg?crop=1%3A1%2Csmart&preferwebp=true&quality=85",
"https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.jpg?height=2048&preferwebp=true&quality=85&width=2048",
ContentType.JPEG);
}

Expand All @@ -96,7 +96,7 @@ void testAsset_JPEG_AutoCrop() {
void testAsset_JPEG_AutoCrop_ImageQuality() {
Asset asset = createNextGenDynamicMediaReferenceAsAsset("sample.jpg");
buildAssertMedia_AutoCrop(asset, 50, 50,
"https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.jpg?crop=1%3A1%2Csmart&preferwebp=true&quality=60",
"https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.jpg?height=2048&preferwebp=true&quality=60&width=2048",
ContentType.JPEG, 0.6d);
}

Expand All @@ -121,7 +121,7 @@ void testAsset_GIF_Original() {
void testAsset_GIF_Rescale() {
Asset asset = createNextGenDynamicMediaReferenceAsAsset("sample.gif");
buildAssertMedia_Rescale(asset, 80, 40,
"https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.gif?crop=80%3A40%2Csmart&preferwebp=true&quality=85&width=80",
"https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.gif?height=40&preferwebp=true&quality=85&width=80",
ContentType.GIF);
}

Expand All @@ -130,7 +130,7 @@ void testAsset_GIF_Rescale() {
void testAsset_GIF_AutoCrop() {
Asset asset = createNextGenDynamicMediaReferenceAsAsset("sample.gif");
buildAssertMedia_AutoCrop(asset, 50, 50,
"https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.gif?crop=1%3A1%2Csmart&preferwebp=true&quality=85",
"https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.gif?height=2048&preferwebp=true&quality=85&width=2048",
ContentType.GIF);
}

Expand All @@ -148,7 +148,7 @@ void testAsset_PNG_Original() {
void testAsset_PNG_Rescale() {
Asset asset = createNextGenDynamicMediaReferenceAsAsset("sample.png");
buildAssertMedia_Rescale(asset, 80, 40,
"https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.png?crop=80%3A40%2Csmart&preferwebp=true&quality=85&width=80",
"https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.png?height=40&preferwebp=true&quality=85&width=80",
ContentType.PNG);
}

Expand All @@ -157,7 +157,7 @@ void testAsset_PNG_Rescale() {
void testAsset_PNG_AutoCrop() {
Asset asset = createNextGenDynamicMediaReferenceAsAsset("sample.png");
buildAssertMedia_AutoCrop(asset, 50, 50,
"https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.png?crop=1%3A1%2Csmart&preferwebp=true&quality=85",
"https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.png?height=2048&preferwebp=true&quality=85&width=2048",
ContentType.PNG);
}

Expand All @@ -184,7 +184,7 @@ void testAsset_TIFF_Original_ContentDisposition() {
void testAsset_TIFF_Rescale() {
Asset asset = createNextGenDynamicMediaReferenceAsAsset("sample.tif");
buildAssertMedia_Rescale(asset, 80, 40,
"https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.jpg?crop=80%3A40%2Csmart&preferwebp=true&quality=85&width=80",
"https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.jpg?height=40&preferwebp=true&quality=85&width=80",
ContentType.JPEG);
}

Expand All @@ -193,7 +193,7 @@ void testAsset_TIFF_Rescale() {
void testAsset_TIFF_AutoCrop() {
Asset asset = createNextGenDynamicMediaReferenceAsAsset("sample.tif");
buildAssertMedia_AutoCrop(asset, 50, 50,
"https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.jpg?crop=1%3A1%2Csmart&preferwebp=true&quality=85",
"https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.jpg?height=2048&preferwebp=true&quality=85&width=2048",
ContentType.JPEG);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,45 @@ void testAllParams() {
underTest.build(params));
}

@Test
void testOnlyRatio_16_9() {
NextGenDynamicMediaImageUrlBuilder underTest = getBuilder();
NextGenDynamicMediaImageDeliveryParams params = new NextGenDynamicMediaImageDeliveryParams()
.cropSmartRatio(new Dimension(16, 9))
.rotation(90)
.quality(60);

assertEquals("https://repo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/my-image.jpg"
+ "?height=1152&preferwebp=true&quality=60&rotate=90&width=2048",
underTest.build(params));
}

@Test
void testOnlyRatio_1_2() {
NextGenDynamicMediaImageUrlBuilder underTest = getBuilder();
NextGenDynamicMediaImageDeliveryParams params = new NextGenDynamicMediaImageDeliveryParams()
.cropSmartRatio(new Dimension(1, 2))
.rotation(90)
.quality(60);

assertEquals("https://repo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/my-image.jpg"
+ "?height=2048&preferwebp=true&quality=60&rotate=90&width=1024",
underTest.build(params));
}

@Test
void testOnlyRatio_1_1() {
NextGenDynamicMediaImageUrlBuilder underTest = getBuilder();
NextGenDynamicMediaImageDeliveryParams params = new NextGenDynamicMediaImageDeliveryParams()
.cropSmartRatio(new Dimension(1, 1))
.rotation(90)
.quality(60);

assertEquals("https://repo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/my-image.jpg"
+ "?height=2048&preferwebp=true&quality=60&rotate=90&width=2048",
underTest.build(params));
}

@Test
void testAllParams_NamedSmartCrop() throws Exception {
NextGenDynamicMediaMetadata metadata = NextGenDynamicMediaMetadata.fromJson(METADATA_JSON_IMAGE);
Expand Down

0 comments on commit fbc713e

Please sign in to comment.