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);
}
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));
}
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));
}
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);
}
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));
}
Aggregations