ATS-532 : Code improvements (#89)

- move startup message from controllers to the Application classes (SpringBoot configuration beans)
- added static imports for most static variables and static methods
- simplified a few nested *if*s
- replaced Arrays.asList() with explicit immutable collections
- fixed a few IntelliJ code inspection warnings
This commit is contained in:
CezarLeahu
2019-08-18 18:45:14 +03:00
committed by GitHub
parent 485347729b
commit 22de0ce5df
40 changed files with 391 additions and 329 deletions

View File

@@ -27,6 +27,8 @@
package org.alfresco.transformer;
import static org.junit.Assert.assertEquals;
import static org.springframework.http.HttpMethod.POST;
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA;
import static org.springframework.test.util.AssertionErrors.assertTrue;
import org.junit.Test;
@@ -35,8 +37,6 @@ 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;
@@ -115,11 +115,11 @@ public abstract class AbstractHttpRequestTest
new org.springframework.core.io.ClassPathResource("quick." + getSourceExtension()));
}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.setContentType(MULTIPART_FORM_DATA);
HttpEntity<LinkedMultiValueMap<String, Object>> entity = new HttpEntity<>(parameters,
headers);
ResponseEntity<String> response = restTemplate.exchange("/transform", HttpMethod.POST,
entity, String.class, "");
ResponseEntity<String> response = restTemplate.exchange("/transform", POST, entity,
String.class, "");
assertEquals(errorMessage, getErrorMessage(response.getBody()));
}

View File

@@ -30,11 +30,13 @@ 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.ACCEPT;
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.http.MediaType.APPLICATION_JSON_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;
@@ -64,8 +66,6 @@ import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.servlet.MockMvc;
@@ -314,8 +314,8 @@ public abstract class AbstractTransformerControllerTest
String transformationReplyAsString = mockMvc
.perform(MockMvcRequestBuilders
.post("/transform")
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.header(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header(ACCEPT, APPLICATION_JSON_VALUE)
.header(CONTENT_TYPE, APPLICATION_JSON_VALUE)
.content(tr))
.andExpect(status().is(BAD_REQUEST.value()))
.andReturn().getResponse().getContentAsString();
@@ -383,10 +383,11 @@ public abstract class AbstractTransformerControllerTest
ReflectionTestUtils.setField(AbstractTransformerController.class, "ENGINE_CONFIG",
"engine_config_incomplete.json");
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();
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);

View File

@@ -32,6 +32,9 @@ 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 static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import javax.jms.Destination;
import javax.jms.JMSException;
@@ -100,7 +103,7 @@ public class QueueTransformServiceTest
TransformReply reply = TransformReply
.builder()
.withStatus(HttpStatus.INTERNAL_SERVER_ERROR.value())
.withStatus(INTERNAL_SERVER_ERROR.value())
.withErrorDetails(
"JMS exception during T-Request deserialization of message with correlationID "
+ msg.getCorrelationId() + ": null")
@@ -127,7 +130,7 @@ public class QueueTransformServiceTest
TransformReply reply = TransformReply
.builder()
.withStatus(HttpStatus.BAD_REQUEST.value())
.withStatus(BAD_REQUEST.value())
.withErrorDetails(
"Message conversion exception during T-Request deserialization of message with correlationID"
+ msg.getCorrelationId() + ": null")
@@ -154,9 +157,10 @@ public class QueueTransformServiceTest
TransformReply reply = TransformReply
.builder()
.withStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()).withErrorDetails(
"JMSException during T-Request deserialization of message with correlationID " + msg
.getCorrelationId() + ": null")
.withStatus(INTERNAL_SERVER_ERROR.value())
.withErrorDetails(
"JMSException during T-Request deserialization of message with correlationID " +
msg.getCorrelationId() + ": null")
.build();
doThrow(JMSException.class).when(transformMessageConverter).fromMessage(msg);
@@ -179,7 +183,7 @@ public class QueueTransformServiceTest
TransformRequest request = new TransformRequest();
TransformReply reply = TransformReply
.builder()
.withStatus(HttpStatus.CREATED.value())
.withStatus(CREATED.value())
.build();
doReturn(request).when(transformMessageConverter).fromMessage(msg);
@@ -218,9 +222,10 @@ 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(CREATED.value())
.build();
doReturn(request).when(transformMessageConverter).fromMessage(msg);
doReturn(new ResponseEntity<>(reply, HttpStatus.valueOf(reply.getStatus())))