Search in sources :

Example 41 with BaseEventContext

use of org.mule.runtime.core.privileged.event.BaseEventContext in project mule by mulesoft.

the class SourceAdapter method getNonCallbackParameterValue.

private <T> Optional<T> getNonCallbackParameterValue(String fieldName, Class<T> type) {
    ValueResolver<T> valueResolver = (ValueResolver<T>) nonCallbackParameters.getResolvers().get(fieldName);
    if (valueResolver == null) {
        return empty();
    }
    T object;
    CoreEvent initialiserEvent = null;
    try {
        initialiserEvent = getInitialiserEvent(muleContext);
        object = valueResolver.resolve(from(initialiserEvent));
    } catch (MuleException e) {
        throw new MuleRuntimeException(createStaticMessage("Unable to get the " + type.getSimpleName() + " value for Message Source"), e);
    } finally {
        if (initialiserEvent != null) {
            ((BaseEventContext) initialiserEvent.getContext()).success();
        }
    }
    if (!(type.isInstance(object))) {
        throw new IllegalStateException("The resolved value is not a " + type.getSimpleName());
    }
    return of(object);
}
Also used : BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) ValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolver) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) MuleException(org.mule.runtime.api.exception.MuleException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException)

Example 42 with BaseEventContext

use of org.mule.runtime.core.privileged.event.BaseEventContext in project mule by mulesoft.

the class TestLegacyEventUtils method getEffectiveExceptionHandler.

/**
 * @return the {@link FlowExceptionHandler} to be applied if an exception is unhandled during the processing of the given
 *         event.
 */
public static FlowExceptionHandler getEffectiveExceptionHandler(CoreEvent event) {
    Field exceptionHandlerField;
    try {
        exceptionHandlerField = event.getContext().getClass().getSuperclass().getDeclaredField("exceptionHandler");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    exceptionHandlerField.setAccessible(true);
    try {
        BaseEventContext eventContext = (BaseEventContext) event.getContext();
        FlowExceptionHandler effectiveMessagingExceptionHandler = (FlowExceptionHandler) exceptionHandlerField.get(eventContext);
        while (eventContext.getParentContext().isPresent() && effectiveMessagingExceptionHandler == HANDLER) {
            eventContext = eventContext.getParentContext().get();
            effectiveMessagingExceptionHandler = (FlowExceptionHandler) exceptionHandlerField.get(eventContext);
        }
        return effectiveMessagingExceptionHandler;
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        exceptionHandlerField.setAccessible(false);
    }
}
Also used : Field(java.lang.reflect.Field) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) FlowExceptionHandler(org.mule.runtime.core.api.exception.FlowExceptionHandler)

Example 43 with BaseEventContext

use of org.mule.runtime.core.privileged.event.BaseEventContext in project mule by mulesoft.

the class ReactiveInterceptorAdapterTestCase method interceptorErrorResumeAround.

@Test
@io.qameta.allure.Description("Simulates the error handling scenario for XML SDK operations")
public void interceptorErrorResumeAround() throws Exception {
    Exception thrown = new Exception();
    ProcessorInterceptor interceptor = prepareInterceptor(new ProcessorInterceptor() {

        @Override
        public CompletableFuture<InterceptionEvent> around(ComponentLocation location, Map<String, ProcessorParameterValue> parameters, InterceptionEvent event, InterceptionAction action) {
            Mono<InterceptionEvent> errorMono = Mono.error(thrown);
            return Mono.from(((BaseEventContext) event.getContext()).error(thrown)).then(errorMono).toFuture();
        }

        @Override
        public void after(ComponentLocation location, InterceptionEvent event, Optional<Throwable> thrown) {
        }
    });
    startFlowWithInterceptors(interceptor);
    expected.expectCause(sameInstance(thrown));
    try {
        process(flow, eventBuilder(muleContext).message(Message.of("")).build());
    } finally {
        if (useMockInterceptor) {
            new PollingProber().probe(() -> {
                verify(interceptor).after(any(), any(), eq(Optional.of(thrown)));
                return true;
            });
        }
    }
}
Also used : Mono(reactor.core.publisher.Mono) PollingProber(org.mule.tck.probe.PollingProber) InterceptionEvent(org.mule.runtime.api.interception.InterceptionEvent) InterceptionAction(org.mule.runtime.api.interception.InterceptionAction) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) MuleException(org.mule.runtime.api.exception.MuleException) ExpectedException(org.junit.rules.ExpectedException) DefaultComponentLocation(org.mule.runtime.dsl.api.component.config.DefaultComponentLocation) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) CompletableFuture(java.util.concurrent.CompletableFuture) ProcessorInterceptor(org.mule.runtime.api.interception.ProcessorInterceptor) ProcessorParameterValue(org.mule.runtime.api.interception.ProcessorParameterValue) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 44 with BaseEventContext

use of org.mule.runtime.core.privileged.event.BaseEventContext in project mule by mulesoft.

the class AsyncDelegateMessageProcessorTestCase method process.

@Test
public void process() throws Exception {
    CoreEvent request = testEvent();
    CoreEvent result = process(messageProcessor, request);
    // Complete parent context so we can assert event context completion based on async completion.
    ((BaseEventContext) request.getContext()).success(result);
    assertThat(((BaseEventContext) request.getContext()).isTerminated(), is(false));
    // Permit async processing now we have already asserted that response alone is not enough to complete event context.
    asyncEntryLatch.countDown();
    assertThat(latch.await(LOCK_TIMEOUT, MILLISECONDS), is(true));
    // Block until async completes, not just target processor.
    while (!((BaseEventContext) target.sensedEvent.getContext()).isTerminated()) {
        park100ns();
    }
    assertThat(target.sensedEvent, notNullValue());
    // Block to ensure async fully completes before testing state
    while (!((BaseEventContext) request.getContext()).isTerminated()) {
        park100ns();
    }
    assertThat(((BaseEventContext) target.sensedEvent.getContext()).isTerminated(), is(true));
    assertThat(((BaseEventContext) request.getContext()).isTerminated(), is(true));
    assertTargetEvent(request);
    assertResponse(result);
}
Also used : BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Test(org.junit.Test)

Example 45 with BaseEventContext

use of org.mule.runtime.core.privileged.event.BaseEventContext in project mule by mulesoft.

the class IdempotentMessageValidatorTestCase method testIdCheckWithDW.

@Test
public void testIdCheckWithDW() throws Exception {
    String dwExpression = "%dw 2.0\n" + "output application/text\n" + "---\n" + "payload ++ ' World'";
    final BaseEventContext context = mock(BaseEventContext.class);
    when(context.getCorrelationId()).thenReturn("1");
    Message okMessage = of("Hello");
    CoreEvent event = CoreEvent.builder(context).message(okMessage).build();
    // Set DW expression to hash value
    idempotent.setIdExpression(dwExpression);
    // This one will process the event on the target endpoint
    CoreEvent processedEvent = idempotent.process(event);
    assertNotNull(processedEvent);
    assertEquals(idempotent.getObjectStore().retrieve("Hello World"), "1");
    // This will not process, because the message is a duplicate
    okMessage = of("Hello");
    event = CoreEvent.builder(context).message(okMessage).build();
    expected.expect(ValidationException.class);
    processedEvent = idempotent.process(event);
}
Also used : BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) Message(org.mule.runtime.api.message.Message) InternalMessage(org.mule.runtime.core.internal.message.InternalMessage) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Test(org.junit.Test)

Aggregations

BaseEventContext (org.mule.runtime.core.privileged.event.BaseEventContext)45 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)34 Test (org.junit.Test)24 MuleException (org.mule.runtime.api.exception.MuleException)10 Message (org.mule.runtime.api.message.Message)10 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)8 MessagingException (org.mule.runtime.core.internal.exception.MessagingException)7 Description (io.qameta.allure.Description)6 Optional.of (java.util.Optional.of)6 HeisenbergExtension (org.mule.test.heisenberg.extension.HeisenbergExtension)6 Optional (java.util.Optional)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 EventContext (org.mule.runtime.api.event.EventContext)5 InitialisationException (org.mule.runtime.api.lifecycle.InitialisationException)5 Publisher (org.reactivestreams.Publisher)5 Mono.from (reactor.core.publisher.Mono.from)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Processor (org.mule.runtime.core.api.processor.Processor)4 InternalMessage (org.mule.runtime.core.internal.message.InternalMessage)4 String.format (java.lang.String.format)3