Search in sources :

Example 81 with Transformer

use of org.mule.runtime.core.api.transformer.Transformer in project mule by mulesoft.

the class TypeBasedTransformerResolverTestCase method doesNotFailIfCannotResolveType.

@Test
public void doesNotFailIfCannotResolveType() throws ResolverException, TransformerException {
    MuleRegistry muleRegistry = mock(MuleRegistry.class);
    when(muleContext.getRegistry()).thenReturn(muleRegistry);
    ArrayList<Transformer> transformers = new ArrayList<>();
    when(muleRegistry.lookupTransformers(dataTypeA, dataTypeB)).thenReturn(transformers);
    TypeBasedTransformerResolver resolver = new TypeBasedTransformerResolver();
    resolver.setMuleContext(muleContext);
    Transformer resolvedTransformer = resolver.resolve(dataTypeA, dataTypeB);
    assertNull(resolvedTransformer);
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) ArrayList(java.util.ArrayList) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 82 with Transformer

use of org.mule.runtime.core.api.transformer.Transformer in project mule by mulesoft.

the class TypeBasedTransformerResolverTestCase method resolvesTypeWithOneMatchingTransformer.

@Test
public void resolvesTypeWithOneMatchingTransformer() throws ResolverException, TransformerException {
    MuleRegistry muleRegistry = mock(MuleRegistry.class);
    when(muleContext.getRegistry()).thenReturn(muleRegistry);
    Transformer aToBConverter = new MockConverterBuilder().from(dataTypeA).to(dataTypeB).build();
    ArrayList<Transformer> transformers = new ArrayList<>();
    transformers.add(aToBConverter);
    when(muleRegistry.lookupTransformers(dataTypeA, dataTypeB)).thenReturn(transformers);
    TypeBasedTransformerResolver resolver = new TypeBasedTransformerResolver();
    resolver.setMuleContext(muleContext);
    Transformer resolvedTransformer = resolver.resolve(dataTypeA, dataTypeB);
    assertEquals(aToBConverter, resolvedTransformer);
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) MockConverterBuilder(org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder) ArrayList(java.util.ArrayList) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 83 with Transformer

use of org.mule.runtime.core.api.transformer.Transformer in project mule by mulesoft.

the class TypeBasedTransformerResolverTestCase method resolvesTypeWithTwoMatchingTransformer.

@Test
public void resolvesTypeWithTwoMatchingTransformer() throws ResolverException, TransformerException {
    MuleRegistry muleRegistry = mock(MuleRegistry.class);
    when(muleContext.getRegistry()).thenReturn(muleRegistry);
    Transformer aToBConverter = new MockConverterBuilder().from(dataTypeA).to(dataTypeB).weighting(1).build();
    Transformer betterAToBConverter = new MockConverterBuilder().from(dataTypeA).to(dataTypeB).weighting(2).build();
    ArrayList<Transformer> transformers = new ArrayList<>();
    transformers.add(aToBConverter);
    transformers.add(betterAToBConverter);
    when(muleRegistry.lookupTransformers(dataTypeA, dataTypeB)).thenReturn(transformers);
    TypeBasedTransformerResolver resolver = new TypeBasedTransformerResolver();
    resolver.setMuleContext(muleContext);
    Transformer resolvedTransformer = resolver.resolve(dataTypeA, dataTypeB);
    assertEquals(betterAToBConverter, resolvedTransformer);
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) MockConverterBuilder(org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder) ArrayList(java.util.ArrayList) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 84 with Transformer

use of org.mule.runtime.core.api.transformer.Transformer 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 85 with Transformer

use of org.mule.runtime.core.api.transformer.Transformer 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

Transformer (org.mule.runtime.core.api.transformer.Transformer)87 Test (org.junit.Test)65 SmallTest (org.mule.tck.size.SmallTest)54 MockConverterBuilder (org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder)37 Message (org.mule.runtime.api.message.Message)35 MockTransformerBuilder (org.mule.runtime.core.internal.transformer.builder.MockTransformerBuilder)19 DataType (org.mule.runtime.api.metadata.DataType)14 Converter (org.mule.runtime.core.api.transformer.Converter)12 ArrayList (java.util.ArrayList)10 TransformerException (org.mule.runtime.core.api.transformer.TransformerException)10 AbstractTransformer (org.mule.runtime.core.api.transformer.AbstractTransformer)9 CompositeConverter (org.mule.runtime.core.privileged.transformer.CompositeConverter)9 Serializable (java.io.Serializable)6 MuleContextWithRegistries (org.mule.runtime.core.internal.context.MuleContextWithRegistries)6 TransformerWeighting (org.mule.runtime.core.internal.registry.TransformerWeighting)6 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)4 MessageTransformerException (org.mule.runtime.core.api.transformer.MessageTransformerException)4 IOException (java.io.IOException)3 LinkedList (java.util.LinkedList)3 List (java.util.List)3