@Override protected boolean matchesSafely(CoreEvent item) { return (messageMatcher != null ? messageMatcher.matches(item.getMessage()) : true) && (variablesMatcher != null ? variablesMatcher.matches(item.getVariables()) : true) && (securityContextMatcher != null ? securityContextMatcher.matches(item.getSecurityContext()) : true) && (errorTypeMatcher != null ? errorTypeMatcher.matches(item.getError().get().getErrorType()) : true); }
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); }
@Test public void acceptsCriticalChild() { ErrorType errorType = mock(ErrorType.class); when(error.getErrorType()).thenReturn(errorType); when(errorType.getParentErrorType()).thenReturn(CRITICAL_ERROR_TYPE); assertThat(handler.accept(muleEvent), is(true)); }
@Test public void acceptsCritical() { when(error.getErrorType()).thenReturn(CRITICAL_ERROR_TYPE); assertThat(handler.accept(muleEvent), is(true)); }
private void assertExceptionErrorType(MessagingException me, ErrorType expected) { Optional<Error> error = me.getEvent().getError(); assertThat("No error found, expecting error with error type [" + expected + "]", error.isPresent(), is(true)); assertThat(error.get().getErrorType(), is(expected)); }
@Override protected boolean matchesSafely(MessagingException item) { errorTypeId = item.getEvent().getError().get().getErrorType().getIdentifier(); return FLOW_BACK_PRESSURE_ERROR_IDENTIFIER.equals(errorTypeId); } };
@Override public Supplier<SourceResult> resolve(ExecutionContext executionContext) { return new LazyValue<>(() -> { Error error = errorArgumentResolver.resolve(executionContext).get(); SourceCallbackContext callbackContext = callbackContextArgumentResolver.resolve(executionContext).get(); if (error == null) { return success(callbackContext); } else { String errorIdentifier = error.getErrorType().getIdentifier(); return isErrorGeneratingErrorResponse(errorIdentifier) ? invocationError(error, callbackContext) : responseError(error, callbackContext); } }); }
@Override protected void describeMismatchSafely(CoreEvent item, Description mismatchDescription) { mismatchDescription.appendText("was "); if (messageMatcher != null) { messageMatcher.describeMismatch(item.getMessage(), mismatchDescription); } if (variablesMatcher != null) { variablesMatcher.describeMismatch(item.getVariables(), mismatchDescription); } if (errorTypeMatcher != null) { errorTypeMatcher.describeMismatch(item.getError().get().getErrorType(), mismatchDescription); } if (securityContextMatcher != null) { securityContextMatcher.describeMismatch(item.getSecurityContext(), mismatchDescription); } }
@Before public void before() throws MuleException { Error mockError = mock(Error.class); when(mockError.getErrorType()).thenReturn(mockErrorType); event = getEventBuilder().message(Message.of("")).error(mockError).build(); mockException = new MessagingException(event, new Exception()); when(mockTestExceptionStrategy2.accept(any(CoreEvent.class))).thenReturn(true); }
/** * Runs the specified flow with the provided event and configuration expecting a failure with an error type that matches the * given {@code matcher}. * <p> * Will fail if there's no failure running the flow. */ public void runExpectingException(ErrorTypeMatcher matcher) throws Exception { try { runNoVerify(); fail("Flow executed successfully. Expecting exception"); } catch (EventProcessingException e) { verify(getFlowConstructName()); assertThat(e.getEvent().getError().get().getErrorType(), matcher); } }
@Test public void onException() { callback.onException(exception); verify(responseCallback).responseSentWithFailure( argThat(e -> e.getRootCause().equals(exception)), argThat(event -> event.getError().isPresent() && event.getError().get().getErrorType().equals(errorType))); }
/** * 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(ErrorTypeMatcher matcher, Matcher<CoreEvent> errorEventMatcher) throws Exception { try { runNoVerify(); fail("Flow executed successfully. Expecting exception"); } catch (EventProcessingException e) { verify(getFlowConstructName()); assertThat(e.getEvent().getError().get().getErrorType(), matcher); assertThat(e.getEvent(), errorEventMatcher); } }
@Test public void sourceFailsInsideOnErrorAndCallsOnTerminate() throws Exception { startFlow("sourceFailsInsideOnError"); probe(TIMEOUT_MILLIS, POLL_DELAY_MILLIS, () -> assertState(false, true, true)); assertThat(HeisenbergSource.terminateStatus, is(ERROR_BODY)); Optional<Error> optionalError = HeisenbergSource.error; assertThat(optionalError, is(not(empty()))); assertThat(optionalError.get().getErrorType(), is(errorType(SOURCE_ERROR_RESPONSE_SEND))); }
@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 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 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 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 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))); }