ATS-434 : Implement /info endpoint in T-Engines (#44)

- implement '/info' endpoint
   - add engine_config files
   - use SNAPSHOT transform-model with new Transform Config models (TODO update after transform-model release)
   - remove 'tests' from travis stages
   - add new junits
   - add test resources
This commit is contained in:
DenisGabriela
2019-05-31 14:14:03 +03:00
committed by CezarLeahu
parent 9e0042e767
commit d2292f94a0
29 changed files with 641 additions and 3 deletions

View File

@@ -21,6 +21,7 @@
*/
package org.alfresco.transformer;
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;
import static org.alfresco.transformer.fs.FileManager.buildFile;
import static org.alfresco.transformer.fs.FileManager.createTargetFileName;
import static org.alfresco.transformer.fs.FileManager.deleteFile;
@@ -28,14 +29,18 @@ import static org.alfresco.transformer.fs.FileManager.getFilenameFromContentDisp
import static org.alfresco.transformer.fs.FileManager.save;
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_VALUE;
import java.io.File;
import java.io.IOException;
import java.util.stream.Collectors;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.alfresco.transform.client.model.TransformReply;
import org.alfresco.transform.client.model.TransformRequest;
import org.alfresco.transform.client.model.TransformRequestValidator;
import org.alfresco.transform.client.model.config.TransformConfig;
import org.alfresco.transformer.clients.AlfrescoSharedFileStoreClient;
import org.alfresco.transform.exceptions.TransformException;
import org.alfresco.transformer.logging.LogEntry;
@@ -44,6 +49,7 @@ import org.alfresco.util.TempFileProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
@@ -53,6 +59,7 @@ import org.springframework.util.StringUtils;
import org.springframework.validation.DirectFieldBindingResult;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@@ -89,6 +96,7 @@ import org.springframework.web.client.HttpClientErrorException;
public abstract class AbstractTransformerController implements TransformController
{
private static final Logger logger = LoggerFactory.getLogger(AbstractTransformerController.class);
private static String ENGINE_CONFIG = "engine_config.json";
@Autowired
private AlfrescoSharedFileStoreClient alfrescoSharedFileStoreClient;
@@ -96,6 +104,29 @@ public abstract class AbstractTransformerController implements TransformControll
@Autowired
private TransformRequestValidator transformRequestValidator;
@Autowired
private ObjectMapper objectMapper;
@GetMapping(value = "/info")
public ResponseEntity<TransformConfig> info()
{
logger.info("GET Transform Config.");
try
{
ClassPathResource classPathResource = new ClassPathResource(ENGINE_CONFIG);
File engineConfigFile = classPathResource.getFile();
TransformConfig transformConfig = objectMapper.setSerializationInclusion(NON_NULL)
.readValue(engineConfigFile, TransformConfig.class);
return new ResponseEntity<>(transformConfig, OK);
}
catch (IOException e)
{
throw new TransformException(INTERNAL_SERVER_ERROR.value(),
"Could not read Transform Config file.", e);
}
}
/**
* '/transform' endpoint which consumes and produces 'application/json'
*

View File

@@ -0,0 +1 @@
{}

View File

@@ -23,6 +23,7 @@ package org.alfresco.transformer;
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.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
@@ -38,23 +39,36 @@ import java.io.IOException;
import java.net.URL;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.alfresco.transform.client.model.TransformReply;
import org.alfresco.transform.client.model.TransformRequest;
import org.alfresco.transform.client.model.config.SupportedSourceAndTarget;
import org.alfresco.transform.client.model.config.TransformConfig;
import org.alfresco.transform.client.model.config.TransformOption;
import org.alfresco.transform.client.model.config.TransformOptionGroup;
import org.alfresco.transform.client.model.config.TransformOptionValue;
import org.alfresco.transform.client.model.config.Transformer;
import org.alfresco.transformer.clients.AlfrescoSharedFileStoreClient;
import org.alfresco.transformer.probes.ProbeTestTransform;
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.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
/**
* Super class for testing controllers without a server. Includes tests for the AbstractTransformerController itself.
@@ -287,4 +301,134 @@ public abstract class AbstractTransformerControllerTest
// Assert the reply
assertEquals(BAD_REQUEST.value(), transformReply.getStatus());
}
@Test
public void testGetTransformConfigInfo() throws Exception
{
TransformConfig expectedTransformConfig = objectMapper
.readValue(new ClassPathResource("engine_config.json").getFile(),
TransformConfig.class);
ReflectionTestUtils
.setField(AbstractTransformerController.class, "ENGINE_CONFIG", "engine_config.json");
String response = mockMvc.perform(MockMvcRequestBuilders.get("/info"))
.andExpect(status().is(OK.value())).andExpect(
header().string(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE))
.andReturn().getResponse().getContentAsString();
TransformConfig transformConfig = objectMapper.readValue(response, TransformConfig.class);
assertEquals(expectedTransformConfig, transformConfig);
}
@Test
public void testGetInfoFromConfigWithDuplicates() throws Exception
{
TransformConfig expectedResult = buildCompleteTransformConfig();
ReflectionTestUtils.setField(AbstractTransformerController.class, "ENGINE_CONFIG",
"engine_config_with_duplicates.json");
String response = mockMvc.perform(MockMvcRequestBuilders.get("/info"))
.andExpect(status().is(OK.value())).andExpect(
header().string(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE))
.andReturn().getResponse().getContentAsString();
TransformConfig transformConfig = objectMapper.readValue(response, TransformConfig.class);
assertNotNull(transformConfig);
assertEquals(expectedResult, transformConfig);
assertEquals(3, transformConfig.getTransformOptions().get("engineXOptions").size());
assertEquals(1,
transformConfig.getTransformers().get(0).getSupportedSourceAndTargetList().size());
assertEquals(1,
transformConfig.getTransformers().get(0).getTransformOptions().size());
}
@Test
public void testGetInfoFromConfigWithEmptyTransformOptions() throws Exception
{
Transformer transformer = buildTransformer("application/pdf", "image/png");
TransformConfig expectedResult = new TransformConfig();
expectedResult.setTransformers(ImmutableList.of(transformer));
ReflectionTestUtils.setField(AbstractTransformerController.class, "ENGINE_CONFIG",
"engine_config_incomplete.json");
String response = mockMvc.perform(MockMvcRequestBuilders.get("/info"))
.andExpect(status().is(OK.value())).andExpect(
header().string(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE))
.andReturn().getResponse().getContentAsString();
TransformConfig transformConfig = objectMapper.readValue(response, TransformConfig.class);
assertNotNull(transformConfig);
assertEquals(expectedResult, transformConfig);
}
@Test
public void testGetInfoFromConfigWithNoTransformOptions() throws Exception
{
Transformer transformer = buildTransformer("application/pdf", "image/png");
transformer.setTransformerName("engineX");
TransformConfig expectedResult = new TransformConfig();
expectedResult.setTransformers(ImmutableList.of(transformer));
ReflectionTestUtils.setField(AbstractTransformerController.class, "ENGINE_CONFIG",
"engine_config_no_transform_options.json");
String response = mockMvc.perform(MockMvcRequestBuilders.get("/info"))
.andExpect(status().is(OK.value())).andExpect(
header().string(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE))
.andReturn().getResponse().getContentAsString();
TransformConfig transformConfig = objectMapper.readValue(response, TransformConfig.class);
assertNotNull(transformConfig);
assertEquals(expectedResult, transformConfig);
}
private TransformConfig buildCompleteTransformConfig()
{
TransformConfig expectedResult = new TransformConfig();
Set<TransformOption> transformOptionGroup = ImmutableSet.of(
new TransformOptionValue(false, "cropGravity"));
Set<TransformOption> transformOptions = ImmutableSet.of(
new TransformOptionValue(false, "page"),
new TransformOptionValue(false, "width"),
new TransformOptionGroup(false, transformOptionGroup));
Map<String, Set<TransformOption>> transformOptionsMap = ImmutableMap.of("engineXOptions", transformOptions);
Transformer transformer = buildTransformer("application/pdf", "image/png", "engineXOptions",
"engineX");
List<Transformer> transformers = ImmutableList.of(transformer);
expectedResult.setTransformOptions(transformOptionsMap);
expectedResult.setTransformers(transformers);
return expectedResult;
}
private Transformer buildTransformer(String sourceMediaType, String targetMediaType,
String transformOptions, String transformerName)
{
Transformer transformer = buildTransformer(sourceMediaType, targetMediaType);
transformer.setTransformerName(transformerName);
transformer.setTransformOptions(ImmutableSet.of(transformOptions));
return transformer;
}
private Transformer buildTransformer(String sourceMediaType, String targetMediaType)
{
Set<SupportedSourceAndTarget> supportedSourceAndTargetList = ImmutableSet.of(
new SupportedSourceAndTarget(sourceMediaType, targetMediaType, -1));
Transformer transformer = new Transformer();
transformer.setSupportedSourceAndTargetList(supportedSourceAndTargetList);
return transformer;
}
}

View File

@@ -0,0 +1,22 @@
{
"transformOptions": {
"engineXOptions": [
{"value": {"name": "page"}},
{"value": {"name": "width"}},
{"group": {"transformOptions": [
{"value": {"name": "cropGravity"}}
]}}
]
},
"transformers": [
{
"transformerName": "engineX",
"supportedSourceAndTargetList": [
{"sourceMediaType": "application/pdf", "targetMediaType": "image/png" }
],
"transformOptions": [
"engineXOptions"
]
}
]
}

View File

@@ -0,0 +1,10 @@
{
"transformOptions": {},
"transformers": [
{
"supportedSourceAndTargetList": [
{"sourceMediaType": "application/pdf", "targetMediaType": "image/png" }
]
}
]
}

View File

@@ -0,0 +1,10 @@
{
"transformers": [
{
"transformerName": "engineX",
"supportedSourceAndTargetList": [
{"sourceMediaType": "application/pdf", "targetMediaType": "image/png" }
]
}
]
}

View File

@@ -0,0 +1,26 @@
{
"transformOptions": {
"engineXOptions": [
{"value": {"name": "page"}},
{"value": {"name": "page"}},
{"value": {"name": "width"}},
{"group": {"transformOptions": [
{"value": {"name": "cropGravity"}}
]}}
]
},
"transformers": [
{
"transformerName": "engineX",
"supportedSourceAndTargetList": [
{"sourceMediaType": "application/pdf", "targetMediaType": "image/png" },
{"sourceMediaType": "application/pdf", "targetMediaType": "image/png" },
{"sourceMediaType": "application/pdf", "targetMediaType": "image/png" }
],
"transformOptions": [
"engineXOptions",
"engineXOptions"
]
}
]
}