Search in sources :

Example 11 with MuleSession

use of org.mule.runtime.core.privileged.event.MuleSession in project mule by mulesoft.

the class MessageChunkAggregatorTestCase method testMessageProcessor.

@Test
public void testMessageProcessor() throws Exception {
    MuleSession session = new DefaultMuleSession();
    Flow flow = createAndRegisterFlow(muleContext, APPLE_FLOW, componentLocator);
    assertNotNull(flow);
    MessageChunkAggregator router = new MessageChunkAggregator();
    router.setAnnotations(getAppleFlowComponentLocationAnnotations());
    initialiseIfNeeded(router, true, muleContext);
    Message message1 = of("test event A");
    Message message2 = of("test event B");
    Message message3 = of("test event C");
    EventContext context = create(flow, TEST_CONNECTOR_LOCATION, "foo");
    CoreEvent event1 = InternalEvent.builder(context).message(message1).groupCorrelation(Optional.of(GroupCorrelation.of(0, 3))).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 resultEvent = router.process(event3);
    assertNotNull(resultEvent);
    Message resultMessage = resultEvent.getMessage();
    assertNotNull(resultMessage);
    String payload = getPayloadAsString(resultMessage);
    assertTrue(payload.contains("test event A"));
    assertTrue(payload.contains("test event B"));
    assertTrue(payload.contains("test event C"));
    assertTrue(payload.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) 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 12 with MuleSession

use of org.mule.runtime.core.privileged.event.MuleSession in project mule by mulesoft.

the class ResequencerTestCase method testMessageResequencerWithComparator.

@Test
public void testMessageResequencerWithComparator() throws Exception {
    MuleSession session = new DefaultMuleSession();
    Flow flow = createFlow(muleContext, "test");
    assertNotNull(flow);
    when(componentLocator.find(any(Location.class))).thenReturn(of(flow));
    TestEventResequencer router = new TestEventResequencer(3);
    Map<QName, Object> fakeComponentLocationAnnotations = ImmutableMap.<QName, Object>builder().put(LOCATION_KEY, fromSingleComponent("test")).build();
    router.setAnnotations(fakeComponentLocationAnnotations);
    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();
    // set a resequencing comparator. We need to reset the router since it will
    // not process the same event group
    // twice
    router = new TestEventResequencer(3);
    router.setMuleContext(muleContext);
    router.setEventComparator(new EventPayloadComparator());
    router.setAnnotations(getAppleFlowComponentLocationAnnotations());
    initialiseIfNeeded(router, true, muleContext);
    assertNull(router.process(event2));
    assertNull(router.process(event3));
    CoreEvent resultEvent = router.process(event1);
    assertNotNull(resultEvent);
    Message resultMessage = resultEvent.getMessage();
    assertNotNull(resultMessage);
    assertEquals("test event C", getPayloadAsString(resultMessage));
}
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) QName(javax.xml.namespace.QName) 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) Location(org.mule.runtime.api.component.location.Location) Test(org.junit.Test)

Example 13 with MuleSession

use of org.mule.runtime.core.privileged.event.MuleSession 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

MuleSession (org.mule.runtime.core.privileged.event.MuleSession)13 Test (org.junit.Test)11 DefaultMuleSession (org.mule.runtime.core.privileged.event.DefaultMuleSession)9 Message (org.mule.runtime.api.message.Message)7 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)7 EventContext (org.mule.runtime.api.event.EventContext)4 Flow (org.mule.runtime.core.api.construct.Flow)4 InternalEvent (org.mule.runtime.core.internal.message.InternalEvent)4 PrivilegedEvent (org.mule.runtime.core.privileged.event.PrivilegedEvent)4 MuleTestUtils.createAndRegisterFlow (org.mule.tck.MuleTestUtils.createAndRegisterFlow)4 DefaultInterceptionEvent (org.mule.runtime.core.internal.interception.DefaultInterceptionEvent)3 InternalMessage (org.mule.runtime.core.internal.message.InternalMessage)3 ArrayList (java.util.ArrayList)2 MuleTestUtils.createFlow (org.mule.tck.MuleTestUtils.createFlow)2 Serializable (java.io.Serializable)1 Collections.singletonList (java.util.Collections.singletonList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1