Search in sources :

Example 51 with PrivilegedEvent

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

the class DefaultRouterResultsHandler method createMessageCollection.

private CoreEvent createMessageCollection(final List<CoreEvent> nonNullResults, final CoreEvent previous) {
    final PrivilegedEvent.Builder resultBuilder = PrivilegedEvent.builder(previous);
    List<Message> list = new ArrayList<>();
    for (CoreEvent event : nonNullResults) {
        for (String flowVarName : event.getVariables().keySet()) {
            resultBuilder.addVariable(flowVarName, event.getVariables().get(flowVarName).getValue(), event.getVariables().get(flowVarName).getDataType());
        }
        list.add(event.getMessage());
    }
    final Message coll = Message.builder().collectionValue(list, Message.class).build();
    PrivilegedEvent resultEvent = resultBuilder.message(coll).build();
    setCurrentEvent(resultEvent);
    return resultEvent;
}
Also used : PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) 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)

Example 52 with PrivilegedEvent

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

the class MuleEventTestCase method testMuleEventSerializationWithRawPayload.

@Test
public void testMuleEventSerializationWithRawPayload() throws Exception {
    StringBuilder payload = new StringBuilder();
    // to reproduce issue we must try to serialize something with a payload bigger than 1020 bytes
    for (int i = 0; i < 108; i++) {
        payload.append("1234567890");
    }
    PrivilegedEvent testEvent = this.<PrivilegedEvent.Builder>getEventBuilder().message(of(new ByteArrayInputStream(payload.toString().getBytes()))).build();
    currentMuleContext.set(muleContext);
    byte[] serializedEvent = muleContext.getObjectSerializer().getExternalProtocol().serialize(testEvent);
    testEvent = muleContext.getObjectSerializer().getExternalProtocol().deserialize(serializedEvent);
    assertArrayEquals((byte[]) testEvent.getMessage().getPayload().getValue(), payload.toString().getBytes());
}
Also used : PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 53 with PrivilegedEvent

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

the class MuleEventTestCase method testEventSerializationRestart.

@Test
public void testEventSerializationRestart() throws Exception {
    // Create and register artifacts
    CoreEvent event = createEventToSerialize();
    // Serialize
    Serializable serialized = (Serializable) createSerializableToByteArrayTransformer().transform(event);
    assertNotNull(serialized);
    // Simulate mule cold restart
    muleContext.dispose();
    muleContext = createMuleContext();
    muleContext.start();
    ByteArrayToObject trans = new ByteArrayToObject();
    trans.setMuleContext(muleContext);
    // Recreate and register artifacts (this would happen if using any kind of static config e.g. XML)
    createAndRegisterTransformersEndpointBuilderService();
    // Deserialize
    PrivilegedEvent deserialized = (PrivilegedEvent) trans.transform(serialized);
    // Assert that deserialized event is not null
    assertNotNull(deserialized);
    // Assert that deserialized event has session with same id
    assertNotNull(deserialized.getSession());
}
Also used : Serializable(java.io.Serializable) ByteArrayToObject(org.mule.runtime.core.privileged.transformer.simple.ByteArrayToObject) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) Test(org.junit.Test)

Example 54 with PrivilegedEvent

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

the class DefaultRouterResultsHandlerTestCase method aggregateSingleEvent.

@Test
public void aggregateSingleEvent() {
    Message message1 = Message.of("test event A");
    InternalEvent event1 = InternalEvent.builder(context).message(message1).addVariable("key1", "value1").build();
    event1.getSession().setProperty("key", "value");
    Message message2 = Message.of("test event B");
    InternalEvent event2 = InternalEvent.builder(context).message(message2).addVariable("key2", "value2").build();
    event2.getSession().setProperty("key", "valueNEW");
    event2.getSession().setProperty("key1", "value1");
    CoreEvent result = resultsHandler.aggregateResults(singletonList(event2), 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"));
    PrivilegedEvent privilegedResult = (PrivilegedEvent) result;
    assertThat(privilegedResult.getSession().getProperty("key"), equalTo("valueNEW"));
    assertThat(privilegedResult.getSession().getProperty("key1"), equalTo("value1"));
}
Also used : PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) Message(org.mule.runtime.api.message.Message) InternalMessage(org.mule.runtime.core.internal.message.InternalMessage) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) InternalEvent(org.mule.runtime.core.internal.message.InternalEvent) Test(org.junit.Test)

Example 55 with PrivilegedEvent

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

PrivilegedEvent (org.mule.runtime.core.privileged.event.PrivilegedEvent)56 Test (org.junit.Test)35 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)22 DataType (org.mule.runtime.api.metadata.DataType)17 Message (org.mule.runtime.api.message.Message)15 CompiledExpression (org.mule.mvel2.compiler.CompiledExpression)13 ParserContext (org.mule.mvel2.ParserContext)10 InternalMessage (org.mule.runtime.core.internal.message.InternalMessage)9 MuleException (org.mule.runtime.api.exception.MuleException)7 UTF_16 (java.nio.charset.StandardCharsets.UTF_16)6 CoreMatchers.equalTo (org.hamcrest.CoreMatchers.equalTo)6 InitialisationException (org.mule.runtime.api.lifecycle.InitialisationException)6 TypedValue (org.mule.runtime.api.metadata.TypedValue)6 Processor (org.mule.runtime.core.api.processor.Processor)6 Serializable (java.io.Serializable)5 HashMap (java.util.HashMap)5 Message.of (org.mule.runtime.api.message.Message.of)5 IOException (java.io.IOException)4 BigDecimal (java.math.BigDecimal)4 BigInteger (java.math.BigInteger)4