use of uk.gov.justice.services.adapter.rest.multipart.FileInputDetails in project microservice_framework by CJSCommonPlatform.
the class DefaultRestProcessor method process.
private Response process(final String responseStrategyName, final Function<InterceptorContext, Optional<JsonEnvelope>> interceptorChain, final String action, final Optional<JsonObject> initialPayload, final HttpHeaders headers, final Collection<Parameter> params, final Optional<List<FileInputDetails>> fileInputDetails) {
traceLogger.trace(logger, () -> format("Processing REST message: %s", httpTraceLoggerHelper.toHttpHeaderTrace(headers)));
final JsonEnvelope envelope = envelopeBuilderFactory.builder().withInitialPayload(initialPayload).withAction(action).withHeaders(headers).withParams(params).build();
traceLogger.trace(logger, () -> format("REST message converted to envelope: %s", envelope));
final InterceptorContext interceptorContext = fileInputDetails.map(value -> fileBasedInterceptorContextFactory.create(value, envelope)).orElseGet(() -> interceptorContextWithInput(envelope));
final Optional<JsonEnvelope> result = interceptorChain.apply(interceptorContext);
traceLogger.trace(logger, () -> format("REST message processed: %s", envelope));
return responseStrategyCache.responseStrategyOf(responseStrategyName).responseFor(action, result);
}
use of uk.gov.justice.services.adapter.rest.multipart.FileInputDetails in project microservice_framework by CJSCommonPlatform.
the class MultipleFileInputDetailsServiceTest method shouldStoreTheInputStreamToTheFileStoreAndReturnTheCorrectJsonEnvelope.
@Test
public void shouldStoreTheInputStreamToTheFileStoreAndReturnTheCorrectJsonEnvelope() throws Exception {
final String fieldName_1 = "fieldName_1";
final UUID fileId_1 = randomUUID();
final String fieldName_2 = "fieldName_2";
final UUID fileId_2 = randomUUID();
final FileInputDetails fileInputDetails_1 = mock(FileInputDetails.class);
final FileInputDetails fileInputDetails_2 = mock(FileInputDetails.class);
when(fileInputDetails_1.getFieldName()).thenReturn(fieldName_1);
when(fileInputDetails_2.getFieldName()).thenReturn(fieldName_2);
when(fileInputDetailsHandler.store(fileInputDetails_1)).thenReturn(fileId_1);
when(fileInputDetailsHandler.store(fileInputDetails_2)).thenReturn(fileId_2);
final Map<String, UUID> results = multipleFileInputDetailsService.storeFileDetails(asList(fileInputDetails_1, fileInputDetails_2));
assertThat(results.size(), is(2));
assertThat(results, hasKey(fieldName_1));
assertThat(results.get(fieldName_1), is(fileId_1));
assertThat(results, hasKey(fieldName_2));
assertThat(results.get(fieldName_2), is(fileId_2));
}
use of uk.gov.justice.services.adapter.rest.multipart.FileInputDetails in project microservice_framework by CJSCommonPlatform.
the class SingleFileInputDetailsServiceTest method shouldThrowAFileStoreFailedExceptionIfStoringTheInputStreamFails.
@Test
public void shouldThrowAFileStoreFailedExceptionIfStoringTheInputStreamFails() throws Exception {
final FileServiceException fileServiceException = new FileServiceException("Ooops");
final FileInputDetails fileInputDetails = mock(FileInputDetails.class);
final JsonObject metadata = mock(JsonObject.class);
final InputStream inputStream = mock(InputStream.class);
when(fileInputDetails.getInputStream()).thenReturn(inputStream);
when(fileStorer.store(metadata, inputStream)).thenThrow(fileServiceException);
try {
singleFileInputDetailsService.store(fileInputDetails, metadata);
fail();
} catch (final FileStoreFailedException expected) {
assertThat(expected.getCause(), is(fileServiceException));
assertThat(expected.getMessage(), is("Failed to store file in FileStore"));
}
verify(inputStream).close();
}
use of uk.gov.justice.services.adapter.rest.multipart.FileInputDetails in project microservice_framework by CJSCommonPlatform.
the class DefaultRestProcessorTest method shouldCreateTheInterceptorContextUsingTheFileBasedInterceptorContextFactoryIfTheInputPartExists.
@Test
public void shouldCreateTheInterceptorContextUsingTheFileBasedInterceptorContextFactoryIfTheInputPartExists() throws Exception {
final List<FileInputDetails> fileInputDetails = emptyList();
final InterceptorContext interceptorContext = mock(InterceptorContext.class);
when(fileBasedInterceptorContextFactory.create(eq(fileInputDetails), any(JsonEnvelope.class))).thenReturn(interceptorContext);
when(responseStrategyCache.responseStrategyOf(anyString())).thenReturn(responseStrategy);
restProcessor.process(NOT_USED_RESPONSE_STRATEGY_NAME, interceptorChain, NOT_USED_ACTION, NOT_USED_HEADERS, NOT_USED_PATH_PARAMS, fileInputDetails);
final ArgumentCaptor<InterceptorContext> interceptorContextCaptor = forClass(InterceptorContext.class);
verify(interceptorChain).apply(interceptorContextCaptor.capture());
final InterceptorContext resultInterceptorContext = interceptorContextCaptor.getValue();
assertThat(resultInterceptorContext, equalTo(interceptorContext));
}
use of uk.gov.justice.services.adapter.rest.multipart.FileInputDetails in project microservice_framework by CJSCommonPlatform.
the class DefaultRestProcessorTest method shouldReturnResponseFromResponseStrategyForCallWithInputPart.
@Test
public void shouldReturnResponseFromResponseStrategyForCallWithInputPart() throws Exception {
final List<FileInputDetails> fileInputDetails = mock(List.class);
final InterceptorContext interceptorContext = mock(InterceptorContext.class);
when(fileBasedInterceptorContextFactory.create(eq(fileInputDetails), any(JsonEnvelope.class))).thenReturn(interceptorContext);
when(interceptorChain.apply(interceptorContext)).thenReturn(Optional.of(jsonEnvelope));
final String responseStrategyName = "someOtherStrategy123";
when(responseStrategyCache.responseStrategyOf(responseStrategyName)).thenReturn(responseStrategy);
when(responseStrategy.responseFor(NOT_USED_ACTION, Optional.of(jsonEnvelope))).thenReturn(response);
final Response result = restProcessor.process(responseStrategyName, interceptorChain, NOT_USED_ACTION, NOT_USED_HEADERS, NOT_USED_PATH_PARAMS, fileInputDetails);
verify(responseStrategy).responseFor(NOT_USED_ACTION, Optional.of(jsonEnvelope));
assertThat(result, equalTo(response));
}
Aggregations