use of org.mule.runtime.core.internal.policy.SourcePolicy 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);
}
}
use of org.mule.runtime.core.internal.policy.SourcePolicy 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);
}
Aggregations