Search in sources :

Example 21 with ComponentLocation

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

the class ReactiveInterceptorAdapterTestCase method secondInterceptorThrowsExceptionBefore.

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

        @Override
        public void before(ComponentLocation location, Map<String, ProcessorParameterValue> parameters, InterceptionEvent event) {
            throw expectedException;
        }
    });
    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, never()).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(of(expectedException)));
            inOrder.verify(interceptor1).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) 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) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 22 with ComponentLocation

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

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

Example 24 with ComponentLocation

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

the class DefaultEventContextTestCase method componentData.

@Test
@Description("Verify that a location produces connector and source data.")
public void componentData() throws Exception {
    TypedComponentIdentifier typedComponentIdentifier = TypedComponentIdentifier.builder().type(SOURCE).identifier(buildFromStringRepresentation("http:listener")).build();
    ComponentLocation location = mock(ComponentLocation.class);
    when(location.getComponentIdentifier()).thenReturn(typedComponentIdentifier);
    when(location.getParts()).thenReturn(asList(new DefaultLocationPart("flow", empty(), empty(), empty())));
    BaseEventContext context = contextWithComponentLocation.apply(location);
    assertThat(context.getOriginatingLocation().getComponentIdentifier().getIdentifier().getNamespace(), is("http"));
    assertThat(context.getOriginatingLocation().getComponentIdentifier().getIdentifier().getName(), is("listener"));
}
Also used : ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) DefaultLocationPart(org.mule.runtime.dsl.api.component.config.DefaultComponentLocation.DefaultLocationPart) TypedComponentIdentifier(org.mule.runtime.api.component.TypedComponentIdentifier) Description(io.qameta.allure.Description) Test(org.junit.Test)

Example 25 with ComponentLocation

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

the class ModuleFlowProcessingPhase method runPhase.

@Override
public void runPhase(final ModuleFlowProcessingPhaseTemplate template, final MessageProcessContext messageProcessContext, final PhaseResultNotifier phaseResultNotifier) {
    try {
        final MessageSource messageSource = messageProcessContext.getMessageSource();
        final FlowConstruct flowConstruct = (FlowConstruct) componentLocator.find(messageSource.getRootContainerLocation()).get();
        final ComponentLocation sourceLocation = messageSource.getLocation();
        final Consumer<Either<MessagingException, CoreEvent>> terminateConsumer = getTerminateConsumer(messageSource, template);
        final CompletableFuture<Void> responseCompletion = new CompletableFuture<>();
        final CoreEvent templateEvent = createEvent(template, sourceLocation, responseCompletion, flowConstruct);
        try {
            FlowProcessor flowExecutionProcessor = new FlowProcessor(template, flowConstruct.getExceptionListener(), templateEvent);
            flowExecutionProcessor.setAnnotations(flowConstruct.getAnnotations());
            final SourcePolicy policy = policyManager.createSourcePolicyInstance(messageSource, templateEvent, flowExecutionProcessor, template);
            final PhaseContext phaseContext = new PhaseContext(template, messageProcessContext, phaseResultNotifier, terminateConsumer);
            just(templateEvent).doOnNext(onMessageReceived(template, messageProcessContext, flowConstruct)).flatMap(request -> from(policy.process(request))).flatMap(policyResult -> policyResult.reduce(policyFailure(phaseContext, flowConstruct, messageSource), policySuccess(phaseContext, flowConstruct, messageSource))).doOnSuccess(aVoid -> phaseResultNotifier.phaseSuccessfully()).doOnError(onFailure(flowConstruct, messageSource, phaseResultNotifier, terminateConsumer)).doAfterTerminate(() -> responseCompletion.complete(null)).subscribe();
        } catch (Exception e) {
            from(template.sendFailureResponseToClient(new MessagingExceptionResolver(messageProcessContext.getMessageSource()).resolve(new MessagingException(templateEvent, e), muleContext), template.getFailedExecutionResponseParametersFunction().apply(templateEvent))).doOnTerminate(() -> phaseResultNotifier.phaseFailure(e)).subscribe();
        }
    } catch (Exception t) {
        phaseResultNotifier.phaseFailure(t);
    }
}
Also used : Message(org.mule.runtime.api.message.Message) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) ErrorTypeRepository(org.mule.runtime.api.exception.ErrorTypeRepository) CoreEvent.builder(org.mule.runtime.core.api.event.CoreEvent.builder) FunctionalUtils.safely(org.mule.runtime.core.internal.util.FunctionalUtils.safely) Map(java.util.Map) SOURCE_ERROR_RESPONSE_GENERATE(org.mule.runtime.core.api.exception.Errors.ComponentIdentifiers.Handleable.SOURCE_ERROR_RESPONSE_GENERATE) Mono.just(reactor.core.publisher.Mono.just) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) Mono.empty(reactor.core.publisher.Mono.empty) PolicyManager(org.mule.runtime.core.internal.policy.PolicyManager) SOURCE_ERROR_RESPONSE_SEND(org.mule.runtime.core.api.exception.Errors.ComponentIdentifiers.Handleable.SOURCE_ERROR_RESPONSE_SEND) PrivilegedMuleContext(org.mule.runtime.core.privileged.PrivilegedMuleContext) MessagingExceptionResolver(org.mule.runtime.core.internal.util.MessagingExceptionResolver) Collection(java.util.Collection) NullExceptionHandler(org.mule.runtime.core.api.exception.NullExceptionHandler) FlowExceptionHandler(org.mule.runtime.core.api.exception.FlowExceptionHandler) Message.of(org.mule.runtime.api.message.Message.of) Either.left(org.mule.runtime.core.api.functional.Either.left) ErrorBuilder.builder(org.mule.runtime.core.internal.message.ErrorBuilder.builder) Either.right(org.mule.runtime.core.api.functional.Either.right) MessageProcessors.processWithChildContext(org.mule.runtime.core.privileged.processor.MessageProcessors.processWithChildContext) MESSAGE_RESPONSE(org.mule.runtime.api.notification.ConnectorMessageNotification.MESSAGE_RESPONSE) MessageUtils.toMessage(org.mule.runtime.core.internal.util.message.MessageUtils.toMessage) ErrorType(org.mule.runtime.api.message.ErrorType) Optional(java.util.Optional) FlowConstruct(org.mule.runtime.core.api.construct.FlowConstruct) SourcePolicy(org.mule.runtime.core.internal.policy.SourcePolicy) SOURCE_RESPONSE_SEND(org.mule.runtime.core.api.exception.Errors.ComponentIdentifiers.Handleable.SOURCE_RESPONSE_SEND) SourcePolicySuccessResult(org.mule.runtime.core.internal.policy.SourcePolicySuccessResult) MessageSource(org.mule.runtime.core.api.source.MessageSource) MessageProcessContext(org.mule.runtime.core.privileged.execution.MessageProcessContext) CompletableFuture(java.util.concurrent.CompletableFuture) InternalExceptionUtils.createErrorEvent(org.mule.runtime.core.internal.util.InternalExceptionUtils.createErrorEvent) Processor(org.mule.runtime.core.api.processor.Processor) Function(java.util.function.Function) MessageUtils.toMessageCollection(org.mule.runtime.core.internal.util.message.MessageUtils.toMessageCollection) ConfigurationComponentLocator(org.mule.runtime.api.component.location.ConfigurationComponentLocator) MuleException(org.mule.runtime.api.exception.MuleException) SourcePolicyFailureResult(org.mule.runtime.core.internal.policy.SourcePolicyFailureResult) Mono.from(reactor.core.publisher.Mono.from) SOURCE_RESPONSE_GENERATE(org.mule.runtime.core.api.exception.Errors.ComponentIdentifiers.Handleable.SOURCE_RESPONSE_GENERATE) Mono.error(reactor.core.publisher.Mono.error) Mono.fromCallable(reactor.core.publisher.Mono.fromCallable) MESSAGE_RECEIVED(org.mule.runtime.api.notification.ConnectorMessageNotification.MESSAGE_RECEIVED) MessageProcessors.processToApply(org.mule.runtime.core.privileged.processor.MessageProcessors.processToApply) MESSAGE_ERROR_RESPONSE(org.mule.runtime.api.notification.ConnectorMessageNotification.MESSAGE_ERROR_RESPONSE) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Publisher(org.reactivestreams.Publisher) EventContextFactory.create(org.mule.runtime.core.api.event.EventContextFactory.create) Mono(reactor.core.publisher.Mono) Initialisable(org.mule.runtime.api.lifecycle.Initialisable) Result(org.mule.runtime.extension.api.runtime.operation.Result) AbstractComponent(org.mule.runtime.api.component.AbstractComponent) Mono.when(reactor.core.publisher.Mono.when) TypedValue(org.mule.runtime.api.metadata.TypedValue) Consumer(java.util.function.Consumer) Either(org.mule.runtime.core.api.functional.Either) MessageProcessTemplate(org.mule.runtime.core.privileged.execution.MessageProcessTemplate) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) Builder(org.mule.runtime.core.internal.message.InternalEvent.Builder) ANY(org.mule.runtime.api.metadata.MediaType.ANY) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) InternalEvent(org.mule.runtime.core.internal.message.InternalEvent) SourcePolicy(org.mule.runtime.core.internal.policy.SourcePolicy) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) FlowConstruct(org.mule.runtime.core.api.construct.FlowConstruct) MessageSource(org.mule.runtime.core.api.source.MessageSource) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) MuleException(org.mule.runtime.api.exception.MuleException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) CompletableFuture(java.util.concurrent.CompletableFuture) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Either(org.mule.runtime.core.api.functional.Either) MessagingExceptionResolver(org.mule.runtime.core.internal.util.MessagingExceptionResolver)

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