use of org.mule.runtime.core.privileged.transformer.TransformerChain in project mule by mulesoft.
the class TransformerChainingTestCase method testIgnoreBadInputBreaksChainWithTransformationOrderValidInvalid.
@Test
public void testIgnoreBadInputBreaksChainWithTransformationOrderValidInvalid() throws Exception {
AbstractTransformer invalidTransformer = (AbstractTransformer) this.getInvalidTransformer();
assertNotNull(invalidTransformer);
invalidTransformer.setIgnoreBadInput(false);
AbstractTransformer validTransformer = (AbstractTransformer) this.getIncreaseByOneTransformer();
assertNotNull(validTransformer);
Message message = of(new Integer(0));
Transformer messageTransformer = new TransformerChain(validTransformer, invalidTransformer);
try {
transformationService.applyTransformers(message, eventBuilder(muleContext).message(of(0)).build(), messageTransformer);
fail("Transformer chain is expected to fail because of invalid transformer within chain.");
} catch (MuleException tfe) {
assertNotNull(tfe);
}
}
use of org.mule.runtime.core.privileged.transformer.TransformerChain in project mule by mulesoft.
the class TypeBasedTransformerResolver method resolve.
public Transformer resolve(DataType source, DataType result) throws ResolverException {
Transformer transformer = exactTransformerCache.get(source.toString() + result.toString());
if (transformer != null) {
return transformer;
}
MuleRegistry registry = ((MuleContextWithRegistries) muleContext).getRegistry();
List<Transformer> trans = registry.lookupTransformers(source, result);
Transformer compositeTransformer = graphTransformerResolver.resolve(source, result);
if (compositeTransformer != null) {
// Needs to create a new list because the lookup returns a cached instance
trans = new LinkedList<>(trans);
trans.add(compositeTransformer);
}
transformer = getNearestTransformerMatch(trans, source.getType(), result.getType());
// If an exact mach is not found, we have a 'second pass' transformer that can be used to converting to String or
// byte[]
Transformer secondPass;
if (transformer == null) {
// using Object.class and then convert to String or byte[] using the second pass transformer
if (String.class.equals(result.getType())) {
secondPass = objectToString;
} else if (byte[].class.equals(result.getType())) {
secondPass = objectToByteArray;
} else {
return null;
}
// Perform a more general search
trans = registry.lookupTransformers(source, DataType.OBJECT);
transformer = getNearestTransformerMatch(trans, source.getType(), result.getType());
if (transformer != null) {
transformer = new TransformerChain(transformer, secondPass);
try {
registry.registerTransformer(transformer);
} catch (MuleException e) {
throw new ResolverException(e.getI18nMessage(), e);
}
}
}
if (transformer != null) {
exactTransformerCache.put(source.toString() + result.toString(), transformer);
}
return transformer;
}
use of org.mule.runtime.core.privileged.transformer.TransformerChain in project mule by mulesoft.
the class TransformerChainingTestCase method testSingleChainedTransformer.
@Test
public void testSingleChainedTransformer() throws Exception {
AbstractTransformer validTransformer = (AbstractTransformer) this.getIncreaseByOneTransformer();
assertNotNull(validTransformer);
Message message = of(new Integer(0));
Transformer messageTransformer = new TransformerChain(validTransformer);
message = transformationService.applyTransformers(message, eventBuilder(muleContext).message(of(0)).build(), messageTransformer);
Object transformedMessage = message.getPayload().getValue();
assertNotNull(transformedMessage);
assertEquals(new Integer(1), transformedMessage);
}
use of org.mule.runtime.core.privileged.transformer.TransformerChain in project mule by mulesoft.
the class TransformerChainingTestCase method testThreeChainedTransformers.
@Test
public void testThreeChainedTransformers() throws Exception {
AbstractTransformer validTransformer = (AbstractTransformer) this.getIncreaseByOneTransformer();
assertNotNull(validTransformer);
Message message = of(new Integer(0));
Transformer messageTransformer = new TransformerChain(validTransformer, validTransformer, validTransformer);
message = transformationService.applyTransformers(message, eventBuilder(muleContext).message(of(0)).build(), messageTransformer);
Object transformedMessage = message.getPayload().getValue();
assertNotNull(transformedMessage);
assertEquals(new Integer(3), transformedMessage);
}
use of org.mule.runtime.core.privileged.transformer.TransformerChain in project mule by mulesoft.
the class TransformerChainingTestCase method testIgnoreBadInputBreaksWithTransformationOrderInvalidValidWhenEnforcedOn.
@Test(expected = MessageTransformerException.class)
public void testIgnoreBadInputBreaksWithTransformationOrderInvalidValidWhenEnforcedOn() throws Exception {
AbstractTransformer invalidTransformer = (AbstractTransformer) this.getInvalidTransformer();
assertNotNull(invalidTransformer);
invalidTransformer.setIgnoreBadInput(true);
AbstractTransformer validTransformer = (AbstractTransformer) this.getIncreaseByOneTransformer();
assertNotNull(validTransformer);
Message message = of(new Integer(0));
Transformer messageTransformer = new TransformerChain(invalidTransformer, validTransformer);
transformationService.applyTransformers(message, eventBuilder(muleContext).message(of(0)).build(), messageTransformer);
}
Aggregations