Search in sources :

Example 26 with InterceptionEvent

use of org.mule.runtime.api.interception.InterceptionEvent in project mule by mulesoft.

the class ReactiveInterceptorAdapterTestCase method firstInterceptorMutatesEventAroundAfterProceed.

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

        @Override
        public CompletableFuture<InterceptionEvent> around(ComponentLocation location, Map<String, ProcessorParameterValue> parameters, InterceptionEvent event, InterceptionAction action) {
            action.proceed();
            return supplyAsync(() -> {
                event.message(Message.of(TEST_PAYLOAD));
                return event;
            });
        }
    });
    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 : 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 InterceptionEvent

use of org.mule.runtime.api.interception.InterceptionEvent in project mule by mulesoft.

the class ReactiveInterceptorAdapterTestCase method firstInterceptorMutatesEventAroundBeforeProceed.

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

        @Override
        public CompletableFuture<InterceptionEvent> around(ComponentLocation location, Map<String, ProcessorParameterValue> parameters, InterceptionEvent event, InterceptionAction action) {
            event.message(Message.of(TEST_PAYLOAD));
            return action.proceed();
        }
    });
    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", ""), argThat(interceptionHasPayloadValue(TEST_PAYLOAD)), any());
        inOrder.verify(interceptor2).around(any(), mapArgWithEntry("param", TEST_PAYLOAD), argThat(interceptionHasPayloadValue(TEST_PAYLOAD)), any());
        inOrder.verify(processor).process(argThat(hasPayloadValue(TEST_PAYLOAD)));
        inOrder.verify(interceptor2).after(any(), argThat(interceptionHasPayloadValue(TEST_PAYLOAD)), eq(empty()));
        inOrder.verify(interceptor1).after(any(), argThat(interceptionHasPayloadValue(TEST_PAYLOAD)), eq(empty()));
        assertThat(((InternalEvent) result).getInternalParameters().entrySet(), hasSize(0));
        verifyParametersResolvedAndDisposed(times(2));
    }
}
Also used : 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 28 with InterceptionEvent

use of org.mule.runtime.api.interception.InterceptionEvent in project mule by mulesoft.

the class ReactiveInterceptorAdapterTestCase method firstInterceptorSkipsProcessor.

@Test
public void firstInterceptorSkipsProcessor() 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.skip();
        }
    });
    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(""));
    if (useMockInterceptor) {
        InOrder inOrder = inOrder(processor, interceptor1, interceptor2);
        inOrder.verify(interceptor1).before(any(), any(), any());
        inOrder.verify(interceptor2).before(any(), any(), any());
        inOrder.verify(interceptor1).around(any(), any(), any(), any());
        inOrder.verify(interceptor2, never()).around(any(), any(), any(), any());
        inOrder.verify(processor, never()).process(any());
        inOrder.verify(interceptor2).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 : 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 29 with InterceptionEvent

use of org.mule.runtime.api.interception.InterceptionEvent in project mule by mulesoft.

the class ReactiveInterceptorAdapterTestCase method threadForBeforeAfter.

@Test
public void threadForBeforeAfter() throws Exception {
    AtomicReference<Thread> threadBefore = new AtomicReference<>();
    AtomicReference<Thread> threadAfter = new AtomicReference<>();
    ProcessorInterceptor interceptor = prepareInterceptor(new ProcessorInterceptor() {

        @Override
        public void before(ComponentLocation location, Map<String, ProcessorParameterValue> parameters, InterceptionEvent event) {
            threadBefore.set(currentThread());
        }

        @Override
        public void after(ComponentLocation location, InterceptionEvent event, java.util.Optional<Throwable> thrown) {
            threadAfter.set(currentThread());
        }
    });
    startFlowWithInterceptors(interceptor);
    process(flow, eventBuilder(muleContext).message(Message.of("")).build());
    assertThat(threadAfter.get().getName(), threadAfter.get().getName(), not(is(NonBlockingProcessorInApp.SELECTOR_EMULATOR_SCHEDULER_NAME)));
    assertThat(threadAfter.get().getName(), threadAfter.get().getThreadGroup().getName(), is(threadBefore.get().getThreadGroup().getName()));
}
Also used : InterceptionEvent(org.mule.runtime.api.interception.InterceptionEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) Thread.currentThread(java.lang.Thread.currentThread) DefaultComponentLocation(org.mule.runtime.dsl.api.component.config.DefaultComponentLocation) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) 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 30 with InterceptionEvent

use of org.mule.runtime.api.interception.InterceptionEvent in project mule by mulesoft.

the class ReactiveInterceptorAdapterTestCase method interceptorSkipsProcessor.

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

        @Override
        public CompletableFuture<InterceptionEvent> around(ComponentLocation location, Map<String, ProcessorParameterValue> parameters, InterceptionEvent event, InterceptionAction action) {
            return action.skip();
        }
    });
    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(any(), any(), any());
        inOrder.verify(interceptor).around(any(), any(), any(), any());
        inOrder.verify(processor, never()).process(any());
        inOrder.verify(interceptor).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) 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

InterceptionEvent (org.mule.runtime.api.interception.InterceptionEvent)44 ComponentLocation (org.mule.runtime.api.component.location.ComponentLocation)42 ProcessorInterceptor (org.mule.runtime.api.interception.ProcessorInterceptor)42 Test (org.junit.Test)41 DefaultComponentLocation (org.mule.runtime.dsl.api.component.config.DefaultComponentLocation)41 SmallTest (org.mule.tck.size.SmallTest)41 InOrder (org.mockito.InOrder)39 ProcessorParameterValue (org.mule.runtime.api.interception.ProcessorParameterValue)35 CompletableFuture (java.util.concurrent.CompletableFuture)31 InterceptionAction (org.mule.runtime.api.interception.InterceptionAction)29 ExpressionRuntimeException (org.mule.runtime.core.api.expression.ExpressionRuntimeException)22 InternalEvent (org.mule.runtime.core.internal.message.InternalEvent)22 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)21 MessagingException (org.mule.runtime.core.internal.exception.MessagingException)7 Component (org.mule.runtime.api.component.Component)6 Thread.currentThread (java.lang.Thread.currentThread)5 Map (java.util.Map)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 Arrays.asList (java.util.Arrays.asList)4