Search in sources :

Example 26 with InternalEvent

use of org.mule.runtime.core.internal.message.InternalEvent in project mule by mulesoft.

the class ReactiveInterceptorAdapterTestCase method firstInterceptorMutatesEventAroundAfterProceedChained.

@Test
public void firstInterceptorMutatesEventAroundAfterProceedChained() throws Exception {
    ProcessorInterceptor interceptor1 = prepareInterceptor(new TestProcessorInterceptor("outer") {

        @Override
        public CompletableFuture<InterceptionEvent> around(ComponentLocation location, Map<String, ProcessorParameterValue> parameters, InterceptionEvent event, InterceptionAction action) {
            return action.proceed().thenApplyAsync(e -> {
                e.message(Message.of(TEST_PAYLOAD));
                return e;
            });
        }
    });
    ProcessorInterceptor interceptor2 = prepareInterceptor(new TestProcessorInterceptor("inner") {
    });
    startFlowWithInterceptors(interceptor1, interceptor2);
    CoreEvent result = process(flow, eventBuilder(muleContext).message(Message.of("")).build());
    assertThat(result.getMessage().getPayload().getValue(), is(TEST_PAYLOAD));
    assertThat(result.getError().isPresent(), is(false));
    if (useMockInterceptor) {
        InOrder inOrder = inOrder(processor, interceptor1, interceptor2);
        inOrder.verify(interceptor1).before(any(), mapArgWithEntry("param", ""), any());
        inOrder.verify(interceptor2).before(any(), mapArgWithEntry("param", ""), any());
        inOrder.verify(interceptor1).around(any(), mapArgWithEntry("param", ""), any(), any());
        inOrder.verify(interceptor2).around(any(), mapArgWithEntry("param", ""), any(), any());
        inOrder.verify(processor).process(argThat(hasPayloadValue("")));
        inOrder.verify(interceptor2).after(any(), any(), eq(empty()));
        inOrder.verify(interceptor1).after(any(), argThat(interceptionHasPayloadValue(TEST_PAYLOAD)), eq(empty()));
        assertThat(((InternalEvent) result).getInternalParameters().entrySet(), hasSize(0));
        verifyParametersResolvedAndDisposed(times(1));
    }
}
Also used : Schedulers.fromExecutorService(reactor.core.scheduler.Schedulers.fromExecutorService) Message(org.mule.runtime.api.message.Message) Optional.of(java.util.Optional.of) Matchers.not(org.hamcrest.Matchers.not) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) Thread.currentThread(java.lang.Thread.currentThread) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) Is.is(org.hamcrest.core.Is.is) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) ParametersResolverProcessor(org.mule.runtime.core.internal.processor.ParametersResolverProcessor) TypedComponentIdentifier.builder(org.mule.runtime.api.component.TypedComponentIdentifier.builder) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Matchers.any(org.mockito.Matchers.any) VerificationMode(org.mockito.verification.VerificationMode) Matchers.argThat(org.mockito.Matchers.argThat) QName(javax.xml.namespace.QName) Mockito.mock(org.mockito.Mockito.mock) Optional.empty(java.util.Optional.empty) ComponentIdentifier.buildFromStringRepresentation(org.mule.runtime.api.component.ComponentIdentifier.buildFromStringRepresentation) DefaultComponentLocation(org.mule.runtime.dsl.api.component.config.DefaultComponentLocation) RunWith(org.junit.runner.RunWith) Mockito.spy(org.mockito.Mockito.spy) OPERATION(org.mule.runtime.api.component.TypedComponentIdentifier.ComponentType.OPERATION) EventMatcher.hasErrorTypeThat(org.mule.tck.junit4.matcher.EventMatcher.hasErrorTypeThat) ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) MuleException(org.mule.runtime.api.exception.MuleException) MessagingExceptionMatcher.withEventThat(org.mule.tck.junit4.matcher.MessagingExceptionMatcher.withEventThat) Component(org.mule.runtime.api.component.Component) Mono.from(reactor.core.publisher.Mono.from) BiConsumer(java.util.function.BiConsumer) MuleContextUtils.eventBuilder(org.mule.tck.util.MuleContextUtils.eventBuilder) Before(org.junit.Before) Disposable(org.mule.runtime.api.lifecycle.Disposable) ProcessorParameterValue(org.mule.runtime.api.interception.ProcessorParameterValue) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Publisher(org.reactivestreams.Publisher) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Mockito.never(org.mockito.Mockito.never) Matcher(org.hamcrest.Matcher) InternalEvent(org.mule.runtime.core.internal.message.InternalEvent) Flow.builder(org.mule.runtime.core.api.construct.Flow.builder) PollingProber(org.mule.tck.probe.PollingProber) Assert.assertThat(org.junit.Assert.assertThat) SmallTest(org.mule.tck.size.SmallTest) Scheduler(org.mule.runtime.api.scheduler.Scheduler) Matchers.eq(org.mockito.Matchers.eq) After(org.junit.After) Parameterized(org.junit.runners.Parameterized) IsMapContaining.hasEntry(org.hamcrest.collection.IsMapContaining.hasEntry) TypedComponentIdentifier(org.mule.runtime.api.component.TypedComponentIdentifier) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Flow(org.mule.runtime.core.api.construct.Flow) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) AbstractMuleContextTestCase(org.mule.tck.junit4.AbstractMuleContextTestCase) Mockito.inOrder(org.mockito.Mockito.inOrder) Matchers.equalTo(org.hamcrest.Matchers.equalTo) ErrorType(org.mule.runtime.api.message.ErrorType) Optional(java.util.Optional) ANNOTATION_PARAMETERS(org.mule.runtime.core.internal.component.ComponentAnnotations.ANNOTATION_PARAMETERS) ProcessorInterceptor(org.mule.runtime.api.interception.ProcessorInterceptor) ComponentModel(org.mule.runtime.api.meta.model.ComponentModel) Parameters(org.junit.runners.Parameterized.Parameters) InterceptionAction(org.mule.runtime.api.interception.InterceptionAction) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Processor(org.mule.runtime.core.api.processor.Processor) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) DefaultLocationPart(org.mule.runtime.dsl.api.component.config.DefaultComponentLocation.DefaultLocationPart) ProcessorInterceptorFactory(org.mule.runtime.api.interception.ProcessorInterceptorFactory) Inject(javax.inject.Inject) CompletableFuture.supplyAsync(java.util.concurrent.CompletableFuture.supplyAsync) ExpectedException.none(org.junit.rules.ExpectedException.none) CPU_LITE_ASYNC(org.mule.runtime.core.api.processor.ReactiveProcessor.ProcessingType.CPU_LITE_ASYNC) Collections.singletonMap(java.util.Collections.singletonMap) InterceptionEvent(org.mule.runtime.api.interception.InterceptionEvent) ExpectedException(org.junit.rules.ExpectedException) EventMatcher.hasErrorType(org.mule.tck.junit4.matcher.EventMatcher.hasErrorType) SchedulerConfig(org.mule.runtime.api.scheduler.SchedulerConfig) CheckedConsumer(org.mule.runtime.core.api.util.func.CheckedConsumer) ExecutionContext(org.mule.runtime.extension.api.runtime.operation.ExecutionContext) Description(org.hamcrest.Description) InOrder(org.mockito.InOrder) Mockito.when(org.mockito.Mockito.when) Initialisable(org.mule.runtime.api.lifecycle.Initialisable) Mockito.verify(org.mockito.Mockito.verify) UNKNOWN(org.mule.runtime.core.api.exception.Errors.ComponentIdentifiers.Handleable.UNKNOWN) AbstractComponent(org.mule.runtime.api.component.AbstractComponent) Collectors.toList(java.util.stream.Collectors.toList) LazyValue(org.mule.runtime.api.util.LazyValue) Rule(org.junit.Rule) IsSame.sameInstance(org.hamcrest.core.IsSame.sameInstance) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) InOrder(org.mockito.InOrder) InterceptionEvent(org.mule.runtime.api.interception.InterceptionEvent) InterceptionAction(org.mule.runtime.api.interception.InterceptionAction) InternalEvent(org.mule.runtime.core.internal.message.InternalEvent) DefaultComponentLocation(org.mule.runtime.dsl.api.component.config.DefaultComponentLocation) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) CompletableFuture(java.util.concurrent.CompletableFuture) ProcessorInterceptor(org.mule.runtime.api.interception.ProcessorInterceptor) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) ProcessorParameterValue(org.mule.runtime.api.interception.ProcessorParameterValue) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 27 with InternalEvent

use of org.mule.runtime.core.internal.message.InternalEvent in project mule by mulesoft.

the class ReactiveInterceptorAdapterTestCase method interceptorApplied.

@Test
public void interceptorApplied() throws Exception {
    ProcessorInterceptor interceptor = prepareInterceptor(new ProcessorInterceptor() {
    });
    startFlowWithInterceptors(interceptor);
    CoreEvent result = process(flow, eventBuilder(muleContext).message(Message.of("")).build());
    assertThat(result.getMessage().getPayload().getValue(), is(""));
    assertThat(result.getError().isPresent(), is(false));
    if (useMockInterceptor) {
        InOrder inOrder = inOrder(processor, interceptor);
        inOrder.verify(interceptor).before(eq(((Component) processor).getLocation()), mapArgWithEntry("param", ""), any());
        inOrder.verify(interceptor).around(eq(((Component) processor).getLocation()), mapArgWithEntry("param", ""), any(), any());
        inOrder.verify(processor).process(argThat(hasPayloadValue("")));
        inOrder.verify(interceptor).after(eq(((Component) processor).getLocation()), any(), eq(empty()));
        assertThat(((InternalEvent) result).getInternalParameters().entrySet(), hasSize(0));
        verifyParametersResolvedAndDisposed(times(1));
    }
}
Also used : InOrder(org.mockito.InOrder) ProcessorInterceptor(org.mule.runtime.api.interception.ProcessorInterceptor) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Component(org.mule.runtime.api.component.Component) AbstractComponent(org.mule.runtime.api.component.AbstractComponent) InternalEvent(org.mule.runtime.core.internal.message.InternalEvent) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 28 with InternalEvent

use of org.mule.runtime.core.internal.message.InternalEvent in project mule by mulesoft.

the class ReactiveInterceptorAdapterTestCase method interceptorMutatesEventAroundAfterSkip.

@Test
public void interceptorMutatesEventAroundAfterSkip() throws Exception {
    ProcessorInterceptor interceptor = prepareInterceptor(new ProcessorInterceptor() {

        @Override
        public CompletableFuture<InterceptionEvent> around(ComponentLocation location, Map<String, ProcessorParameterValue> parameters, InterceptionEvent event, InterceptionAction action) {
            action.skip();
            return supplyAsync(() -> {
                event.message(Message.of(TEST_PAYLOAD));
                return event;
            });
        }
    });
    startFlowWithInterceptors(interceptor);
    CoreEvent result = process(flow, eventBuilder(muleContext).message(Message.of("")).build());
    assertThat(result.getMessage().getPayload().getValue(), is(TEST_PAYLOAD));
    assertThat(result.getError().isPresent(), is(false));
    if (useMockInterceptor) {
        InOrder inOrder = inOrder(processor, interceptor);
        inOrder.verify(interceptor).before(any(), mapArgWithEntry("param", ""), any());
        inOrder.verify(interceptor).around(any(), mapArgWithEntry("param", ""), any(), any());
        inOrder.verify(processor, never()).process(any());
        inOrder.verify(interceptor).after(any(), argThat(interceptionHasPayloadValue(TEST_PAYLOAD)), eq(empty()));
        assertThat(((InternalEvent) result).getInternalParameters().entrySet(), hasSize(0));
        verifyParametersResolvedAndDisposed(times(1));
    }
}
Also used : DefaultComponentLocation(org.mule.runtime.dsl.api.component.config.DefaultComponentLocation) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) CompletableFuture(java.util.concurrent.CompletableFuture) InOrder(org.mockito.InOrder) ProcessorInterceptor(org.mule.runtime.api.interception.ProcessorInterceptor) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) InterceptionEvent(org.mule.runtime.api.interception.InterceptionEvent) ProcessorParameterValue(org.mule.runtime.api.interception.ProcessorParameterValue) InterceptionAction(org.mule.runtime.api.interception.InterceptionAction) InternalEvent(org.mule.runtime.core.internal.message.InternalEvent) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 29 with InternalEvent

use of org.mule.runtime.core.internal.message.InternalEvent in project mule by mulesoft.

the class ReactiveInterceptorAdapterTestCase method secondInterceptorDoesntApply.

@Test
public void secondInterceptorDoesntApply() throws Exception {
    ProcessorInterceptor interceptor1 = prepareInterceptor(new TestProcessorInterceptor("outer") {
    });
    ProcessorInterceptor interceptor2 = prepareInterceptor(new TestProcessorInterceptor("inner") {
    });
    startFlowWithInterceptorFactories(() -> interceptor1, new ProcessorInterceptorFactory() {

        @Override
        public boolean intercept(ComponentLocation location) {
            return false;
        }

        @Override
        public ProcessorInterceptor get() {
            return interceptor2;
        }
    });
    CoreEvent result = process(flow, eventBuilder(muleContext).message(Message.of("")).build());
    assertThat(result.getMessage().getPayload().getValue(), is(""));
    assertThat(result.getError().isPresent(), is(false));
    if (useMockInterceptor) {
        InOrder inOrder = inOrder(processor, interceptor1, interceptor2);
        inOrder.verify(interceptor1).before(any(), any(), any());
        inOrder.verify(interceptor1).around(any(), any(), any(), any());
        inOrder.verify(interceptor2, never()).before(any(), any(), any());
        inOrder.verify(interceptor2, never()).around(any(), any(), any(), any());
        inOrder.verify(processor).process(any());
        inOrder.verify(interceptor2, never()).after(any(), any(), eq(empty()));
        inOrder.verify(interceptor1).after(any(), any(), eq(empty()));
        assertThat(((InternalEvent) result).getInternalParameters().entrySet(), hasSize(0));
        verifyParametersResolvedAndDisposed(times(1));
    }
}
Also used : DefaultComponentLocation(org.mule.runtime.dsl.api.component.config.DefaultComponentLocation) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) InOrder(org.mockito.InOrder) ProcessorInterceptor(org.mule.runtime.api.interception.ProcessorInterceptor) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) ProcessorInterceptorFactory(org.mule.runtime.api.interception.ProcessorInterceptorFactory) InternalEvent(org.mule.runtime.core.internal.message.InternalEvent) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 30 with InternalEvent

use of org.mule.runtime.core.internal.message.InternalEvent in project mule by mulesoft.

the class ReactiveInterceptorAdapterTestCase method interceptorMutatesEventAroundBeforeSkip.

@Test
public void interceptorMutatesEventAroundBeforeSkip() throws Exception {
    ProcessorInterceptor interceptor = prepareInterceptor(new ProcessorInterceptor() {

        @Override
        public CompletableFuture<InterceptionEvent> around(ComponentLocation location, Map<String, ProcessorParameterValue> parameters, InterceptionEvent event, InterceptionAction action) {
            event.message(Message.of(TEST_PAYLOAD));
            return action.skip();
        }
    });
    startFlowWithInterceptors(interceptor);
    CoreEvent result = process(flow, eventBuilder(muleContext).message(Message.of("")).build());
    assertThat(result.getMessage().getPayload().getValue(), is(TEST_PAYLOAD));
    assertThat(result.getError().isPresent(), is(false));
    if (useMockInterceptor) {
        InOrder inOrder = inOrder(processor, interceptor);
        inOrder.verify(interceptor).before(any(), mapArgWithEntry("param", ""), any());
        inOrder.verify(interceptor).around(any(), mapArgWithEntry("param", ""), any(), any());
        inOrder.verify(processor, never()).process(any());
        inOrder.verify(interceptor).after(any(), argThat(interceptionHasPayloadValue(TEST_PAYLOAD)), eq(empty()));
        assertThat(((InternalEvent) result).getInternalParameters().entrySet(), hasSize(0));
        verifyParametersResolvedAndDisposed(times(1));
    }
}
Also used : DefaultComponentLocation(org.mule.runtime.dsl.api.component.config.DefaultComponentLocation) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) CompletableFuture(java.util.concurrent.CompletableFuture) InOrder(org.mockito.InOrder) ProcessorInterceptor(org.mule.runtime.api.interception.ProcessorInterceptor) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) InterceptionEvent(org.mule.runtime.api.interception.InterceptionEvent) ProcessorParameterValue(org.mule.runtime.api.interception.ProcessorParameterValue) InterceptionAction(org.mule.runtime.api.interception.InterceptionAction) InternalEvent(org.mule.runtime.core.internal.message.InternalEvent) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Aggregations

InternalEvent (org.mule.runtime.core.internal.message.InternalEvent)32 Test (org.junit.Test)30 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)24 ProcessorInterceptor (org.mule.runtime.api.interception.ProcessorInterceptor)23 InOrder (org.mockito.InOrder)22 ComponentLocation (org.mule.runtime.api.component.location.ComponentLocation)22 SmallTest (org.mule.tck.size.SmallTest)22 DefaultComponentLocation (org.mule.runtime.dsl.api.component.config.DefaultComponentLocation)21 InterceptionEvent (org.mule.runtime.api.interception.InterceptionEvent)20 ProcessorParameterValue (org.mule.runtime.api.interception.ProcessorParameterValue)16 CompletableFuture (java.util.concurrent.CompletableFuture)14 InterceptionAction (org.mule.runtime.api.interception.InterceptionAction)14 DefaultInterceptionEvent (org.mule.runtime.core.internal.interception.DefaultInterceptionEvent)7 ProcessorInterceptorFactory (org.mule.runtime.api.interception.ProcessorInterceptorFactory)5 Component (org.mule.runtime.api.component.Component)4 Message (org.mule.runtime.api.message.Message)4 MuleSession (org.mule.runtime.core.privileged.event.MuleSession)4 Collections.singletonList (java.util.Collections.singletonList)3 Map (java.util.Map)3 AbstractComponent (org.mule.runtime.api.component.AbstractComponent)3