ATS-531 : Reformat code

This commit is contained in:
Cezar.Leahu
2019-08-14 22:21:06 +03:00
committed by CezarLeahu
parent 70ab0241dd
commit 485347729b
58 changed files with 1310 additions and 1074 deletions

View File

@@ -26,16 +26,20 @@
*/
package org.alfresco.transformer;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import static org.junit.Assert.assertEquals;
import static org.springframework.test.util.AssertionErrors.assertTrue;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
/**
* Super class for testing controllers with a server. Includes tests for the AbstractTransformerController itself.
* Note: Currently uses json rather than HTML as json is returned by this spring boot test harness.
@@ -64,7 +68,8 @@ public abstract class AbstractHttpRequestTest
@Test
public void logPageExists()
{
String result = restTemplate.getForObject("http://localhost:" + port + "/log", String.class);
String result = restTemplate.getForObject("http://localhost:" + port + "/log",
String.class);
String title = getTransformerName() + ' ' + "Log";
assertTrue("\"" + title + "\" should be part of the page title", result.contains(title));
@@ -73,10 +78,12 @@ public abstract class AbstractHttpRequestTest
@Test
public void errorPageExists()
{
String result = restTemplate.getForObject("http://localhost:" + port + "/error", String.class);
String result = restTemplate.getForObject("http://localhost:" + port + "/error",
String.class);
String title = getTransformerName() + ' ' + "Error Page";
assertTrue("\"" + title + "\" should be part of the page title", result.contains("Error Page"));
assertTrue("\"" + title + "\" should be part of the page title",
result.contains("Error Page"));
}
@Test
@@ -84,7 +91,7 @@ public abstract class AbstractHttpRequestTest
{
// Transformer name is not part of the title as this is checked by another handler
assertTransformError(false,
"Required request part 'file' is not present");
"Required request part 'file' is not present");
}
@Test
@@ -96,7 +103,7 @@ public abstract class AbstractHttpRequestTest
private void assertMissingParameter(String name)
{
assertTransformError(true,
getTransformerName() + " - Request parameter '" + name + "' is missing");
getTransformerName() + " - Request parameter '" + name + "' is missing");
}
private void assertTransformError(boolean addFile, String errorMessage)
@@ -104,12 +111,15 @@ public abstract class AbstractHttpRequestTest
LinkedMultiValueMap<String, Object> parameters = new LinkedMultiValueMap<>();
if (addFile)
{
parameters.add("file", new org.springframework.core.io.ClassPathResource("quick."+getSourceExtension()));
parameters.add("file",
new org.springframework.core.io.ClassPathResource("quick." + getSourceExtension()));
}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<LinkedMultiValueMap<String, Object>> entity = new HttpEntity<>(parameters, headers);
ResponseEntity<String> response = restTemplate.exchange("/transform", HttpMethod.POST, entity, String.class, "");
HttpEntity<LinkedMultiValueMap<String, Object>> entity = new HttpEntity<>(parameters,
headers);
ResponseEntity<String> response = restTemplate.exchange("/transform", HttpMethod.POST,
entity, String.class, "");
assertEquals(errorMessage, getErrorMessage(response.getBody()));
}
@@ -125,7 +135,7 @@ public abstract class AbstractHttpRequestTest
int j = content.indexOf("\",\"path\":", i);
if (j != -1)
{
message = content.substring(i+11, j);
message = content.substring(i + 11, j);
}
}
return message;

View File

@@ -54,11 +54,12 @@ public abstract class AbstractQueueTransformServiceIT
@Autowired
private JmsTemplate jmsTemplate;
private final ActiveMQQueue testingQueue = new ActiveMQQueue("org.alfresco.transform.engine.IT");
private final ActiveMQQueue testingQueue = new ActiveMQQueue(
"org.alfresco.transform.engine.IT");
@Test
public void queueTransformServiceIT() {
public void queueTransformServiceIT()
{
TransformRequest request = buildRequest();
jmsTemplate.convertAndSend(engineRequestQueue, request, m -> {

View File

@@ -30,9 +30,11 @@ import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;
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.status;
@@ -107,7 +109,8 @@ public abstract class AbstractTransformerControllerTest
protected abstract AbstractTransformerController getController();
protected abstract void updateTransformRequestWithSpecificOptions(TransformRequest transformRequest);
protected abstract void updateTransformRequestWithSpecificOptions(
TransformRequest transformRequest);
/**
* This method ends up being the core of the mock.
@@ -151,14 +154,17 @@ public abstract class AbstractTransformerControllerTest
URL testFileUrl = classLoader.getResource(testFilename);
if (required && testFileUrl == null)
{
throw new IOException("The test file " + testFilename + " does not exist in the resources directory");
throw new IOException("The test file " + testFilename +
" does not exist in the resources directory");
}
return testFileUrl == null ? null : new File(testFileUrl.getFile());
}
protected MockHttpServletRequestBuilder mockMvcRequest(String url, MockMultipartFile sourceFile, String... params)
protected MockHttpServletRequestBuilder mockMvcRequest(String url, MockMultipartFile sourceFile,
String... params)
{
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.multipart("/transform").file(sourceFile);
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.multipart("/transform").file(
sourceFile);
if (params.length % 2 != 0)
{
@@ -175,20 +181,24 @@ public abstract class AbstractTransformerControllerTest
@Test
public void simpleTransformTest() throws Exception
{
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
mockMvc.perform(
mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
.andExpect(status().is(OK.value()))
.andExpect(content().bytes(expectedTargetFileBytes))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick." + targetExtension));
.andExpect(header().string("Content-Disposition",
"attachment; filename*= UTF-8''quick." + targetExtension));
}
@Test
public void testDelayTest() throws Exception
{
long start = System.currentTimeMillis();
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension, "testDelay", "400"))
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension,
"testDelay", "400"))
.andExpect(status().is(OK.value()))
.andExpect(content().bytes(expectedTargetFileBytes))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick." + targetExtension));
.andExpect(header().string("Content-Disposition",
"attachment; filename*= UTF-8''quick." + targetExtension));
long ms = System.currentTimeMillis() - start;
System.out.println("Transform incluing test delay was " + ms);
assertTrue("Delay sending the result back was too small " + ms, ms >= 400);
@@ -206,24 +216,30 @@ public abstract class AbstractTransformerControllerTest
// Looks dangerous but is okay as we only use the final filename
public void dotDotSourceFilenameTest() throws Exception
{
sourceFile = new MockMultipartFile("file", "../quick." + sourceExtension, sourceMimetype, expectedSourceFileBytes);
sourceFile = new MockMultipartFile("file", "../quick." + sourceExtension, sourceMimetype,
expectedSourceFileBytes);
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
mockMvc.perform(
mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
.andExpect(status().is(OK.value()))
.andExpect(content().bytes(expectedTargetFileBytes))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick." + targetExtension));
.andExpect(header().string("Content-Disposition",
"attachment; filename*= UTF-8''quick." + targetExtension));
}
@Test
// Is okay, as the target filename is built up from the whole source filename and the targetExtenstion
public void noExtensionSourceFilenameTest() throws Exception
{
sourceFile = new MockMultipartFile("file", "../quick", sourceMimetype, expectedSourceFileBytes);
sourceFile = new MockMultipartFile("file", "../quick", sourceMimetype,
expectedSourceFileBytes);
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
mockMvc.perform(
mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
.andExpect(status().is(OK.value()))
.andExpect(content().bytes(expectedTargetFileBytes))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick." + targetExtension));
.andExpect(header().string("Content-Disposition",
"attachment; filename*= UTF-8''quick." + targetExtension));
}
@Test
@@ -232,7 +248,8 @@ public abstract class AbstractTransformerControllerTest
{
sourceFile = new MockMultipartFile("file", "abc/", sourceMimetype, expectedSourceFileBytes);
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
mockMvc.perform(
mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
.andExpect(status().is(BAD_REQUEST.value()))
.andExpect(status().reason(containsString("The source filename was not supplied")));
}
@@ -242,7 +259,8 @@ public abstract class AbstractTransformerControllerTest
{
sourceFile = new MockMultipartFile("file", "", sourceMimetype, expectedSourceFileBytes);
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
mockMvc.perform(
mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
.andExpect(status().is(BAD_REQUEST.value()))
.andExpect(status().reason(containsString("The source filename was not supplied")));
}
@@ -252,7 +270,8 @@ public abstract class AbstractTransformerControllerTest
{
mockMvc.perform(mockMvcRequest("/transform", sourceFile))
.andExpect(status().is(BAD_REQUEST.value()))
.andExpect(status().reason(containsString("Request parameter 'targetExtension' is missing")));
.andExpect(status().reason(
containsString("Request parameter 'targetExtension' is missing")));
}
@Test
@@ -296,12 +315,13 @@ public abstract class AbstractTransformerControllerTest
.perform(MockMvcRequestBuilders
.post("/transform")
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.content(tr))
.andExpect(status().is(BAD_REQUEST.value()))
.andReturn().getResponse().getContentAsString();
TransformReply transformReply = objectMapper.readValue(transformationReplyAsString, TransformReply.class);
TransformReply transformReply = objectMapper.readValue(transformationReplyAsString,
TransformReply.class);
// Assert the reply
assertEquals(BAD_REQUEST.value(), transformReply.getStatus());
@@ -317,9 +337,10 @@ public abstract class AbstractTransformerControllerTest
ReflectionTestUtils
.setField(AbstractTransformerController.class, "ENGINE_CONFIG", "engine_config.json");
String response = mockMvc.perform(MockMvcRequestBuilders.get("/transform/config"))
.andExpect(status().is(OK.value())).andExpect(
header().string(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE))
String response = mockMvc
.perform(MockMvcRequestBuilders.get("/transform/config"))
.andExpect(status().is(OK.value()))
.andExpect(header().string(CONTENT_TYPE, APPLICATION_JSON_UTF8_VALUE))
.andReturn().getResponse().getContentAsString();
TransformConfig transformConfig = objectMapper.readValue(response, TransformConfig.class);
@@ -335,9 +356,10 @@ public abstract class AbstractTransformerControllerTest
ReflectionTestUtils.setField(AbstractTransformerController.class, "ENGINE_CONFIG",
"engine_config_with_duplicates.json");
String response = mockMvc.perform(MockMvcRequestBuilders.get("/transform/config"))
.andExpect(status().is(OK.value())).andExpect(
header().string(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE))
String response = mockMvc
.perform(MockMvcRequestBuilders.get("/transform/config"))
.andExpect(status().is(OK.value()))
.andExpect(header().string(CONTENT_TYPE, APPLICATION_JSON_UTF8_VALUE))
.andReturn().getResponse().getContentAsString();
TransformConfig transformConfig = objectMapper.readValue(response, TransformConfig.class);
@@ -349,7 +371,6 @@ public abstract class AbstractTransformerControllerTest
transformConfig.getTransformers().get(0).getSupportedSourceAndTargetList().size());
assertEquals(1,
transformConfig.getTransformers().get(0).getTransformOptions().size());
}
@Test
@@ -363,9 +384,9 @@ public abstract class AbstractTransformerControllerTest
"engine_config_incomplete.json");
String response = mockMvc.perform(MockMvcRequestBuilders.get("/transform/config"))
.andExpect(status().is(OK.value())).andExpect(
header().string(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE))
.andReturn().getResponse().getContentAsString();
.andExpect(status().is(OK.value())).andExpect(
header().string(CONTENT_TYPE, APPLICATION_JSON_UTF8_VALUE))
.andReturn().getResponse().getContentAsString();
TransformConfig transformConfig = objectMapper.readValue(response, TransformConfig.class);
@@ -384,9 +405,10 @@ public abstract class AbstractTransformerControllerTest
ReflectionTestUtils.setField(AbstractTransformerController.class, "ENGINE_CONFIG",
"engine_config_no_transform_options.json");
String response = mockMvc.perform(MockMvcRequestBuilders.get("/transform/config"))
.andExpect(status().is(OK.value())).andExpect(
header().string(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE))
String response = mockMvc
.perform(MockMvcRequestBuilders.get("/transform/config"))
.andExpect(status().is(OK.value()))
.andExpect(header().string(CONTENT_TYPE, APPLICATION_JSON_UTF8_VALUE))
.andReturn().getResponse().getContentAsString();
TransformConfig transformConfig = objectMapper.readValue(response, TransformConfig.class);
@@ -405,7 +427,8 @@ public abstract class AbstractTransformerControllerTest
new TransformOptionValue(false, "page"),
new TransformOptionValue(false, "width"),
new TransformOptionGroup(false, transformOptionGroup));
Map<String, Set<TransformOption>> transformOptionsMap = ImmutableMap.of("engineXOptions", transformOptions);
Map<String, Set<TransformOption>> transformOptionsMap = ImmutableMap.of("engineXOptions",
transformOptions);
Transformer transformer = buildTransformer("application/pdf", "image/png", "engineXOptions",
"engineX");

View File

@@ -27,12 +27,16 @@
package org.alfresco.transformer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import org.alfresco.transform.client.model.TransformReply;
import org.alfresco.transform.client.model.TransformRequest;
import org.alfresco.transformer.messaging.TransformMessageConverter;
@@ -48,10 +52,6 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.jms.support.converter.MessageConversionException;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
public class QueueTransformServiceTest
{
@Mock
@@ -98,10 +98,13 @@ public class QueueTransformServiceTest
ActiveMQQueue destination = new ActiveMQQueue();
msg.setJMSReplyTo(destination);
TransformReply reply = TransformReply.builder()
.withStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()).withErrorDetails(
TransformReply reply = TransformReply
.builder()
.withStatus(HttpStatus.INTERNAL_SERVER_ERROR.value())
.withErrorDetails(
"JMS exception during T-Request deserialization of message with correlationID "
+ msg.getCorrelationId() + ": null").build();
+ msg.getCorrelationId() + ": null")
.build();
doReturn(null).when(transformMessageConverter).fromMessage(msg);
@@ -122,10 +125,13 @@ public class QueueTransformServiceTest
ActiveMQQueue destination = new ActiveMQQueue();
msg.setJMSReplyTo(destination);
TransformReply reply = TransformReply.builder().withStatus(HttpStatus.BAD_REQUEST.value())
TransformReply reply = TransformReply
.builder()
.withStatus(HttpStatus.BAD_REQUEST.value())
.withErrorDetails(
"Message conversion exception during T-Request deserialization of message with correlationID"
+ msg.getCorrelationId() + ": null").build();
+ msg.getCorrelationId() + ": null")
.build();
doThrow(MessageConversionException.class).when(transformMessageConverter).fromMessage(msg);
@@ -146,10 +152,12 @@ public class QueueTransformServiceTest
ActiveMQQueue destination = new ActiveMQQueue();
msg.setJMSReplyTo(destination);
TransformReply reply = TransformReply.builder()
TransformReply reply = TransformReply
.builder()
.withStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()).withErrorDetails(
"JMSException during T-Request deserialization of message with correlationID " + msg
.getCorrelationId() + ": null").build();
.getCorrelationId() + ": null")
.build();
doThrow(JMSException.class).when(transformMessageConverter).fromMessage(msg);
@@ -169,7 +177,9 @@ public class QueueTransformServiceTest
msg.setJMSReplyTo(destination);
TransformRequest request = new TransformRequest();
TransformReply reply = TransformReply.builder().withStatus(HttpStatus.CREATED.value())
TransformReply reply = TransformReply
.builder()
.withStatus(HttpStatus.CREATED.value())
.build();
doReturn(request).when(transformMessageConverter).fromMessage(msg);
@@ -208,8 +218,9 @@ public class QueueTransformServiceTest
doReturn(destination).when(msg).getJMSReplyTo();
TransformRequest request = new TransformRequest();
TransformReply reply = TransformReply.builder().withStatus(HttpStatus.CREATED.value())
.build();
TransformReply reply = TransformReply.builder()
.withStatus(HttpStatus.CREATED.value())
.build();
doReturn(request).when(transformMessageConverter).fromMessage(msg);
doReturn(new ResponseEntity<>(reply, HttpStatus.valueOf(reply.getStatus())))