Search in sources :

Example 1 with EventContext

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

the class ResequencerTestCase method testMessageResequencer.

@Test
public void testMessageResequencer() throws Exception {
    MuleSession session = new DefaultMuleSession();
    Flow flow = createAndRegisterFlow(muleContext, APPLE_FLOW, componentLocator);
    assertNotNull(flow);
    TestEventResequencer router = new TestEventResequencer(3);
    router.setMuleContext(muleContext);
    router.setAnnotations(getAppleFlowComponentLocationAnnotations());
    initialiseIfNeeded(router, true, muleContext);
    EventContext context = 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(context).message(message1).session(session).build();
    CoreEvent event2 = InternalEvent.builder(context).message(message2).session(session).build();
    CoreEvent event3 = InternalEvent.builder(context).message(message3).session(session).build();
    assertNull(router.process(event2));
    assertNull(router.process(event3));
    CoreEvent resultEvent = router.process(event1);
    assertNotNull(resultEvent);
    Message resultMessage = resultEvent.getMessage();
    assertNotNull(resultMessage);
    assertTrue(getPayloadAsString(resultMessage).equals("test event A") || getPayloadAsString(resultMessage).equals("test event B") || getPayloadAsString(resultMessage).equals("test event C"));
}
Also used : EventContext(org.mule.runtime.api.event.EventContext) Message(org.mule.runtime.api.message.Message) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) DefaultMuleSession(org.mule.runtime.core.privileged.event.DefaultMuleSession) MuleSession(org.mule.runtime.core.privileged.event.MuleSession) DefaultMuleSession(org.mule.runtime.core.privileged.event.DefaultMuleSession) MuleTestUtils.createAndRegisterFlow(org.mule.tck.MuleTestUtils.createAndRegisterFlow) Flow(org.mule.runtime.core.api.construct.Flow) MuleTestUtils.createFlow(org.mule.tck.MuleTestUtils.createFlow) Test(org.junit.Test)

Example 2 with EventContext

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

the class SimpleCollectionAggregatorTestCase method testAggregateMessageCollections.

@Test
public void testAggregateMessageCollections() throws Exception {
    Flow flow = createAndRegisterFlow(muleContext, APPLE_FLOW, componentLocator);
    assertNotNull(flow);
    SimpleCollectionAggregator router = new SimpleCollectionAggregator();
    router.setMuleContext(muleContext);
    router.setAnnotations(getAppleFlowComponentLocationAnnotations());
    initialiseIfNeeded(router, true, muleContext);
    EventContext executionContext = create(flow, TEST_CONNECTOR_LOCATION, "foo");
    Message message1 = of("test event A");
    Message message2 = of("test event B");
    Message message3 = of("test event C");
    Message message4 = of("test event D");
    List<Message> list = new ArrayList<>();
    List<Message> list2 = new ArrayList<>();
    list.add(message1);
    list.add(message2);
    list2.add(message3);
    list2.add(message4);
    Message messageCollection1 = Message.of(list);
    Message messageCollection2 = Message.of(list2);
    CoreEvent event1 = InternalEvent.builder(executionContext).message(messageCollection1).groupCorrelation(Optional.of(GroupCorrelation.of(0, 2))).build();
    CoreEvent event2 = InternalEvent.builder(executionContext).message(messageCollection2).groupCorrelation(Optional.of(GroupCorrelation.of(0, 2))).build();
    assertNull(router.process(event1));
    CoreEvent resultEvent = router.process(event2);
    assertNotNull(resultEvent);
    Message resultMessage = resultEvent.getMessage();
    assertNotNull(resultMessage);
    List<InternalMessage> payload = (List<InternalMessage>) resultMessage.getPayload().getValue();
    assertEquals(2, payload.size());
    assertEquals("test event A", ((List<InternalMessage>) payload.get(0).getPayload().getValue()).get(0).getPayload().getValue());
    assertEquals("test event B", ((List<InternalMessage>) payload.get(0).getPayload().getValue()).get(1).getPayload().getValue());
    assertEquals("test event C", ((List<InternalMessage>) payload.get(1).getPayload().getValue()).get(0).getPayload().getValue());
    assertEquals("test event D", ((List<InternalMessage>) payload.get(1).getPayload().getValue()).get(1).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) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) MuleTestUtils.createAndRegisterFlow(org.mule.tck.MuleTestUtils.createAndRegisterFlow) Flow(org.mule.runtime.core.api.construct.Flow) Test(org.junit.Test)

Example 3 with EventContext

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

the class AggregatorTestCase method testMessageAggregator.

@Test
public void testMessageAggregator() throws Exception {
    Flow flow = createAndRegisterFlow(muleContext, APPLE_FLOW, componentLocator);
    MuleSession session = new DefaultMuleSession();
    TestEventAggregator router = new TestEventAggregator(3);
    router.setAnnotations(getAppleFlowComponentLocationAnnotations());
    initialiseIfNeeded(router, true, muleContext);
    EventContext context = 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(context).message(message1).session(session).build();
    CoreEvent event2 = InternalEvent.builder(context).message(message2).session(session).build();
    CoreEvent event3 = InternalEvent.builder(context).message(message3).session(session).build();
    assertNull(router.process(event1));
    assertNull(router.process(event2));
    CoreEvent result = router.process(event3);
    assertNotNull(result);
    PrivilegedEvent privilegedResult = (PrivilegedEvent) result;
    assertTrue(privilegedResult.getMessageAsString(muleContext).contains("test event A"));
    assertTrue(privilegedResult.getMessageAsString(muleContext).contains("test event B"));
    assertTrue(privilegedResult.getMessageAsString(muleContext).contains("test event C"));
    assertTrue(privilegedResult.getMessageAsString(muleContext).matches("test event [A,B,C] test event [A,B,C] test event [A,B,C] "));
}
Also used : EventContext(org.mule.runtime.api.event.EventContext) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) Message(org.mule.runtime.api.message.Message) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) DefaultMuleSession(org.mule.runtime.core.privileged.event.DefaultMuleSession) MuleSession(org.mule.runtime.core.privileged.event.MuleSession) DefaultMuleSession(org.mule.runtime.core.privileged.event.DefaultMuleSession) MuleTestUtils.createAndRegisterFlow(org.mule.tck.MuleTestUtils.createAndRegisterFlow) Flow(org.mule.runtime.core.api.construct.Flow) Test(org.junit.Test)

Example 4 with EventContext

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

the class TestEventBuilder method build.

/**
 * Produces an event with the specified configuration.
 *
 * @param flow the recipient for the event to be built.
 * @return an event with the specified configuration.
 */
public CoreEvent build(FlowConstruct flow) {
    final Message.Builder messageBuilder;
    messageBuilder = Message.builder().value(payload).mediaType(mediaType);
    setInboundProperties(messageBuilder, inboundProperties);
    setOutboundProperties(messageBuilder, outboundProperties);
    if (attributes != null) {
        messageBuilder.attributesValue(attributes);
    }
    final Message muleMessage = messageBuilder.build();
    EventContext eventContext;
    if (externalCompletionCallback != null) {
        eventContext = create(flow, TEST_CONNECTOR_LOCATION, sourceCorrelationId, of(externalCompletionCallback));
    } else {
        eventContext = create(flow, TEST_CONNECTOR_LOCATION, sourceCorrelationId);
    }
    CoreEvent.Builder builder = InternalEvent.builder(eventContext).message(spyMessage.apply(muleMessage)).replyToHandler(replyToHandler).itemSequenceInfo(ofNullable(itemSequenceInfo));
    for (Entry<String, TypedValue> variableEntry : variables.entrySet()) {
        builder.addVariable(variableEntry.getKey(), variableEntry.getValue().getValue(), variableEntry.getValue().getDataType());
    }
    CoreEvent event = builder.build();
    for (Entry<String, Attachment> outboundAttachmentEntry : outboundAttachments.entrySet()) {
        event = outboundAttachmentEntry.getValue().addOutboundTo(event, outboundAttachmentEntry.getKey());
    }
    for (Entry<String, Object> sessionPropertyEntry : sessionProperties.entrySet()) {
        ((PrivilegedEvent) event).getSession().setProperty(sessionPropertyEntry.getKey(), sessionPropertyEntry.getValue());
    }
    return spyEvent.apply(event);
}
Also used : EventContext(org.mule.runtime.api.event.EventContext) Message(org.mule.runtime.api.message.Message) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Example 5 with EventContext

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

the class MessageProcessingFlowTraceManager method onMessageProcessorNotificationPreInvoke.

/**
 * Callback method for when a message processor is about to be invoked.
 * <p/>
 * Updates the internal state of the event's {@link ProcessorsTrace} and {@link FlowCallStack} accordingly.
 *
 * @see DefaultProcessorsTrace#addExecutedProcessors(String)
 * @see DefaultFlowCallStack#setCurrentProcessorPath(String)
 *
 * @param notification the notification that contains the event and the processor that is about to be invoked.
 */
public void onMessageProcessorNotificationPreInvoke(MessageProcessorNotification notification) {
    String resolveProcessorRepresentation = resolveProcessorRepresentation(muleContext.getConfiguration().getId(), notification.getComponent().getLocation() != null ? notification.getComponent().getLocation().getLocation() : null, notification.getComponent());
    EventContext eventContext = notification.getEventContext();
    if (eventContext != null) {
        ((DefaultProcessorsTrace) ((BaseEventContext) eventContext).getProcessorsTrace()).addExecutedProcessors(resolveProcessorRepresentation);
    }
    FlowCallStack flowCallStack = ((CoreEvent) notification.getEvent()).getFlowCallStack();
    if (flowCallStack != null) {
        ((DefaultFlowCallStack) flowCallStack).setCurrentProcessorPath(resolveProcessorRepresentation);
    }
}
Also used : EventContext(org.mule.runtime.api.event.EventContext) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) FlowCallStack(org.mule.runtime.core.api.context.notification.FlowCallStack)

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