Search in sources :

Example 26 with InternalMessage

use of org.mule.runtime.core.internal.message.InternalMessage 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)

Example 27 with InternalMessage

use of org.mule.runtime.core.internal.message.InternalMessage in project mule by mulesoft.

the class DefaultRouterResultsHandlerTestCase method aggregateMultipleEvents.

@Test
public void aggregateMultipleEvents() throws Exception {
    DataType simpleDateType1 = DataType.builder().type(String.class).mediaType("text/plain").build();
    Message message1 = Message.of("test event A");
    Message message2 = Message.of("test event B");
    Message message3 = Message.of("test event C");
    PrivilegedEvent event1 = InternalEvent.builder(context).message(message1).addVariable("key1", "value1", simpleDateType1).build();
    MuleSession session = event1.getSession();
    PrivilegedEvent event2 = InternalEvent.builder(context).message(message2).session(session).addVariable("key2", "value2", simpleDateType1).build();
    InternalEvent event3 = InternalEvent.builder(context).message(message3).session(session).addVariable("key3", "value3", simpleDateType1).build();
    event1.getSession().setProperty("key", "value");
    event2.getSession().setProperty("key1", "value1");
    event2.getSession().setProperty("key2", "value2");
    event3.getSession().setProperty("KEY2", "value2NEW");
    event3.getSession().setProperty("key3", "value3");
    List<CoreEvent> events = new ArrayList<>();
    events.add(event2);
    events.add(event3);
    PrivilegedEvent result = (PrivilegedEvent) resultsHandler.aggregateResults(events, event1);
    assertThat(result, notNullValue());
    assertThat((List<InternalMessage>) result.getMessage().getPayload().getValue(), hasSize(2));
    assertThat(result.getMessage().getPayload().getValue(), instanceOf(List.class));
    assertThat(((List<InternalMessage>) result.getMessage().getPayload().getValue()).get(0), is(message2));
    assertThat(((List<InternalMessage>) result.getMessage().getPayload().getValue()).get(1), is(message3));
    // Because a new MuleMessageCollection is created, propagate properties from
    // original event
    assertThat(result.getVariables().get("key1").getValue(), equalTo("value1"));
    assertThat(result.getVariables().get("key1").getDataType(), equalTo(simpleDateType1));
    assertThat(result.getVariables().get("key2").getValue(), equalTo("value2"));
    assertThat(result.getVariables().get("key3").getValue(), equalTo("value3"));
    // Root id
    assertThat(result.getCorrelationId(), equalTo(event1.getCorrelationId()));
    assertThat(result.getSession().getProperty("key"), is("value"));
    assertThat(result.getSession().getProperty("key1"), is("value1"));
    assertThat(result.getSession().getProperty("key2"), is("value2NEW"));
    assertThat(result.getSession().getProperty("key3"), is("value3"));
    assertThat(result.getSession().getProperty("key4"), nullValue());
}
Also used : PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) 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) DataType(org.mule.runtime.api.metadata.DataType) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) MuleSession(org.mule.runtime.core.privileged.event.MuleSession) InternalEvent(org.mule.runtime.core.internal.message.InternalEvent) Test(org.junit.Test)

Aggregations

InternalMessage (org.mule.runtime.core.internal.message.InternalMessage)27 Test (org.junit.Test)22 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)20 Message (org.mule.runtime.api.message.Message)12 DataType (org.mule.runtime.api.metadata.DataType)12 PrivilegedEvent (org.mule.runtime.core.privileged.event.PrivilegedEvent)7 UTF_16 (java.nio.charset.StandardCharsets.UTF_16)6 ArrayList (java.util.ArrayList)6 CoreMatchers.equalTo (org.hamcrest.CoreMatchers.equalTo)6 CoreMatchers.hasItem (org.hamcrest.CoreMatchers.hasItem)6 CoreMatchers.not (org.hamcrest.CoreMatchers.not)6 CoreMatchers.sameInstance (org.hamcrest.CoreMatchers.sameInstance)6 Assert.assertEquals (org.junit.Assert.assertEquals)6 Assert.assertNull (org.junit.Assert.assertNull)6 Assert.assertThat (org.junit.Assert.assertThat)6 Rule (org.junit.Rule)6 ExpectedException (org.junit.rules.ExpectedException)6 ExpectedException.none (org.junit.rules.ExpectedException.none)6 Message.of (org.mule.runtime.api.message.Message.of)6 JSON (org.mule.runtime.api.metadata.MediaType.JSON)6