Save point: [skip ci]

* Testing the controller
This commit is contained in:
alandavis
2022-07-18 11:15:31 +01:00
parent e5728d5d44
commit d312a31c21
30 changed files with 541 additions and 222 deletions

View File

@@ -74,6 +74,11 @@
<artifactId>google-collections</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.6</version>
</dependency>
</dependencies>
<build>

View File

@@ -27,7 +27,7 @@
package org.alfresco.transform.base;
import org.alfresco.transform.base.logging.LogEntry;
import org.alfresco.transform.base.probes.ProbeTestTransform;
import org.alfresco.transform.base.probes.ProbeTransform;
import org.alfresco.transform.common.TransformException;
import org.alfresco.transform.config.TransformConfig;
import org.alfresco.transform.registry.TransformServiceRegistry;
@@ -66,11 +66,14 @@ import static java.text.MessageFormat.format;
import static org.alfresco.transform.common.RequestParamMap.CONFIG_VERSION;
import static org.alfresco.transform.common.RequestParamMap.CONFIG_VERSION_DEFAULT;
import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_ERROR;
import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_LIVE;
import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_LOG;
import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_READY;
import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_ROOT;
import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TEST;
import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM;
import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM_CONFIG;
import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_VERSION;
import static org.alfresco.transform.common.RequestParamMap.FILE;
import static org.alfresco.transform.common.RequestParamMap.SOURCE_MIMETYPE;
import static org.alfresco.transform.common.RequestParamMap.TARGET_MIMETYPE;
@@ -93,17 +96,17 @@ public class TransformController
private TransformServiceRegistry transformRegistry;
@Autowired
private TransformHandler transformHandler;
@Value("${transform.core.version}")
@Autowired
private String coreVersion;
private TransformEngine transformEngine;
ProbeTestTransform probeTestTransform;
TransformEngine transformEngine;
ProbeTransform probeTransform;
@PostConstruct
private void init()
{
transformEngine = transformHandler.getTransformEngine();
probeTestTransform = transformHandler.getProbeTestTransform();
probeTransform = transformHandler.getProbeTestTransform();
}
@EventListener(ApplicationReadyEvent.class)
@@ -122,7 +125,7 @@ public class TransformController
/**
* @return a string that may be used in client debug.
*/
@RequestMapping("/version")
@RequestMapping(ENDPOINT_VERSION)
@ResponseBody
public String version()
{
@@ -167,21 +170,21 @@ public class TransformController
/**
* Kubernetes readiness probe.
*/
@GetMapping("/ready")
@GetMapping(ENDPOINT_READY)
@ResponseBody
public String ready(HttpServletRequest request)
{
return probeTestTransform.doTransformOrNothing(request, false, this);
return probeTransform.doTransformOrNothing(request, false, this);
}
/**
* Kubernetes liveness probe.
*/
@GetMapping("/live")
@GetMapping(ENDPOINT_LIVE)
@ResponseBody
public String live(HttpServletRequest request)
{
return probeTestTransform.doTransformOrNothing(request, true, this);
return probeTransform.doTransformOrNothing(request, true, this);
}
@GetMapping(value = ENDPOINT_TRANSFORM_CONFIG)
@@ -197,8 +200,8 @@ public class TransformController
@PostMapping(value = ENDPOINT_TRANSFORM, consumes = MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<StreamingResponseBody> transform(HttpServletRequest request,
@RequestParam(value = FILE, required = false) MultipartFile sourceMultipartFile,
@RequestParam(value = SOURCE_MIMETYPE, required = false) String sourceMimetype,
@RequestParam(value = TARGET_MIMETYPE, required = false) String targetMimetype,
@RequestParam(value = SOURCE_MIMETYPE, required = true) String sourceMimetype,
@RequestParam(value = TARGET_MIMETYPE, required = true) String targetMimetype,
@RequestParam Map<String, String> requestParameters)
{
return transformHandler.handleHttpRequest(request, sourceMultipartFile, sourceMimetype,
@@ -270,7 +273,7 @@ public class TransformController
}
@ExceptionHandler(TransformException.class)
public ModelAndView transformExceptionWithMessage(HttpServletResponse response, TransformException e)
public ModelAndView handleTransformException(HttpServletResponse response, TransformException e)
throws IOException
{
final String message = e.getMessage();
@@ -278,7 +281,7 @@ public class TransformController
logger.error(message);
long time = LogEntry.setStatusCodeAndMessage(statusCode, message);
probeTestTransform.recordTransformTime(time);
probeTransform.recordTransformTime(time);
response.sendError(statusCode, message);
ModelAndView mav = new ModelAndView();

View File

@@ -26,7 +26,7 @@
*/
package org.alfresco.transform.base;
import org.alfresco.transform.base.probes.ProbeTestTransform;
import org.alfresco.transform.base.probes.ProbeTransform;
import org.alfresco.transform.common.TransformConfigResourceReader;
import org.alfresco.transform.config.TransformConfig;
@@ -55,7 +55,7 @@ public interface TransformEngine
TransformConfig getTransformConfig();
/**
* @return a ProbeTestTransform (will do a quick transform) for k8 liveness and readiness probes.
* @return a ProbeTransform (will do a quick transform) for k8 liveness and readiness probes.
*/
ProbeTestTransform getLivenessAndReadinessProbeTestTransform();
ProbeTransform getProbeTransform();
}

View File

@@ -30,7 +30,7 @@ import org.alfresco.transform.base.clients.AlfrescoSharedFileStoreClient;
import org.alfresco.transform.base.fs.FileManager;
import org.alfresco.transform.base.logging.LogEntry;
import org.alfresco.transform.base.model.FileRefResponse;
import org.alfresco.transform.base.probes.ProbeTestTransform;
import org.alfresco.transform.base.probes.ProbeTransform;
import org.alfresco.transform.base.util.OutputStreamLengthRecorder;
import org.alfresco.transform.client.model.InternalContext;
import org.alfresco.transform.client.model.TransformReply;
@@ -73,7 +73,6 @@ import java.util.concurrent.atomic.AtomicInteger;
import static java.util.stream.Collectors.joining;
import static org.alfresco.transform.base.fs.FileManager.createTargetFile;
import static org.alfresco.transform.base.fs.FileManager.deleteFile;
import static org.alfresco.transform.base.fs.FileManager.getDirectAccessUrlInputStream;
import static org.alfresco.transform.common.RequestParamMap.DIRECT_ACCESS_URL;
import static org.alfresco.transform.common.RequestParamMap.SOURCE_ENCODING;
@@ -112,7 +111,7 @@ public class TransformHandler
private AtomicInteger httpRequestCount = new AtomicInteger(1);
private TransformEngine transformEngine;
private ProbeTestTransform probeTestTransform;
private ProbeTransform probeTransform;
private Map<String, CustomTransformer> customTransformersByName = new HashMap<>();
@PostConstruct
@@ -147,7 +146,7 @@ public class TransformHandler
{
if (transformEngine != null)
{
probeTestTransform = transformEngine.getLivenessAndReadinessProbeTestTransform();
probeTransform = transformEngine.getProbeTransform();
}
}
@@ -170,9 +169,9 @@ public class TransformHandler
return transformEngine;
}
public ProbeTestTransform getProbeTestTransform()
public ProbeTransform getProbeTestTransform()
{
return probeTestTransform;
return probeTransform;
}
public ResponseEntity<StreamingResponseBody> handleHttpRequest(HttpServletRequest request,
@@ -191,7 +190,7 @@ public class TransformHandler
logger.debug("Processing request via HTTP endpoint. Params: sourceMimetype: '{}', targetMimetype: '{}', "
+ "requestParameters: {}", sourceMimetype, targetMimetype, requestParameters);
}
probeTestTransform.incrementTransformerCount();
probeTransform.incrementTransformerCount();
final String directUrl = requestParameters.getOrDefault(DIRECT_ACCESS_URL, "");
InputStream inputStream = new BufferedInputStream(directUrl.isBlank() ?
@@ -222,7 +221,7 @@ public class TransformHandler
LogEntry.setTargetSize(outputStream.getLength());
long time = LogEntry.setStatusCodeAndMessage(OK.value(), "Success");
probeTestTransform.recordTransformTime(time);
probeTransform.recordTransformTime(time);
transformerDebug.popTransform(reference, time);
}
catch (TransformException e)
@@ -252,7 +251,7 @@ public class TransformHandler
try
{
logger.trace("Received {}, timeout {} ms", request, timeout);
probeTestTransform.incrementTransformerCount();
probeTransform.incrementTransformerCount();
checkTransformRequestValid(request, reply);
inputStream = getInputStream(request, reply);
String targetMimetype = request.getTargetMediaType();
@@ -300,7 +299,7 @@ public class TransformHandler
deleteTmpFiles(transformManager);
closeInputStreamWithoutException(inputStream);
probeTestTransform.recordTransformTime(System.currentTimeMillis()-start);
probeTransform.recordTransformTime(System.currentTimeMillis()-start);
transformerDebug.popTransform(reply);
logger.trace("Sending successful {}, timeout {} ms", reply, timeout);

View File

@@ -50,7 +50,7 @@ public class TransformRegistryImpl extends AbstractTransformRegistry
@Autowired(required = false)
private List<TransformEngine> transformEngines;
@Value("${transform.core.version}")
@Autowired
private String coreVersion;
private TransformConfig transformConfigBeforeIncompleteTransformsAreRemoved;

View File

@@ -32,6 +32,8 @@ import org.alfresco.transform.base.clients.AlfrescoSharedFileStoreClient;
import org.alfresco.transform.common.TransformerDebug;
import org.alfresco.transform.messages.TransformRequestValidator;
import org.alfresco.transform.registry.TransformServiceRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@@ -41,6 +43,7 @@ import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM;
import static org.alfresco.transform.config.CoreFunction.standardizeCoreVersion;
@Configuration
@ComponentScan(
@@ -48,6 +51,9 @@ import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM;
excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = ".*Test.*"))
public class WebApplicationConfig implements WebMvcConfigurer
{
@Value("${transform.core.version}")
private String coreVersionString;
@Override
public void addInterceptors(InterceptorRegistry registry)
{
@@ -91,4 +97,10 @@ public class WebApplicationConfig implements WebMvcConfigurer
{
return new TransformerDebug().setIsTEngine(true);
}
@Bean
public String coreVersion()
{
return standardizeCoreVersion(coreVersionString);
}
}

View File

@@ -48,7 +48,7 @@ import org.slf4j.LoggerFactory;
public final class LogEntry
{
private static final Logger logger = LoggerFactory.getLogger(LogEntry.class);
// TODO allow ProbeTestTransform to find out if there are any transforms running longer than the max time.
// TODO allow ProbeTransform to find out if there are any transforms running longer than the max time.
private static final AtomicInteger count = new AtomicInteger(0);
private static final Deque<LogEntry> log = new ConcurrentLinkedDeque<>();

View File

@@ -81,9 +81,9 @@ import org.springframework.beans.factory.annotation.Autowired;
* <li>livenessTransformPeriodSeconds The number of seconds between test transforms done for live probes</li>
* </ul>
*/
public class ProbeTestTransform
public class ProbeTransform
{
private final Logger logger = LoggerFactory.getLogger(ProbeTestTransform.class);
private final Logger logger = LoggerFactory.getLogger(ProbeTransform.class);
@Autowired
private TransformServiceRegistry transformRegistry;
@@ -124,7 +124,7 @@ public class ProbeTestTransform
return maxTime;
}
public ProbeTestTransform(String sourceFilename, String targetFilename,
public ProbeTransform(String sourceFilename, String targetFilename,
String sourceMimetype, String targetMimetype, Map<String, String> transformOptions,
long expectedLength, long plusOrMinus, int livenessPercent, long maxTransforms, long maxTransformSeconds,
long livenessTransformPeriodSeconds)

View File

@@ -62,7 +62,7 @@ import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.alfresco.transform.base.probes.ProbeTestTransform;
import org.alfresco.transform.base.probes.ProbeTransform;
import org.alfresco.transform.client.model.InternalContext;
import org.alfresco.transform.client.model.TransformReply;
import org.alfresco.transform.client.model.TransformRequest;
@@ -124,7 +124,7 @@ public abstract class AbstractBaseTest
@SpyBean
protected TransformServiceRegistry transformRegistry;
@Value("${transform.core.version}")
@Autowired
private String coreVersion;
protected String sourceExtension;
@@ -152,9 +152,9 @@ public abstract class AbstractBaseTest
String targetExtension, String sourceMimetype,
boolean readTargetFileBytes) throws IOException;
protected ProbeTestTransform getProbeTestTransform()
protected ProbeTransform getProbeTestTransform()
{
return controller.probeTestTransform;
return controller.probeTransform;
}
protected abstract void updateTransformRequestWithSpecificOptions(TransformRequest transformRequest);
@@ -364,8 +364,8 @@ public abstract class AbstractBaseTest
@Test
public void calculateMaxTime() throws Exception
{
ProbeTestTransform probeTestTransform = controller.probeTestTransform;
probeTestTransform.setLivenessPercent(110);
ProbeTransform probeTransform = controller.probeTransform;
probeTransform.setLivenessPercent(110);
long[][] values = new long[][]{
{5000, 0, Long.MAX_VALUE}, // 1st transform is ignored
@@ -384,9 +384,9 @@ public abstract class AbstractBaseTest
long expectedNormalTime = v[1];
long expectedMaxTime = v[2];
probeTestTransform.calculateMaxTime(time, true);
assertEquals(expectedNormalTime, probeTestTransform.getNormalTime());
assertEquals(expectedMaxTime, probeTestTransform.getMaxTime());
probeTransform.calculateMaxTime(time, true);
assertEquals(expectedNormalTime, probeTransform.getNormalTime());
assertEquals(expectedMaxTime, probeTransform.getMaxTime());
}
}

View File

@@ -1,99 +0,0 @@
/*
* #%L
* Alfresco Transform Core
* %%
* Copyright (C) 2005 - 2022 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* -
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
* -
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* -
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* -
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.transform.base;
import org.alfresco.transform.base.components.TestTransformEngineTwoTransformers;
import org.alfresco.transform.base.components.TestTransformerPdf2Png;
import org.alfresco.transform.base.components.TestTransformerTxT2Pdf;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.nio.charset.StandardCharsets;
import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_PNG;
import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF;
import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN;
import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM;
import static org.alfresco.transform.common.RequestParamMap.SOURCE_MIMETYPE;
import static org.alfresco.transform.common.RequestParamMap.TARGET_MIMETYPE;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* Testing base t-engine TransformController functionality.
*/
@AutoConfigureMockMvc
@SpringBootTest(classes={org.alfresco.transform.base.Application.class})
@ContextConfiguration(classes = {
TestTransformerTxT2Pdf.class,
TestTransformerPdf2Png.class,
TestTransformEngineTwoTransformers.class})
public class TransformControllerTestWithBasicConfig
{
@Autowired
protected MockMvc mockMvc;
private void assertGoodTransform(String originalValue, String expectedValue, String sourceMimetype, String targetMimetype,
String expectedTargetExtension) throws Exception
{
mockMvc.perform(
MockMvcRequestBuilders.multipart(ENDPOINT_TRANSFORM)
.file(new MockMultipartFile("file", null, sourceMimetype,
originalValue.getBytes(StandardCharsets.UTF_8)))
.param(SOURCE_MIMETYPE, sourceMimetype)
.param(TARGET_MIMETYPE, targetMimetype))
.andExpect(request().asyncStarted())
.andDo(MvcResult::getAsyncResult)
.andExpect(status().isOk())
.andExpect(header().string("Content-Disposition",
"attachment; filename*=UTF-8''transform." + expectedTargetExtension))
.andExpect(content().string(expectedValue));
}
@Test
public void singleStepTransform() throws Exception
{
assertGoodTransform("Start", "Start -> TxT2Pdf()",
MIMETYPE_TEXT_PLAIN, MIMETYPE_PDF, "pdf");
}
@Test
public void pipelineTransform() throws Exception
{
assertGoodTransform("Start", "Start -> TxT2Pdf() -> Pdf2Png()",
MIMETYPE_TEXT_PLAIN, MIMETYPE_IMAGE_PNG, "png");
}
}

View File

@@ -0,0 +1,313 @@
/*
* #%L
* Alfresco Transform Core
* %%
* Copyright (C) 2005 - 2022 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* -
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
* -
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* -
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* -
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.transform.base;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.alfresco.transform.base.components.TestTransformEngineTwoTransformers;
import org.alfresco.transform.base.components.TestTransformerPdf2Png;
import org.alfresco.transform.base.components.TestTransformerTxT2Pdf;
import org.alfresco.transform.config.TransformConfig;
import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.nio.charset.StandardCharsets;
import java.util.StringJoiner;
import java.util.stream.Collectors;
import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF;
import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN;
import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_ERROR;
import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_LIVE;
import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_LOG;
import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_READY;
import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_ROOT;
import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM;
import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM_CONFIG;
import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM_CONFIG_LATEST;
import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_VERSION;
import static org.alfresco.transform.common.RequestParamMap.FILE;
import static org.alfresco.transform.common.RequestParamMap.PAGE_REQUEST_PARAM;
import static org.alfresco.transform.common.RequestParamMap.SOURCE_MIMETYPE;
import static org.alfresco.transform.common.RequestParamMap.TARGET_MIMETYPE;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* Testing base t-engine TransformController functionality.
*/
@AutoConfigureMockMvc
@SpringBootTest(classes={org.alfresco.transform.base.Application.class})
@ContextConfiguration(classes = {
TestTransformEngineTwoTransformers.class,
TestTransformerTxT2Pdf.class,
TestTransformerPdf2Png.class})
public class TransformControllerWithSingleEngineTest
{
@Autowired
private MockMvc mockMvc;
@Autowired
private TransformController transformController;
@Autowired
protected ObjectMapper objectMapper;
@Autowired
private String coreVersion;
@Test
public void initEngine() throws Exception
{
assertEquals(TestTransformEngineTwoTransformers.class.getSimpleName(),
transformController.transformEngine.getClass().getSimpleName());
assertNotNull(transformController.probeTransform);
}
@Test
public void startupLogsIncludeEngineMessages() throws Exception
{
StringJoiner controllerLogMessages = getLogMessagesFor(TransformController.class);
transformController.startup();
assertEquals(
"--------------------------------------------------------------------------------------------------------------------------------------------------------------\n"
+ "Startup TwoTransformers\n"
+ "Line 2 TwoTransformers\n"
+ "Line 3\n"
+ "--------------------------------------------------------------------------------------------------------------------------------------------------------------\n"
+ "Starting application components... Done",
controllerLogMessages.toString());
}
private StringJoiner getLogMessagesFor(Class classBeingLogged)
{
StringJoiner logMessages = new StringJoiner("\n");
Logger logger = (Logger) LoggerFactory.getLogger(classBeingLogged);
AppenderBase<ILoggingEvent> logAppender = new AppenderBase<>()
{
@Override
protected void append(ILoggingEvent iLoggingEvent)
{
logMessages.add(iLoggingEvent.getMessage());
}
};
logAppender.setContext((LoggerContext)LoggerFactory.getILoggerFactory());
logger.setLevel(Level.DEBUG);
logger.addAppender(logAppender);
logAppender.start();
return logMessages;
}
@Test
public void versionEndpointIncludesAvailable() throws Exception
{
mockMvc.perform(MockMvcRequestBuilders.get(ENDPOINT_VERSION))
.andExpect(status().isOk())
.andExpect(content().string("TwoTransformers "+coreVersion+" available"));
}
@Test
public void testRootEndpointReturnsTestPage() throws Exception
{
mockMvc.perform(MockMvcRequestBuilders.get(ENDPOINT_ROOT))
.andExpect(status().isOk())
.andExpect(content().string(containsString("TwoTransformers Test Page")));
}
@Test
public void testErrorEndpointReturnsErrorPage() throws Exception
{
mockMvc.perform(MockMvcRequestBuilders.get(ENDPOINT_ERROR))
.andExpect(status().isOk())
.andExpect(content().string(containsString("TwoTransformers Error Page")));
}
@Test
public void testLogEndpointReturnsLogPage() throws Exception
{
mockMvc.perform(MockMvcRequestBuilders.get(ENDPOINT_LOG))
.andExpect(status().isOk())
.andExpect(content().string(containsString("TwoTransformers Log Entries")));
}
@Test
public void testReadyEndpointReturnsSuccessful() throws Exception
{
mockMvc.perform(MockMvcRequestBuilders.get(ENDPOINT_READY))
.andExpect(status().isOk())
.andExpect(content().string(containsString("Success - ")));
}
@Test
public void testLiveEndpointReturnsSuccessful() throws Exception
{
mockMvc.perform(MockMvcRequestBuilders.get(ENDPOINT_LIVE))
.andExpect(status().isOk())
.andExpect(content().string(containsString("Success - ")));
}
@Test
public void testConfigEndpointReturnsOriginalConfigFormat() throws Exception
{
// Includes Txt2PngViaPdf as Pdf2Jpg might exist in another t-engine
// coreValue is not set as this is the default version of config
// The transformer's options should not include directAccessUrl as this is the default version of config
assertConfig(ENDPOINT_TRANSFORM_CONFIG,
"Pdf2Png,null,imageOptions\n"
+ "TxT2Pdf,null,docOptions\n"
+ "Txt2JpgViaPdf,null,imageOptions\n"
+ "Txt2PngViaPdf,null,imageOptions",
"docOptions,imageOptions");
}
@Test
public void testConfigLatestEndpointReturnsCoreVersionAndDirectAccessUrlOption() throws Exception
{
assertConfig(ENDPOINT_TRANSFORM_CONFIG_LATEST,
"Pdf2Png,"+coreVersion+",directAccessUrl,imageOptions\n"
+ "TxT2Pdf,"+coreVersion+",directAccessUrl,docOptions\n"
+ "Txt2JpgViaPdf,null,imageOptions\n"
+ "Txt2PngViaPdf,"+coreVersion+",directAccessUrl,imageOptions",
"directAccessUrl,docOptions,imageOptions");
}
private void assertConfig(String url, String expectedTransformers, String expectedOptions) throws Exception
{
TransformConfig config = objectMapper.readValue(
mockMvc.perform(MockMvcRequestBuilders.get(url))
.andExpect(status().isOk())
.andReturn()
.getResponse()
.getContentAsString(), TransformConfig.class);
// Gets a list of transformerNames,coreVersion,optionNames
assertEquals(expectedTransformers,
config.getTransformers().stream()
.map(t -> t.getTransformerName()+","
+t.getCoreVersion()+","
+t.getTransformOptions().stream().sorted().collect(Collectors.joining(",")))
.sorted()
.collect(Collectors.joining("\n")));
assertEquals(expectedOptions,
config.getTransformOptions().keySet().stream()
.sorted()
.collect(Collectors.joining(",")));
}
@Test
public void testTransformEndpoint() throws Exception
{
mockMvc.perform(
MockMvcRequestBuilders.multipart(ENDPOINT_TRANSFORM)
.file(new MockMultipartFile("file", null, MIMETYPE_TEXT_PLAIN,
"Start".getBytes(StandardCharsets.UTF_8)))
.param(SOURCE_MIMETYPE, MIMETYPE_TEXT_PLAIN)
.param(TARGET_MIMETYPE, MIMETYPE_PDF)
.param(PAGE_REQUEST_PARAM, "1"))
.andExpect(request().asyncStarted())
.andDo(MvcResult::getAsyncResult)
.andExpect(status().isOk())
.andExpect(header().string("Content-Disposition",
"attachment; filename*=UTF-8''transform." + "pdf"))
.andExpect(content().string("Start -> TxT2Pdf(page=1)"));
}
@Test
public void testTestTransformEndpointConvertsRequestParameters() throws Exception
{
// TODO
}
@Test
public void testInterceptOfTypeMismatchException() throws Exception
{
String message =
mockMvc.perform(
MockMvcRequestBuilders.get(ENDPOINT_TRANSFORM+"?"+FILE+"=NotaMultipartFile"))
// .param(SOURCE_MIMETYPE, MIMETYPE_TEXT_PLAIN)
// .param(TARGET_MIMETYPE, MIMETYPE_PDF)
// .param(PAGE_REQUEST_PARAM, "1")
.andExpect(status().isBadRequest())
.andReturn()
.getResponse()
.getContentAsString();
assertTrue(message.contains("Request parameter "+PAGE_REQUEST_PARAM+" is of the wrong type"));
}
@Test
public void testInterceptOfMissingServletRequestParameterException() throws Exception
{
mockMvc.perform(
MockMvcRequestBuilders.multipart(ENDPOINT_TRANSFORM)
.file(new MockMultipartFile("file", null, MIMETYPE_TEXT_PLAIN,
"Start".getBytes(StandardCharsets.UTF_8))))
.andExpect(status().isBadRequest())
.andExpect(status().reason(containsString("Request parameter '"+SOURCE_MIMETYPE+"' is missing")));
}
@Test
public void testInterceptOfTransformException() throws Exception
{
mockMvc.perform(
MockMvcRequestBuilders.multipart(ENDPOINT_TRANSFORM)
.file(new MockMultipartFile("file", null, MIMETYPE_TEXT_PLAIN,
"Start".getBytes(StandardCharsets.UTF_8)))
.param(SOURCE_MIMETYPE, MIMETYPE_TEXT_PLAIN)
.param(TARGET_MIMETYPE, MIMETYPE_PDF)
.param("unknown", "1"))
// .andExpect(status().isBadRequest())
.andExpect(status().reason(containsString("No transforms were able to handle the request")));
// mockMvc.perform(
// MockMvcRequestBuilders.multipart(ENDPOINT_TRANSFORM)
// .file(new MockMultipartFile("file", null, MIMETYPE_TEXT_PLAIN,
// "Start".getBytes(StandardCharsets.UTF_8))))
// .andExpect(status().isBadRequest())
// .andExpect(status().reason(containsString("Request parameter '"+SOURCE_MIMETYPE+"' is missing")));
}
}

View File

@@ -30,7 +30,7 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.alfresco.transform.base.TransformEngine;
import org.alfresco.transform.base.probes.ProbeTestTransform;
import org.alfresco.transform.base.probes.ProbeTransform;
import org.alfresco.transform.config.SupportedSourceAndTarget;
import org.alfresco.transform.config.TransformConfig;
import org.alfresco.transform.config.TransformOptionValue;
@@ -57,46 +57,16 @@ public abstract class AbstractTestTransformEngine implements TransformEngine
@Override public String getStartupMessage()
{
return "Startup";
return "Startup "+getTransformEngineName()+
"\nLine 2 "+getTransformEngineName()+
"\nLine 3";
}
@Override public TransformConfig getTransformConfig()
@Override public ProbeTransform getProbeTransform()
{
String docOptions = "docOptions";
String imageOptions = "imageOptions";
return TransformConfig.builder()
// .withTransformOptions(ImmutableMap.of(
// docOptions, ImmutableSet.of(
// new TransformOptionValue(false, "page")),
// imageOptions, ImmutableSet.of(
// new TransformOptionValue(false, "width"),
// new TransformOptionValue(false, "height"))))
.withTransformers(ImmutableList.of(
Transformer.builder()
.withTransformerName("TxT2Pdf")
.withSupportedSourceAndTargetList(ImmutableSet.of(
SupportedSourceAndTarget.builder()
.withSourceMediaType(MIMETYPE_TEXT_PLAIN)
.withTargetMediaType(MIMETYPE_PDF)
.build()))
// .withTransformOptions(ImmutableSet.of(docOptions))
.build(),
Transformer.builder()
.withTransformerName("Pdf2Png")
.withSupportedSourceAndTargetList(ImmutableSet.of(
SupportedSourceAndTarget.builder()
.withSourceMediaType(MIMETYPE_PDF)
.withTargetMediaType(MIMETYPE_IMAGE_PNG)
.build()))
// .withTransformOptions(ImmutableSet.of(imageOptions))
.build()))
.build();
}
@Override public ProbeTestTransform getLivenessAndReadinessProbeTestTransform()
{
return new ProbeTestTransform("quick.html", "quick.txt",
return new ProbeTransform("quick.html", "quick.txt",
MIMETYPE_HTML, MIMETYPE_TEXT_PLAIN, ImmutableMap.of(SOURCE_ENCODING, "UTF-8"),
119, 30, 150, 1024, 60 * 2 + 1, 60 * 2);
119, 30, 150, 1024, 60 * 2 + 1,
60 * 2);
}
}

View File

@@ -26,6 +26,78 @@
*/
package org.alfresco.transform.base.components;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.alfresco.transform.config.SupportedSourceAndTarget;
import org.alfresco.transform.config.TransformConfig;
import org.alfresco.transform.config.TransformOptionValue;
import org.alfresco.transform.config.TransformStep;
import org.alfresco.transform.config.Transformer;
import java.util.List;
import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_JPEG;
import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_PNG;
import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF;
import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN;
public class TestTransformEngineTwoTransformers extends AbstractTestTransformEngine
{
@Override public TransformConfig getTransformConfig()
{
String docOptions = "docOptions";
String imageOptions = "imageOptions";
return TransformConfig.builder()
.withTransformOptions(ImmutableMap.of(
docOptions, ImmutableSet.of(
new TransformOptionValue(false, "page")),
imageOptions, ImmutableSet.of(
new TransformOptionValue(false, "width"),
new TransformOptionValue(false, "height"))))
.withTransformers(ImmutableList.of(
Transformer.builder()
.withTransformerName("TxT2Pdf")
.withSupportedSourceAndTargetList(ImmutableSet.of(
SupportedSourceAndTarget.builder()
.withSourceMediaType(MIMETYPE_TEXT_PLAIN)
.withTargetMediaType(MIMETYPE_PDF)
.build()))
.withTransformOptions(ImmutableSet.of(docOptions))
.build(),
Transformer.builder()
.withTransformerName("Pdf2Png")
.withSupportedSourceAndTargetList(ImmutableSet.of(
SupportedSourceAndTarget.builder()
.withSourceMediaType(MIMETYPE_PDF)
.withTargetMediaType(MIMETYPE_IMAGE_PNG)
.build()))
.withTransformOptions(ImmutableSet.of(imageOptions))
.build(),
Transformer.builder()
.withTransformerName("Txt2PngViaPdf")
.withTransformerPipeline(List.of(
new TransformStep("TxT2Pdf", MIMETYPE_PDF),
new TransformStep("Pdf2Png", null)))
.withSupportedSourceAndTargetList(ImmutableSet.of(
SupportedSourceAndTarget.builder()
.withSourceMediaType(MIMETYPE_TEXT_PLAIN)
.withTargetMediaType(MIMETYPE_PDF)
.build()))
.withTransformOptions(ImmutableSet.of(imageOptions))
.build(),
Transformer.builder() // Unavailable until Pdf2Jpg is added
.withTransformerName("Txt2JpgViaPdf")
.withTransformerPipeline(List.of(
new TransformStep("TxT2Pdf", MIMETYPE_PDF),
new TransformStep("Pdf2Jpg", null)))
.withSupportedSourceAndTargetList(ImmutableSet.of(
SupportedSourceAndTarget.builder()
.withSourceMediaType(MIMETYPE_TEXT_PLAIN)
.withTargetMediaType(MIMETYPE_IMAGE_JPEG)
.build()))
.withTransformOptions(ImmutableSet.of(imageOptions))
.build()))
.build();
}
}

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<root level="error">
<appender-ref ref="CONSOLE"/>
</root>
</configuration>