Search in sources :

Example 96 with DataType

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

the class TransformerCachingTestCase method testCacheUpdate.

@Test
public void testCacheUpdate() throws Exception {
    DataType sourceType = DataType.fromType(FilterInputStream.class);
    MuleRegistry registry = ((MuleContextWithRegistries) muleContext).getRegistry();
    Transformer trans = registry.lookupTransformer(sourceType, BYTE_ARRAY);
    assertNotNull(trans);
    assertTrue(trans instanceof InputStreamToByteArray);
    Transformer trans2 = new FilterInputStreamToByteArray();
    registry.registerTransformer(trans2);
    trans = registry.lookupTransformer(sourceType, BYTE_ARRAY);
    assertNotNull(trans);
    assertTrue(trans instanceof FilterInputStreamToByteArray);
    trans = registry.lookupTransformer(INPUT_STREAM, BYTE_ARRAY);
    assertNotNull(trans);
    assertTrue(trans instanceof InputStreamToByteArray);
    registry.unregisterTransformer(trans2.getName());
    trans = registry.lookupTransformer(sourceType, BYTE_ARRAY);
    assertNotNull(trans);
    assertTrue(trans instanceof InputStreamToByteArray);
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) DiscoverableTransformer(org.mule.runtime.core.api.transformer.DiscoverableTransformer) AbstractTransformer(org.mule.runtime.core.api.transformer.AbstractTransformer) InputStreamToByteArray(org.mule.runtime.core.internal.transformer.simple.InputStreamToByteArray) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) DataType(org.mule.runtime.api.metadata.DataType) Test(org.junit.Test)

Example 97 with DataType

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

the class DefaultRouterResultsHandlerTestCase method aggregateMultipleEvents.

@Test
public void aggregateMultipleEvents() throws Exception {
    DataType simpleDateType1 = DataType.builder().type(String.class).mediaType("text/plain").build();
    Message message1 = Message.of("test event A");
    Message message2 = Message.of("test event B");
    Message message3 = Message.of("test event C");
    PrivilegedEvent event1 = InternalEvent.builder(context).message(message1).addVariable("key1", "value1", simpleDateType1).build();
    MuleSession session = event1.getSession();
    PrivilegedEvent event2 = InternalEvent.builder(context).message(message2).session(session).addVariable("key2", "value2", simpleDateType1).build();
    InternalEvent event3 = InternalEvent.builder(context).message(message3).session(session).addVariable("key3", "value3", simpleDateType1).build();
    event1.getSession().setProperty("key", "value");
    event2.getSession().setProperty("key1", "value1");
    event2.getSession().setProperty("key2", "value2");
    event3.getSession().setProperty("KEY2", "value2NEW");
    event3.getSession().setProperty("key3", "value3");
    List<CoreEvent> events = new ArrayList<>();
    events.add(event2);
    events.add(event3);
    PrivilegedEvent result = (PrivilegedEvent) resultsHandler.aggregateResults(events, event1);
    assertThat(result, notNullValue());
    assertThat((List<InternalMessage>) result.getMessage().getPayload().getValue(), hasSize(2));
    assertThat(result.getMessage().getPayload().getValue(), instanceOf(List.class));
    assertThat(((List<InternalMessage>) result.getMessage().getPayload().getValue()).get(0), is(message2));
    assertThat(((List<InternalMessage>) result.getMessage().getPayload().getValue()).get(1), is(message3));
    // Because a new MuleMessageCollection is created, propagate properties from
    // original event
    assertThat(result.getVariables().get("key1").getValue(), equalTo("value1"));
    assertThat(result.getVariables().get("key1").getDataType(), equalTo(simpleDateType1));
    assertThat(result.getVariables().get("key2").getValue(), equalTo("value2"));
    assertThat(result.getVariables().get("key3").getValue(), equalTo("value3"));
    // Root id
    assertThat(result.getCorrelationId(), equalTo(event1.getCorrelationId()));
    assertThat(result.getSession().getProperty("key"), is("value"));
    assertThat(result.getSession().getProperty("key1"), is("value1"));
    assertThat(result.getSession().getProperty("key2"), is("value2NEW"));
    assertThat(result.getSession().getProperty("key3"), is("value3"));
    assertThat(result.getSession().getProperty("key4"), nullValue());
}
Also used : PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) 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) DataType(org.mule.runtime.api.metadata.DataType) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) MuleSession(org.mule.runtime.core.privileged.event.MuleSession) InternalEvent(org.mule.runtime.core.internal.message.InternalEvent) Test(org.junit.Test)

Example 98 with DataType

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

the class MuleMessageDataTypePropagationTestCase method assertVariableDataType.

private void assertVariableDataType(CoreEvent event, DataType dataType) {
    DataType actualDataType = event.getVariables().get(TEST_PROPERTY).getDataType();
    assertThat(actualDataType, like(dataType));
}
Also used : DataType(org.mule.runtime.api.metadata.DataType)

Example 99 with DataType

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

the class MuleMessageDataTypePropagationTestCase method maintainsCurrentDataTypeClassWhenTransformerOutputTypeIsObject.

@Test
public void maintainsCurrentDataTypeClassWhenTransformerOutputTypeIsObject() throws Exception {
    Message message = of(TEST_PAYLOAD);
    Transformer transformer = mock(Transformer.class);
    when(transformer.isSourceDataTypeSupported(any())).thenReturn(true);
    DataType outputDataType = DataType.builder().type(Object.class).mediaType(ANY).build();
    when(transformer.getReturnDataType()).thenReturn(outputDataType);
    when(transformer.transform(message)).thenReturn(TEST_PAYLOAD);
    CoreEvent muleEvent = mock(CoreEvent.class);
    Message result = transformationService.applyTransformers(message, muleEvent, singletonList(transformer));
    assertDataType(result, String.class, ANY, DEFAULT_ENCODING);
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) Message(org.mule.runtime.api.message.Message) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) DataType(org.mule.runtime.api.metadata.DataType) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 100 with DataType

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

the class MuleMessageDataTypePropagationTestCase method updatesMimeTypeOnTransformation.

@Test
public void updatesMimeTypeOnTransformation() throws Exception {
    Message message = Message.builder().value(TEST_PAYLOAD).mediaType(ANY.withCharset(CUSTOM_ENCODING)).build();
    Transformer transformer = mock(Transformer.class);
    when(transformer.isSourceDataTypeSupported(any())).thenReturn(true);
    DataType outputDataType = DataType.builder().type(Integer.class).mediaType(APPLICATION_XML).build();
    when(transformer.getReturnDataType()).thenReturn(outputDataType);
    when(transformer.transform(anyString())).thenReturn(1);
    CoreEvent muleEvent = mock(CoreEvent.class);
    Message result = transformationService.applyTransformers(message, muleEvent, singletonList(transformer));
    assertDataType(result, Integer.class, APPLICATION_XML, CUSTOM_ENCODING);
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) Message(org.mule.runtime.api.message.Message) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) DataType(org.mule.runtime.api.metadata.DataType) 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