Search in sources :

Example 1 with CompositeConverter

use of org.mule.runtime.core.privileged.transformer.CompositeConverter in project mule by mulesoft.

the class GraphTransformerResolverTestCase method resolvesTransformersWithSameLengthAndDifferentWeight.

@Test
public void resolvesTransformersWithSameLengthAndDifferentWeight() throws ResolverException {
    Converter xmlToInputStream = new MockConverterBuilder().named("xmlToInputStream").from(XML_DATA_TYPE).to(INPUT_STREAM_DATA_TYPE).weighting(1).build();
    Converter xmlToString = new MockConverterBuilder().named("xmlToString").from(XML_DATA_TYPE).to(STRING_DATA_TYPE).weighting(1).build();
    Converter inputStreamToJson = new MockConverterBuilder().named("inputStreamToJson").from(INPUT_STREAM_DATA_TYPE).to(JSON_DATA_TYPE).weighting(2).build();
    Converter stringToJson = new MockConverterBuilder().named("stringToJson").from(STRING_DATA_TYPE).to(JSON_DATA_TYPE).weighting(1).build();
    graphResolver.transformerChange(xmlToInputStream, TransformerResolver.RegistryAction.ADDED);
    graphResolver.transformerChange(xmlToString, TransformerResolver.RegistryAction.ADDED);
    graphResolver.transformerChange(inputStreamToJson, TransformerResolver.RegistryAction.ADDED);
    graphResolver.transformerChange(stringToJson, TransformerResolver.RegistryAction.ADDED);
    Transformer transformer = graphResolver.resolve(XML_DATA_TYPE, JSON_DATA_TYPE);
    assertTrue(transformer instanceof CompositeConverter);
    CompositeConverter compositeConverter = (CompositeConverter) transformer;
    assertEquals(2, compositeConverter.getConverters().size());
    assertEquals(xmlToInputStream, compositeConverter.getConverters().get(0));
    assertEquals(inputStreamToJson, compositeConverter.getConverters().get(1));
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) MockConverterBuilder(org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder) Converter(org.mule.runtime.core.api.transformer.Converter) CompositeConverter(org.mule.runtime.core.privileged.transformer.CompositeConverter) CompositeConverter(org.mule.runtime.core.privileged.transformer.CompositeConverter) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 2 with CompositeConverter

use of org.mule.runtime.core.privileged.transformer.CompositeConverter in project mule by mulesoft.

the class ShortestTransformationLengthTransformerFilterTestCase method filtersDifferentLengthWithBetterTransformerLast.

@Test
public void filtersDifferentLengthWithBetterTransformerLast() throws ResolverException {
    Converter xmlToString = new MockConverterBuilder().named("xmlToString").from(XML_DATA_TYPE).to(STRING_DATA_TYPE).build();
    Converter stringToJson = new MockConverterBuilder().named("stringToJson").from(STRING_DATA_TYPE).to(JSON_DATA_TYPE).build();
    List<Converter> availableConverters = new ArrayList<Converter>();
    CompositeConverter compositeConverter = new CompositeConverter(xmlToString, stringToJson);
    availableConverters.add(compositeConverter);
    availableConverters.add(xmlToString);
    List<Converter> transformers = filter.filter(availableConverters, UNUSED_DATA_TYPE, UNUSED_DATA_TYPE);
    assertEquals(1, transformers.size());
    assertEquals(xmlToString, transformers.get(0));
}
Also used : MockConverterBuilder(org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder) ArrayList(java.util.ArrayList) Converter(org.mule.runtime.core.api.transformer.Converter) CompositeConverter(org.mule.runtime.core.privileged.transformer.CompositeConverter) CompositeConverter(org.mule.runtime.core.privileged.transformer.CompositeConverter) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 3 with CompositeConverter

use of org.mule.runtime.core.privileged.transformer.CompositeConverter in project mule by mulesoft.

the class TransformationGraphLookupStrategyTestCase method assertContainsCompositeTransformer.

private void assertContainsCompositeTransformer(List<Converter> converters, Converter... composedConverters) {
    for (Converter converter : converters) {
        if (converter instanceof CompositeConverter) {
            CompositeConverter compositeConverter = (CompositeConverter) converter;
            if (compositeConverter.getConverters().size() != composedConverters.length) {
                continue;
            }
            boolean matches = true;
            for (int i = 0; i < composedConverters.length - 1; i++) {
                if (composedConverters[i] != compositeConverter.getConverters().get(i)) {
                    matches = false;
                    break;
                }
            }
            if (matches) {
                return;
            }
        }
    }
    fail("Converter list does not contain a composite converter with: " + composedConverters);
}
Also used : Converter(org.mule.runtime.core.api.transformer.Converter) CompositeConverter(org.mule.runtime.core.privileged.transformer.CompositeConverter) CompositeConverter(org.mule.runtime.core.privileged.transformer.CompositeConverter)

Example 4 with CompositeConverter

use of org.mule.runtime.core.privileged.transformer.CompositeConverter in project mule by mulesoft.

the class TransformationGraphLookupStrategy method createConverters.

private List<Converter> createConverters(List<List<TransformationEdge>> transformationPaths) {
    // Using a set instead of a list for when a path of just one converter is found multiple times.
    Set<Converter> converters = new HashSet<>();
    for (List<TransformationEdge> transformationPath : transformationPaths) {
        Converter[] pathConverters = new Converter[transformationPath.size()];
        int index = 0;
        for (TransformationEdge edge : transformationPath) {
            pathConverters[index++] = edge.getConverter();
        }
        Converter converter;
        if (transformationPath.size() == 1) {
            converter = transformationPath.get(0).getConverter();
        } else {
            converter = new CompositeConverter(pathConverters);
        }
        converters.add(converter);
    }
    return new ArrayList<>(converters);
}
Also used : ArrayList(java.util.ArrayList) Converter(org.mule.runtime.core.api.transformer.Converter) CompositeConverter(org.mule.runtime.core.privileged.transformer.CompositeConverter) HashSet(java.util.HashSet) CompositeConverter(org.mule.runtime.core.privileged.transformer.CompositeConverter)

Example 5 with CompositeConverter

use of org.mule.runtime.core.privileged.transformer.CompositeConverter in project mule by mulesoft.

the class GraphTransformerResolverTestCase method resolvesTransformerWithSameLengthAndSameWeight.

@Test
public void resolvesTransformerWithSameLengthAndSameWeight() throws ResolverException {
    Converter xmlToInputStream = new MockConverterBuilder().named("xmlToInputStream").from(XML_DATA_TYPE).to(INPUT_STREAM_DATA_TYPE).weighting(1).build();
    Converter xmlToString = new MockConverterBuilder().named("xmlToString").from(XML_DATA_TYPE).to(STRING_DATA_TYPE).weighting(1).build();
    Converter inputStreamToJson = new MockConverterBuilder().named("inputStreamToJson").from(INPUT_STREAM_DATA_TYPE).to(JSON_DATA_TYPE).weighting(1).build();
    Converter stringToJson = new MockConverterBuilder().named("stringToJson").from(STRING_DATA_TYPE).to(JSON_DATA_TYPE).weighting(1).build();
    graphResolver.transformerChange(xmlToInputStream, TransformerResolver.RegistryAction.ADDED);
    graphResolver.transformerChange(xmlToString, TransformerResolver.RegistryAction.ADDED);
    graphResolver.transformerChange(inputStreamToJson, TransformerResolver.RegistryAction.ADDED);
    graphResolver.transformerChange(stringToJson, TransformerResolver.RegistryAction.ADDED);
    Transformer transformer = graphResolver.resolve(XML_DATA_TYPE, JSON_DATA_TYPE);
    assertTrue(transformer instanceof CompositeConverter);
    CompositeConverter compositeConverter = (CompositeConverter) transformer;
    assertEquals(2, compositeConverter.getConverters().size());
    assertEquals(xmlToInputStream, compositeConverter.getConverters().get(0));
    assertEquals(inputStreamToJson, compositeConverter.getConverters().get(1));
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) MockConverterBuilder(org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder) Converter(org.mule.runtime.core.api.transformer.Converter) CompositeConverter(org.mule.runtime.core.privileged.transformer.CompositeConverter) CompositeConverter(org.mule.runtime.core.privileged.transformer.CompositeConverter) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Aggregations

Converter (org.mule.runtime.core.api.transformer.Converter)6 CompositeConverter (org.mule.runtime.core.privileged.transformer.CompositeConverter)6 Test (org.junit.Test)4 MockConverterBuilder (org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder)4 SmallTest (org.mule.tck.size.SmallTest)4 ArrayList (java.util.ArrayList)3 Transformer (org.mule.runtime.core.api.transformer.Transformer)2 HashSet (java.util.HashSet)1