Search in sources :

Example 1 with Event

use of org.mule.runtime.api.event.Event in project mule by mulesoft.

the class SplitAggregateScopeTestCase method customTargetDefaultPayload.

@Test
@Description("When a custom target is configured the router result is set in a variable and the input event is output.")
public void customTargetDefaultPayload() throws Exception {
    final String variableName = "foo";
    CoreEvent original = createListEvent();
    MessageProcessorChain nested = newChain(empty(), event -> event);
    nested.setMuleContext(muleContext);
    router.setMessageProcessors(singletonList(nested));
    router.setTarget(variableName);
    muleContext.getInjector().inject(router);
    router.setAnnotations(getAppleFlowComponentLocationAnnotations());
    router.initialise();
    Event result = router.process(original);
    assertThat(result.getMessage(), equalTo(original.getMessage()));
    final TypedValue<?> typedValue = result.getVariables().get(variableName);
    assertThat(typedValue.getValue(), instanceOf(List.class));
    assertThat(List.class.isAssignableFrom(typedValue.getDataType().getType()), is(true));
    List<Message> resultList = (List<Message>) typedValue.getValue();
    assertThat(resultList, hasSize(2));
}
Also used : MessageProcessorChain(org.mule.runtime.core.privileged.processor.chain.MessageProcessorChain) Message(org.mule.runtime.api.message.Message) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Event(org.mule.runtime.api.event.Event) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ArrayList(java.util.ArrayList) Description(io.qameta.allure.Description) Test(org.junit.Test)

Example 2 with Event

use of org.mule.runtime.api.event.Event in project mule by mulesoft.

the class SplitAggregateScopeTestCase method defaultTarget.

@Test
@Description("By default the router result populates the outgoing message payload.")
public void defaultTarget() throws Exception {
    CoreEvent original = createListEvent();
    MessageProcessorChain nested = newChain(empty(), event -> event);
    nested.setMuleContext(muleContext);
    router.setMessageProcessors(singletonList(nested));
    muleContext.getInjector().inject(router);
    router.setAnnotations(getAppleFlowComponentLocationAnnotations());
    router.initialise();
    Event result = router.process(original);
    assertThat(result.getMessage().getPayload().getValue(), instanceOf(List.class));
    List<Message> resultList = (List<Message>) result.getMessage().getPayload().getValue();
    assertThat(resultList, hasSize(2));
}
Also used : MessageProcessorChain(org.mule.runtime.core.privileged.processor.chain.MessageProcessorChain) Message(org.mule.runtime.api.message.Message) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Event(org.mule.runtime.api.event.Event) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ArrayList(java.util.ArrayList) Description(io.qameta.allure.Description) Test(org.junit.Test)

Example 3 with Event

use of org.mule.runtime.api.event.Event in project mule by mulesoft.

the class SplitAggregateScopeTestCase method customTargetMessage.

@Test
@Description("When a custom target is configured the router result is set in a variable and the input event is output.")
public void customTargetMessage() throws Exception {
    final String variableName = "foo";
    CoreEvent original = createListEvent();
    MessageProcessorChain nested = newChain(empty(), event -> event);
    nested.setMuleContext(muleContext);
    router.setMessageProcessors(singletonList(nested));
    router.setTarget(variableName);
    router.setTargetValue("#[message]");
    muleContext.getInjector().inject(router);
    router.setAnnotations(getAppleFlowComponentLocationAnnotations());
    router.initialise();
    Event result = router.process(original);
    assertThat(result.getMessage(), equalTo(original.getMessage()));
    assertThat(((Message) result.getVariables().get(variableName).getValue()).getPayload().getValue(), instanceOf(List.class));
    List<Message> resultList = (List<Message>) ((Message) result.getVariables().get(variableName).getValue()).getPayload().getValue();
    assertThat(resultList, hasSize(2));
}
Also used : MessageProcessorChain(org.mule.runtime.core.privileged.processor.chain.MessageProcessorChain) Message(org.mule.runtime.api.message.Message) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Event(org.mule.runtime.api.event.Event) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ArrayList(java.util.ArrayList) Description(io.qameta.allure.Description) Test(org.junit.Test)

Example 4 with Event

use of org.mule.runtime.api.event.Event in project mule by mulesoft.

the class MessagingExceptionResolver method resolve.

/**
 * Resolves a new {@link MessagingException} with the real cause of the problem based on the content of an Incoming
 * {@link MessagingException} with a chain of causes inside it and the current event that the exception is carrying.
 * <p>
 * This method will pick the FIRST cause exception that has a mule or extension KNOWN error as the real cause, if there is not
 * an exception in the causes that match with an Known error type then this method will try to find the error that the current
 * {@link Event} is carrying.
 * <p>
 * When there are multiple exceptions that contains the same root error type, then this method will wrap the one that has
 * highest position in the causes list
 *
 * @return a {@link MessagingException} with the proper {@link Error} associated to it's {@link CoreEvent}
 */
public MessagingException resolve(final MessagingException me, MuleContext context) {
    ErrorTypeLocator locator = ((PrivilegedMuleContext) context).getErrorTypeLocator();
    Optional<Pair<Throwable, ErrorType>> rootCause = findRoot(component, me, locator);
    if (!rootCause.isPresent()) {
        return updateCurrent(me, component, context);
    }
    Throwable root = rootCause.get().getFirst();
    ErrorType rootErrorType = rootCause.get().getSecond();
    Component failingComponent = getFailingProcessor(me, root).orElse(component);
    ErrorType errorType = getErrorMappings(component).stream().filter(m -> m.match(rootErrorType)).findFirst().map(ErrorMapping::getTarget).orElse(rootErrorType);
    Error error = ErrorBuilder.builder(getMessagingExceptionCause(root)).errorType(errorType).build();
    CoreEvent event = CoreEvent.builder(me.getEvent()).error(error).build();
    MessagingException result;
    if (root instanceof MessagingException) {
        ((MessagingException) root).setProcessedEvent(event);
        result = ((MessagingException) root);
    } else {
        result = me instanceof FlowExecutionException ? new FlowExecutionException(event, root, failingComponent) : new MessagingException(event, root, failingComponent);
    }
    if (me.getInfo().containsKey(INFO_ALREADY_LOGGED_KEY)) {
        result.addInfo(INFO_ALREADY_LOGGED_KEY, me.getInfo().get(INFO_ALREADY_LOGGED_KEY));
    }
    return enrich(result, failingComponent, event, context);
}
Also used : EnrichedNotificationInfo(org.mule.runtime.api.notification.EnrichedNotificationInfo) ErrorMapping(org.mule.runtime.core.internal.exception.ErrorMapping) FlowExecutionException(org.mule.runtime.core.internal.policy.FlowExecutionException) ExceptionUtils.getComponentIdentifier(org.mule.runtime.core.api.util.ExceptionUtils.getComponentIdentifier) InternalExceptionUtils.createErrorEvent(org.mule.runtime.core.internal.util.InternalExceptionUtils.createErrorEvent) ExceptionHelper.getExceptionsAsList(org.mule.runtime.api.exception.ExceptionHelper.getExceptionsAsList) Event(org.mule.runtime.api.event.Event) MuleContext(org.mule.runtime.core.api.MuleContext) SingleErrorTypeMatcher(org.mule.runtime.core.api.exception.SingleErrorTypeMatcher) Component(org.mule.runtime.api.component.Component) ExceptionUtils.getMessagingExceptionCause(org.mule.runtime.core.api.util.ExceptionUtils.getMessagingExceptionCause) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) CRITICAL_IDENTIFIER(org.mule.runtime.core.api.exception.Errors.Identifiers.CRITICAL_IDENTIFIER) Pair(org.mule.runtime.api.util.Pair) LinkedList(java.util.LinkedList) Error(org.mule.runtime.api.message.Error) PrivilegedMuleContext(org.mule.runtime.core.privileged.PrivilegedMuleContext) INFO_ALREADY_LOGGED_KEY(org.mule.runtime.api.exception.MuleException.INFO_ALREADY_LOGGED_KEY) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) EnrichedNotificationInfo.createInfo(org.mule.runtime.api.notification.EnrichedNotificationInfo.createInfo) ErrorBuilder(org.mule.runtime.core.internal.message.ErrorBuilder) List(java.util.List) InternalExceptionUtils.getErrorMappings(org.mule.runtime.core.internal.util.InternalExceptionUtils.getErrorMappings) Reference(org.mule.runtime.api.util.Reference) ErrorType(org.mule.runtime.api.message.ErrorType) ExceptionUtils.isUnknownMuleError(org.mule.runtime.core.api.util.ExceptionUtils.isUnknownMuleError) Optional(java.util.Optional) CORE_NAMESPACE_NAME(org.mule.runtime.core.api.exception.Errors.CORE_NAMESPACE_NAME) ComponentIdentifier(org.mule.runtime.api.component.ComponentIdentifier) ErrorTypeLocator(org.mule.runtime.core.privileged.exception.ErrorTypeLocator) ErrorType(org.mule.runtime.api.message.ErrorType) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) ErrorTypeLocator(org.mule.runtime.core.privileged.exception.ErrorTypeLocator) FlowExecutionException(org.mule.runtime.core.internal.policy.FlowExecutionException) Error(org.mule.runtime.api.message.Error) ExceptionUtils.isUnknownMuleError(org.mule.runtime.core.api.util.ExceptionUtils.isUnknownMuleError) PrivilegedMuleContext(org.mule.runtime.core.privileged.PrivilegedMuleContext) Component(org.mule.runtime.api.component.Component) Pair(org.mule.runtime.api.util.Pair)

Example 5 with Event

use of org.mule.runtime.api.event.Event in project mule by mulesoft.

the class AbstractExecutableComponent method execute.

@Override
public final CompletableFuture<ExecutionResult> execute(InputEvent inputEvent) {
    CompletableFuture completableFuture = new CompletableFuture();
    CoreEvent.Builder builder = CoreEvent.builder(createEventContext(of(completableFuture)));
    CoreEvent event = builder.message(inputEvent.getMessage()).error(inputEvent.getError().orElse(null)).variables(inputEvent.getVariables()).build();
    return from(MessageProcessors.process(event, getExecutableFunction())).onErrorMap(throwable -> {
        MessagingException messagingException = (MessagingException) throwable;
        CoreEvent messagingExceptionEvent = messagingException.getEvent();
        return new ComponentExecutionException(messagingExceptionEvent.getError().get().getCause(), messagingExceptionEvent);
    }).<ExecutionResult>map(result -> new ExecutionResultImplementation(result, completableFuture)).toFuture();
}
Also used : Optional.of(java.util.Optional.of) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) CoreEvent.builder(org.mule.runtime.core.api.event.CoreEvent.builder) Event(org.mule.runtime.api.event.Event) ExecutionResult(org.mule.runtime.api.component.execution.ExecutionResult) Inject(javax.inject.Inject) MuleContext(org.mule.runtime.core.api.MuleContext) Mono.from(reactor.core.publisher.Mono.from) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) EventContext(org.mule.runtime.api.event.EventContext) Optional.ofNullable(java.util.Optional.ofNullable) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Publisher(org.reactivestreams.Publisher) EventContextFactory.create(org.mule.runtime.core.api.event.EventContextFactory.create) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) NullExceptionHandler(org.mule.runtime.core.api.exception.NullExceptionHandler) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ReactiveProcessor(org.mule.runtime.core.api.processor.ReactiveProcessor) InputEvent(org.mule.runtime.api.component.execution.InputEvent) String.format(java.lang.String.format) AbstractComponent(org.mule.runtime.api.component.AbstractComponent) ComponentExecutionException(org.mule.runtime.api.component.execution.ComponentExecutionException) MessageProcessors(org.mule.runtime.core.privileged.processor.MessageProcessors) ExecutableComponent(org.mule.runtime.api.component.execution.ExecutableComponent) Optional(java.util.Optional) DefaultEventContext.child(org.mule.runtime.core.internal.event.DefaultEventContext.child) CompletableFuture(java.util.concurrent.CompletableFuture) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) ComponentExecutionException(org.mule.runtime.api.component.execution.ComponentExecutionException)

Aggregations

Event (org.mule.runtime.api.event.Event)15 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)15 Test (org.junit.Test)12 Description (io.qameta.allure.Description)6 Message (org.mule.runtime.api.message.Message)6 MessageProcessorChain (org.mule.runtime.core.privileged.processor.chain.MessageProcessorChain)6 List (java.util.List)5 InputEvent (org.mule.runtime.api.component.execution.InputEvent)4 MessagingException (org.mule.runtime.core.internal.exception.MessagingException)4 BaseEventContext (org.mule.runtime.core.privileged.event.BaseEventContext)4 ArrayList (java.util.ArrayList)3 Collections.singletonList (java.util.Collections.singletonList)3 Collections.singletonMap (java.util.Collections.singletonMap)3 Map (java.util.Map)3 Optional (java.util.Optional)3 ExecutionResult (org.mule.runtime.api.component.execution.ExecutionResult)3 Reference (org.mule.runtime.api.util.Reference)3 MuleContext (org.mule.runtime.core.api.MuleContext)3 String.format (java.lang.String.format)2 Optional.of (java.util.Optional.of)2