Search in sources :

Example 11 with EventContext

use of org.mule.runtime.api.event.EventContext in project mule by mulesoft.

the class SimpleCollectionAggregatorTestCase method testAggregateMultipleEvents.

@Test
public void testAggregateMultipleEvents() throws Exception {
    Flow flow = createAndRegisterFlow(muleContext, APPLE_FLOW, componentLocator);
    assertNotNull(flow);
    SimpleCollectionAggregator router = new SimpleCollectionAggregator();
    SensingNullMessageProcessor sensingMessageProcessor = getSensingNullMessageProcessor();
    router.setListener(sensingMessageProcessor);
    router.setAnnotations(getAppleFlowComponentLocationAnnotations());
    initialiseIfNeeded(router, true, muleContext);
    EventContext executionContext = create(flow, TEST_CONNECTOR_LOCATION, "foo");
    Message message1 = Message.of("test event A");
    Message message2 = Message.of("test event B");
    Message message3 = Message.of("test event C");
    CoreEvent event1 = InternalEvent.builder(executionContext).message(message1).groupCorrelation(Optional.of(GroupCorrelation.of(0, 3))).build();
    CoreEvent event2 = InternalEvent.builder(executionContext).message(message2).build();
    CoreEvent event3 = InternalEvent.builder(executionContext).message(message3).build();
    assertNull(router.process(event1));
    assertNull(router.process(event2));
    CoreEvent resultEvent = router.process(event3);
    assertNotNull(sensingMessageProcessor.event);
    assertThat(resultEvent, equalTo(sensingMessageProcessor.event));
    Message nextMessage = sensingMessageProcessor.event.getMessage();
    assertNotNull(nextMessage);
    assertTrue(nextMessage.getPayload().getValue() instanceof List<?>);
    List<InternalMessage> list = (List<InternalMessage>) nextMessage.getPayload().getValue();
    assertEquals(3, list.size());
    String[] results = new String[3];
    list.stream().map(msg -> msg.getPayload().getValue()).collect(toList()).toArray(results);
    // Need to sort result because of MULE-5998
    Arrays.sort(results);
    assertEquals("test event A", results[0]);
    assertEquals("test event B", results[1]);
    assertEquals("test event C", results[2]);
}
Also used : EventContext(org.mule.runtime.api.event.EventContext) InternalMessage(org.mule.runtime.core.internal.message.InternalMessage) Message(org.mule.runtime.api.message.Message) InternalMessage(org.mule.runtime.core.internal.message.InternalMessage) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) SensingNullMessageProcessor(org.mule.tck.SensingNullMessageProcessor) MuleTestUtils.createAndRegisterFlow(org.mule.tck.MuleTestUtils.createAndRegisterFlow) Flow(org.mule.runtime.core.api.construct.Flow) Test(org.junit.Test)

Example 12 with EventContext

use of org.mule.runtime.api.event.EventContext in project mule by mulesoft.

the class SimpleCollectionAggregatorTestCase method testAggregateSingleEvent.

@Test
public void testAggregateSingleEvent() throws Exception {
    Flow flow = createAndRegisterFlow(muleContext, APPLE_FLOW, componentLocator);
    assertNotNull(flow);
    SimpleCollectionAggregator router = new SimpleCollectionAggregator();
    SensingNullMessageProcessor sensingMessageProcessor = getSensingNullMessageProcessor();
    router.setListener(sensingMessageProcessor);
    router.setMuleContext(muleContext);
    router.setAnnotations(getAppleFlowComponentLocationAnnotations());
    initialiseIfNeeded(router, true, muleContext);
    EventContext executionContext = create(flow, TEST_CONNECTOR_LOCATION, "foo");
    Message message1 = of("test event A");
    CoreEvent event1 = InternalEvent.builder(executionContext).message(message1).groupCorrelation(Optional.of(GroupCorrelation.of(0, 1))).build();
    CoreEvent resultEvent = router.process(event1);
    assertNotNull(sensingMessageProcessor.event);
    assertThat(resultEvent, equalTo(sensingMessageProcessor.event));
    Message nextMessage = sensingMessageProcessor.event.getMessage();
    assertNotNull(nextMessage);
    assertTrue(nextMessage.getPayload().getValue() instanceof List<?>);
    List<InternalMessage> payload = (List<InternalMessage>) nextMessage.getPayload().getValue();
    assertEquals(1, payload.size());
    assertEquals("test event A", payload.get(0).getPayload().getValue());
}
Also used : EventContext(org.mule.runtime.api.event.EventContext) InternalMessage(org.mule.runtime.core.internal.message.InternalMessage) Message(org.mule.runtime.api.message.Message) InternalMessage(org.mule.runtime.core.internal.message.InternalMessage) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) SensingNullMessageProcessor(org.mule.tck.SensingNullMessageProcessor) MuleTestUtils.createAndRegisterFlow(org.mule.tck.MuleTestUtils.createAndRegisterFlow) Flow(org.mule.runtime.core.api.construct.Flow) Test(org.junit.Test)

Aggregations

EventContext (org.mule.runtime.api.event.EventContext)12 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)12 Message (org.mule.runtime.api.message.Message)11 Flow (org.mule.runtime.core.api.construct.Flow)9 Test (org.junit.Test)7 MuleTestUtils.createAndRegisterFlow (org.mule.tck.MuleTestUtils.createAndRegisterFlow)7 List (java.util.List)4 BaseEventContext (org.mule.runtime.core.privileged.event.BaseEventContext)4 DefaultMuleSession (org.mule.runtime.core.privileged.event.DefaultMuleSession)4 MuleSession (org.mule.runtime.core.privileged.event.MuleSession)4 ArrayList (java.util.ArrayList)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Collectors.toList (java.util.stream.Collectors.toList)3 MuleException (org.mule.runtime.api.exception.MuleException)3 InternalMessage (org.mule.runtime.core.internal.message.InternalMessage)3 Optional.of (java.util.Optional.of)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 NANOSECONDS (java.util.concurrent.TimeUnit.NANOSECONDS)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 SchedulerService (org.mule.runtime.api.scheduler.SchedulerService)2