Search in sources :

Example 11 with BaseEventContext

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

the class CompositeSourcePolicyTestCase method nextProcessorExecutionFailurePropagates.

@Test
public void nextProcessorExecutionFailurePropagates() throws Exception {
    RuntimeException policyException = new RuntimeException("policy failure");
    reset(flowExecutionProcessor);
    when(flowExecutionProcessor.apply(any())).thenAnswer(invocation -> {
        Mono<CoreEvent> mono = from(invocation.getArgumentAt(0, Publisher.class));
        mono.doOnNext(event -> ((BaseEventContext) event.getContext()).error(new MessagingException(event, policyException))).subscribe();
        return empty();
    });
    compositeSourcePolicy = new CompositeSourcePolicy(asList(firstPolicy, secondPolicy), sourcePolicyParametersTransformer, sourcePolicyProcessorFactory, flowExecutionProcessor, sourceParametersTransformer);
    Either<SourcePolicyFailureResult, SourcePolicySuccessResult> sourcePolicyResult = from(compositeSourcePolicy.process(initialEvent)).block();
    assertThat(sourcePolicyResult.isLeft(), is(true));
    assertThat(sourcePolicyResult.getLeft().getMessagingException(), instanceOf(FlowExecutionException.class));
    assertThat(sourcePolicyResult.getLeft().getMessagingException().getCause(), is(policyException));
}
Also used : Matchers.same(org.mockito.Matchers.same) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Message(org.mule.runtime.api.message.Message) Optional.of(java.util.Optional.of) Processor(org.mule.runtime.core.api.processor.Processor) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) ArgumentCaptor(org.mockito.ArgumentCaptor) Arrays.asList(java.util.Arrays.asList) Is.is(org.hamcrest.core.Is.is) ExpectedException.none(org.junit.rules.ExpectedException.none) Mono.from(reactor.core.publisher.Mono.from) Mono.just(reactor.core.publisher.Mono.just) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) RETURNS_DEEP_STUBS(org.mockito.Mockito.RETURNS_DEEP_STUBS) Mono.empty(reactor.core.publisher.Mono.empty) Mono.error(reactor.core.publisher.Mono.error) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) SourcePolicyParametersTransformer(org.mule.runtime.core.api.policy.SourcePolicyParametersTransformer) Collections.emptyList(java.util.Collections.emptyList) 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) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Matchers.any(org.mockito.Matchers.any) Either(org.mule.runtime.core.api.functional.Either) Rule(org.junit.Rule) ArgumentCaptor.forClass(org.mockito.ArgumentCaptor.forClass) Policy(org.mule.runtime.core.api.policy.Policy) AbstractMuleContextTestCase(org.mule.tck.junit4.AbstractMuleContextTestCase) Optional(java.util.Optional) DefaultComponentLocation.fromSingleComponent(org.mule.runtime.dsl.api.component.config.DefaultComponentLocation.fromSingleComponent) FlowConstruct(org.mule.runtime.core.api.construct.FlowConstruct) Mockito.reset(org.mockito.Mockito.reset) Mockito.mock(org.mockito.Mockito.mock) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) Publisher(org.reactivestreams.Publisher) Test(org.junit.Test)

Example 12 with BaseEventContext

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

the class DefaultPolicyStateHandlerTestCase method destroyStateWhenEventIsCompleted.

@Test
public void destroyStateWhenEventIsCompleted() {
    PolicyStateId policyStateExecutionId = new PolicyStateId(TEST_EXECUTION_ID, TEST_POLICY_ID);
    Reference<BiConsumer> subscriberReference = new Reference<>();
    BaseEventContext rootEventContext = eventTestExecutionId.getContext().getRootContext();
    Mockito.doAnswer(invocationOnMock -> {
        subscriberReference.set((BiConsumer) invocationOnMock.getArguments()[0]);
        return null;
    }).when(rootEventContext).onTerminated(any(BiConsumer.class));
    defaultPolicyStateHandler.updateState(policyStateExecutionId, eventTestExecutionId);
    subscriberReference.get().accept(null, null);
    assertThat(defaultPolicyStateHandler.getLatestState(policyStateExecutionId).isPresent(), is(false));
    assertThat(defaultPolicyStateHandler.policyStateIdsByExecutionIdentifier.containsKey(policyStateExecutionId.getExecutionIdentifier()), is(false));
    assertThat(defaultPolicyStateHandler.nextOperationMap.containsKey(policyStateExecutionId.getExecutionIdentifier()), is(false));
    assertThat(defaultPolicyStateHandler.stateMap.containsKey(policyStateExecutionId), is(false));
}
Also used : BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) Reference(org.mule.runtime.api.util.Reference) PolicyStateId(org.mule.runtime.core.api.policy.PolicyStateId) BiConsumer(java.util.function.BiConsumer) Test(org.junit.Test)

Example 13 with BaseEventContext

use of org.mule.runtime.core.privileged.event.BaseEventContext 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 14 with BaseEventContext

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

the class CompositeProcessorChainRouterTestCase method childContextChain.

@Test
@Description("Ensure that when a child context is created as part of the execution of one of the composite chains then the chain does not complete and the next chains is not executed until the child context completes.")
public void childContextChain() throws Exception {
    Latch latch = new Latch();
    AtomicReference<BaseEventContext> childEventContext = new AtomicReference<>();
    MessageProcessorChain chainUsingChildContext = newChain(empty(), event -> {
        latch.release();
        childEventContext.set(child((BaseEventContext) event.getContext(), empty()));
        return event;
    });
    chainRouter = createCompositeProcessorChainRouter(chainUsingChildContext, newChain(empty(), event -> event));
    // CompletableFuture is not returned immediately because simply invoking CompositeProcessorChainRouter there is no async
    // hand-off and so this blocks until child context completes.
    Future<CompletableFuture<Event>> future = scheduler.submit(() -> chainRouter.execute(testEvent()));
    latch.await();
    try {
        future.get(BLOCK_TIMEOUT, MILLISECONDS);
        fail("Timeout expected");
    } catch (TimeoutException te) {
    }
    childEventContext.get().success();
    assertThat(future.get(BLOCK_TIMEOUT, MILLISECONDS).get().getMessage(), equalTo(testEvent().getMessage()));
}
Also used : BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) MessageProcessorChain(org.mule.runtime.core.privileged.processor.chain.MessageProcessorChain) CompletableFuture(java.util.concurrent.CompletableFuture) Latch(org.mule.runtime.api.util.concurrent.Latch) AtomicReference(java.util.concurrent.atomic.AtomicReference) TimeoutException(java.util.concurrent.TimeoutException) Description(io.qameta.allure.Description) Test(org.junit.Test)

Example 15 with BaseEventContext

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

the class ParameterizedConfigParserTestCase method configWithExpressionFunctionStillDynamic.

@Test
public void configWithExpressionFunctionStillDynamic() throws Exception {
    CoreEvent event = null;
    CoreEvent anotherEvent = null;
    try {
        event = getHeisenbergEvent();
        anotherEvent = CoreEvent.builder(getHeisenbergEvent()).addVariable("age", 40).build();
        HeisenbergExtension config = lookupHeisenberg(HEISENBERG_EXPRESSION, event);
        HeisenbergExtension anotherConfig = lookupHeisenberg(HEISENBERG_EXPRESSION, anotherEvent);
        assertThat(config, is(not(sameInstance(anotherConfig))));
    } finally {
        if (event != null) {
            ((BaseEventContext) event.getContext()).success();
        }
        if (anotherEvent != null) {
            ((BaseEventContext) anotherEvent.getContext()).success();
        }
    }
}
Also used : BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) HeisenbergExtension(org.mule.test.heisenberg.extension.HeisenbergExtension) 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