use of org.mule.runtime.api.interception.InterceptionEvent 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));
}
}
}
use of org.mule.runtime.api.interception.InterceptionEvent 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));
}
}
}
use of org.mule.runtime.api.interception.InterceptionEvent 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));
}
}
}
use of org.mule.runtime.api.interception.InterceptionEvent 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));
}
}
use of org.mule.runtime.api.interception.InterceptionEvent in project mule by mulesoft.
the class ReactiveAroundInterceptorAdapter method doAround.
private CompletableFuture<InternalEvent> doAround(InternalEvent event, ProcessorInterceptor interceptor, Processor component, Map<String, String> dslParameters, ReactiveProcessor next) {
final InternalEvent eventWithResolvedParams = addResolvedParameters(event, component, dslParameters);
DefaultInterceptionEvent interceptionEvent = new DefaultInterceptionEvent(eventWithResolvedParams);
final ReactiveInterceptionAction reactiveInterceptionAction = new ReactiveInterceptionAction(interceptionEvent, next, component, ((PrivilegedMuleContext) getMuleContext()).getErrorTypeLocator());
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Calling around() for '{}' in processor '{}'...", interceptor, ((Component) component).getLocation().getLocation());
}
try {
return withContextClassLoader(interceptor.getClass().getClassLoader(), () -> interceptor.around(((Component) component).getLocation(), getResolvedParams(eventWithResolvedParams), interceptionEvent, reactiveInterceptionAction)).exceptionally(t -> {
if (t instanceof MessagingException) {
throw new CompletionException(t);
} else {
throw new CompletionException(createMessagingException(eventWithResolvedParams, t instanceof CompletionException ? t.getCause() : t, ((Component) component)));
}
}).thenApply(interceptedEvent -> interceptedEvent != null ? ((DefaultInterceptionEvent) interceptedEvent).resolve() : null);
} catch (Exception e) {
throw propagate(createMessagingException(interceptionEvent.resolve(), e, (Component) component));
}
}
Aggregations