Search in sources :

Example 91 with DataType

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

the class IntrospectionUtilsTestCase method getDataTypeFromObject.

@Test
public void getDataTypeFromObject() {
    Class<Object> objectClass = Object.class;
    ObjectTypeBuilder objectTypeBuilder = BaseTypeBuilder.create(JAVA).objectType().id(objectClass.getName());
    DataType dataType = toDataType(objectTypeBuilder.build());
    assertThat(dataType.getType(), is(equalTo(objectClass)));
}
Also used : MapDataType(org.mule.runtime.api.metadata.MapDataType) IntrospectionUtils.toDataType(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.toDataType) DataType(org.mule.runtime.api.metadata.DataType) CollectionDataType(org.mule.runtime.api.metadata.CollectionDataType) ObjectTypeBuilder(org.mule.metadata.api.builder.ObjectTypeBuilder) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 92 with DataType

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

the class OperationMessageProcessorTestCase method operationReturnsCollectionWithCorrectDataType.

@Test
public void operationReturnsCollectionWithCorrectDataType() throws Exception {
    Object payload = new ArrayList<>();
    setUpOperationReturning(payload, new TypeToken<List<Integer>>() {
    }.getType());
    Message message = messageProcessor.process(event).getMessage();
    assertThat(message, is(notNullValue()));
    assertThat(message.getPayload().getValue(), is(sameInstance(payload)));
    DataType dataType = message.getPayload().getDataType();
    assertThat(dataType, instanceOf(CollectionDataType.class));
    assertThat(((CollectionDataType) dataType).getItemDataType(), like(Integer.class, ANY.withCharset(null)));
}
Also used : Message(org.mule.runtime.api.message.Message) TypeToken(com.google.common.reflect.TypeToken) CollectionDataType(org.mule.runtime.api.metadata.CollectionDataType) ArrayList(java.util.ArrayList) MapDataType(org.mule.runtime.api.metadata.MapDataType) DataType(org.mule.runtime.api.metadata.DataType) CollectionDataType(org.mule.runtime.api.metadata.CollectionDataType) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 93 with DataType

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

the class TransformerChainingTestCase method getIncreaseByOneTransformer.

private Transformer getIncreaseByOneTransformer() throws Exception {
    AbstractTransformer transformer = new AbstractTransformer() {

        @Override
        protected Object doTransform(Object src, Charset encoding) throws TransformerException {
            return new Integer(((Integer) src).intValue() + 1);
        }
    };
    DataType integerDataType = DataType.fromType(Integer.class);
    transformer.registerSourceType(integerDataType);
    transformer.setReturnDataType(DataType.builder(integerDataType).charset(getDefaultEncoding(muleContext)).build());
    return transformer;
}
Also used : Charset(java.nio.charset.Charset) DataType(org.mule.runtime.api.metadata.DataType)

Example 94 with DataType

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

the class TransformationServiceTestCase method failsOnConverterWhenSourceAndReturnTypeDoesNotMatchAndThereIsNoImplicitConversion2.

@Test
public void failsOnConverterWhenSourceAndReturnTypeDoesNotMatchAndThereIsNoImplicitConversion2() throws MuleException {
    // Converter(B->C), payload A: FAIL
    ByteArrayInputStream payload = new ByteArrayInputStream(TEST_PAYLOAD.getBytes());
    DataType originalSourceType = DataType.fromType(payload.getClass());
    Transformer converter1 = new MockConverterBuilder().from(originalSourceType).to(dataTypeC).build();
    A transformedPayload = new A();
    when(converter1.transform(any())).thenReturn(transformedPayload);
    Message message = of(payload);
    try {
        transformationService.applyTransformers(message, null, converter1);
        fail("Transformation is supposed to fail");
    } catch (MessageTransformerException expected) {
        assertThat(expected.getErrorMessage().getPayload().getValue(), is(transformedPayload));
    }
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) MockConverterBuilder(org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder) Message(org.mule.runtime.api.message.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) DataType(org.mule.runtime.api.metadata.DataType) MessageTransformerException(org.mule.runtime.core.api.transformer.MessageTransformerException) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 95 with DataType

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

the class StreamingUtilsTestCase method consumeTypedRepeatableIteratorPayload.

@Test
@Description("Test that repeatable iterator is consumed into a list while maintaining the collection data type")
public void consumeTypedRepeatableIteratorPayload() throws Exception {
    CursorIteratorProvider payload = asCursorProvider(TEST_LIST);
    DataType dataType = DataType.builder().collectionType(ArrayList.class).itemType(String.class).build();
    CoreEvent event = consumeRepeatablePayload(getEventBuilder().message(Message.builder().payload(new TypedValue<>(payload, dataType)).build()).build());
    assertConsumedObjectStream(payload, event);
    dataType = event.getMessage().getPayload().getDataType();
    assertThat(dataType, is(instanceOf(CollectionDataType.class)));
    assertThat(((CollectionDataType) dataType).getItemDataType(), equalTo(STRING));
}
Also used : InMemoryCursorIteratorProvider(org.mule.runtime.core.internal.streaming.object.InMemoryCursorIteratorProvider) CursorIteratorProvider(org.mule.runtime.api.streaming.object.CursorIteratorProvider) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) DataType(org.mule.runtime.api.metadata.DataType) CollectionDataType(org.mule.runtime.api.metadata.CollectionDataType) Description(io.qameta.allure.Description) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

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