Search in sources :

Example 6 with InterceptorContext

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

the class RestAdapterGenerator_POSTMethodBodyTest method shouldProcessAsynchronouslyIfAcceptedResponseTypePresent.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void shouldProcessAsynchronouslyIfAcceptedResponseTypePresent() throws Exception {
    final Map<String, org.raml.model.Response> responses = new HashMap<>();
    responses.put(valueOf(INTERNAL_SERVER_ERROR.getStatusCode()), response().build());
    responses.put(valueOf(BAD_REQUEST.getStatusCode()), response().build());
    responses.put(valueOf(ACCEPTED.getStatusCode()), response().build());
    generator.run(restRamlWithCommandApiDefaults().with(resource("/path").with(httpActionWithDefaultMapping(POST).withHttpActionOfDefaultRequestType().withResponsesFrom(responses))).build(), configurationWithBasePackage(BASE_PACKAGE, outputFolder, new CommonGeneratorProperties()));
    final Class<?> resourceClass = compiler.compiledClassOf(BASE_PACKAGE, "resource", "DefaultCommandApiPathResource");
    final Object resourceObject = getInstanceOf(resourceClass);
    final Method method = firstMethodOf(resourceClass).get();
    method.invoke(resourceObject, NOT_USED_JSONOBJECT);
    final ArgumentCaptor<Function> functionCaptor = ArgumentCaptor.forClass(Function.class);
    verify(restProcessor).process(anyString(), functionCaptor.capture(), anyString(), any(Optional.class), any(HttpHeaders.class), any(Collection.class));
    final JsonEnvelope envelope = mock(JsonEnvelope.class);
    final InterceptorContext interceptorContext = interceptorContextWithInput(envelope);
    functionCaptor.getValue().apply(interceptorContext);
    verify(interceptorChainProcessor).process(interceptorContext);
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders) ThreadLocalHttpHeaders(org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpHeaders) Optional(java.util.Optional) HashMap(java.util.HashMap) CommonGeneratorProperties(uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Matchers.anyString(org.mockito.Matchers.anyString) Method(java.lang.reflect.Method) Response(javax.ws.rs.core.Response) Function(java.util.function.Function) InterceptorContext(uk.gov.justice.services.core.interceptor.InterceptorContext) Collection(java.util.Collection) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 7 with InterceptorContext

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

the class EventBufferInterceptorTest method ensureStreamCLosedIfExceptionOccurs.

@Test
public void ensureStreamCLosedIfExceptionOccurs() throws Exception {
    final Deque<Interceptor> interceptors = new LinkedList<>();
    interceptors.add(eventBufferInterceptor);
    interceptors.add(new ExceptionThrowingInterceptor());
    target = new TestTarget();
    interceptorChain = new DefaultInterceptorChain(interceptors, target);
    final InterceptorContext inputContext = interceptorContextWithInput(envelope_1);
    final StreamCloseSpy streamSpy = new StreamCloseSpy();
    final Stream<JsonEnvelope> envelopes = Stream.of(this.envelope_1, envelope_2).onClose(streamSpy);
    when(eventBufferService.currentOrderedEventsWith(this.envelope_1)).thenReturn(envelopes);
    try {
        interceptorChain.processNext(inputContext);
    } catch (TestException expected) {
    // do nothing
    }
    assertThat(streamSpy.streamClosed(), is(true));
}
Also used : StreamCloseSpy(uk.gov.justice.services.test.utils.common.stream.StreamCloseSpy) InterceptorContext(uk.gov.justice.services.core.interceptor.InterceptorContext) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Interceptor(uk.gov.justice.services.core.interceptor.Interceptor) LinkedList(java.util.LinkedList) DefaultInterceptorChain(uk.gov.justice.services.core.interceptor.DefaultInterceptorChain) Test(org.junit.Test)

Example 8 with InterceptorContext

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

the class RetryInterceptorTest method shouldRetryStraightAwayForThreeAttemptsThenWaitBeforeRetry.

@Test
public void shouldRetryStraightAwayForThreeAttemptsThenWaitBeforeRetry() throws Exception {
    final InterceptorContext currentContext = interceptorContextWithInput(envelope().with(metadataWithRandomUUID("nameABC")).build());
    final InterceptorContext nextInChain = interceptorContextWithInput(mock(JsonEnvelope.class));
    when(interceptorChain.processNext(currentContext)).thenThrow(new OptimisticLockingRetryException("Locking Error")).thenThrow(new OptimisticLockingRetryException("Locking Error")).thenThrow(new OptimisticLockingRetryException("Locking Error")).thenThrow(new OptimisticLockingRetryException("Locking Error")).thenReturn(nextInChain);
    retryInterceptor.maxRetry = "5";
    retryInterceptor.waitTime = "1000";
    retryInterceptor.immediateRetries = "3";
    final long start = currentTimeMillis();
    assertThat(retryInterceptor.process(currentContext, interceptorChain), is(nextInChain));
    final long end = currentTimeMillis();
    final long runTime = end - start;
    assertThat(runTime > 999, is(true));
    assertThat(runTime < 1999, is(true));
}
Also used : InterceptorContext(uk.gov.justice.services.core.interceptor.InterceptorContext) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) OptimisticLockingRetryException(uk.gov.justice.services.eventsourcing.repository.jdbc.exception.OptimisticLockingRetryException) Test(org.junit.Test)

Example 9 with InterceptorContext

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

the class RetryInterceptorTest method shouldThrowExceptionIfRetryMaxValueIsExceeded.

@Test
public void shouldThrowExceptionIfRetryMaxValueIsExceeded() throws Exception {
    final UUID streamId = UUID.randomUUID();
    final JsonEnvelope envelope = envelope().with(metadataWithRandomUUID("nameABC").withStreamId(streamId)).build();
    final InterceptorContext currentContext = interceptorContextWithInput(envelope);
    when(interceptorChain.processNext(currentContext)).thenThrow(new OptimisticLockingRetryException("Locking Error"));
    retryInterceptor.maxRetry = "1";
    retryInterceptor.waitTime = "500";
    retryInterceptor.immediateRetries = "0";
    expectedException.expect(OptimisticLockingRetryFailedException.class);
    expectedException.expectMessage("Retry count of 1 exceeded for command " + envelope.metadata().asJsonObject());
    retryInterceptor.process(currentContext, interceptorChain);
}
Also used : InterceptorContext(uk.gov.justice.services.core.interceptor.InterceptorContext) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) UUID(java.util.UUID) MetadataBuilderFactory.metadataWithRandomUUID(uk.gov.justice.services.test.utils.core.messaging.MetadataBuilderFactory.metadataWithRandomUUID) OptimisticLockingRetryException(uk.gov.justice.services.eventsourcing.repository.jdbc.exception.OptimisticLockingRetryException) Test(org.junit.Test)

Example 10 with InterceptorContext

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

the class RetryInterceptorTest method shouldRetryIfExceptionThrownByDispatcher.

@Test
public void shouldRetryIfExceptionThrownByDispatcher() throws Exception {
    final InterceptorContext currentContext = interceptorContextWithInput(envelope().with(metadataWithRandomUUID("nameABC")).build());
    final InterceptorContext nextInChain = interceptorContextWithInput(mock(JsonEnvelope.class));
    when(interceptorChain.processNext(currentContext)).thenThrow(new OptimisticLockingRetryException("Locking Error")).thenReturn(nextInChain);
    retryInterceptor.maxRetry = "2";
    retryInterceptor.waitTime = "500";
    retryInterceptor.immediateRetries = "0";
    assertThat(retryInterceptor.process(currentContext, interceptorChain), is(nextInChain));
}
Also used : InterceptorContext(uk.gov.justice.services.core.interceptor.InterceptorContext) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) OptimisticLockingRetryException(uk.gov.justice.services.eventsourcing.repository.jdbc.exception.OptimisticLockingRetryException) 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