Search in sources :

Example 91 with Message

use of org.mule.runtime.api.message.Message in project mule by mulesoft.

the class ValueReturnDelegateContractTestCase method operationReturnsOperationResultThatOnlySpecifiesPayloadAndAttributes.

@Test
public void operationReturnsOperationResultThatOnlySpecifiesPayloadAndAttributes() throws Exception {
    Object payload = "hello world!";
    Object newAttributes = mock(Object.class);
    CoreEvent result = delegate.asReturnValue(Result.builder().output(payload).attributes(newAttributes).build(), operationContext);
    Message message = getOutputMessage(result);
    assertThat(message.getPayload().getValue(), is(sameInstance(payload)));
    assertThat(message.getAttributes().getValue(), is(sameInstance(newAttributes)));
    assertThat(message.getPayload().getDataType().getType().equals(String.class), is(true));
}
Also used : Message(org.mule.runtime.api.message.Message) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 92 with Message

use of org.mule.runtime.api.message.Message in project mule by mulesoft.

the class ValueReturnDelegateContractTestCase method operationReturnsOperationResultButKeepsAttributes.

@Test
public void operationReturnsOperationResultButKeepsAttributes() throws Exception {
    Object payload = new Object();
    MediaType mediaType = ANY.withCharset(getDefaultEncoding(muleContext));
    CoreEvent result = delegate.asReturnValue(Result.builder().output(payload).mediaType(mediaType).build(), operationContext);
    Message message = getOutputMessage(result);
    assertThat(message.getPayload().getValue(), is(sameInstance(payload)));
    assertThat(message.getAttributes().getValue(), is(nullValue()));
    assertThat(message.getPayload().getDataType().getMediaType(), equalTo(mediaType));
}
Also used : Message(org.mule.runtime.api.message.Message) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MediaType(org.mule.runtime.api.metadata.MediaType) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 93 with Message

use of org.mule.runtime.api.message.Message in project mule by mulesoft.

the class TestEventBuilder method build.

/**
 * Produces an event with the specified configuration.
 *
 * @param flow the recipient for the event to be built.
 * @return an event with the specified configuration.
 */
public CoreEvent build(FlowConstruct flow) {
    final Message.Builder messageBuilder;
    messageBuilder = Message.builder().value(payload).mediaType(mediaType);
    setInboundProperties(messageBuilder, inboundProperties);
    setOutboundProperties(messageBuilder, outboundProperties);
    if (attributes != null) {
        messageBuilder.attributesValue(attributes);
    }
    final Message muleMessage = messageBuilder.build();
    EventContext eventContext;
    if (externalCompletionCallback != null) {
        eventContext = create(flow, TEST_CONNECTOR_LOCATION, sourceCorrelationId, of(externalCompletionCallback));
    } else {
        eventContext = create(flow, TEST_CONNECTOR_LOCATION, sourceCorrelationId);
    }
    CoreEvent.Builder builder = InternalEvent.builder(eventContext).message(spyMessage.apply(muleMessage)).replyToHandler(replyToHandler).itemSequenceInfo(ofNullable(itemSequenceInfo));
    for (Entry<String, TypedValue> variableEntry : variables.entrySet()) {
        builder.addVariable(variableEntry.getKey(), variableEntry.getValue().getValue(), variableEntry.getValue().getDataType());
    }
    CoreEvent event = builder.build();
    for (Entry<String, Attachment> outboundAttachmentEntry : outboundAttachments.entrySet()) {
        event = outboundAttachmentEntry.getValue().addOutboundTo(event, outboundAttachmentEntry.getKey());
    }
    for (Entry<String, Object> sessionPropertyEntry : sessionProperties.entrySet()) {
        ((PrivilegedEvent) event).getSession().setProperty(sessionPropertyEntry.getKey(), sessionPropertyEntry.getValue());
    }
    return spyEvent.apply(event);
}
Also used : EventContext(org.mule.runtime.api.event.EventContext) Message(org.mule.runtime.api.message.Message) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Example 94 with Message

use of org.mule.runtime.api.message.Message in project mule by mulesoft.

the class FlowExecutionLogger method assertRouteNthExecution.

public static void assertRouteNthExecution(String routeKey, int n, Object... values) {
    waitUntilNthExecution(routeKey, n);
    ExecutionLog executionLog = executionLogsMap.get(routeKey);
    Message message = executionLog.getCollectedMessages().get(n - 1);
    if (message.getPayload().getValue() instanceof List) {
        List<TypedValue> aggregatedElements = (List<TypedValue>) message.getPayload().getValue();
        assertThat(aggregatedElements.stream().map(element -> element.getValue()).collect(toList()), contains(values));
    } else {
        assertThat(values, arrayWithSize(1));
        assertThat(message.getPayload().getValue(), is(equalTo(values[0])));
    }
}
Also used : Message(org.mule.runtime.api.message.Message) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Example 95 with Message

use of org.mule.runtime.api.message.Message in project mule by mulesoft.

the class FlowExecutionLogger method process.

@Override
public CoreEvent process(CoreEvent coreEvent) throws MuleException {
    String routeName = coreEvent.getVariables().get(EXECUTION_ROUTE_KEY).getValue().toString();
    Message message = coreEvent.getMessage();
    if (executionLogsMap.containsKey(routeName)) {
        executionLogsMap.get(routeName).logExecution(message);
    } else {
        ExecutionLog newLog = new ExecutionLog(message);
        executionLogsMap.put(routeName, newLog);
    }
    return coreEvent;
}
Also used : Message(org.mule.runtime.api.message.Message)

Aggregations

Message (org.mule.runtime.api.message.Message)226 Test (org.junit.Test)189 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)104 SmallTest (org.mule.tck.size.SmallTest)68 InternalMessage (org.mule.runtime.core.internal.message.InternalMessage)46 Transformer (org.mule.runtime.core.api.transformer.Transformer)35 DataType (org.mule.runtime.api.metadata.DataType)33 ArrayList (java.util.ArrayList)26 PrivilegedEvent (org.mule.runtime.core.privileged.event.PrivilegedEvent)26 Processor (org.mule.runtime.core.api.processor.Processor)25 List (java.util.List)20 Assert.assertThat (org.junit.Assert.assertThat)19 ExpectedException (org.junit.rules.ExpectedException)19 MuleContextUtils.eventBuilder (org.mule.tck.util.MuleContextUtils.eventBuilder)19 Map (java.util.Map)18 Rule (org.junit.Rule)18 MockConverterBuilder (org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder)18 ExpectedException.none (org.junit.rules.ExpectedException.none)17 MockTransformerBuilder (org.mule.runtime.core.internal.transformer.builder.MockTransformerBuilder)17 TypedValue (org.mule.runtime.api.metadata.TypedValue)16