Search in sources :

Example 1 with FileInputDetails

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);
}
Also used : FileInputDetails(uk.gov.justice.services.adapter.rest.multipart.FileInputDetails) Optional.empty(java.util.Optional.empty) JsonObject(javax.json.JsonObject) Parameter(uk.gov.justice.services.adapter.rest.parameter.Parameter) Logger(org.slf4j.Logger) Collection(java.util.Collection) Function(java.util.function.Function) String.format(java.lang.String.format) InterceptorContext.interceptorContextWithInput(uk.gov.justice.services.core.interceptor.InterceptorContext.interceptorContextWithInput) RestEnvelopeBuilderFactory(uk.gov.justice.services.adapter.rest.envelope.RestEnvelopeBuilderFactory) HttpTraceLoggerHelper(uk.gov.justice.services.messaging.logging.HttpTraceLoggerHelper) Inject(javax.inject.Inject) FileBasedInterceptorContextFactory(uk.gov.justice.services.adapter.rest.multipart.FileBasedInterceptorContextFactory) List(java.util.List) HttpHeaders(javax.ws.rs.core.HttpHeaders) Response(javax.ws.rs.core.Response) InterceptorContext(uk.gov.justice.services.core.interceptor.InterceptorContext) TraceLogger(uk.gov.justice.services.messaging.logging.TraceLogger) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) InterceptorContext(uk.gov.justice.services.core.interceptor.InterceptorContext) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope)

Example 2 with FileInputDetails

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));
}
Also used : FileInputDetails(uk.gov.justice.services.adapter.rest.multipart.FileInputDetails) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) Test(org.junit.Test)

Example 3 with FileInputDetails

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();
}
Also used : FileServiceException(uk.gov.justice.services.fileservice.api.FileServiceException) FileInputDetails(uk.gov.justice.services.adapter.rest.multipart.FileInputDetails) InputStream(java.io.InputStream) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 4 with FileInputDetails

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));
}
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) Test(org.junit.Test)

Example 5 with FileInputDetails

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));
}
Also used : Response(javax.ws.rs.core.Response) FileInputDetails(uk.gov.justice.services.adapter.rest.multipart.FileInputDetails) InterceptorContext(uk.gov.justice.services.core.interceptor.InterceptorContext) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Matchers.anyString(org.mockito.Matchers.anyString) 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