Search in sources :

Example 1 with InternalMessage

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

the class DataWeaveExpressionLanguageAdaptorTestCase method dataTypeBinding.

@Test
public void dataTypeBinding() throws Exception {
    CoreEvent event = getEventWithError(empty());
    InternalMessage message = mock(InternalMessage.class, RETURNS_DEEP_STUBS);
    when(event.getMessage()).thenReturn(message);
    TypedValue payload = new TypedValue<>("hey", STRING);
    TypedValue attributes = new TypedValue<>(null, OBJECT);
    when(message.getPayload()).thenReturn(payload);
    when(message.getAttributes()).thenReturn(attributes);
    TypedValue result = expressionLanguage.evaluate(DATA_TYPE, event, BindingContext.builder().build());
    assertThat(result.getValue(), is(equalTo(STRING)));
    assertThat(result.getDataType(), is(assignableTo(fromType(DataType.class))));
}
Also used : InternalMessage(org.mule.runtime.core.internal.message.InternalMessage) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) DataType(org.mule.runtime.api.metadata.DataType) TypedValue(org.mule.runtime.api.metadata.TypedValue) Test(org.junit.Test)

Example 2 with InternalMessage

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

the class DataWeaveExpressionLanguageAdaptorTestCase method attributesBinding.

@Test
public void attributesBinding() throws Exception {
    CoreEvent event = getEventWithError(empty());
    SomeAttributes attributes = new SomeAttributes();
    InternalMessage message = (InternalMessage) Message.builder().nullValue().attributesValue(attributes).build();
    when(event.getMessage()).thenReturn(message);
    TypedValue result = expressionLanguage.evaluate(ATTRIBUTES, event, BindingContext.builder().build());
    assertThat(result.getValue(), is(equalTo(attributes)));
    assertThat(result.getDataType().getType(), is((equalTo(SomeAttributes.class))));
}
Also used : InternalMessage(org.mule.runtime.core.internal.message.InternalMessage) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) TypedValue(org.mule.runtime.api.metadata.TypedValue) Test(org.junit.Test)

Example 3 with InternalMessage

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

the class MessageContextTestCase method payloadAsType.

@Test
public void payloadAsType() throws Exception {
    InternalMessage transformedMessage = mock(InternalMessage.class, RETURNS_DEEP_STUBS);
    final TypedValue<Object> expectedPayload = new TypedValue<>(new Object(), OBJECT);
    when(transformedMessage.getPayload()).thenReturn(expectedPayload);
    TransformationService transformationService = mock(TransformationService.class);
    muleContext.setTransformationService(transformationService);
    when(transformationService.transform(any(InternalMessage.class), any(DataType.class))).thenReturn(transformedMessage);
    assertSame(transformedMessage.getPayload().getValue(), evaluate("message.payloadAs(org.mule.tck.testmodels.fruit.Banana)", event));
}
Also used : InternalMessage(org.mule.runtime.core.internal.message.InternalMessage) DataType(org.mule.runtime.api.metadata.DataType) TransformationService(org.mule.runtime.api.transformation.TransformationService) TypedValue(org.mule.runtime.api.metadata.TypedValue) Test(org.junit.Test)

Example 4 with InternalMessage

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

the class MessageContextTestCase method payload.

@Test
public void payload() throws Exception {
    InternalMessage message = mock(InternalMessage.class);
    when(event.getMessage()).thenReturn(message);
    Object payload = new Object();
    when(message.getPayload()).thenReturn(new TypedValue<>(payload, DataType.OBJECT));
    assertSame(payload, evaluate("message.payload", event));
}
Also used : InternalMessage(org.mule.runtime.core.internal.message.InternalMessage) Test(org.junit.Test)

Example 5 with InternalMessage

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

the class SimpleCollectionAggregatorTestCase method testAggregateMessageCollections.

@Test
public void testAggregateMessageCollections() throws Exception {
    Flow flow = createAndRegisterFlow(muleContext, APPLE_FLOW, componentLocator);
    assertNotNull(flow);
    SimpleCollectionAggregator router = new SimpleCollectionAggregator();
    router.setMuleContext(muleContext);
    router.setAnnotations(getAppleFlowComponentLocationAnnotations());
    initialiseIfNeeded(router, true, muleContext);
    EventContext executionContext = create(flow, TEST_CONNECTOR_LOCATION, "foo");
    Message message1 = of("test event A");
    Message message2 = of("test event B");
    Message message3 = of("test event C");
    Message message4 = of("test event D");
    List<Message> list = new ArrayList<>();
    List<Message> list2 = new ArrayList<>();
    list.add(message1);
    list.add(message2);
    list2.add(message3);
    list2.add(message4);
    Message messageCollection1 = Message.of(list);
    Message messageCollection2 = Message.of(list2);
    CoreEvent event1 = InternalEvent.builder(executionContext).message(messageCollection1).groupCorrelation(Optional.of(GroupCorrelation.of(0, 2))).build();
    CoreEvent event2 = InternalEvent.builder(executionContext).message(messageCollection2).groupCorrelation(Optional.of(GroupCorrelation.of(0, 2))).build();
    assertNull(router.process(event1));
    CoreEvent resultEvent = router.process(event2);
    assertNotNull(resultEvent);
    Message resultMessage = resultEvent.getMessage();
    assertNotNull(resultMessage);
    List<InternalMessage> payload = (List<InternalMessage>) resultMessage.getPayload().getValue();
    assertEquals(2, payload.size());
    assertEquals("test event A", ((List<InternalMessage>) payload.get(0).getPayload().getValue()).get(0).getPayload().getValue());
    assertEquals("test event B", ((List<InternalMessage>) payload.get(0).getPayload().getValue()).get(1).getPayload().getValue());
    assertEquals("test event C", ((List<InternalMessage>) payload.get(1).getPayload().getValue()).get(0).getPayload().getValue());
    assertEquals("test event D", ((List<InternalMessage>) payload.get(1).getPayload().getValue()).get(1).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) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) MuleTestUtils.createAndRegisterFlow(org.mule.tck.MuleTestUtils.createAndRegisterFlow) Flow(org.mule.runtime.core.api.construct.Flow) 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