/** * Creates an {@link Error} mock that will return the provided exception when calling {@link Error#getCause()} * * @param exception the exception to use to create the mock * @return a mocked {@link Error} */ public static Error createErrorMock(Exception exception) { Error errorMock = mock(Error.class); when(errorMock.getCause()).thenReturn(exception); return errorMock; }
@Override public CoreEvent process(CoreEvent event) throws MuleException { event.getError().ifPresent(theError -> exception = theError.getCause()); TypedValue<Object> payload = event.getMessage().getPayload(); if (payload.getValue() != null) { messages.add((TestTransactionalConnection) payload.getValue()); } return event; }
private Optional<Error> mockError(ErrorType errorType, Throwable cause) { Error error = mock(Error.class); when(error.getErrorType()).thenReturn(errorType); when(error.getCause()).thenReturn(cause); return Optional.of(error); }
@Override public Publisher<Void> onFailure(MessagingException exception, Map<String, Object> parameters) { final CoreEvent event = exception.getEvent(); final boolean isBackPressureError = event.getError() .map(e -> flowBackPressueErrorType.equals(e.getErrorType())) .orElse(false); SourceCallbackExecutor executor; if (isBackPressureError) { LOGGER.info("FLOW OVERLOAD - {}.", event.getError().get().getCause().getMessage()); executor = onBackPressureExecutor; parameters = emptyMap(); context.addVariable(BACK_PRESSURE_ACTION_CONTEXT_PARAM, backPressureAction); } else { executor = onErrorExecutor; } return from(executor.execute(event, parameters, context)).doAfterTerminate(this::rollback); }
/** * Runs the specified flow with the provided event and configuration expecting a failure with a cause that matches the given * {@code matcher}. * <p> * Will fail if there's no failure running the flow. */ public void runExpectingException(Matcher<Throwable> causeMatcher) throws Exception { try { runNoVerify(); fail("Flow executed successfully. Expecting exception"); } catch (EventProcessingException e) { verify(getFlowConstructName()); assertThat(e.getEvent().getError().get().getCause(), causeMatcher); } }
private void assertPostErrorNotification(CoreEvent inEvent, MessageProcessorNotification postNotification) { assertThat(postNotification.getEvent(), not(equalTo(inEvent))); assertThat(postNotification.getEvent().getError().isPresent(), is(true)); assertThat(postNotification.getEvent().getError().get().getCause(), is(illegalStateException)); assertThat(postNotification.getException(), is(instanceOf(MessagingException.class))); assertThat(postNotification.getException().getCause(), is(illegalStateException)); }
/** * Runs the specified flow with the provided event and configuration expecting a failure with an {@link CoreEvent} that matches * the given {@code errorEventMatcher}. * <p> * Will fail if there's no failure running the flow. */ public void runExpectingException(Matcher<Throwable> causeMatcher, Matcher<CoreEvent> errorEventMatcher) throws Exception { try { runNoVerify(); fail("Flow executed successfully. Expecting exception"); } catch (EventProcessingException e) { verify(getFlowConstructName()); assertThat(e.getEvent().getError().get().getCause(), causeMatcher); assertThat(e.getEvent(), errorEventMatcher); } }
@Test public void failureInFlowCallsOnErrorDirectlyAndFailsHandlingIt() throws Exception { startFlow("failureInFlowCallsOnErrorDirectlyAndFailsHandlingIt"); probe(TIMEOUT_MILLIS, POLL_DELAY_MILLIS, () -> assertState(false, false, true)); assertThat(HeisenbergSource.terminateStatus, is(ERROR_INVOKE)); Optional<Error> optionalError = HeisenbergSource.error; assertThat(optionalError, is(not(empty()))); assertThat(optionalError.get().getErrorType(), is(errorType(SOURCE_ERROR_RESPONSE_GENERATE))); MuleException me = (MuleException) unwrap(optionalError.get().getCause()); assertThat((String) me.getInfo().get(INFO_LOCATION_KEY), containsString("failureInFlowCallsOnErrorDirectlyAndFailsHandlingIt/source")); assertThat((String) me.getInfo().get(INFO_SOURCE_XML_KEY), containsString("heisenberg:success-info")); }
@Test public void exception() throws Exception { CoreEvent event = createEvent(); RuntimeException rte = new RuntimeException(); when(mockError.getCause()).thenReturn(rte); when(mockError.getErrorType()).thenReturn(mockErrorType); event = CoreEvent.builder(event).message(InternalMessage.builder(event.getMessage()).build()).build(); Object exception = evaluate("exception", event); assertThat(exception, is(instanceOf(MessagingException.class))); assertThat(((MessagingException) exception).getCause(), is(rte)); }
@Test public void buildErrorFromCustomException() { Error error = builder(new ComposedErrorMessageAwareException(createStaticMessage(EXCEPTION_MESSAGE))) .errorType(mockErrorType) .build(); assertThat(error.getDescription(), is(EXCEPTION_MESSAGE)); assertThat(error.getDetailedDescription(), is(EXCEPTION_MESSAGE)); assertThat(error.getCause(), is(instanceOf(ComposedErrorMessageAwareException.class))); assertThat(error.getErrorType(), is(mockErrorType)); assertThat(error.getErrorMessage().getPayload().getValue(), is(TEST_PAYLOAD)); List<Error> childErrors = error.getChildErrors(); assertThat(childErrors, hasSize(2)); assertThat(childErrors.get(0).getCause(), is(instanceOf(RuntimeException.class))); assertThat(childErrors.get(1).getCause(), is(instanceOf(IOException.class))); }
@Test public void sourceFailsOnSuccessAndOnErrorParametersCallsOnTerminate() throws Exception { startFlow("sourceWithInvalidSuccessAndErrorParameters"); probe(TIMEOUT_MILLIS, POLL_DELAY_MILLIS, () -> assertState(false, false, true)); assertThat(HeisenbergSource.terminateStatus, is(ERROR_INVOKE)); Optional<Error> optionalError = HeisenbergSource.error; assertThat(optionalError, is(not(empty()))); assertThat(optionalError.get().getErrorType(), is(errorType(SOURCE_ERROR_RESPONSE_GENERATE))); MuleException me = (MuleException) unwrap(optionalError.get().getCause()); assertThat((String) me.getInfo().get(INFO_LOCATION_KEY), containsString("sourceWithInvalidSuccessAndErrorParameters/source")); assertThat((String) me.getInfo().get(INFO_SOURCE_XML_KEY), containsString("heisenberg:success-info")); }
@Test public void assignException() throws Exception { CoreEvent event = createEvent(); event = CoreEvent.builder(event).message(InternalMessage.builder(event.getMessage()).build()).build(); RuntimeException runtimeException = new RuntimeException(); when(mockError.getCause()).thenReturn(runtimeException); when(mockError.getErrorType()).thenReturn(mockErrorType); assertImmutableVariable("exception='other'", event); }
@Test public void buildErrorFromMuleException() { MuleException exception = new DefaultMuleException(new RuntimeException(EXCEPTION_MESSAGE)); Error error = builder(exception).errorType(mockErrorType).build(); assertThat(error.getCause(), is(exception)); assertThat(error.getDescription(), containsString(EXCEPTION_MESSAGE)); assertThat(error.getDetailedDescription(), containsString(EXCEPTION_MESSAGE)); assertThat(error.getErrorType(), is(mockErrorType)); assertThat(error.getChildErrors(), is(empty())); }
@Test public void buildErrorFromException() { RuntimeException exception = new RuntimeException(EXCEPTION_MESSAGE); Error error = builder(exception).errorType(mockErrorType).build(); assertThat(error.getCause(), is(exception)); assertThat(error.getDescription(), is(EXCEPTION_MESSAGE)); assertThat(error.getDetailedDescription(), is(EXCEPTION_MESSAGE)); assertThat(error.getErrorType(), is(mockErrorType)); assertThat(error.getChildErrors(), is(empty())); }
@Test public void exceptionCausedBy() throws Exception { CoreEvent event = createEvent(); Message message = event.getMessage(); MessagingException me = new MessagingException(CoreMessages.createStaticMessage(""), InternalEvent.builder(context).message(message).build(), new IllegalAccessException()); when(mockError.getCause()).thenReturn(me); assertTrue((Boolean) evaluate("exception.causedBy(java.lang.IllegalAccessException)", event)); }
@Test @Description("Errors are thrown via CompositeRoutingException with RoutingResult containing details of failures.") public void error() throws Throwable { RuntimeException exception = new IllegalStateException(); RoutingPair failingPair = of(testEvent(), createFailingRoutingPair(exception)); expectedException.expect(instanceOf(CompositeRoutingException.class)); invokeStrategyBlocking(strategy, testEvent(), asList(failingPair), throwable -> { CompositeRoutingException compositeRoutingException = assertCompositeRoutingException(throwable, 1); RoutingResult routingResult = assertRoutingResult(compositeRoutingException, 0, 1); assertThat(routingResult.getFailures().get("0").getCause(), is(exception)); }); }
@Test @Description("When a route timeout occurs a CompositeRoutingException is thrown with details of timeout error in RoutingResult.") public void timeout() throws Throwable { strategy = createStrategy(processingStrategy, 1, true, 50); expectedException.expect(instanceOf(CompositeRoutingException.class)); invokeStrategyBlocking(strategy, testEvent(), asList(createRoutingPairWithSleep(of(1), 250)), throwable -> { CompositeRoutingException compositeRoutingException = assertCompositeRoutingException(throwable, 1); RoutingResult routingResult = assertRoutingResult(compositeRoutingException, 0, 1); assertThat(routingResult.getFailures().get("0").getCause(), instanceOf(TimeoutException.class)); }); }
@Test @Description("When an error occurs all routes are executed regardless and a CompositeRoutingException is thrown containing a RoutingResult with details of both failures and successes.") public void errorDelayed() throws Throwable { Processor processorSpy = createProcessorSpy(testEvent().getMessage()); RuntimeException exception1 = new IllegalStateException(); RoutingPair failingPair1 = of(testEvent(), createFailingRoutingPair(exception1)); RuntimeException exception2 = new UnsupportedOperationException(); RoutingPair failingPair2 = of(testEvent(), createFailingRoutingPair(exception2)); RuntimeException exception3 = new IndexOutOfBoundsException(); RoutingPair failingPair3 = of(testEvent(), createFailingRoutingPair(exception3)); RoutingPair okPair = of(testEvent(), createChain(processorSpy)); expectedException.expect(instanceOf(CompositeRoutingException.class)); invokeStrategyBlocking(strategy, testEvent(), asList(failingPair1, failingPair2, failingPair3, okPair), throwable -> { verify(processorSpy, times(1)).process(any(CoreEvent.class)); CompositeRoutingException compositeRoutingException = assertCompositeRoutingException(throwable, 3); RoutingResult routingResult = assertRoutingResult(compositeRoutingException, 1, 3); assertThat(routingResult.getFailures().get("0").getCause(), is(exception1)); assertThat(routingResult.getFailures().get("1").getCause(), is(exception2)); assertThat(routingResult.getFailures().get("2").getCause(), is(exception3)); assertThat(routingResult.getFailures().get("3"), is(nullValue())); }); }
@Test @Description("When a route timeout occurs all routes are still executed and a CompositeRoutingException is thrown with details of timeout error and successful routes in RoutingResult.") public void timeoutDelayed() throws Throwable { strategy = createStrategy(processingStrategy, 1, true, 50); Message pair2Result = of(2); Processor pair2Processor = createProcessorSpy(pair2Result); RoutingPair pair2 = of(testEvent(), createChain(pair2Processor)); expectedException.expect(instanceOf(CompositeRoutingException.class)); invokeStrategyBlocking(strategy, testEvent(), asList(createRoutingPairWithSleep(of(1), 250), pair2), throwable -> { verify(pair2Processor, times(1)).process(any(CoreEvent.class)); CompositeRoutingException compositeRoutingException = assertCompositeRoutingException(throwable, 1); RoutingResult routingResult = assertRoutingResult(compositeRoutingException, 1, 1); assertThat(routingResult.getFailures().get("0").getCause(), instanceOf(TimeoutException.class)); assertThat(routingResult.getResults().get("1"), is(pair2Result)); }); }
@Test public void buildError() { String detailedDescription = "detailed description"; String description = "description"; ErrorType errorType = mockErrorType; Message errorMessage = of(null); IllegalArgumentException exception = new IllegalArgumentException("some message"); Error error = builder() .errorType(errorType) .description(description) .detailedDescription(detailedDescription) .exception(exception) .errorMessage(errorMessage) .build(); assertThat(error.getDescription(), is(description)); assertThat(error.getDetailedDescription(), is(detailedDescription)); assertThat(error.getCause(), is(exception)); assertThat(error.getErrorType(), is(errorType)); assertThat(error.getErrorMessage(), is(errorMessage)); assertThat(error.getChildErrors(), is(empty())); }