Search in sources :

Example 16 with ComponentLocation

use of org.mule.runtime.api.component.location.ComponentLocation in project mule by mulesoft.

the class ReactiveInterceptorAdapterTestCase method firstInterceptorMutatesEventBefore.

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

        @Override
        public void before(ComponentLocation location, Map<String, ProcessorParameterValue> parameters, InterceptionEvent event) {
            event.message(Message.of(TEST_PAYLOAD));
        }
    });
    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", TEST_PAYLOAD), argThat(interceptionHasPayloadValue(TEST_PAYLOAD)));
        inOrder.verify(interceptor1).around(any(), mapArgWithEntry("param", TEST_PAYLOAD), 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 : 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) InterceptionEvent(org.mule.runtime.api.interception.InterceptionEvent) ProcessorParameterValue(org.mule.runtime.api.interception.ProcessorParameterValue) InternalEvent(org.mule.runtime.core.internal.message.InternalEvent) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 17 with ComponentLocation

use of org.mule.runtime.api.component.location.ComponentLocation 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 18 with ComponentLocation

use of org.mule.runtime.api.component.location.ComponentLocation in project mule by mulesoft.

the class ReactiveInterceptorAdapterTestCase method firstInterceptorDoesntApply.

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

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

        @Override
        public ProcessorInterceptor get() {
            return interceptor1;
        }
    }, () -> 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, never()).before(any(), any(), any());
        inOrder.verify(interceptor2).before(any(), any(), any());
        inOrder.verify(interceptor1, never()).around(any(), any(), any(), any());
        inOrder.verify(interceptor2).around(any(), any(), any(), any());
        inOrder.verify(processor).process(any());
        inOrder.verify(interceptor2).after(any(), any(), eq(empty()));
        inOrder.verify(interceptor1, never()).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 19 with ComponentLocation

use of org.mule.runtime.api.component.location.ComponentLocation 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 20 with ComponentLocation

use of org.mule.runtime.api.component.location.ComponentLocation in project mule by mulesoft.

the class ReactiveInterceptorAdapterTestCase method firstInterceptorThrowsExceptionAfter.

@Test
public void firstInterceptorThrowsExceptionAfter() throws Exception {
    RuntimeException expectedException = new RuntimeException("Some Error");
    ProcessorInterceptor interceptor1 = prepareInterceptor(new TestProcessorInterceptor("outer") {

        @Override
        public void after(ComponentLocation location, InterceptionEvent event, Optional<Throwable> thrown) {
            throw expectedException;
        }
    });
    ProcessorInterceptor interceptor2 = prepareInterceptor(new TestProcessorInterceptor("inner") {
    });
    startFlowWithInterceptors(interceptor1, interceptor2);
    expected.expectCause(sameInstance(expectedException));
    try {
        process(flow, eventBuilder(muleContext).message(Message.of("")).build());
    } finally {
        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).process(any());
            inOrder.verify(interceptor2).after(any(), any(), eq(empty()));
            inOrder.verify(interceptor1).after(any(), any(), eq(empty()));
            verifyParametersResolvedAndDisposed(times(1));
        }
    }
}
Also used : DefaultComponentLocation(org.mule.runtime.dsl.api.component.config.DefaultComponentLocation) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) 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) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Aggregations

ComponentLocation (org.mule.runtime.api.component.location.ComponentLocation)54 Test (org.junit.Test)46 ProcessorInterceptor (org.mule.runtime.api.interception.ProcessorInterceptor)44 SmallTest (org.mule.tck.size.SmallTest)44 DefaultComponentLocation (org.mule.runtime.dsl.api.component.config.DefaultComponentLocation)43 InOrder (org.mockito.InOrder)41 InterceptionEvent (org.mule.runtime.api.interception.InterceptionEvent)41 ProcessorParameterValue (org.mule.runtime.api.interception.ProcessorParameterValue)35 CompletableFuture (java.util.concurrent.CompletableFuture)29 InterceptionAction (org.mule.runtime.api.interception.InterceptionAction)28 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)26 InternalEvent (org.mule.runtime.core.internal.message.InternalEvent)24 ExpressionRuntimeException (org.mule.runtime.core.api.expression.ExpressionRuntimeException)23 Map (java.util.Map)7 Component (org.mule.runtime.api.component.Component)7 InitialisationException (org.mule.runtime.api.lifecycle.InitialisationException)7 HashMap (java.util.HashMap)6 MuleException (org.mule.runtime.api.exception.MuleException)6 ProcessorInterceptorFactory (org.mule.runtime.api.interception.ProcessorInterceptorFactory)6 Message (org.mule.runtime.api.message.Message)6