Search in sources :

Example 71 with MockConverterBuilder

use of org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder in project mule by mulesoft.

the class TransformationServiceTestCase method appliesImplicitConversionWhenAvailable.

@Test
public void appliesImplicitConversionWhenAvailable() throws MuleException {
    Transformer transformer = new MockTransformerBuilder().from(DataType.BYTE_ARRAY).to(DataType.STRING).returning("bar").build();
    Transformer converter = new MockConverterBuilder().from(DataType.STRING).to(DataType.BYTE_ARRAY).returning("bar".getBytes()).build();
    when(conversionResolver.resolve(any(DataType.class), anyList())).thenReturn(converter);
    Message message = of("TEST");
    message = transformationService.applyTransformers(message, null, transformer);
    assertEquals("bar", message.getPayload().getValue());
    verifyTransformerExecuted(transformer);
    verifyTransformerExecuted(converter);
}
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) MockTransformerBuilder(org.mule.runtime.core.internal.transformer.builder.MockTransformerBuilder) DataType(org.mule.runtime.api.metadata.DataType) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 72 with MockConverterBuilder

use of org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder in project mule by mulesoft.

the class TransformationServiceTestCase method skipsConverterAppliesTransformer.

@Test
public void skipsConverterAppliesTransformer() throws MuleException {
    // Converter(B -> D) Transformer(C->D), payload C: skips converter, transforms C to D -> OK
    Transformer converter1 = new MockConverterBuilder().from(dataTypeB).to(dataTypeD).build();
    Transformer transformer2 = new MockTransformerBuilder().from(dataTypeC).to(dataTypeD).returning(new D()).build();
    Message message = of(new C());
    message = transformationService.applyTransformers(message, null, converter1, transformer2);
    assertTrue(message.getPayload().getValue() instanceof D);
    verifyTransformerNotExecuted(converter1);
    verifyTransformerExecuted(transformer2);
}
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) MockTransformerBuilder(org.mule.runtime.core.internal.transformer.builder.MockTransformerBuilder) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 73 with MockConverterBuilder

use of org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder 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 74 with MockConverterBuilder

use of org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder in project mule by mulesoft.

the class TransformationServiceTestCase method appliesBothConverters.

@Test
public void appliesBothConverters() throws MuleException {
    // Converter(B -> C) Converter(C->D), payload B: converts B->C, C->D
    Transformer converter1 = new MockConverterBuilder().from(dataTypeB).to(dataTypeC).returning(new C()).build();
    Transformer converter2 = new MockConverterBuilder().from(dataTypeC).to(dataTypeD).returning(new D()).build();
    Message message = of(new B());
    message = transformationService.applyTransformers(message, null, converter1, converter2);
    assertTrue(message.getPayload().getValue() instanceof D);
    verifyTransformerExecuted(converter1);
    verifyTransformerExecuted(converter2);
}
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) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 75 with MockConverterBuilder

use of org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder in project mule by mulesoft.

the class TransformationServiceTestCase method skipBothConvertersButPayloadMatchesExpectedOutputType.

@Test
public void skipBothConvertersButPayloadMatchesExpectedOutputType() throws MuleException {
    // Converter(B -> C) Converter(C->D), payload D: skips converter(B-C), skips converter(C->D), but D is the expected output
    // type -> OK
    Transformer converter1 = new MockConverterBuilder().from(dataTypeB).to(dataTypeC).build();
    Transformer converter2 = new MockConverterBuilder().from(dataTypeC).to(dataTypeD).build();
    Message message = of(new D());
    message = transformationService.applyTransformers(message, null, converter1, converter2);
    assertTrue(message.getPayload().getValue() instanceof D);
    verifyTransformerNotExecuted(converter1);
    verifyTransformerNotExecuted(converter2);
}
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) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Aggregations

MockConverterBuilder (org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder)78 Test (org.junit.Test)77 SmallTest (org.mule.tck.size.SmallTest)74 Converter (org.mule.runtime.core.api.transformer.Converter)48 Transformer (org.mule.runtime.core.api.transformer.Transformer)37 CompositeConverter (org.mule.runtime.core.privileged.transformer.CompositeConverter)24 Message (org.mule.runtime.api.message.Message)18 ArrayList (java.util.ArrayList)13 MockTransformerBuilder (org.mule.runtime.core.internal.transformer.builder.MockTransformerBuilder)11 TransformerWeighting (org.mule.runtime.core.internal.registry.TransformerWeighting)6 Serializable (java.io.Serializable)4 DataType (org.mule.runtime.api.metadata.DataType)4 IOException (java.io.IOException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Before (org.junit.Before)1 MessageTransformerException (org.mule.runtime.core.api.transformer.MessageTransformerException)1 MuleContextWithRegistries (org.mule.runtime.core.internal.context.MuleContextWithRegistries)1 FruitBowl (org.mule.tck.testmodels.fruit.FruitBowl)1