Search in sources :

Example 16 with InterceptorContext

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

the class SubscriptionJmsEndpointGeneratorTest method shouldCallJmsProcessorWhenOnMessageIsInvoked.

@Test
@SuppressWarnings("unchecked")
public void shouldCallJmsProcessorWhenOnMessageIsInvoked() throws Exception {
    SubscriptionDescriptor subscriptionDescriptor = setUpMessageSubscription("jms:topic:somecontext.controller.command", "somecontext.command1", serviceName, componentName);
    generator.run(subscriptionDescriptor, configurationWithBasePackage(BASE_PACKAGE, outputFolder, generatorProperties));
    Class<?> clazz = compiler.compiledClassOf(BASE_PACKAGE, "ContextEventProcessorSomecontextControllerCommandJmsListener");
    Object object = instantiate(clazz);
    assertThat(object, is(instanceOf(MessageListener.class)));
    MessageListener jmsListener = (MessageListener) object;
    Message message = mock(Message.class);
    jmsListener.onMessage(message);
    ArgumentCaptor<Consumer> consumerCaptor = ArgumentCaptor.forClass(Consumer.class);
    verify(jmsProcessor).process(consumerCaptor.capture(), eq(message));
    JsonEnvelope envelope = mock(JsonEnvelope.class);
    final InterceptorContext interceptorContext = interceptorContextWithInput(envelope);
    consumerCaptor.getValue().accept(interceptorContext);
    verify(interceptorChainProcessor).process(interceptorContext);
}
Also used : Message(javax.jms.Message) Consumer(java.util.function.Consumer) InterceptorContext(uk.gov.justice.services.core.interceptor.InterceptorContext) MessageListener(javax.jms.MessageListener) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) SubscriptionDescriptor(uk.gov.justice.subscription.domain.SubscriptionDescriptor) Test(org.junit.Test)

Example 17 with InterceptorContext

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

the class EventFilterInterceptorCodeGeneratorTest method nowTestTheFailureCase.

private void nowTestTheFailureCase(final Class<?> generatedClass) throws Exception {
    final String eventName = "an.event.name";
    final String aDifferentventName = "a.different.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(aDifferentventName);
    when(interceptorChain.processNext(interceptorContext_1)).thenReturn(interceptorContext_2);
    assertThat(interceptor.process(interceptorContext_1, interceptorChain), is(interceptorContext_1));
}
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 18 with InterceptorContext

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

the class QueryViewDirectAdapterTest method shouldPassEnvelopeToInterceptorChain.

@Test
public void shouldPassEnvelopeToInterceptorChain() throws Exception {
    when(interceptorChainProcessor.process(any(InterceptorContext.class))).thenReturn(Optional.of(envelope().build()));
    final JsonEnvelope envelopePassedToAdapter = envelope().build();
    directAdapter.process(envelopePassedToAdapter);
    ArgumentCaptor<InterceptorContext> interceptorContext = ArgumentCaptor.forClass(InterceptorContext.class);
    verify(interceptorChainProcessor).process(interceptorContext.capture());
    assertThat(interceptorContext.getValue().inputEnvelope(), is(envelopePassedToAdapter));
}
Also used : InterceptorContext(uk.gov.justice.services.core.interceptor.InterceptorContext) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 19 with InterceptorContext

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

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

the class DefaultRestProcessorTest method shouldPassEnvelopeWithPayloadToInterceptorChain.

@Test
public void shouldPassEnvelopeWithPayloadToInterceptorChain() throws Exception {
    final String userId = "userId1234";
    final String action = "anAction123";
    final String payloadIdValue = "payloadIdValue1";
    final List<Parameter> pathParams = singletonList(DefaultParameter.valueOf("paramName", "someParamValue", ParameterType.STRING));
    final JsonObject payload = createObjectBuilder().add("payloadId", payloadIdValue).build();
    when(responseStrategyCache.responseStrategyOf(anyString())).thenReturn(responseStrategy);
    restProcessor.process(NOT_USED_RESPONSE_STRATEGY_NAME, interceptorChain, action, Optional.of(payload), 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(allOf(withJsonPath("$.payloadId", equalTo(payloadIdValue)), withJsonPath("$.paramName", equalTo("someParamValue"))))));
}
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) JsonObject(javax.json.JsonObject) 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