Search in sources :

Example 16 with Flow

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;
}
Also used : MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) MuleRegistry(org.mule.runtime.core.internal.registry.MuleRegistry) Flow(org.mule.runtime.core.api.construct.Flow)

Example 17 with 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();
}
Also used : Disposable(org.mule.runtime.api.lifecycle.Disposable) INITIAL_STATE_STOPPED(org.mule.runtime.core.api.construct.Flow.INITIAL_STATE_STOPPED) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) BiFunction(java.util.function.BiFunction) DefaultFlow(org.mule.runtime.core.internal.construct.DefaultFlowBuilder.DefaultFlow) Thread.currentThread(java.lang.Thread.currentThread) Collections.singletonList(java.util.Collections.singletonList) Assert.assertThat(org.junit.Assert.assertThat) Mockito.doThrow(org.mockito.Mockito.doThrow) Arrays.asList(java.util.Arrays.asList) Matchers.eq(org.mockito.Matchers.eq) After(org.junit.After) Mono.just(reactor.core.publisher.Mono.just) SensingNullMessageProcessor(org.mule.tck.SensingNullMessageProcessor) Assert.fail(org.junit.Assert.fail) Parameterized(org.junit.runners.Parameterized) Startable(org.mule.runtime.api.lifecycle.Startable) I18nMessage(org.mule.runtime.api.i18n.I18nMessage) StringAppendTransformer(org.mule.runtime.core.internal.transformer.simple.StringAppendTransformer) Collection(java.util.Collection) ReactiveProcessor(org.mule.runtime.core.api.processor.ReactiveProcessor) Message.of(org.mule.runtime.api.message.Message.of) Flow(org.mule.runtime.core.api.construct.Flow) ProcessingStrategy(org.mule.runtime.core.api.processor.strategy.ProcessingStrategy) ResponseMessageProcessorAdapter(org.mule.runtime.core.internal.processor.ResponseMessageProcessorAdapter) Matchers.any(org.mockito.Matchers.any) List(java.util.List) Mockito.inOrder(org.mockito.Mockito.inOrder) Mockito.withSettings(org.mockito.Mockito.withSettings) FlowConstruct(org.mule.runtime.core.api.construct.FlowConstruct) Mockito.mock(org.mockito.Mockito.mock) LifecycleTrackerProcessor(org.mule.tck.core.lifecycle.LifecycleTrackerProcessor) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) CoreMatchers.not(org.hamcrest.CoreMatchers.not) MessageSource(org.mule.runtime.core.api.source.MessageSource) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) Mockito.spy(org.mockito.Mockito.spy) Processor(org.mule.runtime.core.api.processor.Processor) BLOCKING(org.mule.runtime.core.api.processor.ReactiveProcessor.ProcessingType.BLOCKING) ArrayList(java.util.ArrayList) DEFAULT_MAX_CONCURRENCY(org.mule.runtime.core.api.processor.strategy.AsyncProcessingStrategyFactory.DEFAULT_MAX_CONCURRENCY) MuleException(org.mule.runtime.api.exception.MuleException) ExpectedException.none(org.junit.rules.ExpectedException.none) MuleContextUtils.eventBuilder(org.mule.tck.util.MuleContextUtils.eventBuilder) ExpectedException(org.junit.rules.ExpectedException) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) CoreMatchers.sameInstance(org.hamcrest.CoreMatchers.sameInstance) Disposable(org.mule.runtime.api.lifecycle.Disposable) LifecycleException(org.mule.runtime.api.lifecycle.LifecycleException) BlockingProcessingStrategyFactory(org.mule.runtime.core.internal.processor.strategy.BlockingProcessingStrategyFactory) InOrder(org.mockito.InOrder) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Sink(org.mule.runtime.core.api.processor.Sink) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Rule(org.junit.Rule) Stoppable(org.mule.runtime.api.lifecycle.Stoppable) Startable(org.mule.runtime.api.lifecycle.Startable) SensingNullMessageProcessor(org.mule.tck.SensingNullMessageProcessor) ReactiveProcessor(org.mule.runtime.core.api.processor.ReactiveProcessor) LifecycleTrackerProcessor(org.mule.tck.core.lifecycle.LifecycleTrackerProcessor) Processor(org.mule.runtime.core.api.processor.Processor) InOrder(org.mockito.InOrder) Sink(org.mule.runtime.core.api.processor.Sink) ReactiveProcessor(org.mule.runtime.core.api.processor.ReactiveProcessor) FlowConstruct(org.mule.runtime.core.api.construct.FlowConstruct) Stoppable(org.mule.runtime.api.lifecycle.Stoppable) ProcessingStrategy(org.mule.runtime.core.api.processor.strategy.ProcessingStrategy) Test(org.junit.Test)

Example 18 with Flow

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));
    }
}
Also used : Message(org.mule.runtime.api.message.Message) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) Error(org.mule.runtime.api.message.Error) Flow(org.mule.runtime.core.api.construct.Flow) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) ComponentExecutionException(org.mule.runtime.api.component.execution.ComponentExecutionException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) Component(org.mule.runtime.api.component.Component) ExecutableComponent(org.mule.runtime.api.component.execution.ExecutableComponent) ExecutionException(java.util.concurrent.ExecutionException) ComponentExecutionException(org.mule.runtime.api.component.execution.ComponentExecutionException) Location(org.mule.runtime.api.component.location.Location)

Example 19 with Flow

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 };
}
Also used : Measurement(org.openjdk.jmh.annotations.Measurement) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) Message(org.mule.runtime.api.message.Message) Optional.of(java.util.Optional.of) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Warmup(org.openjdk.jmh.annotations.Warmup) MuleContext(org.mule.runtime.core.api.MuleContext) MuleException(org.mule.runtime.api.exception.MuleException) OutputTimeUnit(org.openjdk.jmh.annotations.OutputTimeUnit) NullExceptionHandler.getInstance(org.mule.runtime.core.api.exception.NullExceptionHandler.getInstance) Mono.from(reactor.core.publisher.Mono.from) LifecycleUtils.stopIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.stopIfNeeded) TearDown(org.openjdk.jmh.annotations.TearDown) EventContext(org.mule.runtime.api.event.EventContext) Setup(org.openjdk.jmh.annotations.Setup) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) EventContextFactory.create(org.mule.runtime.core.api.event.EventContextFactory.create) UUID(org.mule.runtime.core.api.util.UUID) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) LegacyRegistryUtils.lookupObject(org.mule.runtime.core.privileged.registry.LegacyRegistryUtils.lookupObject) SchedulerService(org.mule.runtime.api.scheduler.SchedulerService) Flow(org.mule.runtime.core.api.construct.Flow) Benchmark(org.openjdk.jmh.annotations.Benchmark) LegacyRegistryUtils.registerObject(org.mule.runtime.core.privileged.registry.LegacyRegistryUtils.registerObject) FlowConstruct(org.mule.runtime.core.api.construct.FlowConstruct) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) CompletableFuture(java.util.concurrent.CompletableFuture) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) LegacyRegistryUtils.lookupObject(org.mule.runtime.core.privileged.registry.LegacyRegistryUtils.lookupObject) LegacyRegistryUtils.registerObject(org.mule.runtime.core.privileged.registry.LegacyRegistryUtils.registerObject) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Example 20 with Flow

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 };
}
Also used : Measurement(org.openjdk.jmh.annotations.Measurement) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) Message(org.mule.runtime.api.message.Message) Optional.of(java.util.Optional.of) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Warmup(org.openjdk.jmh.annotations.Warmup) MuleContext(org.mule.runtime.core.api.MuleContext) MuleException(org.mule.runtime.api.exception.MuleException) OutputTimeUnit(org.openjdk.jmh.annotations.OutputTimeUnit) NullExceptionHandler.getInstance(org.mule.runtime.core.api.exception.NullExceptionHandler.getInstance) Mono.from(reactor.core.publisher.Mono.from) LifecycleUtils.stopIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.stopIfNeeded) TearDown(org.openjdk.jmh.annotations.TearDown) EventContext(org.mule.runtime.api.event.EventContext) Setup(org.openjdk.jmh.annotations.Setup) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) EventContextFactory.create(org.mule.runtime.core.api.event.EventContextFactory.create) UUID(org.mule.runtime.core.api.util.UUID) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) LegacyRegistryUtils.lookupObject(org.mule.runtime.core.privileged.registry.LegacyRegistryUtils.lookupObject) SchedulerService(org.mule.runtime.api.scheduler.SchedulerService) Flow(org.mule.runtime.core.api.construct.Flow) Benchmark(org.openjdk.jmh.annotations.Benchmark) LegacyRegistryUtils.registerObject(org.mule.runtime.core.privileged.registry.LegacyRegistryUtils.registerObject) FlowConstruct(org.mule.runtime.core.api.construct.FlowConstruct) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) LegacyRegistryUtils.lookupObject(org.mule.runtime.core.privileged.registry.LegacyRegistryUtils.lookupObject) LegacyRegistryUtils.registerObject(org.mule.runtime.core.privileged.registry.LegacyRegistryUtils.registerObject) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Aggregations

Flow (org.mule.runtime.core.api.construct.Flow)37 Test (org.junit.Test)25 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)19 Message (org.mule.runtime.api.message.Message)14 MuleException (org.mule.runtime.api.exception.MuleException)10 Processor (org.mule.runtime.core.api.processor.Processor)10 EventContext (org.mule.runtime.api.event.EventContext)9 ArrayList (java.util.ArrayList)8 Arrays.asList (java.util.Arrays.asList)8 List (java.util.List)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 Collectors.toList (java.util.stream.Collectors.toList)8 Assert.assertThat (org.junit.Assert.assertThat)8 Thread.currentThread (java.lang.Thread.currentThread)7 Collection (java.util.Collection)7 After (org.junit.After)7 Before (org.junit.Before)7 Rule (org.junit.Rule)7 ExpectedException (org.junit.rules.ExpectedException)7 RunWith (org.junit.runner.RunWith)7