use of org.mule.runtime.core.api.construct.Flow in project mule by mulesoft.
the class MuleTestUtils method createAndRegisterFlow.
/**
* Creates a new flow and registers it in the given {@code mockComponentLocator}
*
* @param mockComponentLocator a {@link Mockito#mock(Class)} {@link ConfigurationComponentLocator}
*/
public static Flow createAndRegisterFlow(MuleContext context, String flowName, ConfigurationComponentLocator mockComponentLocator) throws MuleException {
Flow flow = createFlow(context, flowName);
MuleRegistry registry = ((MuleContextWithRegistries) context).getRegistry();
if (registry != null) {
registry.registerFlowConstruct(flow);
}
when(mockComponentLocator.find(Location.builder().globalName(flowName).build())).thenReturn(Optional.of(flow));
return flow;
}
use of org.mule.runtime.core.api.construct.Flow in project mule by mulesoft.
the class DefaultFlowTestCase method lifecycleOrder.
@Test
public void lifecycleOrder() throws MuleException {
Sink sink = mock(Sink.class, withSettings().extraInterfaces(Disposable.class));
Processor processor = mock(Processor.class, withSettings().extraInterfaces(Startable.class, Stoppable.class));
ProcessingStrategy processingStrategy = mock(ProcessingStrategy.class, withSettings().extraInterfaces(Startable.class, Stoppable.class));
when(processingStrategy.createSink(any(FlowConstruct.class), any(ReactiveProcessor.class))).thenReturn(sink);
flow = (DefaultFlow) Flow.builder(FLOW_NAME, muleContext).source(directInboundMessageSource).processors(singletonList(processor)).processingStrategyFactory((muleContext, s) -> processingStrategy).build();
flow.initialise();
flow.start();
InOrder inOrder = inOrder(sink, processor, processingStrategy);
inOrder.verify((Startable) processingStrategy).start();
inOrder.verify(processingStrategy).createSink(any(FlowConstruct.class), any(ReactiveProcessor.class));
inOrder.verify((Startable) processor).start();
flow.stop();
inOrder.verify((Disposable) sink).dispose();
inOrder.verify((Stoppable) processor).stop();
inOrder.verify((Stoppable) processingStrategy).stop();
}
use of org.mule.runtime.core.api.construct.Flow in project mule by mulesoft.
the class LookupFunction method call.
@Override
public Object call(Object[] parameters, BindingContext context) {
String flowName = (String) parameters[0];
Object payload = parameters[1];
Location componentLocation = Location.builder().globalName(flowName).build();
Component component = componentLocator.find(componentLocation).orElseThrow(() -> new IllegalArgumentException(format("There is no component named '%s'.", flowName)));
if (component instanceof Flow) {
try {
Message incomingMessage = lookupValue(context, MESSAGE, Message.builder().nullValue().build());
Map<String, ?> incomingVariables = lookupValue(context, VARS, EMPTY_MAP);
Error incomingError = lookupValue(context, ERROR, null);
Message message = Message.builder(incomingMessage).value(payload).mediaType(APPLICATION_JAVA).build();
CoreEvent event = CoreEvent.builder(PrivilegedEvent.getCurrentEvent().getContext()).variables(incomingVariables).error(incomingError).message(message).build();
return ((ExecutableComponent) component).execute(event).get().getMessage().getPayload();
} catch (ExecutionException e) {
ComponentExecutionException componentExecutionException = (ComponentExecutionException) e.getCause();
Error error = componentExecutionException.getEvent().getError().get();
throw new MuleRuntimeException(createStaticMessage(format("Flow '%s' has failed with error '%s' (%s)", flowName, error.getErrorType(), error.getDescription())), error.getCause());
} catch (InterruptedException e) {
throw new MuleRuntimeException(e);
}
} else {
throw new IllegalArgumentException(format("Component '%s' is not a flow.", flowName));
}
}
use of org.mule.runtime.core.api.construct.Flow in project mule by mulesoft.
the class EventContextBenchmark method createEventContextWithFlowAndCompleteWithExternalCompletion.
@Benchmark
public Object[] createEventContextWithFlowAndCompleteWithExternalCompletion() {
CompletableFuture<Void> completableFuture = new CompletableFuture<>();
AtomicReference<CoreEvent> result = new AtomicReference();
AtomicBoolean complete = new AtomicBoolean();
BaseEventContext eventContext = (BaseEventContext) create(flow, CONNECTOR_LOCATION, null, of(completableFuture));
from(from(eventContext.getResponsePublisher())).doOnSuccess(response -> result.set(response)).subscribe();
eventContext.onTerminated((response, throwable) -> complete.set(true));
eventContext.success(event);
completableFuture.complete(null);
return new Object[] { result, complete };
}
use of org.mule.runtime.core.api.construct.Flow in project mule by mulesoft.
the class EventContextBenchmark method createEventContextWithFlowAndComplete.
@Benchmark
public Object[] createEventContextWithFlowAndComplete() {
AtomicReference<CoreEvent> result = new AtomicReference();
AtomicBoolean complete = new AtomicBoolean();
BaseEventContext eventContext = (BaseEventContext) create(flow, CONNECTOR_LOCATION);
from(from(eventContext.getResponsePublisher())).doOnSuccess(response -> result.set(response)).subscribe();
eventContext.onTerminated((response, throwable) -> complete.set(true));
eventContext.success(event);
return new Object[] { result, complete };
}
Aggregations