Search in sources :

Example 71 with TypedValue

use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.

the class SetPayloadMessageProcessorTestCase method setsExpressionPayload.

@Test
public void setsExpressionPayload() throws MuleException {
    setPayloadMessageProcessor.setValue(EXPRESSION);
    when(expressionManager.isExpression(EXPRESSION)).thenReturn(true);
    setPayloadMessageProcessor.initialise();
    TypedValue typedValue = new TypedValue(PLAIN_TEXT, DataType.STRING);
    when(expressionManager.evaluate(EXPRESSION, testEvent())).thenReturn(typedValue);
    when(expressionManager.evaluate(eq(EXPRESSION), eq(testEvent()), any(CoreEvent.Builder.class), eq(null))).thenReturn(typedValue);
    CoreEvent response = setPayloadMessageProcessor.process(testEvent());
    assertThat(response.getMessage().getPayload().getValue(), is(PLAIN_TEXT));
}
Also used : CoreEvent(org.mule.runtime.core.api.event.CoreEvent) TypedValue(org.mule.runtime.api.metadata.TypedValue) Test(org.junit.Test)

Example 72 with TypedValue

use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.

the class SetPayloadTransformerTestCase method testSetPayloadTransformerExpression.

@Test
public void testSetPayloadTransformerExpression() throws InitialisationException, MessageTransformerException {
    setPayloadTransformer.setValue(EXPRESSION);
    when(mockExpressionManager.isExpression(EXPRESSION)).thenReturn(true);
    setPayloadTransformer.initialise();
    TypedValue typedValue = new TypedValue<>(PLAIN_TEXT, STRING);
    when(mockExpressionManager.evaluate(EXPRESSION, mockMuleEvent)).thenReturn(typedValue);
    Object response = setPayloadTransformer.transformMessage(mockMuleEvent, UTF_8);
    assertThat(response, is(PLAIN_TEXT));
}
Also used : TypedValue(org.mule.runtime.api.metadata.TypedValue) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 73 with TypedValue

use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.

the class StreamingUtils method updateTypedValueForStreaming.

/**
 * Updates the value a given {@link TypedValue} instance by replacing it with a {@link CursorProvider}.
 *
 * @param value            the typed value to update
 * @param event            the current event
 * @param streamingManager the streaming manager
 * @return updated {@link TypedValue instance}
 */
public static TypedValue updateTypedValueForStreaming(final TypedValue value, final CoreEvent event, final StreamingManager streamingManager) {
    if (event == null) {
        return value;
    } else {
        Object payload = value.getValue();
        if (payload instanceof CursorProvider) {
            CursorProvider cursorProvider = streamingManager.manage((CursorProvider) payload, event);
            DataType dataType = DataType.builder(value.getDataType()).type(cursorProvider.getClass()).build();
            return new TypedValue<>(cursorProvider, dataType, value.getByteLength());
        }
        return value;
    }
}
Also used : CursorProvider(org.mule.runtime.api.streaming.CursorProvider) DataType(org.mule.runtime.api.metadata.DataType) CollectionDataType(org.mule.runtime.api.metadata.CollectionDataType) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Example 74 with TypedValue

use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.

the class StreamingUtils method updateTypedValueWithCursorProvider.

/**
 * Updates the {@link Cursor} value a given {@link TypedValue} instance by replacing it with a {@link CursorProvider}.
 *
 * @param value the typed value to update
 * @param event the current event
 * @param streamingManager the streaming manager
 * @return updated {@link TypedValue instance}
 */
public static TypedValue updateTypedValueWithCursorProvider(final TypedValue value, final CoreEvent event, final StreamingManager streamingManager) {
    if (event == null) {
        return value;
    } else {
        Object payload = value.getValue();
        if (payload instanceof CursorStream) {
            CursorProvider provider = ((CursorStream) value.getValue()).getProvider();
            DataType dataType = DataType.builder(value.getDataType()).type(provider.getClass()).build();
            return new TypedValue(provider, dataType, value.getByteLength());
        } else {
            return value;
        }
    }
}
Also used : CursorProvider(org.mule.runtime.api.streaming.CursorProvider) DataType(org.mule.runtime.api.metadata.DataType) CollectionDataType(org.mule.runtime.api.metadata.CollectionDataType) CursorStream(org.mule.runtime.api.streaming.bytes.CursorStream) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Example 75 with TypedValue

use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.

the class MessageEnricher method enrich.

protected PrivilegedEvent enrich(PrivilegedEvent currentEvent, PrivilegedEvent enrichmentEvent, String sourceExpressionArg, String targetExpressionArg, ExtendedExpressionManager expressionManager) {
    if (StringUtils.isEmpty(sourceExpressionArg)) {
        sourceExpressionArg = "#[mel:payload:]";
    }
    TypedValue typedValue = expressionManager.evaluate(sourceExpressionArg, enrichmentEvent, getLocation());
    if (typedValue.getValue() instanceof Message) {
        Message muleMessage = (Message) typedValue.getValue();
        typedValue = new TypedValue(muleMessage.getPayload().getValue(), muleMessage.getPayload().getDataType());
    }
    if (!StringUtils.isEmpty(targetExpressionArg)) {
        PrivilegedEvent.Builder eventBuilder = PrivilegedEvent.builder(currentEvent);
        expressionManager.enrich(targetExpressionArg, currentEvent, eventBuilder, getLocation(), typedValue);
        return eventBuilder.build();
    } else {
        return PrivilegedEvent.builder(currentEvent).message(Message.builder(currentEvent.getMessage()).value(typedValue.getValue()).mediaType(typedValue.getDataType().getMediaType()).build()).build();
    }
}
Also used : PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) Message(org.mule.runtime.api.message.Message) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Aggregations

TypedValue (org.mule.runtime.api.metadata.TypedValue)97 Test (org.junit.Test)74 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)47 DataType (org.mule.runtime.api.metadata.DataType)17 Message (org.mule.runtime.api.message.Message)16 Description (io.qameta.allure.Description)13 Matchers.containsString (org.hamcrest.Matchers.containsString)13 List (java.util.List)11 SmallTest (org.mule.tck.size.SmallTest)10 BindingContext (org.mule.runtime.api.el.BindingContext)9 Map (java.util.Map)8 Optional (java.util.Optional)8 InputStream (java.io.InputStream)6 HashMap (java.util.HashMap)6 InternalMessage (org.mule.runtime.core.internal.message.InternalMessage)5 ArrayList (java.util.ArrayList)4 Matchers.anyString (org.mockito.Matchers.anyString)4 MuleException (org.mule.runtime.api.exception.MuleException)4 Error (org.mule.runtime.api.message.Error)4 ErrorType (org.mule.runtime.api.message.ErrorType)4