Search in sources :

Example 1 with RoutingPair

use of org.mule.runtime.core.internal.routing.ForkJoinStrategy.RoutingPair in project mule by mulesoft.

the class AbstractForkJoinStrategyTestCase method flowVarsMerged.

@Test
@Description("After successful completion of all routes the variables from each route are merged into the result.")
public void flowVarsMerged() throws Throwable {
    final String beforeVarName = "before";
    final String beforeVarValue = "beforeValue";
    final String beforeVar2Name = "before2";
    final String beforeVar2Value = "before2Value";
    final String beforeVar2NewValue = "before2NewValue";
    final String fooVarName = "foo";
    final String fooVarValue = "fooValue1";
    final String fooVar2Name = "foo2";
    final String fooVar2Value1 = "foo2Value1";
    final String fooVar2Value2 = "foo2Value2";
    final String fooVar3Name = "foo3";
    final String fooVar3Value1 = "foo3Value1";
    final Apple fooVar3Value2 = new Apple();
    CoreEvent original = builder(this.<CoreEvent>newEvent()).addVariable(beforeVarName, beforeVarValue).addVariable(beforeVar2Name, beforeVar2Value).build();
    RoutingPair pair1 = of(original, createChain(event -> builder(event).addVariable(beforeVar2Name, beforeVar2NewValue).addVariable(fooVarName, fooVarValue).addVariable(fooVar2Name, fooVar2Value1).addVariable(fooVar3Name, fooVar3Value1).build()));
    RoutingPair pair2 = of(original, createChain(event -> builder(event).addVariable(fooVar2Name, fooVar2Value2).addVariable(fooVar3Name, fooVar3Value2).build()));
    CoreEvent result = invokeStrategyBlocking(strategy, original, asList(pair1, pair2));
    assertThat(result.getVariables().keySet(), hasSize(5));
    assertThat(result.getVariables().keySet(), hasItems(beforeVarName, beforeVar2Name, fooVarName, fooVarName, fooVar2Name, fooVar3Name));
    assertThat(result.getVariables().get(beforeVarName).getValue(), equalTo(beforeVarValue));
    assertThat(result.getVariables().get(beforeVar2Name).getValue(), equalTo(beforeVar2NewValue));
    assertThat(result.getVariables().get(fooVarName).getValue(), equalTo(fooVarValue));
    TypedValue fooVar2 = result.getVariables().get(fooVar2Name);
    assertThat(fooVar2.getDataType(), equalTo(DataType.builder().collectionType(List.class).itemType(String.class).build()));
    assertThat(((List<String>) fooVar2.getValue()), hasItems(fooVar2Value1, fooVar2Value2));
    TypedValue fooVar3 = result.getVariables().get(fooVar3Name);
    assertThat(fooVar3.getDataType(), equalTo(DataType.builder().collectionType(List.class).itemType(Object.class).build()));
    assertThat(((List<Object>) fooVar3.getValue()), hasItems(fooVar3Value1, fooVar3Value2));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) MessageProcessors.newChain(org.mule.runtime.core.privileged.processor.MessageProcessors.newChain) Schedulers.fromExecutorService(reactor.core.scheduler.Schedulers.fromExecutorService) Apple(org.mule.tck.testmodels.fruit.Apple) IntStream.range(java.util.stream.IntStream.range) Message(org.mule.runtime.api.message.Message) MAX_VALUE(java.lang.Integer.MAX_VALUE) TimeoutException(java.util.concurrent.TimeoutException) Matchers.hasItems(org.hamcrest.Matchers.hasItems) CoreEvent.builder(org.mule.runtime.core.api.event.CoreEvent.builder) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) RoutingPair(org.mule.runtime.core.internal.routing.ForkJoinStrategy.RoutingPair) Scheduler(org.mule.runtime.api.scheduler.Scheduler) Arrays.asList(java.util.Arrays.asList) After(org.junit.After) Thread.sleep(java.lang.Thread.sleep) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) Flux.fromIterable(reactor.core.publisher.Flux.fromIterable) ForkJoinStrategy(org.mule.runtime.core.internal.routing.ForkJoinStrategy) ReactiveProcessor(org.mule.runtime.core.api.processor.ReactiveProcessor) Message.of(org.mule.runtime.api.message.Message.of) ProcessingStrategy(org.mule.runtime.core.api.processor.strategy.ProcessingStrategy) MessageProcessorChain(org.mule.runtime.core.privileged.processor.chain.MessageProcessorChain) Matchers.any(org.mockito.Matchers.any) List(java.util.List) AbstractMuleContextTestCase(org.mule.tck.junit4.AbstractMuleContextTestCase) Mockito.atMost(org.mockito.Mockito.atMost) ErrorType(org.mule.runtime.api.message.ErrorType) Optional(java.util.Optional) RoutingPair.of(org.mule.runtime.core.internal.routing.ForkJoinStrategy.RoutingPair.of) Mockito.mock(org.mockito.Mockito.mock) CompositeRoutingException(org.mule.runtime.core.privileged.routing.CompositeRoutingException) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) InternalProcessor(org.mule.runtime.core.privileged.processor.InternalProcessor) Callable(java.util.concurrent.Callable) Mockito.spy(org.mockito.Mockito.spy) Processor(org.mule.runtime.core.api.processor.Processor) Function(java.util.function.Function) TIMEOUT(org.mule.runtime.core.api.exception.Errors.ComponentIdentifiers.Handleable.TIMEOUT) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Feature(io.qameta.allure.Feature) MuleException(org.mule.runtime.api.exception.MuleException) RoutingResult(org.mule.runtime.core.privileged.routing.RoutingResult) Mono.from(reactor.core.publisher.Mono.from) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ExpectedException(org.junit.rules.ExpectedException) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) Before(org.junit.Before) CheckedConsumer(org.mule.runtime.core.api.util.func.CheckedConsumer) DataType(org.mule.runtime.api.metadata.DataType) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Mockito.times(org.mockito.Mockito.times) FORK_JOIN_STRATEGIES(org.mule.test.allure.AllureConstants.ForkJoinStrategiesFeature.FORK_JOIN_STRATEGIES) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) AbstractComponent(org.mule.runtime.api.component.AbstractComponent) TypedValue(org.mule.runtime.api.metadata.TypedValue) Collectors.toList(java.util.stream.Collectors.toList) Mockito.never(org.mockito.Mockito.never) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) Rule(org.junit.Rule) Exceptions.rxExceptionToMuleException(org.mule.runtime.core.api.rx.Exceptions.rxExceptionToMuleException) Description(io.qameta.allure.Description) Apple(org.mule.tck.testmodels.fruit.Apple) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) RoutingPair(org.mule.runtime.core.internal.routing.ForkJoinStrategy.RoutingPair) TypedValue(org.mule.runtime.api.metadata.TypedValue) Description(io.qameta.allure.Description) Test(org.junit.Test)

Example 2 with RoutingPair

use of org.mule.runtime.core.internal.routing.ForkJoinStrategy.RoutingPair in project mule by mulesoft.

the class AbstractForkJoinStrategyTestCase method errorDelayed.

@Test
@Description("When an error occurs all routes are executed regardless and a CompositeRoutingException is thrown containing a RoutingResult with details of both failures and successes.")
public void errorDelayed() throws Throwable {
    Processor processorSpy = createProcessorSpy(testEvent().getMessage());
    RuntimeException exception1 = new IllegalStateException();
    RoutingPair failingPair1 = of(testEvent(), createFailingRoutingPair(exception1));
    RuntimeException exception2 = new UnsupportedOperationException();
    RoutingPair failingPair2 = of(testEvent(), createFailingRoutingPair(exception2));
    RuntimeException exception3 = new IndexOutOfBoundsException();
    RoutingPair failingPair3 = of(testEvent(), createFailingRoutingPair(exception3));
    RoutingPair okPair = of(testEvent(), createChain(processorSpy));
    expectedException.expect(instanceOf(CompositeRoutingException.class));
    invokeStrategyBlocking(strategy, testEvent(), asList(failingPair1, failingPair2, failingPair3, okPair), throwable -> {
        verify(processorSpy, times(1)).process(any(CoreEvent.class));
        CompositeRoutingException compositeRoutingException = assertCompositeRoutingException(throwable, 3);
        RoutingResult routingResult = assertRoutingResult(compositeRoutingException, 1, 3);
        assertThat(routingResult.getFailures().get("0").getCause(), is(exception1));
        assertThat(routingResult.getFailures().get("1").getCause(), is(exception2));
        assertThat(routingResult.getFailures().get("2").getCause(), is(exception3));
        assertThat(routingResult.getFailures().get("3"), is(nullValue()));
    });
}
Also used : CompositeRoutingException(org.mule.runtime.core.privileged.routing.CompositeRoutingException) RoutingResult(org.mule.runtime.core.privileged.routing.RoutingResult) ReactiveProcessor(org.mule.runtime.core.api.processor.ReactiveProcessor) InternalProcessor(org.mule.runtime.core.privileged.processor.InternalProcessor) Processor(org.mule.runtime.core.api.processor.Processor) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) RoutingPair(org.mule.runtime.core.internal.routing.ForkJoinStrategy.RoutingPair) Description(io.qameta.allure.Description) Test(org.junit.Test)

Example 3 with RoutingPair

use of org.mule.runtime.core.internal.routing.ForkJoinStrategy.RoutingPair in project mule by mulesoft.

the class AbstractForkJoinStrategyTestCase method error.

@Test
@Description("Errors are thrown via CompositeRoutingException with RoutingResult containing details of failures.")
public void error() throws Throwable {
    RuntimeException exception = new IllegalStateException();
    RoutingPair failingPair = of(testEvent(), createFailingRoutingPair(exception));
    expectedException.expect(instanceOf(CompositeRoutingException.class));
    invokeStrategyBlocking(strategy, testEvent(), asList(failingPair), throwable -> {
        CompositeRoutingException compositeRoutingException = assertCompositeRoutingException(throwable, 1);
        RoutingResult routingResult = assertRoutingResult(compositeRoutingException, 0, 1);
        assertThat(routingResult.getFailures().get("0").getCause(), is(exception));
    });
}
Also used : CompositeRoutingException(org.mule.runtime.core.privileged.routing.CompositeRoutingException) RoutingResult(org.mule.runtime.core.privileged.routing.RoutingResult) RoutingPair(org.mule.runtime.core.internal.routing.ForkJoinStrategy.RoutingPair) Description(io.qameta.allure.Description) Test(org.junit.Test)

Example 4 with RoutingPair

use of org.mule.runtime.core.internal.routing.ForkJoinStrategy.RoutingPair in project mule by mulesoft.

the class AbstractForkJoinStrategyTestCase method timeoutEager.

@Test
@Description("When configured with delayErrors='false' the first timeout causes strategy to throw a TimeoutException.")
public void timeoutEager() throws Throwable {
    strategy = createStrategy(processingStrategy, 1, false, 50);
    Message pair2Result = of(2);
    Processor pair2Processor = createProcessorSpy(pair2Result);
    RoutingPair pair2 = of(testEvent(), createChain(pair2Processor));
    expectedException.expect(instanceOf(DefaultMuleException.class));
    expectedException.expectCause(instanceOf(TimeoutException.class));
    invokeStrategyBlocking(strategy, testEvent(), asList(createRoutingPairWithSleep(of(1), 250), pair2), throwable -> verify(pair2Processor, never()).process(any(CoreEvent.class)));
}
Also used : DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) ReactiveProcessor(org.mule.runtime.core.api.processor.ReactiveProcessor) InternalProcessor(org.mule.runtime.core.privileged.processor.InternalProcessor) Processor(org.mule.runtime.core.api.processor.Processor) Message(org.mule.runtime.api.message.Message) RoutingPair(org.mule.runtime.core.internal.routing.ForkJoinStrategy.RoutingPair) TimeoutException(java.util.concurrent.TimeoutException) Description(io.qameta.allure.Description) Test(org.junit.Test)

Example 5 with RoutingPair

use of org.mule.runtime.core.internal.routing.ForkJoinStrategy.RoutingPair in project mule by mulesoft.

the class CollectListForkJoinStrategyTestCase method collectList.

@Test
@Description("This strategy waits for all routes to return and then collects results into a list.")
public void collectList() throws Throwable {
    CoreEvent original = testEvent();
    Message route1Result = of(1);
    Message route2Result = of(2);
    Message route3Result = of(3);
    RoutingPair pair1 = createRoutingPair(route1Result);
    RoutingPair pair2 = createRoutingPair(route2Result);
    RoutingPair pair3 = createRoutingPair(route3Result);
    CoreEvent result = invokeStrategyBlocking(strategy, original, asList(pair1, pair2, pair3));
    assertThat(result.getMessage().getPayload().getValue(), instanceOf(List.class));
    List<Message> resultList = (List<Message>) result.getMessage().getPayload().getValue();
    assertThat(resultList, hasSize(3));
    assertThat(resultList, hasItems(route1Result, route2Result, route3Result));
}
Also used : Message(org.mule.runtime.api.message.Message) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) List(java.util.List) Arrays.asList(java.util.Arrays.asList) RoutingPair(org.mule.runtime.core.internal.routing.ForkJoinStrategy.RoutingPair) Description(io.qameta.allure.Description) Test(org.junit.Test)

Aggregations

Description (io.qameta.allure.Description)12 Test (org.junit.Test)12 RoutingPair (org.mule.runtime.core.internal.routing.ForkJoinStrategy.RoutingPair)12 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)9 Processor (org.mule.runtime.core.api.processor.Processor)7 ReactiveProcessor (org.mule.runtime.core.api.processor.ReactiveProcessor)6 InternalProcessor (org.mule.runtime.core.privileged.processor.InternalProcessor)6 Message (org.mule.runtime.api.message.Message)5 CompositeRoutingException (org.mule.runtime.core.privileged.routing.CompositeRoutingException)4 RoutingResult (org.mule.runtime.core.privileged.routing.RoutingResult)4 List (java.util.List)3 TimeoutException (java.util.concurrent.TimeoutException)3 Arrays.asList (java.util.Arrays.asList)2 DefaultMuleException (org.mule.runtime.api.exception.DefaultMuleException)2 MessagingException (org.mule.runtime.core.internal.exception.MessagingException)2 MessageProcessorChain (org.mule.runtime.core.privileged.processor.chain.MessageProcessorChain)2 Feature (io.qameta.allure.Feature)1 MAX_VALUE (java.lang.Integer.MAX_VALUE)1 Thread.sleep (java.lang.Thread.sleep)1 ArrayList (java.util.ArrayList)1