Save point: [skip ci]

* test the /test endpoint
This commit is contained in:
alandavis
2022-07-18 13:27:46 +01:00
parent d312a31c21
commit c91e78fcf6
2 changed files with 66 additions and 53 deletions

View File

@@ -95,7 +95,7 @@ public class TransformController
@Autowired @Autowired
private TransformServiceRegistry transformRegistry; private TransformServiceRegistry transformRegistry;
@Autowired @Autowired
private TransformHandler transformHandler; TransformHandler transformHandler;
@Autowired @Autowired
private String coreVersion; private String coreVersion;
@@ -200,8 +200,8 @@ public class TransformController
@PostMapping(value = ENDPOINT_TRANSFORM, consumes = MULTIPART_FORM_DATA_VALUE) @PostMapping(value = ENDPOINT_TRANSFORM, consumes = MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<StreamingResponseBody> transform(HttpServletRequest request, public ResponseEntity<StreamingResponseBody> transform(HttpServletRequest request,
@RequestParam(value = FILE, required = false) MultipartFile sourceMultipartFile, @RequestParam(value = FILE, required = false) MultipartFile sourceMultipartFile,
@RequestParam(value = SOURCE_MIMETYPE, required = true) String sourceMimetype, @RequestParam(value = SOURCE_MIMETYPE) String sourceMimetype,
@RequestParam(value = TARGET_MIMETYPE, required = true) String targetMimetype, @RequestParam(value = TARGET_MIMETYPE) String targetMimetype,
@RequestParam Map<String, String> requestParameters) @RequestParam Map<String, String> requestParameters)
{ {
return transformHandler.handleHttpRequest(request, sourceMultipartFile, sourceMimetype, return transformHandler.handleHttpRequest(request, sourceMultipartFile, sourceMimetype,
@@ -250,19 +250,9 @@ public class TransformController
return value; return value;
} }
@ExceptionHandler(TypeMismatchException.class)
public void handleParamsTypeMismatch(HttpServletResponse response, MissingServletRequestParameterException e) throws IOException
{
final String message = format("Request parameter ''{0}'' is of the wrong type", e.getParameterName());
final int statusCode = BAD_REQUEST.value();
logger.error(message, e);
LogEntry.setStatusCodeAndMessage(statusCode, message);
response.sendError(statusCode, transformEngine.getTransformEngineName() + " - " + message);
}
@ExceptionHandler(MissingServletRequestParameterException.class) @ExceptionHandler(MissingServletRequestParameterException.class)
public void handleMissingParams(HttpServletResponse response, MissingServletRequestParameterException e) throws IOException public void handleMissingParams(HttpServletResponse response, MissingServletRequestParameterException e)
throws IOException
{ {
final String message = format("Request parameter ''{0}'' is missing", e.getParameterName()); final String message = format("Request parameter ''{0}'' is missing", e.getParameterName());
final int statusCode = BAD_REQUEST.value(); final int statusCode = BAD_REQUEST.value();

View File

@@ -32,6 +32,7 @@ import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase; import ch.qos.logback.core.AppenderBase;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
import org.alfresco.transform.base.components.TestTransformEngineTwoTransformers; import org.alfresco.transform.base.components.TestTransformEngineTwoTransformers;
import org.alfresco.transform.base.components.TestTransformerPdf2Png; import org.alfresco.transform.base.components.TestTransformerPdf2Png;
import org.alfresco.transform.base.components.TestTransformerTxT2Pdf; import org.alfresco.transform.base.components.TestTransformerTxT2Pdf;
@@ -39,7 +40,6 @@ import org.alfresco.transform.config.TransformConfig;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; 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.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockMultipartFile; import org.springframework.mock.web.MockMultipartFile;
@@ -52,6 +52,7 @@ import java.nio.charset.StandardCharsets;
import java.util.StringJoiner; import java.util.StringJoiner;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_BMP;
import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF; 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.Mimetype.MIMETYPE_TEXT_PLAIN;
import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_ERROR; import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_ERROR;
@@ -59,18 +60,23 @@ 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_LOG;
import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_READY; 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_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;
import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM_CONFIG; 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_TRANSFORM_CONFIG_LATEST;
import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_VERSION; 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.PAGE_REQUEST_PARAM;
import static org.alfresco.transform.common.RequestParamMap.SOURCE_ENCODING;
import static org.alfresco.transform.common.RequestParamMap.SOURCE_MIMETYPE; import static org.alfresco.transform.common.RequestParamMap.SOURCE_MIMETYPE;
import static org.alfresco.transform.common.RequestParamMap.TARGET_MIMETYPE; import static org.alfresco.transform.common.RequestParamMap.TARGET_MIMETYPE;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; 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.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
@@ -241,7 +247,7 @@ public class TransformControllerWithSingleEngineTest
@Test @Test
public void testTransformEndpoint() throws Exception public void testTransformEndpoint() throws Exception
{ {
mockMvc.perform( MvcResult mvcResult = mockMvc.perform(
MockMvcRequestBuilders.multipart(ENDPOINT_TRANSFORM) MockMvcRequestBuilders.multipart(ENDPOINT_TRANSFORM)
.file(new MockMultipartFile("file", null, MIMETYPE_TEXT_PLAIN, .file(new MockMultipartFile("file", null, MIMETYPE_TEXT_PLAIN,
"Start".getBytes(StandardCharsets.UTF_8))) "Start".getBytes(StandardCharsets.UTF_8)))
@@ -249,7 +255,9 @@ public class TransformControllerWithSingleEngineTest
.param(TARGET_MIMETYPE, MIMETYPE_PDF) .param(TARGET_MIMETYPE, MIMETYPE_PDF)
.param(PAGE_REQUEST_PARAM, "1")) .param(PAGE_REQUEST_PARAM, "1"))
.andExpect(request().asyncStarted()) .andExpect(request().asyncStarted())
.andDo(MvcResult::getAsyncResult) .andReturn();
mockMvc.perform(asyncDispatch(mvcResult))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(header().string("Content-Disposition", .andExpect(header().string("Content-Disposition",
"attachment; filename*=UTF-8''transform." + "pdf")) "attachment; filename*=UTF-8''transform." + "pdf"))
@@ -259,23 +267,41 @@ public class TransformControllerWithSingleEngineTest
@Test @Test
public void testTestTransformEndpointConvertsRequestParameters() throws Exception public void testTestTransformEndpointConvertsRequestParameters() throws Exception
{ {
// TODO TransformHandler orig = transformController.transformHandler;
} try
{
TransformHandler spy = spy(orig);
transformController.transformHandler = spy;
@Test MvcResult mvcResult = mockMvc.perform(
public void testInterceptOfTypeMismatchException() throws Exception MockMvcRequestBuilders.multipart(ENDPOINT_TEST)
{ .file(new MockMultipartFile("file", null, MIMETYPE_TEXT_PLAIN,
String message = "Start".getBytes(StandardCharsets.UTF_8)))
mockMvc.perform( .param(SOURCE_MIMETYPE, MIMETYPE_IMAGE_BMP)
MockMvcRequestBuilders.get(ENDPOINT_TRANSFORM+"?"+FILE+"=NotaMultipartFile")) .param("_"+SOURCE_MIMETYPE, MIMETYPE_TEXT_PLAIN)
// .param(SOURCE_MIMETYPE, MIMETYPE_TEXT_PLAIN) .param(TARGET_MIMETYPE, MIMETYPE_PDF)
// .param(TARGET_MIMETYPE, MIMETYPE_PDF) .param("_"+TARGET_MIMETYPE, "")
// .param(PAGE_REQUEST_PARAM, "1") .param(PAGE_REQUEST_PARAM, "replaced")
.andExpect(status().isBadRequest()) .param("name1", "hasNoValueSoRemoved").param("value1", "")
.andReturn() .param("name2", PAGE_REQUEST_PARAM).param("value2", "1")
.getResponse() .param("name3", SOURCE_ENCODING).param("value3", "UTF-8"))
.getContentAsString(); .andExpect(request().asyncStarted())
assertTrue(message.contains("Request parameter "+PAGE_REQUEST_PARAM+" is of the wrong type")); .andReturn();
// Do the dispatch, just in case not doing it leaves it in a strange state.
mockMvc.perform(asyncDispatch(mvcResult));
verify(spy).handleHttpRequest(any(), any(), eq(MIMETYPE_TEXT_PLAIN), eq(MIMETYPE_PDF),
eq(ImmutableMap.of(
SOURCE_MIMETYPE, MIMETYPE_TEXT_PLAIN,
TARGET_MIMETYPE, MIMETYPE_PDF,
PAGE_REQUEST_PARAM, "1",
SOURCE_ENCODING, "UTF-8")));
}
finally
{
transformController.transformHandler = orig;
}
} }
@Test @Test
@@ -290,24 +316,21 @@ public class TransformControllerWithSingleEngineTest
} }
@Test @Test
public void testInterceptOfTransformException() throws Exception public void testInterceptOfTransformException_noTransformers() throws Exception
{ {
mockMvc.perform( MvcResult mvcResult = mockMvc.perform(
MockMvcRequestBuilders.multipart(ENDPOINT_TRANSFORM) MockMvcRequestBuilders.multipart(ENDPOINT_TRANSFORM)
.file(new MockMultipartFile("file", null, MIMETYPE_TEXT_PLAIN, .file(new MockMultipartFile("file", null, MIMETYPE_TEXT_PLAIN,
"Start".getBytes(StandardCharsets.UTF_8))) "Start".getBytes(StandardCharsets.UTF_8)))
.param(SOURCE_MIMETYPE, MIMETYPE_TEXT_PLAIN) .param(SOURCE_MIMETYPE, MIMETYPE_TEXT_PLAIN)
.param(TARGET_MIMETYPE, MIMETYPE_PDF) .param(TARGET_MIMETYPE, MIMETYPE_PDF)
.param("unknown", "1")) .param("unknown", "1"))
// .andExpect(status().isBadRequest()) .andExpect(request().asyncStarted())
.andExpect(status().reason(containsString("No transforms were able to handle the request"))); .andReturn();
mockMvc.perform(asyncDispatch(mvcResult))
// mockMvc.perform( .andExpect(status().isBadRequest())
// MockMvcRequestBuilders.multipart(ENDPOINT_TRANSFORM) .andExpect(content().string(containsString("TwoTransformers Error Page")))
// .file(new MockMultipartFile("file", null, MIMETYPE_TEXT_PLAIN, .andExpect(content().string(containsString("No transforms were able to handle the request")));
// "Start".getBytes(StandardCharsets.UTF_8))))
// .andExpect(status().isBadRequest())
// .andExpect(status().reason(containsString("Request parameter '"+SOURCE_MIMETYPE+"' is missing")));
} }
} }