Search in sources :

Example 1 with MethodInvokingMessageProcessor

use of org.springframework.integration.handler.MethodInvokingMessageProcessor in project spring-integration by spring-projects.

the class BeanFactoryTypeConverterTests method testMapOfMapOfCollectionIsConverted.

@SuppressWarnings("unchecked")
@Test
public void testMapOfMapOfCollectionIsConverted() {
    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    DefaultConversionService conversionService = new DefaultConversionService();
    conversionService.addConverter(new Converter<Foo, Bar>() {

        @Override
        public Bar convert(Foo source) {
            return new Bar();
        }
    });
    BeanFactoryTypeConverter typeConverter = new BeanFactoryTypeConverter(conversionService);
    beanFactory.setConversionService(conversionService);
    typeConverter.setBeanFactory(beanFactory);
    Map<String, Map<String, Set<Foo>>> foos;
    Map<String, Map<String, Set<Bar>>> bars;
    TypeDescriptor sourceType = TypeDescriptor.map(Map.class, null, null);
    TypeDescriptor targetType = TypeDescriptor.map(Map.class, TypeDescriptor.valueOf(String.class), TypeDescriptor.map(Map.class, TypeDescriptor.valueOf(String.class), TypeDescriptor.collection(Set.class, TypeDescriptor.valueOf(Bar.class))));
    Set<Foo> fooSet = new HashSet<Foo>();
    fooSet.add(new Foo());
    Map<String, Set<Foo>> fooMap = new HashMap<String, Set<Foo>>();
    fooMap.put("foo", fooSet);
    foos = new HashMap<String, Map<String, Set<Foo>>>();
    foos.put("foo", fooMap);
    bars = (Map<String, Map<String, Set<Bar>>>) typeConverter.convertValue(foos, sourceType, targetType);
    assertThat(bars.get("foo").get("foo").iterator().next(), instanceOf(Bar.class));
    Service service = new Service();
    MethodInvokingMessageProcessor<Service> processor = new MethodInvokingMessageProcessor<>(service, "handle");
    processor.setConversionService(conversionService);
    processor.setUseSpelInvoker(true);
    ServiceActivatingHandler handler = new ServiceActivatingHandler(processor);
    QueueChannel replyChannel = new QueueChannel();
    handler.setOutputChannel(replyChannel);
    handler.handleMessage(new GenericMessage<Map<String, Map<String, Set<Foo>>>>(foos));
    Message<?> message = replyChannel.receive(0);
    assertNotNull(message);
    assertEquals("bar", message.getPayload());
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) QueueChannel(org.springframework.integration.channel.QueueChannel) HashMap(java.util.HashMap) HashSet(java.util.HashSet) MethodInvokingMessageProcessor(org.springframework.integration.handler.MethodInvokingMessageProcessor) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ConversionService(org.springframework.core.convert.ConversionService) ExecutorService(java.util.concurrent.ExecutorService) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) HashMap(java.util.HashMap) Map(java.util.Map) ServiceActivatingHandler(org.springframework.integration.handler.ServiceActivatingHandler) Test(org.junit.Test)

Example 2 with MethodInvokingMessageProcessor

use of org.springframework.integration.handler.MethodInvokingMessageProcessor in project spring-integration by spring-projects.

the class BeanFactoryTypeConverterTests method testCollectionIsConverted.

@Test
public void testCollectionIsConverted() {
    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    DefaultConversionService conversionService = new DefaultConversionService();
    conversionService.addConverter(new Converter<Foo, Bar>() {

        @Override
        public Bar convert(Foo source) {
            return new Bar();
        }
    });
    BeanFactoryTypeConverter typeConverter = new BeanFactoryTypeConverter(conversionService);
    beanFactory.setConversionService(conversionService);
    typeConverter.setBeanFactory(beanFactory);
    Service service = new Service();
    MethodInvokingMessageProcessor<Service> processor = new MethodInvokingMessageProcessor<>(service, "handle");
    processor.setConversionService(conversionService);
    processor.setUseSpelInvoker(true);
    ServiceActivatingHandler handler = new ServiceActivatingHandler(processor);
    QueueChannel replyChannel = new QueueChannel();
    handler.setOutputChannel(replyChannel);
    handler.handleMessage(new GenericMessage<Collection<Foo>>(Collections.singletonList(new Foo())));
    Message<?> message = replyChannel.receive(10000);
    assertNotNull(message);
    assertEquals("baz", message.getPayload());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) MethodInvokingMessageProcessor(org.springframework.integration.handler.MethodInvokingMessageProcessor) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ConversionService(org.springframework.core.convert.ConversionService) ExecutorService(java.util.concurrent.ExecutorService) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) Collection(java.util.Collection) ServiceActivatingHandler(org.springframework.integration.handler.ServiceActivatingHandler) Test(org.junit.Test)

Example 3 with MethodInvokingMessageProcessor

use of org.springframework.integration.handler.MethodInvokingMessageProcessor in project spring-integration by spring-projects.

the class MethodInvokingTransformerTests method headerEnricherConfiguredWithMethodName.

@SuppressWarnings({ "rawtypes", "unchecked" })
// this changed in 2.0 see INT-785 and INT-1130
@Test
public void headerEnricherConfiguredWithMethodName() throws Exception {
    TestBean testBean = new TestBean();
    HeaderEnricher transformer = new HeaderEnricher();
    MethodInvokingMessageProcessor processor = new MethodInvokingMessageProcessor(testBean, "propertyEnricherTest");
    transformer.setMessageProcessor(processor);
    transformer.setDefaultOverwrite(true);
    Message<String> message = MessageBuilder.withPayload("test").setHeader("prop1", "bad").setHeader("prop3", "baz").build();
    Message<?> result = transformer.transform(message);
    assertEquals("test", result.getPayload());
    assertEquals("foo", result.getHeaders().get("prop1"));
    assertEquals("bar", result.getHeaders().get("prop2"));
    assertEquals("baz", result.getHeaders().get("prop3"));
}
Also used : MethodInvokingMessageProcessor(org.springframework.integration.handler.MethodInvokingMessageProcessor) Test(org.junit.Test)

Example 4 with MethodInvokingMessageProcessor

use of org.springframework.integration.handler.MethodInvokingMessageProcessor in project spring-integration by spring-projects.

the class MethodInvokingTransformerTests method headerEnricherConfiguredWithMethodReference.

@SuppressWarnings({ "rawtypes", "unchecked" })
// this changed in 2.0 see INT-785 and INT-1130
@Test
public void headerEnricherConfiguredWithMethodReference() throws Exception {
    TestBean testBean = new TestBean();
    Method testMethod = testBean.getClass().getMethod("propertyEnricherTest", String.class);
    HeaderEnricher transformer = new HeaderEnricher();
    transformer.setDefaultOverwrite(true);
    MethodInvokingMessageProcessor processor = new MethodInvokingMessageProcessor(testBean, testMethod);
    transformer.setMessageProcessor(processor);
    Message<String> message = MessageBuilder.withPayload("test").setHeader("prop1", "bad").setHeader("prop3", "baz").build();
    Message<?> result = transformer.transform(message);
    assertEquals("test", result.getPayload());
    assertEquals("foo", result.getHeaders().get("prop1"));
    assertEquals("bar", result.getHeaders().get("prop2"));
    assertEquals("baz", result.getHeaders().get("prop3"));
}
Also used : MethodInvokingMessageProcessor(org.springframework.integration.handler.MethodInvokingMessageProcessor) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 MethodInvokingMessageProcessor (org.springframework.integration.handler.MethodInvokingMessageProcessor)4 ExecutorService (java.util.concurrent.ExecutorService)2 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)2 ConversionService (org.springframework.core.convert.ConversionService)2 DefaultConversionService (org.springframework.core.convert.support.DefaultConversionService)2 QueueChannel (org.springframework.integration.channel.QueueChannel)2 ServiceActivatingHandler (org.springframework.integration.handler.ServiceActivatingHandler)2 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)1