Search in sources :

Example 71 with CoreEvent

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

the class DefaultRouterResultsHandlerTestCase method aggregateMultipleEventsAllButOneNull.

@Test
public void aggregateMultipleEventsAllButOneNull() {
    Message message1 = Message.of("test event A");
    Message message2 = Message.of("test event B");
    CoreEvent event1 = InternalEvent.builder(context).message(message1).addVariable("key", "value").build();
    CoreEvent event2 = InternalEvent.builder(context).message(message2).addVariable("key2", "value2").build();
    List<CoreEvent> events = new ArrayList<>();
    events.add(null);
    events.add(event2);
    CoreEvent result = resultsHandler.aggregateResults(events, event1);
    assertSame(event2, result);
    // Because same event instance is returned rather than MessageCollection
    // don't copy invocation properties
    assertThat(result.getVariables().keySet(), not(hasItem("key1")));
    assertThat(result.getVariables().get("key2").getValue(), equalTo("value2"));
}
Also used : 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) Test(org.junit.Test)

Example 72 with CoreEvent

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

the class DefaultRouterResultsHandlerTestCase method aggregateMultipleMuleMessageCollections.

@Test
public void aggregateMultipleMuleMessageCollections() {
    Message message1 = Message.of("test event A");
    CoreEvent event1 = InternalEvent.builder(context).message(message1).addVariable("key1", "value1").build();
    Message message2 = Message.of("test event B");
    Message message3 = Message.of("test event C");
    Message message4 = Message.of("test event D");
    Message message5 = Message.of("test event E");
    List<Message> list = new ArrayList<>();
    list.add(message2);
    list.add(message3);
    Message messageCollection = Message.of(list);
    CoreEvent event2 = InternalEvent.builder(context).message(messageCollection).addVariable("key2", "value2").build();
    List<InternalMessage> list2 = new ArrayList<>();
    list.add(message4);
    list.add(message5);
    Message messageCollection2 = Message.of(list2);
    CoreEvent event3 = InternalEvent.builder(context).message(messageCollection2).addVariable("key3", "value3").build();
    List<CoreEvent> events = new ArrayList<>();
    events.add(event2);
    events.add(event3);
    CoreEvent result = resultsHandler.aggregateResults(events, event1);
    assertNotNull(result);
    assertEquals(2, ((List<InternalMessage>) result.getMessage().getPayload().getValue()).size());
    assertTrue(result.getMessage().getPayload().getValue() instanceof List<?>);
    assertEquals(messageCollection, ((List<InternalMessage>) result.getMessage().getPayload().getValue()).get(0));
    assertEquals(messageCollection2, ((List<InternalMessage>) result.getMessage().getPayload().getValue()).get(1));
    // Because a new MuleMessageCollection is created, propagate properties from
    // original event
    assertThat(result.getVariables().get("key1").getValue(), equalTo("value1"));
    assertThat(result.getVariables().get("key2").getValue(), equalTo("value2"));
    assertThat(result.getVariables().get("key3").getValue(), equalTo("value3"));
    // Root id
    assertThat(result.getCorrelationId(), equalTo(event1.getCorrelationId()));
}
Also used : 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) Test(org.junit.Test)

Example 73 with CoreEvent

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

the class FirstSuccessfulTestCase method getPayload.

private String getPayload(Processor mp, MuleSession session, String message) throws Exception {
    Message msg = of(message);
    try {
        CoreEvent event = mp.process(this.<PrivilegedEvent.Builder>getEventBuilder().message(msg).session(session).build());
        Message returnedMessage = event.getMessage();
        if (event.getError().isPresent()) {
            return EXCEPTION_SEEN;
        } else {
            return getPayloadAsString(returnedMessage);
        }
    } catch (Exception ex) {
        return EXCEPTION_SEEN;
    }
}
Also used : Message(org.mule.runtime.api.message.Message) InternalMessage(org.mule.runtime.core.internal.message.InternalMessage) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MuleContextUtils.eventBuilder(org.mule.tck.util.MuleContextUtils.eventBuilder) CouldNotRouteOutboundMessageException(org.mule.runtime.core.privileged.routing.CouldNotRouteOutboundMessageException) MuleException(org.mule.runtime.api.exception.MuleException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException)

Example 74 with CoreEvent

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

the class AsyncRequestReplyRequesterTestCase method testSingleEventNoTimeout.

@Test
public void testSingleEventNoTimeout() throws Exception {
    asyncReplyMP = new TestAsyncRequestReplyRequester(muleContext);
    SensingNullMessageProcessor target = getSensingNullMessageProcessor();
    asyncReplyMP.setListener(target);
    asyncReplyMP.setReplySource(target.getMessageSource());
    asyncReplyMP.setMuleContext(muleContext);
    CoreEvent resultEvent = asyncReplyMP.process(testEvent());
    // Can't assert same because we copy event when we receive async reply
    assertEquals(((PrivilegedEvent) testEvent()).getMessageAsString(muleContext), ((PrivilegedEvent) resultEvent).getMessageAsString(muleContext));
}
Also used : CoreEvent(org.mule.runtime.core.api.event.CoreEvent) SensingNullMessageProcessor(org.mule.tck.SensingNullMessageProcessor) Test(org.junit.Test)

Example 75 with CoreEvent

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

the class AsyncRequestReplyRequesterTestCase method testMultiple.

@Test
@Ignore("See MULE-8830")
public void testMultiple() throws Exception {
    asyncReplyMP = new TestAsyncRequestReplyRequester(muleContext);
    SensingNullMessageProcessor target = getSensingNullMessageProcessor();
    target.setWaitTime(50);
    AsyncDelegateMessageProcessor asyncMP = createAsyncMessageProcessor(target);
    asyncMP.initialise();
    asyncReplyMP.setListener(asyncMP);
    asyncReplyMP.setReplySource(target.getMessageSource());
    final AtomicInteger count = new AtomicInteger();
    for (int i = 0; i < 500; i++) {
        scheduler.execute(() -> {
            try {
                CoreEvent resultEvent = asyncReplyMP.process(testEvent());
                // Can't assert same because we copy event for async currently
                assertEquals(((PrivilegedEvent) testEvent()).getMessageAsString(muleContext), ((PrivilegedEvent) resultEvent).getMessageAsString(muleContext));
                count.incrementAndGet();
                LOGGER.debug("Finished " + count.get());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
    }
    new PollingProber().check(new JUnitLambdaProbe(() -> {
        assertThat(count.get(), greaterThanOrEqualTo(500));
        return true;
    }));
}
Also used : JUnitLambdaProbe(org.mule.tck.probe.JUnitLambdaProbe) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) PollingProber(org.mule.tck.probe.PollingProber) AsyncDelegateMessageProcessor(org.mule.runtime.core.internal.processor.AsyncDelegateMessageProcessor) SensingNullMessageProcessor(org.mule.tck.SensingNullMessageProcessor) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) ResponseTimeoutException(org.mule.runtime.core.privileged.routing.ResponseTimeoutException) MuleException(org.mule.runtime.api.exception.MuleException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

CoreEvent (org.mule.runtime.core.api.event.CoreEvent)485 Test (org.junit.Test)394 Message (org.mule.runtime.api.message.Message)103 SmallTest (org.mule.tck.size.SmallTest)100 TypedValue (org.mule.runtime.api.metadata.TypedValue)44 MessagingException (org.mule.runtime.core.internal.exception.MessagingException)39 BaseEventContext (org.mule.runtime.core.privileged.event.BaseEventContext)38 Description (io.qameta.allure.Description)37 InternalMessage (org.mule.runtime.core.internal.message.InternalMessage)37 List (java.util.List)36 MuleException (org.mule.runtime.api.exception.MuleException)33 Processor (org.mule.runtime.core.api.processor.Processor)33 DataType (org.mule.runtime.api.metadata.DataType)28 InternalEvent (org.mule.runtime.core.internal.message.InternalEvent)28 ComponentLocation (org.mule.runtime.api.component.location.ComponentLocation)27 ArrayList (java.util.ArrayList)26 Map (java.util.Map)26 Optional (java.util.Optional)25 PrivilegedEvent (org.mule.runtime.core.privileged.event.PrivilegedEvent)23 InOrder (org.mockito.InOrder)22