Search in sources :

Example 31 with InterceptorContext

use of uk.gov.justice.services.core.interceptor.InterceptorContext in project microservice_framework by CJSCommonPlatform.

the class EventFilterInterceptorCodeGeneratorTest method nowTestTheGeneratedClass.

private void nowTestTheGeneratedClass(final Class<?> generatedClass) throws Exception {
    final String eventName = "an.event.name";
    final Interceptor interceptor = buildTheClassForTest(generatedClass, new MyCustomEventFilter(eventName));
    final InterceptorContext interceptorContext_1 = mock(InterceptorContext.class, "interceptorContext_1");
    final InterceptorContext interceptorContext_2 = mock(InterceptorContext.class, "interceptorContext_2");
    final InterceptorChain interceptorChain = mock(InterceptorChain.class);
    final JsonEnvelope jsonEnvelope = mock(JsonEnvelope.class);
    final Metadata metadata = mock(Metadata.class);
    when(interceptorContext_1.inputEnvelope()).thenReturn(jsonEnvelope);
    when(jsonEnvelope.metadata()).thenReturn(metadata);
    when(metadata.name()).thenReturn(eventName);
    when(interceptorChain.processNext(interceptorContext_1)).thenReturn(interceptorContext_2);
    assertThat(interceptor.process(interceptorContext_1, interceptorChain), is(interceptorContext_2));
}
Also used : InterceptorChain(uk.gov.justice.services.core.interceptor.InterceptorChain) InterceptorContext(uk.gov.justice.services.core.interceptor.InterceptorContext) Metadata(uk.gov.justice.services.messaging.Metadata) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Interceptor(uk.gov.justice.services.core.interceptor.Interceptor)

Example 32 with InterceptorContext

use of uk.gov.justice.services.core.interceptor.InterceptorContext in project microservice_framework by CJSCommonPlatform.

the class DefaultDirectAdapterProcessorTest method shouldApplyInterceptorChainFunctionToTheIncomingTheEnvelope.

@Test
public void shouldApplyInterceptorChainFunctionToTheIncomingTheEnvelope() throws Exception {
    when(interceptorChainFunction.apply(any(InterceptorContext.class))).thenReturn(Optional.of(envelope().build()));
    final JsonEnvelope incomingEnvelope = envelope().with(metadataWithRandomUUID("action1")).build();
    directAdapterProcessor.process(incomingEnvelope, interceptorChainFunction);
    ArgumentCaptor<InterceptorContext> interceptorContextCaptor = ArgumentCaptor.forClass(InterceptorContext.class);
    verify(interceptorChainFunction).apply(interceptorContextCaptor.capture());
    assertThat(interceptorContextCaptor.getValue().inputEnvelope(), is(incomingEnvelope));
}
Also used : InterceptorContext(uk.gov.justice.services.core.interceptor.InterceptorContext) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 33 with InterceptorContext

use of uk.gov.justice.services.core.interceptor.InterceptorContext in project microservice_framework by CJSCommonPlatform.

the class InputStreamFileInterceptorTest method shouldNotStoreTheInputStreamIfNoFileDetailsFoundInTheInterceptorContext.

@Test
public void shouldNotStoreTheInputStreamIfNoFileDetailsFoundInTheInterceptorContext() throws Exception {
    final InterceptorContext inputInterceptorContext = createEmptyInterceptorContext();
    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));
    verifyZeroInteractions(multipleFileInputDetailsService);
}
Also used : InterceptorContext(uk.gov.justice.services.core.interceptor.InterceptorContext) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 34 with InterceptorContext

use of uk.gov.justice.services.core.interceptor.InterceptorContext 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 35 with InterceptorContext

use of uk.gov.justice.services.core.interceptor.InterceptorContext in project microservice_framework by CJSCommonPlatform.

the class DefaultRestProcessorTest method shouldPassEnvelopeWithEmptyPayloadToInterceptorChain.

@Test
public void shouldPassEnvelopeWithEmptyPayloadToInterceptorChain() throws Exception {
    final String action = "actionABC";
    final String userId = "usrABC";
    final List<Parameter> pathParams = singletonList(DefaultParameter.valueOf("name", "value123", ParameterType.STRING));
    when(responseStrategyCache.responseStrategyOf(anyString())).thenReturn(responseStrategy);
    restProcessor.process(NOT_USED_RESPONSE_STRATEGY_NAME, interceptorChain, action, headersWithUserId(userId), pathParams);
    final ArgumentCaptor<InterceptorContext> interceptorContextCaptor = forClass(InterceptorContext.class);
    verify(interceptorChain).apply(interceptorContextCaptor.capture());
    final InterceptorContext interceptorContext = interceptorContextCaptor.getValue();
    final JsonEnvelope envelope = interceptorContext.inputEnvelope();
    assertThat(envelope, jsonEnvelope().withMetadataOf(metadata().withName(action).withUserId(userId)).withPayloadOf(payloadIsJson(withJsonPath("$.name", equalTo("value123")))));
}
Also used : InterceptorContext(uk.gov.justice.services.core.interceptor.InterceptorContext) DefaultParameter(uk.gov.justice.services.adapter.rest.parameter.DefaultParameter) Parameter(uk.gov.justice.services.adapter.rest.parameter.Parameter) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Aggregations

InterceptorContext (uk.gov.justice.services.core.interceptor.InterceptorContext)35 Test (org.junit.Test)29 JsonEnvelope (uk.gov.justice.services.messaging.JsonEnvelope)28 Collection (java.util.Collection)7 Function (java.util.function.Function)7 JsonObject (javax.json.JsonObject)7 HttpHeaders (javax.ws.rs.core.HttpHeaders)7 Method (java.lang.reflect.Method)6 Optional (java.util.Optional)6 CommonGeneratorProperties (uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties)6 FileInputDetails (uk.gov.justice.services.adapter.rest.multipart.FileInputDetails)5 Matchers.anyString (org.mockito.Matchers.anyString)4 Response (javax.ws.rs.core.Response)3 ThreadLocalHttpHeaders (org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpHeaders)3 Parameter (uk.gov.justice.services.adapter.rest.parameter.Parameter)3 Interceptor (uk.gov.justice.services.core.interceptor.Interceptor)3 OptimisticLockingRetryException (uk.gov.justice.services.eventsourcing.repository.jdbc.exception.OptimisticLockingRetryException)3 List (java.util.List)2 UUID (java.util.UUID)2 DefaultParameter (uk.gov.justice.services.adapter.rest.parameter.DefaultParameter)2