Search in sources :

Example 6 with InterceptionAction

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

the class ReactiveInterceptorAdapterTestCase method interceptorThrowsExceptionAroundAfterProceed.

@Test
public void interceptorThrowsExceptionAroundAfterProceed() throws Exception {
    RuntimeException expectedException = new RuntimeException("Some Error");
    ProcessorInterceptor interceptor = prepareInterceptor(new ProcessorInterceptor() {

        @Override
        public CompletableFuture<InterceptionEvent> around(ComponentLocation location, Map<String, ProcessorParameterValue> parameters, InterceptionEvent event, InterceptionAction action) {
            action.proceed();
            throw expectedException;
        }
    });
    startFlowWithInterceptors(interceptor);
    expected.expectCause(sameInstance(expectedException));
    try {
        process(flow, eventBuilder(muleContext).message(Message.of("")).build());
    } finally {
        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).process(any());
            inOrder.verify(interceptor).after(any(), any(), eq(of(expectedException)));
            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) ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) InOrder(org.mockito.InOrder) ProcessorInterceptor(org.mule.runtime.api.interception.ProcessorInterceptor) InterceptionEvent(org.mule.runtime.api.interception.InterceptionEvent) ProcessorParameterValue(org.mule.runtime.api.interception.ProcessorParameterValue) InterceptionAction(org.mule.runtime.api.interception.InterceptionAction) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 7 with InterceptionAction

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

the class ReactiveInterceptorAdapterTestCase method secondInterceptorMutatesEventAroundAfterProceed.

@Test
public void secondInterceptorMutatesEventAroundAfterProceed() throws Exception {
    ProcessorInterceptor interceptor1 = prepareInterceptor(new TestProcessorInterceptor("outer") {
    });
    ProcessorInterceptor interceptor2 = prepareInterceptor(new TestProcessorInterceptor("inner") {

        @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;
            });
        }
    });
    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(), 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(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 8 with InterceptionAction

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

the class ReactiveInterceptorAdapterTestCase method interceptorThrowsExceptionAroundAfterProceedInCallbackChained.

@Test
public void interceptorThrowsExceptionAroundAfterProceedInCallbackChained() throws Exception {
    RuntimeException expectedException = new RuntimeException("Some Error");
    ProcessorInterceptor interceptor = prepareInterceptor(new ProcessorInterceptor() {

        @Override
        public CompletableFuture<InterceptionEvent> around(ComponentLocation location, Map<String, ProcessorParameterValue> parameters, InterceptionEvent event, InterceptionAction action) {
            return action.proceed().thenApplyAsync(e -> {
                throw expectedException;
            });
        }
    });
    startFlowWithInterceptors(interceptor);
    expected.expect(MessagingException.class);
    expected.expect(withEventThat(hasErrorType(UNKNOWN.getNamespace(), UNKNOWN.getName())));
    expected.expectCause(sameInstance(expectedException));
    try {
        process(flow, eventBuilder(muleContext).message(Message.of("")).build());
    } finally {
        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).process(any());
            inOrder.verify(interceptor).after(any(), any(), eq(of(expectedException)));
            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) DefaultComponentLocation(org.mule.runtime.dsl.api.component.config.DefaultComponentLocation) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) CompletableFuture(java.util.concurrent.CompletableFuture) ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) InOrder(org.mockito.InOrder) ProcessorInterceptor(org.mule.runtime.api.interception.ProcessorInterceptor) InterceptionEvent(org.mule.runtime.api.interception.InterceptionEvent) ProcessorParameterValue(org.mule.runtime.api.interception.ProcessorParameterValue) InterceptionAction(org.mule.runtime.api.interception.InterceptionAction) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 9 with InterceptionAction

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

the class ReactiveInterceptorAdapterTestCase method interceptorMutatesEventAroundAfterFailWithErrorType.

@Test
public void interceptorMutatesEventAroundAfterFailWithErrorType() throws Exception {
    ErrorType errorTypeMock = mock(ErrorType.class);
    when(errorTypeMock.getIdentifier()).thenReturn("ID");
    when(errorTypeMock.getNamespace()).thenReturn("NS");
    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.fail(errorTypeMock);
        }
    });
    startFlowWithInterceptors(interceptor);
    expected.expect(MessagingException.class);
    expected.expect(withEventThat(hasErrorTypeThat(sameInstance(errorTypeMock))));
    expected.expectCause(instanceOf(InterceptionException.class));
    try {
        process(flow, eventBuilder(muleContext).message(Message.of("")).build());
    } finally {
        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)), argThat(not(empty())));
            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) ErrorType(org.mule.runtime.api.message.ErrorType) EventMatcher.hasErrorType(org.mule.tck.junit4.matcher.EventMatcher.hasErrorType) ProcessorInterceptor(org.mule.runtime.api.interception.ProcessorInterceptor) InterceptionEvent(org.mule.runtime.api.interception.InterceptionEvent) ProcessorParameterValue(org.mule.runtime.api.interception.ProcessorParameterValue) InterceptionAction(org.mule.runtime.api.interception.InterceptionAction) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 10 with InterceptionAction

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

the class ReactiveInterceptorAdapterTestCase method secondInterceptorSkipsProcessor.

@Test
public void secondInterceptorSkipsProcessor() throws Exception {
    ProcessorInterceptor interceptor1 = prepareInterceptor(new TestProcessorInterceptor("outer") {
    });
    ProcessorInterceptor interceptor2 = prepareInterceptor(new TestProcessorInterceptor("inner") {

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

Aggregations

CompletableFuture (java.util.concurrent.CompletableFuture)28 Test (org.junit.Test)28 ComponentLocation (org.mule.runtime.api.component.location.ComponentLocation)28 InterceptionAction (org.mule.runtime.api.interception.InterceptionAction)28 InterceptionEvent (org.mule.runtime.api.interception.InterceptionEvent)28 ProcessorInterceptor (org.mule.runtime.api.interception.ProcessorInterceptor)28 ProcessorParameterValue (org.mule.runtime.api.interception.ProcessorParameterValue)28 DefaultComponentLocation (org.mule.runtime.dsl.api.component.config.DefaultComponentLocation)28 SmallTest (org.mule.tck.size.SmallTest)28 InOrder (org.mockito.InOrder)27 ExpressionRuntimeException (org.mule.runtime.core.api.expression.ExpressionRuntimeException)16 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)15 InternalEvent (org.mule.runtime.core.internal.message.InternalEvent)15 ImmutableMap (com.google.common.collect.ImmutableMap)4 Thread.currentThread (java.lang.Thread.currentThread)4 Arrays.asList (java.util.Arrays.asList)4 Collection (java.util.Collection)4 Collections.singletonList (java.util.Collections.singletonList)4 Collections.singletonMap (java.util.Collections.singletonMap)4 HashMap (java.util.HashMap)4