Search in sources :

Example 6 with FileInputDetails

use of uk.gov.justice.services.adapter.rest.multipart.FileInputDetails in project microservice_framework by CJSCommonPlatform.

the class FileBasedInterceptorContextFactoryTest method shouldCreateAnInterceptorContextContainingTheFileDetailsList.

@Test
@SuppressWarnings("unchecked")
public void shouldCreateAnInterceptorContextContainingTheFileDetailsList() throws Exception {
    final JsonEnvelope inputEnvelope = mock(JsonEnvelope.class);
    final FileInputDetails fileInputDetails_1 = mock(FileInputDetails.class);
    final FileInputDetails fileInputDetails_2 = mock(FileInputDetails.class);
    final List<FileInputDetails> fileInputDetailss = asList(fileInputDetails_1, fileInputDetails_2);
    final InterceptorContext interceptorContext = fileBasedInterceptorContextFactory.create(fileInputDetailss, inputEnvelope);
    assertThat(interceptorContext.inputEnvelope(), is(inputEnvelope));
    final Optional<Object> inputParameter = interceptorContext.getInputParameter(FILE_INPUT_DETAILS_LIST);
    assertThat(inputParameter.isPresent(), is(true));
    final List<FileInputDetails> fileInputDetailsList = (List<FileInputDetails>) inputParameter.get();
    assertThat(fileInputDetailsList.size(), is(2));
    assertThat(fileInputDetailsList, hasItem(fileInputDetails_1));
    assertThat(fileInputDetailsList, hasItem(fileInputDetails_2));
}
Also used : FileInputDetails(uk.gov.justice.services.adapter.rest.multipart.FileInputDetails) InterceptorContext(uk.gov.justice.services.core.interceptor.InterceptorContext) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Test(org.junit.Test)

Example 7 with FileInputDetails

use of uk.gov.justice.services.adapter.rest.multipart.FileInputDetails in project microservice_framework by CJSCommonPlatform.

the class InputStreamFileInterceptor method process.

@Override
@SuppressWarnings("unchecked")
public InterceptorContext process(final InterceptorContext interceptorContext, final InterceptorChain interceptorChain) {
    final Optional<Object> inputParameterOptional = interceptorContext.getInputParameter(FILE_INPUT_DETAILS_LIST);
    if (inputParameterOptional.isPresent()) {
        final List<FileInputDetails> fileInputDetails = (List<FileInputDetails>) inputParameterOptional.get();
        final Map<String, UUID> results = multipleFileInputDetailsService.storeFileDetails(fileInputDetails);
        final JsonEnvelope inputEnvelope = resultsHandler.addResultsTo(interceptorContext.inputEnvelope(), results);
        return interceptorChain.processNext(interceptorContext.copyWithInput(inputEnvelope));
    }
    return interceptorChain.processNext(interceptorContext);
}
Also used : FileInputDetails(uk.gov.justice.services.adapter.rest.multipart.FileInputDetails) List(java.util.List) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) UUID(java.util.UUID)

Example 8 with FileInputDetails

use of uk.gov.justice.services.adapter.rest.multipart.FileInputDetails in project microservice_framework by CJSCommonPlatform.

the class FileInputDetailsHandlerTest method shouldCreateMetadataAndStoreFile.

@Test
public void shouldCreateMetadataAndStoreFile() throws Exception {
    final String fileName = "fileName";
    final UUID fileId = randomUUID();
    final FileInputDetails fileInputDetails = mock(FileInputDetails.class);
    when(fileInputDetails.getFileName()).thenReturn(fileName);
    when(singleFileInputDetailsService.store(eq(fileInputDetails), metadataCaptor.capture())).thenReturn(fileId);
    assertThat(fileInputDetailsHandler.store(fileInputDetails), is(fileId));
    final JsonObject metadata = metadataCaptor.getValue();
    with(metadata.toString()).assertThat("$.fileName", is(fileName));
}
Also used : FileInputDetails(uk.gov.justice.services.adapter.rest.multipart.FileInputDetails) JsonObject(javax.json.JsonObject) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) Test(org.junit.Test)

Example 9 with FileInputDetails

use of uk.gov.justice.services.adapter.rest.multipart.FileInputDetails in project microservice_framework by CJSCommonPlatform.

the class InputStreamFileInterceptorTest method shouldStoreTheInputStreamInTheFileStoreAndUpdateTheInputEnvelope.

@Test
public void shouldStoreTheInputStreamInTheFileStoreAndUpdateTheInputEnvelope() throws Exception {
    final FileInputDetails fileInputDetails = mock(FileInputDetails.class);
    final List<FileInputDetails> fileInputDetailsList = singletonList(fileInputDetails);
    final Map<String, UUID> results = ImmutableMap.of("fieldName", randomUUID());
    final InterceptorContext inputInterceptorContext = createInterceptorContext(fileInputDetailsList);
    when(multipleFileInputDetailsService.storeFileDetails(fileInputDetailsList)).thenReturn(results);
    when(resultsHandler.addResultsTo(inputEnvelope, results)).thenReturn(inputEnvelope);
    final InterceptorContext outputInterceptorContext = interceptorChain.processNext(inputInterceptorContext);
    final Optional<JsonEnvelope> jsonEnvelope = outputInterceptorContext.outputEnvelope();
    assertThat(jsonEnvelope.isPresent(), is(true));
    assertThat(jsonEnvelope.get(), is(outputEnvelope));
    assertThat(resultJsonEnvelope, is(inputEnvelope));
}
Also used : FileInputDetails(uk.gov.justice.services.adapter.rest.multipart.FileInputDetails) InterceptorContext(uk.gov.justice.services.core.interceptor.InterceptorContext) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) Test(org.junit.Test)

Example 10 with FileInputDetails

use of uk.gov.justice.services.adapter.rest.multipart.FileInputDetails in project microservice_framework by CJSCommonPlatform.

the class SingleFileInputDetailsServiceTest method shouldLogAWarningIfClosingTheInputStreamFails.

@Test
public void shouldLogAWarningIfClosingTheInputStreamFails() throws Exception {
    final IOException ioException = new IOException("Ooops");
    final UUID fileId = randomUUID();
    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)).thenReturn(fileId);
    doThrow(ioException).when(inputStream).close();
    assertThat(singleFileInputDetailsService.store(fileInputDetails, metadata), is(fileId));
    verify(logger).warn("Error closing InputStream", ioException);
}
Also used : FileInputDetails(uk.gov.justice.services.adapter.rest.multipart.FileInputDetails) InputStream(java.io.InputStream) JsonObject(javax.json.JsonObject) IOException(java.io.IOException) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) Test(org.junit.Test)

Aggregations

FileInputDetails (uk.gov.justice.services.adapter.rest.multipart.FileInputDetails)11 Test (org.junit.Test)9 UUID (java.util.UUID)6 JsonEnvelope (uk.gov.justice.services.messaging.JsonEnvelope)6 UUID.randomUUID (java.util.UUID.randomUUID)5 JsonObject (javax.json.JsonObject)5 InterceptorContext (uk.gov.justice.services.core.interceptor.InterceptorContext)5 InputStream (java.io.InputStream)3 List (java.util.List)3 Response (javax.ws.rs.core.Response)2 IOException (java.io.IOException)1 String.format (java.lang.String.format)1 Arrays.asList (java.util.Arrays.asList)1 Collection (java.util.Collection)1 Optional (java.util.Optional)1 Optional.empty (java.util.Optional.empty)1 Function (java.util.function.Function)1 ApplicationScoped (javax.enterprise.context.ApplicationScoped)1 Inject (javax.inject.Inject)1 HttpHeaders (javax.ws.rs.core.HttpHeaders)1