Search in sources :

Example 1 with DefaultMuleSession

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

the class SplitterTestCase method assertRouted.

private void assertRouted(Object payload, int count, boolean counted) throws Exception, MuleException {
    MuleSession session = new DefaultMuleSession();
    Map<String, Serializable> inboundProps = new HashMap<>();
    inboundProps.put("inbound1", "1");
    inboundProps.put("inbound2", 2);
    inboundProps.put("inbound3", session);
    Map<String, Serializable> outboundProps = new HashMap<>();
    outboundProps.put("outbound1", "3");
    outboundProps.put("outbound2", 4);
    outboundProps.put("outbound3", session);
    Map<String, Object> invocationProps = new HashMap<>();
    invocationProps.put("invoke1", "5");
    invocationProps.put("invoke2", 6);
    invocationProps.put("invoke3", session);
    Set<Integer> expectedSequences = new HashSet<>();
    for (int i = 1; i <= count; i++) {
        expectedSequences.add(i);
    }
    Message toSplit = InternalMessage.builder().value(payload).inboundProperties(inboundProps).outboundProperties(outboundProps).build();
    Splitter splitter = new Splitter();
    Grabber grabber = new Grabber();
    splitter.setMuleContext(muleContext);
    splitter.setListener(grabber);
    splitter.initialise();
    final Builder eventBuilder = this.<PrivilegedEvent.Builder>getEventBuilder().message(toSplit).session(session);
    for (Map.Entry<String, Object> entry : invocationProps.entrySet()) {
        eventBuilder.addVariable(entry.getKey(), entry.getValue());
    }
    CoreEvent event = eventBuilder.build();
    splitter.process(event);
    List<CoreEvent> splits = grabber.getEvents();
    assertEquals(count, splits.size());
    Set<Object> actualSequences = new HashSet<>();
    assertSplitParts(count, counted, inboundProps, outboundProps, invocationProps, splits, actualSequences);
    assertEquals(expectedSequences, actualSequences);
}
Also used : Serializable(java.io.Serializable) Message(org.mule.runtime.api.message.Message) InternalMessage(org.mule.runtime.core.internal.message.InternalMessage) HashMap(java.util.HashMap) Builder(org.mule.runtime.core.api.event.CoreEvent.Builder) MuleSession(org.mule.runtime.core.privileged.event.MuleSession) DefaultMuleSession(org.mule.runtime.core.privileged.event.DefaultMuleSession) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) DefaultMuleSession(org.mule.runtime.core.privileged.event.DefaultMuleSession) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 2 with DefaultMuleSession

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

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

the class RoundRobinTestCase method testRoundRobin.

@Test
public void testRoundRobin() throws Exception {
    RoundRobin rr = new RoundRobin();
    rr.setAnnotations(getAppleFlowComponentLocationAnnotations());
    MuleSession session = new DefaultMuleSession();
    List<TestProcessor> routes = new ArrayList<>(NUMBER_OF_ROUTES);
    for (int i = 0; i < NUMBER_OF_ROUTES; i++) {
        routes.add(new TestProcessor());
    }
    rr.setRoutes(new ArrayList<Processor>(routes));
    initialiseIfNeeded(rr, muleContext);
    List<Thread> threads = new ArrayList<>(NUMBER_OF_ROUTES);
    for (int i = 0; i < NUMBER_OF_ROUTES; i++) {
        threads.add(new Thread(new TestDriver(session, rr, NUMBER_OF_MESSAGES, getTestFlow(muleContext))));
    }
    for (Thread t : threads) {
        t.start();
    }
    for (Thread t : threads) {
        t.join();
    }
    for (TestProcessor route : routes) {
        assertEquals(NUMBER_OF_MESSAGES, route.getCount());
    }
}
Also used : Processor(org.mule.runtime.core.api.processor.Processor) DefaultMuleSession(org.mule.runtime.core.privileged.event.DefaultMuleSession) ArrayList(java.util.ArrayList) MuleSession(org.mule.runtime.core.privileged.event.MuleSession) DefaultMuleSession(org.mule.runtime.core.privileged.event.DefaultMuleSession) Test(org.junit.Test)

Example 4 with DefaultMuleSession

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

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

the class FirstSuccessfulTestCase method testFirstSuccessful.

@Test
public void testFirstSuccessful() throws Exception {
    MuleSession session = new DefaultMuleSession();
    FirstSuccessful fs = createFirstSuccessfulRouter(new TestProcessor("abc"), new TestProcessor("def"), new TestProcessor("ghi"));
    fs.initialise();
    assertThat(getPayload(fs, session, ""), is("No abc"));
    assertThat(getPayload(fs, session, "abc"), is("No def"));
    assertThat(getPayload(fs, session, "abcdef"), is("No ghi"));
    assertThat(getPayload(fs, session, "abcdefghi"), is(EXCEPTION_SEEN));
    assertThat(getPayload(fs, session, "ABC"), is("No def"));
    assertThat(getPayload(fs, session, "ABCDEF"), is("No ghi"));
    assertThat(getPayload(fs, session, "ABCDEFGHI"), is(EXCEPTION_SEEN));
}
Also used : DefaultMuleSession(org.mule.runtime.core.privileged.event.DefaultMuleSession) MuleSession(org.mule.runtime.core.privileged.event.MuleSession) DefaultMuleSession(org.mule.runtime.core.privileged.event.DefaultMuleSession) Test(org.junit.Test)

Aggregations

DefaultMuleSession (org.mule.runtime.core.privileged.event.DefaultMuleSession)11 Test (org.junit.Test)9 MuleSession (org.mule.runtime.core.privileged.event.MuleSession)9 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)8 Message (org.mule.runtime.api.message.Message)6 EventContext (org.mule.runtime.api.event.EventContext)4 Flow (org.mule.runtime.core.api.construct.Flow)4 MuleTestUtils.createAndRegisterFlow (org.mule.tck.MuleTestUtils.createAndRegisterFlow)4 InternalMessage (org.mule.runtime.core.internal.message.InternalMessage)2 PrivilegedEvent (org.mule.runtime.core.privileged.event.PrivilegedEvent)2 MuleTestUtils.createFlow (org.mule.tck.MuleTestUtils.createFlow)2 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 QName (javax.xml.namespace.QName)1 Location (org.mule.runtime.api.component.location.Location)1 Builder (org.mule.runtime.core.api.event.CoreEvent.Builder)1 Processor (org.mule.runtime.core.api.processor.Processor)1