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);
}
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);
}
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);
}
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"));
}
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);
}
Aggregations