Search in sources :

Example 26 with Transformer

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

the class TransformationServiceTestCase method appliesFirstTransformerFailsOnSecondTransformer.

@Test
public void appliesFirstTransformerFailsOnSecondTransformer() throws MuleException {
    // Transformer(B ->D) Transformer(C->D), payload B: applies first transformer, cannot apply second transformer -> FAIL
    Transformer transformer1 = new MockTransformerBuilder().from(dataTypeB).to(dataTypeD).returning(new D()).build();
    Transformer transformer2 = new MockTransformerBuilder().from(dataTypeC).to(dataTypeD).build();
    Message message = of(new B());
    try {
        transformationService.applyTransformers(message, null, transformer1, transformer2);
        fail("Transformation is supposed to fail");
    } catch (IllegalArgumentException expected) {
    }
    verifyTransformerExecuted(transformer1);
    verifyTransformerNotExecuted(transformer2);
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) Message(org.mule.runtime.api.message.Message) MockTransformerBuilder(org.mule.runtime.core.internal.transformer.builder.MockTransformerBuilder) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 27 with Transformer

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

the class MuleMessageDataTypePropagationTestCase method updatesTypeOnTransformation.

@Test
public void updatesTypeOnTransformation() throws Exception {
    Message message = Message.builder().value(1).mediaType(APPLICATION_XML_DEFAULT).build();
    Transformer transformer = mock(Transformer.class);
    when(transformer.isSourceDataTypeSupported(any())).thenReturn(true);
    DataType outputDataType = DataType.builder().type(Integer.class).mediaType(ANY).charset(DEFAULT_ENCODING).build();
    when(transformer.getReturnDataType()).thenReturn(outputDataType);
    when(transformer.transform(anyObject())).thenReturn(1);
    CoreEvent muleEvent = mock(CoreEvent.class);
    Message result = transformationService.applyTransformers(message, muleEvent, singletonList(transformer));
    assertDataType(result, Integer.class, APPLICATION_XML, 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 28 with Transformer

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

the class MuleMessageDataTypePropagationTestCase method updatesEncodingOnTransformation.

@Test
public void updatesEncodingOnTransformation() throws Exception {
    Message message = Message.builder().value(TEST_PAYLOAD).mediaType(APPLICATION_XML_DEFAULT).build();
    Transformer transformer = mock(Transformer.class);
    when(transformer.isSourceDataTypeSupported(any())).thenReturn(true);
    DataType outputDataType = DataType.builder().type(Integer.class).charset(CUSTOM_ENCODING).build();
    when(transformer.getReturnDataType()).thenReturn(outputDataType);
    when(transformer.transform(anyObject())).thenReturn(Integer.valueOf(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)

Example 29 with Transformer

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

the class MuleRegistryHelperTestCase method findsCompositeTransformerEvenIfDirectNotFound.

@Test
public void findsCompositeTransformerEvenIfDirectNotFound() throws Exception {
    Transformer fruitToSeed = new MockConverterBuilder().named("fruitToSeed").from(FRUIT_DATA_TYPE).to(SEED_DATA_TYPE).build();
    Transformer seedToApple = new MockConverterBuilder().named("seedToApple").from(SEED_DATA_TYPE).to(APPLE_DATA_TYPE).build();
    Transformer appleToBanana = new MockConverterBuilder().named("appleToBanana").from(APPLE_DATA_TYPE).to(BANANA_DATA_TYPE).build();
    Transformer bananaToBloodOrange = new MockConverterBuilder().named("bananaToBloodOrange").from(BANANA_DATA_TYPE).to(BLOOD_ORANGE_DATA_TYPE).build();
    ((MuleContextWithRegistries) muleContext).getRegistry().registerTransformer(fruitToSeed);
    ((MuleContextWithRegistries) muleContext).getRegistry().registerTransformer(seedToApple);
    ((MuleContextWithRegistries) muleContext).getRegistry().registerTransformer(appleToBanana);
    ((MuleContextWithRegistries) muleContext).getRegistry().registerTransformer(bananaToBloodOrange);
    Transformer trans = ((MuleContextWithRegistries) muleContext).getRegistry().lookupTransformer(FRUIT_DATA_TYPE, BLOOD_ORANGE_DATA_TYPE);
    assertThat(trans, is(notNullValue()));
    assertThat(trans, instanceOf(CompositeConverter.class));
    assertThat(trans.getName(), is("fruitToSeedseedToAppleappleToBananabananaToBloodOrange"));
    // The same should be returned if we ask for it with compatible data types
    trans = ((MuleContextWithRegistries) muleContext).getRegistry().lookupTransformer(FRUIT_DATA_TYPE, ORANGE_DATA_TYPE);
    assertThat(trans, instanceOf(CompositeConverter.class));
    assertThat(trans.getName(), is("fruitToSeedseedToAppleappleToBananabananaToBloodOrange"));
    trans = ((MuleContextWithRegistries) muleContext).getRegistry().lookupTransformer(PEACH_DATA_TYPE, BLOOD_ORANGE_DATA_TYPE);
    assertThat(trans, instanceOf(CompositeConverter.class));
    assertThat(trans.getName(), is("fruitToSeedseedToAppleappleToBananabananaToBloodOrange"));
    trans = ((MuleContextWithRegistries) muleContext).getRegistry().lookupTransformer(PEACH_DATA_TYPE, ORANGE_DATA_TYPE);
    assertThat(trans, instanceOf(CompositeConverter.class));
    assertThat(trans.getName(), is("fruitToSeedseedToAppleappleToBananabananaToBloodOrange"));
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) MockConverterBuilder(org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) CompositeConverter(org.mule.runtime.core.privileged.transformer.CompositeConverter) Test(org.junit.Test)

Example 30 with Transformer

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

the class MuleRegistryHelperTestCase method lookupsTransformerByPriority.

@Test
public void lookupsTransformerByPriority() throws Exception {
    Transformer result = ((MuleContextWithRegistries) muleContext).getRegistry().lookupTransformer(BLOOD_ORANGE_DATA_TYPE, FRUIT_DATA_TYPE);
    assertNotNull(result);
    assertEquals(t1, result);
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) 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