Search in sources :

Example 1 with FlowExceptionHandler

use of org.mule.runtime.core.api.exception.FlowExceptionHandler in project mule by mulesoft.

the class DefaultFlowBuilderTestCase method buildsFullFlow.

@Test
public void buildsFullFlow() throws Exception {
    Processor processor1 = mock(Processor.class);
    Processor processor2 = mock(Processor.class);
    List<Processor> messageProcessors = new ArrayList<>();
    messageProcessors.add(processor1);
    messageProcessors.add(processor2);
    MessageSource messageSource = mock(MessageSource.class);
    ProcessingStrategyFactory processingStrategyFactory = mock(ProcessingStrategyFactory.class);
    ProcessingStrategy processingStrategy = mock(ProcessingStrategy.class);
    when(processingStrategyFactory.create(any(), any())).thenReturn(processingStrategy);
    FlowExceptionHandler exceptionListener = mock(FlowExceptionHandler.class);
    Flow flow = flowBuilder.processors(messageProcessors).source(messageSource).processingStrategyFactory(processingStrategyFactory).messagingExceptionHandler(exceptionListener).build();
    assertThat(flow.getName(), equalTo(FLOW_NAME));
    assertThat(flow.getMuleContext(), is(muleContext));
    assertThat(flow.getProcessors(), contains(processor1, processor2));
    assertThat(flow.getSource(), is(messageSource));
    assertThat(flow.getExceptionListener(), is(exceptionListener));
    assertThat(flow.getProcessingStrategy(), sameInstance(processingStrategy));
}
Also used : Processor(org.mule.runtime.core.api.processor.Processor) ArrayList(java.util.ArrayList) ProcessingStrategyFactory(org.mule.runtime.core.api.processor.strategy.ProcessingStrategyFactory) MessageSource(org.mule.runtime.core.api.source.MessageSource) ProcessingStrategy(org.mule.runtime.core.api.processor.strategy.ProcessingStrategy) FlowExceptionHandler(org.mule.runtime.core.api.exception.FlowExceptionHandler) Flow(org.mule.runtime.core.api.construct.Flow) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 2 with FlowExceptionHandler

use of org.mule.runtime.core.api.exception.FlowExceptionHandler in project mule by mulesoft.

the class TestLegacyEventUtils method getEffectiveExceptionHandler.

/**
 * @return the {@link FlowExceptionHandler} to be applied if an exception is unhandled during the processing of the given
 *         event.
 */
public static FlowExceptionHandler getEffectiveExceptionHandler(CoreEvent event) {
    Field exceptionHandlerField;
    try {
        exceptionHandlerField = event.getContext().getClass().getSuperclass().getDeclaredField("exceptionHandler");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    exceptionHandlerField.setAccessible(true);
    try {
        BaseEventContext eventContext = (BaseEventContext) event.getContext();
        FlowExceptionHandler effectiveMessagingExceptionHandler = (FlowExceptionHandler) exceptionHandlerField.get(eventContext);
        while (eventContext.getParentContext().isPresent() && effectiveMessagingExceptionHandler == HANDLER) {
            eventContext = eventContext.getParentContext().get();
            effectiveMessagingExceptionHandler = (FlowExceptionHandler) exceptionHandlerField.get(eventContext);
        }
        return effectiveMessagingExceptionHandler;
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        exceptionHandlerField.setAccessible(false);
    }
}
Also used : Field(java.lang.reflect.Field) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) FlowExceptionHandler(org.mule.runtime.core.api.exception.FlowExceptionHandler)

Example 3 with FlowExceptionHandler

use of org.mule.runtime.core.api.exception.FlowExceptionHandler in project mule by mulesoft.

the class ModuleFlowProcessingPhaseTestCase method before.

@Before
public void before() throws Exception {
    final PrivilegedMuleContext muleContext = (PrivilegedMuleContext) mockMuleContext();
    when(muleContext.getErrorTypeRepository()).thenReturn(createDefaultErrorTypeRepository());
    ErrorTypeLocator errorTypeLocator = mock(ErrorTypeLocator.class);
    when(errorTypeLocator.lookupErrorType(any(Throwable.class))).thenReturn(ErrorTypeBuilder.builder().namespace(CORE_NAMESPACE_NAME).identifier(ANY_IDENTIFIER).build());
    when(muleContext.getErrorTypeLocator()).thenReturn(errorTypeLocator);
    event = mock(CoreEvent.class);
    mockException = mock(RuntimeException.class);
    policyManager = mock(PolicyManager.class);
    sourcePolicy = mock(SourcePolicy.class);
    when(policyManager.createSourcePolicyInstance(any(), any(), any(), any())).thenReturn(sourcePolicy);
    successResult = mock(SourcePolicySuccessResult.class);
    when(successResult.getResult()).then(invocation -> event);
    when(successResult.getResponseParameters()).thenReturn(() -> emptyMap());
    when(successResult.createErrorResponseParameters()).thenReturn(event -> emptyMap());
    failureResult = mock(SourcePolicyFailureResult.class);
    when(failureResult.getMessagingException()).then(invocation -> messagingException);
    when(failureResult.getErrorResponseParameters()).thenReturn(() -> emptyMap());
    when(sourcePolicy.process(any())).thenAnswer(invocation -> {
        event = invocation.getArgumentAt(0, CoreEvent.class);
        return just(right(successResult));
    });
    moduleFlowProcessingPhase = new ModuleFlowProcessingPhase(policyManager);
    moduleFlowProcessingPhase.setMuleContext(muleContext);
    initialiseIfNeeded(moduleFlowProcessingPhase, muleContext);
    flow = mock(FlowConstruct.class, withSettings().extraInterfaces(Component.class));
    final FlowExceptionHandler exceptionHandler = mock(FlowExceptionHandler.class);
    when(flow.getExceptionListener()).thenReturn(exceptionHandler);
    when(exceptionHandler.apply(any())).thenAnswer(invocationOnMock -> error(invocationOnMock.getArgumentAt(0, MessagingException.class)));
    when(flow.getMuleContext()).thenReturn(muleContext);
    context = mock(MessageProcessContext.class);
    final MessageSource source = mock(MessageSource.class);
    when(source.getRootContainerLocation()).thenReturn(Location.builder().globalName("root").build());
    when(source.getLocation()).thenReturn(mock(ComponentLocation.class));
    when(context.getMessageSource()).thenReturn(source);
    when(context.getTransactionConfig()).thenReturn(empty());
    when(muleContext.getConfigurationComponentLocator().find(any(Location.class))).thenReturn(of(flow));
    template = mock(ModuleFlowProcessingPhaseTemplate.class);
    when(template.getMessage()).thenReturn(Message.of(null));
    when(template.getNotificationFunctions()).thenReturn(emptyList());
    when(template.sendResponseToClient(any(), any())).thenAnswer(invocation -> Mono.empty());
    when(template.sendFailureResponseToClient(any(), any())).thenAnswer(invocation -> Mono.empty());
    notifier = mock(PhaseResultNotifier.class);
}
Also used : PolicyManager(org.mule.runtime.core.internal.policy.PolicyManager) SourcePolicyFailureResult(org.mule.runtime.core.internal.policy.SourcePolicyFailureResult) SourcePolicy(org.mule.runtime.core.internal.policy.SourcePolicy) FlowConstruct(org.mule.runtime.core.api.construct.FlowConstruct) MessageSource(org.mule.runtime.core.api.source.MessageSource) PrivilegedMuleContext(org.mule.runtime.core.privileged.PrivilegedMuleContext) FlowExceptionHandler(org.mule.runtime.core.api.exception.FlowExceptionHandler) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) ErrorTypeLocator(org.mule.runtime.core.privileged.exception.ErrorTypeLocator) SourcePolicySuccessResult(org.mule.runtime.core.internal.policy.SourcePolicySuccessResult) MessageProcessContext(org.mule.runtime.core.privileged.execution.MessageProcessContext) Location(org.mule.runtime.api.component.location.Location) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) Before(org.junit.Before)

Aggregations

FlowExceptionHandler (org.mule.runtime.core.api.exception.FlowExceptionHandler)3 MessageSource (org.mule.runtime.core.api.source.MessageSource)2 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 Before (org.junit.Before)1 Test (org.junit.Test)1 ComponentLocation (org.mule.runtime.api.component.location.ComponentLocation)1 Location (org.mule.runtime.api.component.location.Location)1 Flow (org.mule.runtime.core.api.construct.Flow)1 FlowConstruct (org.mule.runtime.core.api.construct.FlowConstruct)1 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)1 Processor (org.mule.runtime.core.api.processor.Processor)1 ProcessingStrategy (org.mule.runtime.core.api.processor.strategy.ProcessingStrategy)1 ProcessingStrategyFactory (org.mule.runtime.core.api.processor.strategy.ProcessingStrategyFactory)1 PolicyManager (org.mule.runtime.core.internal.policy.PolicyManager)1 SourcePolicy (org.mule.runtime.core.internal.policy.SourcePolicy)1 SourcePolicyFailureResult (org.mule.runtime.core.internal.policy.SourcePolicyFailureResult)1 SourcePolicySuccessResult (org.mule.runtime.core.internal.policy.SourcePolicySuccessResult)1 PrivilegedMuleContext (org.mule.runtime.core.privileged.PrivilegedMuleContext)1 BaseEventContext (org.mule.runtime.core.privileged.event.BaseEventContext)1