Search in sources :

Example 21 with Transformer

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

the class TransformationServiceTestCase method failsOnFirstTransformerIgnoresSecondTransformer.

@Test
public void failsOnFirstTransformerIgnoresSecondTransformer() throws MuleException {
    // Transformer(B ->D) Transformer(C->D), payload C: FAIL
    Transformer transformer1 = new MockTransformerBuilder().from(dataTypeB).to(dataTypeD).build();
    Transformer transformer2 = new MockTransformerBuilder().from(dataTypeC).to(dataTypeD).build();
    Message message = of(new C());
    try {
        transformationService.applyTransformers(message, null, transformer1, transformer2);
        fail("Transformation is supposed to fail");
    } catch (IllegalArgumentException expected) {
    }
    verifyTransformerNotExecuted(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 22 with Transformer

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

the class TransformationServiceTestCase method appliesTransformerSkipsConverter.

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

Example 23 with Transformer

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

the class MuleEventTestCase method serialization.

@Test
public void serialization() throws Exception {
    Transformer transformer = createSerializableToByteArrayTransformer();
    transformer.setMuleContext(muleContext);
    Serializable serialized = (Serializable) createSerializableToByteArrayTransformer().transform(testEvent());
    assertNotNull(serialized);
    ByteArrayToObject trans = new ByteArrayToObject();
    trans.setMuleContext(muleContext);
    PrivilegedEvent deserialized = (PrivilegedEvent) trans.transform(serialized);
    // Assert that deserialized event is not null
    assertNotNull(deserialized);
    // Assert that deserialized event has session with same id
    assertNotNull(deserialized.getSession());
}
Also used : Serializable(java.io.Serializable) ByteArrayToObject(org.mule.runtime.core.privileged.transformer.simple.ByteArrayToObject) Transformer(org.mule.runtime.core.api.transformer.Transformer) AbstractTransformer(org.mule.runtime.core.api.transformer.AbstractTransformer) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) Test(org.junit.Test)

Example 24 with Transformer

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

the class MuleEventTestCase method securityContextSerialization.

@Test
public void securityContextSerialization() throws Exception {
    Transformer transformer = createSerializableToByteArrayTransformer();
    transformer.setMuleContext(muleContext);
    CoreEvent event = CoreEvent.builder(testEvent()).securityContext(createTestAuthentication()).build();
    Serializable serialized = (Serializable) createSerializableToByteArrayTransformer().transform(event);
    assertNotNull(serialized);
    ByteArrayToObject trans = new ByteArrayToObject();
    trans.setMuleContext(muleContext);
    CoreEvent deserialized = (CoreEvent) trans.transform(serialized);
    assertThat(deserialized.getSecurityContext().getAuthentication().getPrincipal(), is(event.getSecurityContext().getAuthentication().getPrincipal()));
    assertThat(deserialized.getSecurityContext().getAuthentication().getProperties().get("key1"), is(event.getSecurityContext().getAuthentication().getProperties().get("key1")));
    assertThat(deserialized.getSecurityContext().getAuthentication().getCredentials(), is(event.getSecurityContext().getAuthentication().getCredentials()));
}
Also used : Serializable(java.io.Serializable) ByteArrayToObject(org.mule.runtime.core.privileged.transformer.simple.ByteArrayToObject) Transformer(org.mule.runtime.core.api.transformer.Transformer) AbstractTransformer(org.mule.runtime.core.api.transformer.AbstractTransformer) Test(org.junit.Test)

Example 25 with Transformer

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

the class TransformationServiceTestCase method skipsConverterFailsOnTransformer.

@Test
public void skipsConverterFailsOnTransformer() throws MuleException {
    // Converter(B -> D) Transformer(C->D), payload A: FAIL
    Transformer converter1 = new MockConverterBuilder().from(dataTypeB).to(dataTypeD).build();
    Transformer transformer2 = new MockTransformerBuilder().from(dataTypeC).to(dataTypeD).build();
    Message message = of(new A());
    try {
        transformationService.applyTransformers(message, null, converter1, transformer2);
        fail("Transformation is supposed to fail");
    } catch (IllegalArgumentException expected) {
    }
    verifyTransformerNotExecuted(converter1);
    verifyTransformerNotExecuted(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)

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