Search in sources :

Example 76 with DataType

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

the class DataTypeMatchingTestCase method differentCharsetsShouldNotBeCompatible.

@Test
public void differentCharsetsShouldNotBeCompatible() throws Exception {
    DataType jsonUtf8 = builder(JSON_PARENT_DATA_TYPE).charset(UTF_8).build();
    DataType jsonUtf16 = builder(JSON_PARENT_DATA_TYPE).charset(UTF_16).build();
    assertThat(jsonUtf8, is(not(assignableTo(jsonUtf16))));
    assertThat(jsonUtf16, is(not(assignableTo(jsonUtf8))));
}
Also used : DataType(org.mule.runtime.api.metadata.DataType) Test(org.junit.Test)

Example 77 with DataType

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

the class SimpleDataTypeTestCase method acceptsValidMimeType.

@Test
public void acceptsValidMimeType() throws Exception {
    DataType dataType = DataType.builder().mediaType(JSON).build();
    assertThat(dataType.getMediaType(), equalTo(JSON));
}
Also used : DataType(org.mule.runtime.api.metadata.DataType) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 78 with DataType

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

the class DataTypeUtilsTestCase method generatesContentTypeWithoutCharset.

@Test
public void generatesContentTypeWithoutCharset() throws Exception {
    DataType dataType = DataType.builder().type(Object.class).mediaType(MediaType.APPLICATION_JSON).build();
    String contentType = dataType.getMediaType().toRfcString();
    assertThat(contentType, equalTo("application/json"));
}
Also used : DataType(org.mule.runtime.api.metadata.DataType) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 79 with DataType

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

the class CompositeConverterTestCase method getSourceDataTypes.

@Test
public void getSourceDataTypes() {
    DataType[] dataTypes = new DataType[] { DataType.STRING };
    Converter converter = mock(Converter.class);
    when(converter.getSourceDataTypes()).thenReturn(Arrays.asList(dataTypes));
    CompositeConverter chain = new CompositeConverter(converter);
    assertEquals(DataType.STRING, chain.getSourceDataTypes().get(0));
}
Also used : DataType(org.mule.runtime.api.metadata.DataType) Converter(org.mule.runtime.core.api.transformer.Converter) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 80 with DataType

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

the class DefaultTransformationService method getPayload.

/**
 * Attempts to obtain the payload of this message with the desired Class type. This will try and resolve a transformer that can
 * do this transformation. If a transformer cannot be found an exception is thrown. Any transformers added to the registry will
 * be checked for compatibility.
 *
 * @param resultType the desired return type
 * @param encoding the encoding to use if required
 * @return The converted payload of this message. Note that this method will not alter the payload of this message <b>unless</b>
 *         the payload is an {@link InputStream} in which case the stream will be read and the payload will become the fully
 *         read stream.
 * @throws MessageTransformerException if a transformer cannot be found or there is an error during transformation of the payload.
 */
@SuppressWarnings("unchecked")
protected <T> T getPayload(Message message, DataType resultType, Charset encoding) throws MessageTransformerException {
    // Handle null by ignoring the request
    if (resultType == null) {
        throw new IllegalArgumentException(objectIsNull("resultType").getMessage());
    }
    DataType dataType = DataType.builder(resultType).type(message.getPayload().getDataType().getType()).build();
    // If no conversion is necessary, just return the payload as-is
    if (resultType.isCompatibleWith(dataType)) {
        return (T) message.getPayload().getValue();
    }
    // The transformer to execute on this message
    Transformer transformer = null;
    try {
        transformer = ((MuleContextWithRegistries) muleContext).getRegistry().lookupTransformer(dataType, resultType);
        if (transformer == null) {
            throw new MessageTransformerException(noTransformerFoundForMessage(dataType, resultType), null, message);
        }
        // Pass in the message itself
        Object result = transformer.transform(message, encoding);
        // Unless we disallow Object.class as a valid return type we need to do this extra check
        checkResultDataType(message, resultType, result);
        return (T) result;
    } catch (MessageTransformerException e) {
        throw e;
    } catch (TransformerException e) {
        throw new MessageTransformerException(transformer, e, message);
    }
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) DataType(org.mule.runtime.api.metadata.DataType) MessageTransformerException(org.mule.runtime.core.api.transformer.MessageTransformerException) MessageTransformerException(org.mule.runtime.core.api.transformer.MessageTransformerException) TransformerException(org.mule.runtime.core.api.transformer.TransformerException)

Aggregations

DataType (org.mule.runtime.api.metadata.DataType)102 Test (org.junit.Test)67 SmallTest (org.mule.tck.size.SmallTest)29 DefaultCollectionDataType (org.mule.runtime.core.internal.metadata.DefaultCollectionDataType)24 FunctionDataType (org.mule.runtime.api.metadata.FunctionDataType)20 DefaultFunctionDataType (org.mule.runtime.core.internal.metadata.DefaultFunctionDataType)20 DefaultMapDataType (org.mule.runtime.core.internal.metadata.DefaultMapDataType)20 SimpleDataType (org.mule.runtime.core.internal.metadata.SimpleDataType)20 Message (org.mule.runtime.api.message.Message)18 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)18 TypedValue (org.mule.runtime.api.metadata.TypedValue)12 PrivilegedEvent (org.mule.runtime.core.privileged.event.PrivilegedEvent)12 CompiledExpression (org.mule.mvel2.compiler.CompiledExpression)10 CollectionDataType (org.mule.runtime.api.metadata.CollectionDataType)10 Transformer (org.mule.runtime.core.api.transformer.Transformer)10 ArrayList (java.util.ArrayList)9 List (java.util.List)9 ParserContext (org.mule.mvel2.ParserContext)7 MapDataType (org.mule.runtime.api.metadata.MapDataType)6 MVELExpressionLanguage (org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguage)6