mirror of
https://github.com/Alfresco/alfresco-transform-core.git
synced 2025-07-31 17:38:33 +00:00
ACS-9835-Improve code quality in alfresco-transform-core (#1116)
This commit is contained in:
@@ -26,15 +26,17 @@
|
||||
*/
|
||||
package org.alfresco.transform.imagemagick;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import org.alfresco.transform.exceptions.TransformException;
|
||||
import static org.springframework.http.HttpStatus.BAD_REQUEST;
|
||||
|
||||
import static org.alfresco.transform.base.util.Util.stringToBoolean;
|
||||
import static org.alfresco.transform.base.util.Util.stringToInteger;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
import static org.alfresco.transform.base.util.Util.stringToBoolean;
|
||||
import static org.alfresco.transform.base.util.Util.stringToInteger;
|
||||
import static org.springframework.http.HttpStatus.BAD_REQUEST;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
import org.alfresco.transform.exceptions.TransformException;
|
||||
|
||||
/**
|
||||
* ImageMagick options builder.
|
||||
@@ -44,7 +46,7 @@ import static org.springframework.http.HttpStatus.BAD_REQUEST;
|
||||
public final class ImageMagickOptionsBuilder
|
||||
{
|
||||
private static final List<String> GRAVITY_VALUES = ImmutableList.of("North", "NorthEast",
|
||||
"East", "SouthEast", "South", "SouthWest", "West", "NorthWest", "Center");
|
||||
"East", "SouthEast", "South", "SouthWest", "West", "NorthWest", "Center");
|
||||
|
||||
private Integer startPage;
|
||||
private Integer endPage;
|
||||
@@ -64,7 +66,8 @@ public final class ImageMagickOptionsBuilder
|
||||
private Boolean maintainAspectRatio;
|
||||
private String commandOptions;
|
||||
|
||||
private ImageMagickOptionsBuilder() {}
|
||||
private ImageMagickOptionsBuilder()
|
||||
{}
|
||||
|
||||
public ImageMagickOptionsBuilder withStartPage(final String startPage)
|
||||
{
|
||||
@@ -270,7 +273,7 @@ public final class ImageMagickOptionsBuilder
|
||||
}
|
||||
|
||||
if (cropGravity != null || cropWidth != null || cropHeight != null || cropPercentage != null ||
|
||||
cropXOffset != null || cropYOffset != null)
|
||||
cropXOffset != null || cropYOffset != null)
|
||||
{
|
||||
if (cropGravity != null)
|
||||
{
|
||||
@@ -349,8 +352,8 @@ public final class ImageMagickOptionsBuilder
|
||||
}
|
||||
|
||||
return (commandOptions == null || "".equals(
|
||||
commandOptions.trim()) ? "" : commandOptions + ' ') +
|
||||
args.toString();
|
||||
commandOptions.trim()) ? "" : commandOptions + ' ') +
|
||||
args.toString();
|
||||
}
|
||||
|
||||
public static ImageMagickOptionsBuilder builder()
|
||||
|
@@ -26,19 +26,20 @@
|
||||
*/
|
||||
package org.alfresco.transform.imagemagick;
|
||||
|
||||
import org.alfresco.transform.base.TransformEngine;
|
||||
import org.alfresco.transform.base.probes.ProbeTransform;
|
||||
import org.alfresco.transform.config.reader.TransformConfigResourceReader;
|
||||
import org.alfresco.transform.config.TransformConfig;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.alfresco.transform.base.logging.StandardMessages.COMMUNITY_LICENCE;
|
||||
import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_JPEG;
|
||||
import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_PNG;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import org.alfresco.transform.base.TransformEngine;
|
||||
import org.alfresco.transform.base.probes.ProbeTransform;
|
||||
import org.alfresco.transform.config.TransformConfig;
|
||||
import org.alfresco.transform.config.reader.TransformConfigResourceReader;
|
||||
|
||||
@Component
|
||||
public class ImageMagickTransformEngine implements TransformEngine
|
||||
{
|
||||
@@ -69,6 +70,6 @@ public class ImageMagickTransformEngine implements TransformEngine
|
||||
public ProbeTransform getProbeTransform()
|
||||
{
|
||||
return new ProbeTransform("probe.jpg", MIMETYPE_IMAGE_JPEG, MIMETYPE_IMAGE_PNG, Collections.emptyMap(),
|
||||
25383, 1024, 150, 1024, 60 * 15 + 1, 60 * 15);
|
||||
25383, 1024, 150, 1024, 60 * 15 + 1, 60 * 15);
|
||||
}
|
||||
}
|
||||
|
@@ -52,11 +52,15 @@ import static org.alfresco.transform.common.RequestParamMap.START_PAGE;
|
||||
import static org.alfresco.transform.common.RequestParamMap.THUMBNAIL;
|
||||
import static org.alfresco.transform.common.RequestParamMap.TIMEOUT;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import org.alfresco.transform.base.TransformManager;
|
||||
import org.alfresco.transform.base.executors.AbstractCommandExecutor;
|
||||
@@ -64,13 +68,9 @@ import org.alfresco.transform.base.executors.RuntimeExec;
|
||||
import org.alfresco.transform.base.util.CustomTransformerFileAdaptor;
|
||||
import org.alfresco.transform.exceptions.TransformException;
|
||||
import org.alfresco.transform.imagemagick.ImageMagickOptionsBuilder;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Converts image files into different types of images. Transformer supports multi-page images and allows to specify via parameters `startPage` and `endPage` range of pages that should be converted.
|
||||
* In case of a one-page target image type (like `jpeg` or `png`) parameters `startPage` and `endPage` will be set to 0 by default - this means that only first page will be converted.
|
||||
* Converts image files into different types of images. Transformer supports multi-page images and allows to specify via parameters `startPage` and `endPage` range of pages that should be converted. In case of a one-page target image type (like `jpeg` or `png`) parameters `startPage` and `endPage` will be set to 0 by default - this means that only first page will be converted.
|
||||
*/
|
||||
@Component
|
||||
public class ImageMagickTransformer extends AbstractCommandExecutor implements CustomTransformerFileAdaptor
|
||||
@@ -122,7 +122,7 @@ public class ImageMagickTransformer extends AbstractCommandExecutor implements C
|
||||
RuntimeExec runtimeExec = new RuntimeExec();
|
||||
Map<String, String[]> commandsAndArguments = new HashMap<>();
|
||||
commandsAndArguments.put(".*",
|
||||
new String[]{exe, "${source}", "SPLIT:${options}", "-strip", "-quiet", "${target}"});
|
||||
new String[]{exe, "${source}", "SPLIT:${options}", "-strip", "-quiet", "${target}"});
|
||||
runtimeExec.setCommandsAndArguments(commandsAndArguments);
|
||||
|
||||
Map<String, String> processProperties = new HashMap<>();
|
||||
@@ -130,7 +130,7 @@ public class ImageMagickTransformer extends AbstractCommandExecutor implements C
|
||||
processProperties.put("DYLD_FALLBACK_LIBRARY_PATH", dyn);
|
||||
processProperties.put("LD_LIBRARY_PATH", dyn);
|
||||
|
||||
//Optional properties (see also https://imagemagick.org/script/resources.php#environment)
|
||||
// Optional properties (see also https://imagemagick.org/script/resources.php#environment)
|
||||
if (coders != null && !coders.isBlank())
|
||||
{
|
||||
processProperties.put("MAGICK_CODER_MODULE_PATH", coders);
|
||||
@@ -146,7 +146,7 @@ public class ImageMagickTransformer extends AbstractCommandExecutor implements C
|
||||
runtimeExec.setDefaultProperties(defaultProperties);
|
||||
|
||||
runtimeExec.setErrorCodes(
|
||||
"1,2,255,400,405,410,415,420,425,430,435,440,450,455,460,465,470,475,480,485,490,495,499,700,705,710,715,720,725,730,735,740,750,755,760,765,770,775,780,785,790,795,799");
|
||||
"1,2,255,400,405,410,415,420,425,430,435,440,450,455,460,465,470,475,480,485,490,495,499,700,705,710,715,720,725,730,735,740,750,755,760,765,770,775,780,785,790,795,799");
|
||||
|
||||
return runtimeExec;
|
||||
}
|
||||
@@ -163,7 +163,7 @@ public class ImageMagickTransformer extends AbstractCommandExecutor implements C
|
||||
|
||||
@Override
|
||||
public void transform(String sourceMimetype, String targetMimetype, Map<String, String> transformOptions,
|
||||
File sourceFile, File targetFile, TransformManager transformManager) throws TransformException
|
||||
File sourceFile, File targetFile, TransformManager transformManager) throws TransformException
|
||||
{
|
||||
String startPageString = transformOptions.get(START_PAGE);
|
||||
String endPageString = transformOptions.get(END_PAGE);
|
||||
@@ -202,8 +202,7 @@ public class ImageMagickTransformer extends AbstractCommandExecutor implements C
|
||||
|
||||
String pageRange = calculatePageRange(
|
||||
stringToInteger(startPageString),
|
||||
stringToInteger(endPageString)
|
||||
);
|
||||
stringToInteger(endPageString));
|
||||
|
||||
Long timeout = stringToLong(transformOptions.get(TIMEOUT));
|
||||
|
||||
@@ -214,10 +213,10 @@ public class ImageMagickTransformer extends AbstractCommandExecutor implements C
|
||||
{
|
||||
return startPage == null
|
||||
? endPage == null
|
||||
? ""
|
||||
: "[" + endPage + ']'
|
||||
? ""
|
||||
: "[" + endPage + ']'
|
||||
: endPage == null || startPage.equals(endPage)
|
||||
? "[" + startPage + ']'
|
||||
: "[" + startPage + '-' + endPage + ']';
|
||||
? "[" + startPage + ']'
|
||||
: "[" + startPage + '-' + endPage + ']';
|
||||
}
|
||||
}
|
||||
|
@@ -29,9 +29,11 @@ package org.alfresco.transform.imagemagick;
|
||||
|
||||
import org.alfresco.transform.base.LivenessReadinessProbeTest;
|
||||
|
||||
public class ImageMagickLivenessReadinessProbeIT extends LivenessReadinessProbeTest {
|
||||
public class ImageMagickLivenessReadinessProbeIT extends LivenessReadinessProbeTest
|
||||
{
|
||||
@Override
|
||||
protected ImagesForTests getImageForTest() {
|
||||
protected ImagesForTests getImageForTest()
|
||||
{
|
||||
return new ImagesForTests("alfresco-imagemagick", "image/jpeg", "image/png", "quick.jpg");
|
||||
}
|
||||
}
|
||||
|
@@ -31,12 +31,11 @@ import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_PNG;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.alfresco.transform.client.model.TransformRequest;
|
||||
import org.alfresco.transform.base.messaging.AbstractQueueIT;
|
||||
import org.alfresco.transform.client.model.TransformRequest;
|
||||
|
||||
/**
|
||||
* @author Lucian Tuca
|
||||
* created on 15/01/2019
|
||||
* @author Lucian Tuca created on 15/01/2019
|
||||
*/
|
||||
public class ImageMagickQueueIT extends AbstractQueueIT
|
||||
{
|
||||
@@ -44,16 +43,16 @@ public class ImageMagickQueueIT extends AbstractQueueIT
|
||||
protected TransformRequest buildRequest()
|
||||
{
|
||||
return TransformRequest
|
||||
.builder()
|
||||
.withRequestId(UUID.randomUUID().toString())
|
||||
.withSourceMediaType(MIMETYPE_IMAGE_PNG)
|
||||
.withTargetMediaType(MIMETYPE_IMAGE_JPEG)
|
||||
.withTargetExtension("jpeg")
|
||||
.withSchema(1)
|
||||
.withClientData("ACS")
|
||||
.withSourceReference(UUID.randomUUID().toString())
|
||||
.withSourceSize(32L)
|
||||
.withInternalContextForTransformEngineTests()
|
||||
.build();
|
||||
.builder()
|
||||
.withRequestId(UUID.randomUUID().toString())
|
||||
.withSourceMediaType(MIMETYPE_IMAGE_PNG)
|
||||
.withTargetMediaType(MIMETYPE_IMAGE_JPEG)
|
||||
.withTargetExtension("jpeg")
|
||||
.withSchema(1)
|
||||
.withClientData("ACS")
|
||||
.withSourceReference(UUID.randomUUID().toString())
|
||||
.withSourceSize(32L)
|
||||
.withInternalContextForTransformEngineTests()
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
@@ -26,39 +26,6 @@
|
||||
*/
|
||||
package org.alfresco.transform.imagemagick;
|
||||
|
||||
import org.alfresco.transform.base.AbstractBaseTest;
|
||||
import org.alfresco.transform.base.executors.RuntimeExec;
|
||||
import org.alfresco.transform.base.executors.RuntimeExec.ExecutionResult;
|
||||
import org.alfresco.transform.base.model.FileRefEntity;
|
||||
import org.alfresco.transform.base.model.FileRefResponse;
|
||||
import org.alfresco.transform.client.model.TransformReply;
|
||||
import org.alfresco.transform.client.model.TransformRequest;
|
||||
import org.alfresco.transform.imagemagick.transformers.ImageMagickTransformer;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
@@ -79,6 +46,41 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.springframework.util.StringUtils.getFilenameExtension;
|
||||
|
||||
import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
import org.alfresco.transform.base.AbstractBaseTest;
|
||||
import org.alfresco.transform.base.executors.RuntimeExec;
|
||||
import org.alfresco.transform.base.executors.RuntimeExec.ExecutionResult;
|
||||
import org.alfresco.transform.base.model.FileRefEntity;
|
||||
import org.alfresco.transform.base.model.FileRefResponse;
|
||||
import org.alfresco.transform.client.model.TransformReply;
|
||||
import org.alfresco.transform.client.model.TransformRequest;
|
||||
import org.alfresco.transform.imagemagick.transformers.ImageMagickTransformer;
|
||||
|
||||
/**
|
||||
* Test ImageMagick with mocked external command.
|
||||
*/
|
||||
@@ -123,20 +125,20 @@ public class ImageMagickTest extends AbstractBaseTest
|
||||
protected MockHttpServletRequestBuilder mockMvcRequest(String url, MockMultipartFile sourceFile, String... params)
|
||||
{
|
||||
final MockHttpServletRequestBuilder builder = super.mockMvcRequest(url, sourceFile, params)
|
||||
.param("targetMimetype", targetMimetype)
|
||||
.param("sourceMimetype", sourceMimetype);
|
||||
.param("targetMimetype", targetMimetype)
|
||||
.param("sourceMimetype", sourceMimetype);
|
||||
return builder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mockTransformCommand(String sourceExtension,
|
||||
String targetExtension, String sourceMimetype,
|
||||
boolean readTargetFileBytes) throws IOException
|
||||
String targetExtension, String sourceMimetype,
|
||||
boolean readTargetFileBytes) throws IOException
|
||||
{
|
||||
this.sourceExtension = sourceExtension;
|
||||
this.targetExtension = targetExtension;
|
||||
this.sourceMimetype = sourceMimetype;
|
||||
this.targetMimetype = PREFIX_IMAGE + ("jpg".equals(targetExtension) ? "jpeg" : targetExtension);
|
||||
this.targetMimetype = PREFIX_IMAGE + ("jpg".equals(targetExtension) ? "jpeg" : targetExtension);
|
||||
|
||||
expectedOptions = null;
|
||||
expectedSourceSuffix = null;
|
||||
@@ -145,55 +147,55 @@ public class ImageMagickTest extends AbstractBaseTest
|
||||
sourceFile = new MockMultipartFile("file", "quick." + sourceExtension, sourceMimetype, sourceFileBytes);
|
||||
|
||||
when(mockTransformCommand.execute(any(), anyLong())).thenAnswer(
|
||||
(Answer<RuntimeExec.ExecutionResult>) invocation -> {
|
||||
Map<String, String> actualProperties = invocation.getArgument(0);
|
||||
assertEquals(3, actualProperties.size(), "There should be 3 properties");
|
||||
(Answer<RuntimeExec.ExecutionResult>) invocation -> {
|
||||
Map<String, String> actualProperties = invocation.getArgument(0);
|
||||
assertEquals(3, actualProperties.size(), "There should be 3 properties");
|
||||
|
||||
String actualOptions = actualProperties.get("options");
|
||||
String actualSource = actualProperties.get("source");
|
||||
String actualTarget = actualProperties.get("target");
|
||||
String actualTargetExtension = getFilenameExtension(actualTarget);
|
||||
String actualOptions = actualProperties.get("options");
|
||||
String actualSource = actualProperties.get("source");
|
||||
String actualTarget = actualProperties.get("target");
|
||||
String actualTargetExtension = getFilenameExtension(actualTarget);
|
||||
|
||||
assertNotNull(actualSource);
|
||||
assertNotNull(actualTarget);
|
||||
if (expectedSourceSuffix != null)
|
||||
{
|
||||
assertTrue(actualSource.endsWith(expectedSourceSuffix),
|
||||
"The source file \"" + actualSource + "\" should have ended in \"" + expectedSourceSuffix + "\"");
|
||||
actualSource = actualSource.substring(0, actualSource.length() - expectedSourceSuffix.length());
|
||||
}
|
||||
assertNotNull(actualSource);
|
||||
assertNotNull(actualTarget);
|
||||
if (expectedSourceSuffix != null)
|
||||
{
|
||||
assertTrue(actualSource.endsWith(expectedSourceSuffix),
|
||||
"The source file \"" + actualSource + "\" should have ended in \"" + expectedSourceSuffix + "\"");
|
||||
actualSource = actualSource.substring(0, actualSource.length() - expectedSourceSuffix.length());
|
||||
}
|
||||
|
||||
assertNotNull(actualOptions);
|
||||
if (expectedOptions != null)
|
||||
{
|
||||
assertEquals(expectedOptions, actualOptions,"expectedOptions");
|
||||
}
|
||||
assertNotNull(actualOptions);
|
||||
if (expectedOptions != null)
|
||||
{
|
||||
assertEquals(expectedOptions, actualOptions, "expectedOptions");
|
||||
}
|
||||
|
||||
Long actualTimeout = invocation.getArgument(1);
|
||||
assertNotNull(actualTimeout);
|
||||
if (expectedTimeout != null)
|
||||
{
|
||||
assertEquals(expectedTimeout, actualTimeout,"expectedTimeout");
|
||||
}
|
||||
Long actualTimeout = invocation.getArgument(1);
|
||||
assertNotNull(actualTimeout);
|
||||
if (expectedTimeout != null)
|
||||
{
|
||||
assertEquals(expectedTimeout, actualTimeout, "expectedTimeout");
|
||||
}
|
||||
|
||||
// Copy a test file into the target file location if it exists
|
||||
int i = actualTarget.lastIndexOf('_');
|
||||
if (i >= 0)
|
||||
{
|
||||
String testFilename = actualTarget.substring(i + 1);
|
||||
File testFile = getTestFile(testFilename, false);
|
||||
File targetFile = new File(actualTarget);
|
||||
generateTargetFileFromResourceFile(actualTargetExtension, testFile,
|
||||
targetFile);
|
||||
}
|
||||
// Copy a test file into the target file location if it exists
|
||||
int i = actualTarget.lastIndexOf('_');
|
||||
if (i >= 0)
|
||||
{
|
||||
String testFilename = actualTarget.substring(i + 1);
|
||||
File testFile = getTestFile(testFilename, false);
|
||||
File targetFile = new File(actualTarget);
|
||||
generateTargetFileFromResourceFile(actualTargetExtension, testFile,
|
||||
targetFile);
|
||||
}
|
||||
|
||||
// Check the supplied source file has not been changed.
|
||||
byte[] actualSourceFileBytes = Files.readAllBytes(new File(actualSource).toPath());
|
||||
assertTrue(Arrays.equals(sourceFileBytes, actualSourceFileBytes),
|
||||
"Source file is not the same");
|
||||
// Check the supplied source file has not been changed.
|
||||
byte[] actualSourceFileBytes = Files.readAllBytes(new File(actualSource).toPath());
|
||||
assertTrue(Arrays.equals(sourceFileBytes, actualSourceFileBytes),
|
||||
"Source file is not the same");
|
||||
|
||||
return mockExecutionResult;
|
||||
});
|
||||
return mockExecutionResult;
|
||||
});
|
||||
|
||||
when(mockExecutionResult.getExitValue()).thenReturn(0);
|
||||
when(mockExecutionResult.getStdErr()).thenReturn("STDERROR");
|
||||
@@ -206,31 +208,31 @@ public class ImageMagickTest extends AbstractBaseTest
|
||||
{
|
||||
expectedOptions = "-auto-orient " + "-gravity " + value + " +repage";
|
||||
mockMvc
|
||||
.perform(MockMvcRequestBuilders
|
||||
.multipart(ENDPOINT_TRANSFORM)
|
||||
.file(sourceFile)
|
||||
.param("targetExtension", targetExtension)
|
||||
.param("targetMimetype", targetMimetype)
|
||||
.param("sourceMimetype", sourceMimetype)
|
||||
.param("cropGravity", value))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().bytes(expectedTargetFileBytes))
|
||||
.andExpect(header().string("Content-Disposition",
|
||||
"attachment; filename*=UTF-8''transform." + targetExtension));
|
||||
.perform(MockMvcRequestBuilders
|
||||
.multipart(ENDPOINT_TRANSFORM)
|
||||
.file(sourceFile)
|
||||
.param("targetExtension", targetExtension)
|
||||
.param("targetMimetype", targetMimetype)
|
||||
.param("sourceMimetype", sourceMimetype)
|
||||
.param("cropGravity", value))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().bytes(expectedTargetFileBytes))
|
||||
.andExpect(header().string("Content-Disposition",
|
||||
"attachment; filename*=UTF-8''transform." + targetExtension));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cropGravityBadTest() throws Exception
|
||||
{
|
||||
mockMvc
|
||||
.perform(MockMvcRequestBuilders
|
||||
.multipart(ENDPOINT_TRANSFORM)
|
||||
.file(sourceFile)
|
||||
.param("targetExtension", targetExtension)
|
||||
.param("targetMimetype", targetMimetype)
|
||||
.param("sourceMimetype", sourceMimetype)
|
||||
.param("cropGravity", "badValue"))
|
||||
.andExpect(status().is(BAD_REQUEST.value()));
|
||||
.perform(MockMvcRequestBuilders
|
||||
.multipart(ENDPOINT_TRANSFORM)
|
||||
.file(sourceFile)
|
||||
.param("targetExtension", targetExtension)
|
||||
.param("targetMimetype", targetMimetype)
|
||||
.param("sourceMimetype", sourceMimetype)
|
||||
.param("cropGravity", "badValue"))
|
||||
.andExpect(status().is(BAD_REQUEST.value()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -239,36 +241,36 @@ public class ImageMagickTest extends AbstractBaseTest
|
||||
expectedOptions = "-alpha remove -gravity SouthEast -crop 123x456%+90+12 +repage -thumbnail 321x654%!";
|
||||
expectedSourceSuffix = "[2-3]";
|
||||
mockMvc
|
||||
.perform(MockMvcRequestBuilders
|
||||
.multipart(ENDPOINT_TRANSFORM)
|
||||
.file(sourceFile)
|
||||
.param("targetExtension", targetExtension)
|
||||
.param("targetMimetype", targetMimetype)
|
||||
.param("sourceMimetype", sourceMimetype)
|
||||
.perform(MockMvcRequestBuilders
|
||||
.multipart(ENDPOINT_TRANSFORM)
|
||||
.file(sourceFile)
|
||||
.param("targetExtension", targetExtension)
|
||||
.param("targetMimetype", targetMimetype)
|
||||
.param("sourceMimetype", sourceMimetype)
|
||||
|
||||
.param("startPage", "2")
|
||||
.param("endPage", "3")
|
||||
.param("startPage", "2")
|
||||
.param("endPage", "3")
|
||||
|
||||
.param("alphaRemove", "true")
|
||||
.param("autoOrient", "false")
|
||||
.param("alphaRemove", "true")
|
||||
.param("autoOrient", "false")
|
||||
|
||||
.param("cropGravity", "SouthEast")
|
||||
.param("cropWidth", "123")
|
||||
.param("cropHeight", "456")
|
||||
.param("cropPercentage", "true")
|
||||
.param("cropXOffset", "90")
|
||||
.param("cropYOffset", "12")
|
||||
.param("cropGravity", "SouthEast")
|
||||
.param("cropWidth", "123")
|
||||
.param("cropHeight", "456")
|
||||
.param("cropPercentage", "true")
|
||||
.param("cropXOffset", "90")
|
||||
.param("cropYOffset", "12")
|
||||
|
||||
.param("thumbnail", "true")
|
||||
.param("resizeWidth", "321")
|
||||
.param("resizeHeight", "654")
|
||||
.param("resizePercentage", "true")
|
||||
.param("allowEnlargement", "true")
|
||||
.param("maintainAspectRatio", "false"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().bytes(expectedTargetFileBytes))
|
||||
.andExpect(header().string("Content-Disposition",
|
||||
"attachment; filename*=UTF-8''transform." + targetExtension));
|
||||
.param("thumbnail", "true")
|
||||
.param("resizeWidth", "321")
|
||||
.param("resizeHeight", "654")
|
||||
.param("resizePercentage", "true")
|
||||
.param("allowEnlargement", "true")
|
||||
.param("maintainAspectRatio", "false"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().bytes(expectedTargetFileBytes))
|
||||
.andExpect(header().string("Content-Disposition",
|
||||
"attachment; filename*=UTF-8''transform." + targetExtension));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -277,36 +279,36 @@ public class ImageMagickTest extends AbstractBaseTest
|
||||
expectedOptions = "-auto-orient -gravity SouthEast -crop 123x456+90+12 +repage -resize 321x654>";
|
||||
expectedSourceSuffix = "[2-3]";
|
||||
mockMvc
|
||||
.perform(MockMvcRequestBuilders
|
||||
.multipart(ENDPOINT_TRANSFORM)
|
||||
.file(sourceFile)
|
||||
.param("targetExtension", targetExtension)
|
||||
.param("targetMimetype", targetMimetype)
|
||||
.param("sourceMimetype", sourceMimetype)
|
||||
|
||||
.param("startPage", "2")
|
||||
.param("endPage", "3")
|
||||
.perform(MockMvcRequestBuilders
|
||||
.multipart(ENDPOINT_TRANSFORM)
|
||||
.file(sourceFile)
|
||||
.param("targetExtension", targetExtension)
|
||||
.param("targetMimetype", targetMimetype)
|
||||
.param("sourceMimetype", sourceMimetype)
|
||||
|
||||
.param("alphaRemove", "false")
|
||||
.param("autoOrient", "true")
|
||||
.param("startPage", "2")
|
||||
.param("endPage", "3")
|
||||
|
||||
.param("cropGravity", "SouthEast")
|
||||
.param("cropWidth", "123")
|
||||
.param("cropHeight", "456")
|
||||
.param("cropPercentage", "false")
|
||||
.param("cropXOffset", "90")
|
||||
.param("cropYOffset", "12")
|
||||
.param("alphaRemove", "false")
|
||||
.param("autoOrient", "true")
|
||||
|
||||
.param("thumbnail", "false")
|
||||
.param("resizeWidth", "321")
|
||||
.param("resizeHeight", "654")
|
||||
.param("resizePercentage", "false")
|
||||
.param("allowEnlargement", "false")
|
||||
.param("maintainAspectRatio", "true"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().bytes(expectedTargetFileBytes))
|
||||
.andExpect(header().string("Content-Disposition",
|
||||
"attachment; filename*=UTF-8''transform." + targetExtension));
|
||||
.param("cropGravity", "SouthEast")
|
||||
.param("cropWidth", "123")
|
||||
.param("cropHeight", "456")
|
||||
.param("cropPercentage", "false")
|
||||
.param("cropXOffset", "90")
|
||||
.param("cropYOffset", "12")
|
||||
|
||||
.param("thumbnail", "false")
|
||||
.param("resizeWidth", "321")
|
||||
.param("resizeHeight", "654")
|
||||
.param("resizePercentage", "false")
|
||||
.param("allowEnlargement", "false")
|
||||
.param("maintainAspectRatio", "true"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().bytes(expectedTargetFileBytes))
|
||||
.andExpect(header().string("Content-Disposition",
|
||||
"attachment; filename*=UTF-8''transform." + targetExtension));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -315,20 +317,20 @@ public class ImageMagickTest extends AbstractBaseTest
|
||||
// Example of why the commandOptions parameter is a bad idea.
|
||||
expectedOptions = "( horrible command / ); -auto-orient -resize 321x654";
|
||||
mockMvc
|
||||
.perform(MockMvcRequestBuilders
|
||||
.multipart(ENDPOINT_TRANSFORM)
|
||||
.file(sourceFile)
|
||||
.param("targetExtension", targetExtension)
|
||||
.param("targetMimetype", targetMimetype)
|
||||
.param("sourceMimetype", sourceMimetype)
|
||||
.param("thumbnail", "false")
|
||||
.param("resizeWidth", "321")
|
||||
.param("resizeHeight", "654")
|
||||
.param("commandOptions", "( horrible command / );"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().bytes(expectedTargetFileBytes))
|
||||
.andExpect(header().string("Content-Disposition",
|
||||
"attachment; filename*=UTF-8''transform." + targetExtension));
|
||||
.perform(MockMvcRequestBuilders
|
||||
.multipart(ENDPOINT_TRANSFORM)
|
||||
.file(sourceFile)
|
||||
.param("targetExtension", targetExtension)
|
||||
.param("targetMimetype", targetMimetype)
|
||||
.param("sourceMimetype", sourceMimetype)
|
||||
.param("thumbnail", "false")
|
||||
.param("resizeWidth", "321")
|
||||
.param("resizeHeight", "654")
|
||||
.param("commandOptions", "( horrible command / );"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().bytes(expectedTargetFileBytes))
|
||||
.andExpect(header().string("Content-Disposition",
|
||||
"attachment; filename*=UTF-8''transform." + targetExtension));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -346,9 +348,9 @@ public class ImageMagickTest extends AbstractBaseTest
|
||||
when(mockExecutionResult.getExitValue()).thenReturn(1);
|
||||
|
||||
mockMvc.perform(mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile, "targetExtension", "xxx"))
|
||||
.andExpect(status().is(BAD_REQUEST.value()))
|
||||
.andExpect(status()
|
||||
.reason(containsString("Transformer exit code was not 0: \nSTDERR")));
|
||||
.andExpect(status().is(BAD_REQUEST.value()))
|
||||
.andExpect(status()
|
||||
.reason(containsString("Transformer exit code was not 0: \nSTDERR")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -365,11 +367,11 @@ public class ImageMagickTest extends AbstractBaseTest
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set(CONTENT_DISPOSITION, "attachment; filename=quick." + sourceExtension);
|
||||
ResponseEntity<Resource> response = new ResponseEntity<>(new FileSystemResource(
|
||||
sourceFile), headers, OK);
|
||||
sourceFile), headers, OK);
|
||||
|
||||
when(sharedFileStoreClient.retrieveFile(sourceFileRef)).thenReturn(response);
|
||||
when(sharedFileStoreClient.saveFile(any()))
|
||||
.thenReturn(new FileRefResponse(new FileRefEntity(targetFileRef)));
|
||||
.thenReturn(new FileRefResponse(new FileRefEntity(targetFileRef)));
|
||||
when(mockExecutionResult.getExitValue()).thenReturn(0);
|
||||
|
||||
// Update the Transformation Request with any specific params before sending it
|
||||
@@ -378,16 +380,16 @@ public class ImageMagickTest extends AbstractBaseTest
|
||||
// Serialize and call the transformer
|
||||
String tr = objectMapper.writeValueAsString(transformRequest);
|
||||
String transformationReplyAsString = mockMvc
|
||||
.perform(MockMvcRequestBuilders
|
||||
.post(ENDPOINT_TRANSFORM)
|
||||
.header(ACCEPT, APPLICATION_JSON_VALUE)
|
||||
.header(CONTENT_TYPE, APPLICATION_JSON_VALUE)
|
||||
.content(tr))
|
||||
.andExpect(status().is(CREATED.value()))
|
||||
.andReturn().getResponse().getContentAsString();
|
||||
.perform(MockMvcRequestBuilders
|
||||
.post(ENDPOINT_TRANSFORM)
|
||||
.header(ACCEPT, APPLICATION_JSON_VALUE)
|
||||
.header(CONTENT_TYPE, APPLICATION_JSON_VALUE)
|
||||
.content(tr))
|
||||
.andExpect(status().is(CREATED.value()))
|
||||
.andReturn().getResponse().getContentAsString();
|
||||
|
||||
TransformReply transformReply = objectMapper.readValue(transformationReplyAsString,
|
||||
TransformReply.class);
|
||||
TransformReply.class);
|
||||
|
||||
// Assert the reply
|
||||
assertEquals(transformRequest.getRequestId(), transformReply.getRequestId());
|
||||
@@ -398,7 +400,7 @@ public class ImageMagickTest extends AbstractBaseTest
|
||||
@Test
|
||||
public void testOverridingExecutorPaths()
|
||||
{
|
||||
//System test property values can me modified in the pom.xml
|
||||
// System test property values can me modified in the pom.xml
|
||||
assertEquals(EXE, System.getProperty("IMAGEMAGICK_EXE"));
|
||||
assertEquals(DYN, System.getProperty("IMAGEMAGICK_DYN"));
|
||||
assertEquals(ROOT, System.getProperty("IMAGEMAGICK_ROOT"));
|
||||
|
@@ -30,8 +30,13 @@ import static java.text.MessageFormat.format;
|
||||
import static java.util.Collections.emptyMap;
|
||||
import static java.util.function.Function.identity;
|
||||
import static java.util.stream.Collectors.toMap;
|
||||
import static org.alfresco.transform.base.clients.HttpClient.sendTRequest;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.springframework.http.HttpStatus.OK;
|
||||
|
||||
import static org.alfresco.transform.base.clients.FileInfo.testFile;
|
||||
import static org.alfresco.transform.base.clients.HttpClient.sendTRequest;
|
||||
import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_BMP;
|
||||
import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_CGM;
|
||||
import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_GIF;
|
||||
@@ -64,18 +69,12 @@ import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_TIFF;
|
||||
import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_XBM;
|
||||
import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_XPM;
|
||||
import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_XWD;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.springframework.http.HttpStatus.OK;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
|
||||
import org.alfresco.transform.base.clients.FileInfo;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
@@ -84,10 +83,13 @@ import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
import org.alfresco.transform.base.clients.FileInfo;
|
||||
|
||||
/**
|
||||
* @author Cezar Leahu
|
||||
*/
|
||||
public class ImageMagickTransformationIT {
|
||||
public class ImageMagickTransformationIT
|
||||
{
|
||||
private static final Logger logger = LoggerFactory.getLogger(ImageMagickTransformationIT.class);
|
||||
private static final String ENGINE_URL = "http://localhost:8090";
|
||||
private static final List<Pair<String, String>> targetExtensions = new ImmutableList.Builder<Pair<String, String>>()
|
||||
@@ -152,38 +154,38 @@ public class ImageMagickTransformationIT {
|
||||
.build();
|
||||
|
||||
private static final Map<String, FileInfo> TEST_FILES = Stream.of(
|
||||
testFile(MIMETYPE_IMAGE_BMP, "bmp", "quick.bmp"),
|
||||
testFile(MIMETYPE_IMAGE_BMP, "bmp", "quick.bmp"),
|
||||
testFile(MIMETYPE_IMAGE_GIF, "gif", "quick.gif"),
|
||||
testFile(MIMETYPE_IMAGE_JPEG, "jpg", "quick.jpg"),
|
||||
testFile(MIMETYPE_IMAGE_PBM, "pbm", "quick.pbm"),
|
||||
testFile(MIMETYPE_IMAGE_PBM, "pbm", "quick.pbm"),
|
||||
testFile(MIMETYPE_IMAGE_PGM, "pgm", "quick.pgm"),
|
||||
testFile(MIMETYPE_IMAGE_PNG, "png", "quick.png"),
|
||||
testFile(MIMETYPE_IMAGE_PNG, "png", "quick.png"),
|
||||
testFile(MIMETYPE_IMAGE_PNM, "pnm", "quick.pnm"),
|
||||
testFile(MIMETYPE_IMAGE_PPM, "ppm", "quick.ppm"),
|
||||
testFile(MIMETYPE_IMAGE_PPM, "ppm", "quick.ppm"),
|
||||
testFile(MIMETYPE_IMAGE_XBM, "xbm", "quick.xbm"),
|
||||
testFile(MIMETYPE_IMAGE_XPM, "xpm", "quick.xpm"),
|
||||
testFile(MIMETYPE_IMAGE_XPM, "xpm", "quick.xpm"),
|
||||
testFile(MIMETYPE_IMAGE_PSD, "psd", "quick.psd"),
|
||||
testFile(MIMETYPE_IMAGE_TIFF, "tiff", "quick.tiff"),
|
||||
testFile(MIMETYPE_IMAGE_XWD, "xwd", "quick.xwd")
|
||||
).collect(toMap(FileInfo::getPath, identity()));
|
||||
testFile(MIMETYPE_IMAGE_TIFF, "tiff", "quick.tiff"),
|
||||
testFile(MIMETYPE_IMAGE_XWD, "xwd", "quick.xwd")).collect(toMap(FileInfo::getPath, identity()));
|
||||
|
||||
public static Stream<Pair<FileInfo, Pair<String,String>>> engineTransformations() {
|
||||
public static Stream<Pair<FileInfo, Pair<String, String>>> engineTransformations()
|
||||
{
|
||||
return Stream
|
||||
.of(
|
||||
allTargets("quick.bmp", targetExtensions),
|
||||
allTargets("quick.gif", targetExtensions),
|
||||
allTargets("quick.jpg", targetExtensions),
|
||||
allTargets("quick.pbm", targetExtensions),
|
||||
allTargets("quick.pgm", targetExtensions),
|
||||
allTargets("quick.png", targetExtensions),
|
||||
allTargets("quick.pnm", targetExtensions),
|
||||
allTargets("quick.ppm", targetExtensions),
|
||||
allTargets("quick.psd", targetExtensionsForPSD),
|
||||
allTargets("quick.tiff", targetExtensions),
|
||||
allTargets("quick.xbm", targetExtensions),
|
||||
allTargets("quick.xpm", targetExtensions),
|
||||
allTargets("quick.xwd", targetExtensions)
|
||||
).flatMap(identity());
|
||||
.of(
|
||||
allTargets("quick.bmp", targetExtensions),
|
||||
allTargets("quick.gif", targetExtensions),
|
||||
allTargets("quick.jpg", targetExtensions),
|
||||
allTargets("quick.pbm", targetExtensions),
|
||||
allTargets("quick.pgm", targetExtensions),
|
||||
allTargets("quick.png", targetExtensions),
|
||||
allTargets("quick.pnm", targetExtensions),
|
||||
allTargets("quick.ppm", targetExtensions),
|
||||
allTargets("quick.psd", targetExtensionsForPSD),
|
||||
allTargets("quick.tiff", targetExtensions),
|
||||
allTargets("quick.xbm", targetExtensions),
|
||||
allTargets("quick.xpm", targetExtensions),
|
||||
allTargets("quick.xwd", targetExtensions))
|
||||
.flatMap(identity());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@@ -194,13 +196,13 @@ public class ImageMagickTransformationIT {
|
||||
String targetExtension = entry.getRight().getLeft();
|
||||
String sourceMimetype = entry.getLeft().getMimeType();
|
||||
String targetMimetype = entry.getRight().getRight();
|
||||
|
||||
|
||||
final String descriptor = format("Transform ({0}, {1} -> {2}, {3})",
|
||||
sourceFile, sourceMimetype, targetMimetype, targetExtension);
|
||||
sourceFile, sourceMimetype, targetMimetype, targetExtension);
|
||||
try
|
||||
{
|
||||
final ResponseEntity<Resource> response = sendTRequest(ENGINE_URL, sourceFile, sourceMimetype,
|
||||
targetMimetype, targetExtension, emptyMap());
|
||||
targetMimetype, targetExtension, emptyMap());
|
||||
assertEquals(OK, response.getStatusCode(), descriptor);
|
||||
}
|
||||
catch (Exception e)
|
||||
@@ -209,10 +211,10 @@ public class ImageMagickTransformationIT {
|
||||
}
|
||||
}
|
||||
|
||||
private static Stream<Pair<FileInfo, Pair<String,String>>> allTargets(final String sourceFile, List<Pair<String,String>> targetExtensionsList)
|
||||
private static Stream<Pair<FileInfo, Pair<String, String>>> allTargets(final String sourceFile, List<Pair<String, String>> targetExtensionsList)
|
||||
{
|
||||
return targetExtensionsList
|
||||
.stream()
|
||||
.map(k -> Pair.of(TEST_FILES.get(sourceFile), k));
|
||||
.stream()
|
||||
.map(k -> Pair.of(TEST_FILES.get(sourceFile), k));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user