Search in sources :

Example 6 with Event

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

the class ExecutableComponentTestCase method testExecuteWithEvent.

@Test
public void testExecuteWithEvent() throws Exception {
    Event response = executableComponent.execute(testEvent()).get();
    assertThat(componentInEvent.get().getMessage(), equalTo(requestMessage));
    assertThat(response.getMessage(), equalTo(responseMessage));
    assertThat(componentInEvent.get().getContext(), not(equalTo(response.getContext())));
    assertThat(((BaseEventContext) componentInEvent.get().getContext()).isTerminated(), is(true));
    assertThat(((BaseEventContext) response.getContext()).isTerminated(), is(false));
    BaseEventContext childContext = (BaseEventContext) componentInEvent.get().getContext();
    assertThat(childContext.isTerminated(), is(true));
    BaseEventContext parentContext = (BaseEventContext) testEvent().getContext();
    assertThat(parentContext.isTerminated(), is(false));
    ((BaseEventContext) testEvent().getContext()).success();
    assertThat(parentContext.isTerminated(), is(true));
}
Also used : BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) InputEvent(org.mule.runtime.api.component.execution.InputEvent) Event(org.mule.runtime.api.event.Event) Test(org.junit.Test)

Example 7 with Event

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

the class ExecutableComponentTestCase method testExecuteWithInputEvent.

@Test
public void testExecuteWithInputEvent() throws Exception {
    ExecutionResult executionResult = executableComponent.execute(InputEvent.create().message(requestMessage)).get();
    Event response = executionResult.getEvent();
    assertThat(componentInEvent.get().getMessage(), equalTo(requestMessage));
    assertThat(response.getMessage(), equalTo(responseMessage));
    assertThat(componentInEvent.get().getContext(), equalTo(response.getContext()));
    BaseEventContext eventContext = (BaseEventContext) componentInEvent.get().getContext();
    assertThat(eventContext.isTerminated(), is(false));
    executionResult.complete();
    assertThat(eventContext.isTerminated(), is(true));
}
Also used : BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) InputEvent(org.mule.runtime.api.component.execution.InputEvent) Event(org.mule.runtime.api.event.Event) ExecutionResult(org.mule.runtime.api.component.execution.ExecutionResult) Test(org.junit.Test)

Example 8 with Event

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

the class AbstractExecutableComponent method execute.

@Override
public final CompletableFuture<Event> execute(Event event) {
    CoreEvent internalEvent;
    BaseEventContext child = createChildEventContext(event.getContext());
    if (event instanceof CoreEvent) {
        internalEvent = builder(child, (CoreEvent) event).build();
    } else {
        internalEvent = CoreEvent.builder(createEventContext(null)).message(event.getMessage()).error(event.getError().orElse(null)).variables(event.getVariables()).build();
    }
    return from(MessageProcessors.process(internalEvent, getExecutableFunction())).onErrorMap(throwable -> {
        MessagingException messagingException = (MessagingException) throwable;
        CoreEvent messagingExceptionEvent = messagingException.getEvent();
        return new ComponentExecutionException(messagingExceptionEvent.getError().get().getCause(), messagingExceptionEvent);
    }).map(r -> builder(event.getContext(), r).build()).cast(Event.class).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) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) ComponentExecutionException(org.mule.runtime.api.component.execution.ComponentExecutionException) Event(org.mule.runtime.api.event.Event) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) InputEvent(org.mule.runtime.api.component.execution.InputEvent)

Example 9 with Event

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

the class RoutersExecutionTestCase method stereotypedRoutes.

@Test
public void stereotypedRoutes() throws Exception {
    Event routeEvent = flowRunner("stereotypedRoutes").run();
    Ricin ricin = ((List<Ricin>) routeEvent.getMessage().getPayload().getValue()).get(0);
    assertThat(ricin.getDestination().getVictim(), is("bye bye, someName"));
}
Also used : Ricin(org.mule.test.heisenberg.extension.model.Ricin) Event(org.mule.runtime.api.event.Event) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) List(java.util.List) Test(org.junit.Test)

Example 10 with Event

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

the class ProcessorChainExecutorTestCase method testDoProcessOnErrorGenericException.

@Test
public void testDoProcessOnErrorGenericException() throws InterruptedException {
    when(chain.apply(any())).thenReturn(error(new RuntimeException()));
    ImmutableProcessorChainExecutor chainExecutor = new ImmutableProcessorChainExecutor(coreEvent, chain);
    AtomicInteger successCalls = new AtomicInteger(0);
    AtomicInteger errorCalls = new AtomicInteger(0);
    Reference<Event> errorEvent = new Reference<>();
    doProcessAndWait(chainExecutor, r -> successCalls.incrementAndGet(), (t, r) -> {
        errorCalls.incrementAndGet();
        errorEvent.set(((EventedResult) r).getEvent());
    });
    assertThat(successCalls.get(), is(0));
    assertThat(errorCalls.get(), is(1));
    assertThat(errorEvent.get().getMessage().getPayload().getValue(), is(TEST_PAYLOAD));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Reference(org.mule.runtime.api.util.Reference) Event(org.mule.runtime.api.event.Event) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Test(org.junit.Test)

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