Search in sources :

Example 51 with DefaultConversionService

use of org.springframework.core.convert.support.DefaultConversionService in project spring-integration by spring-projects.

the class DatatypeChannelTests method unsupportedTypeButCustomConversionServiceSupports.

@Test
public void unsupportedTypeButCustomConversionServiceSupports() {
    QueueChannel channel = createChannel(Integer.class);
    GenericConversionService conversionService = new DefaultConversionService();
    conversionService.addConverter(new Converter<Boolean, Integer>() {

        @Override
        public Integer convert(Boolean source) {
            return source ? 1 : 0;
        }
    });
    DefaultDatatypeChannelMessageConverter converter = new DefaultDatatypeChannelMessageConverter();
    converter.setConversionService(conversionService);
    channel.setMessageConverter(converter);
    assertTrue(channel.send(new GenericMessage<Boolean>(Boolean.TRUE)));
    assertEquals(1, channel.receive().getPayload());
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) DefaultDatatypeChannelMessageConverter(org.springframework.integration.support.converter.DefaultDatatypeChannelMessageConverter) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) Test(org.junit.Test)

Example 52 with DefaultConversionService

use of org.springframework.core.convert.support.DefaultConversionService in project spring-integration by spring-projects.

the class DatatypeChannelTests method unsupportedTypeAndConversionServiceDoesNotSupport.

@Test(expected = MessageDeliveryException.class)
public void unsupportedTypeAndConversionServiceDoesNotSupport() {
    QueueChannel channel = createChannel(Integer.class);
    ConversionService conversionService = new DefaultConversionService();
    DefaultDatatypeChannelMessageConverter converter = new DefaultDatatypeChannelMessageConverter();
    converter.setConversionService(conversionService);
    channel.setMessageConverter(converter);
    assertTrue(channel.send(new GenericMessage<Boolean>(Boolean.TRUE)));
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) ConversionService(org.springframework.core.convert.ConversionService) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) DefaultDatatypeChannelMessageConverter(org.springframework.integration.support.converter.DefaultDatatypeChannelMessageConverter) Test(org.junit.Test)

Example 53 with DefaultConversionService

use of org.springframework.core.convert.support.DefaultConversionService in project spring-integration by spring-projects.

the class MethodInvokingMessageGroupProcessorTests method shouldFindSimpleAggregatorMethodWithIterator.

@Test
public void shouldFindSimpleAggregatorMethodWithIterator() throws Exception {
    @SuppressWarnings("unused")
    class SimpleAggregator {

        public Integer and(Iterator<Integer> flags) {
            int result = 0;
            while (flags.hasNext()) {
                result = result | flags.next();
            }
            return result;
        }
    }
    MethodInvokingMessageGroupProcessor processor = new MethodInvokingMessageGroupProcessor(new SimpleAggregator());
    GenericConversionService conversionService = new DefaultConversionService();
    conversionService.addConverter(new Converter<ArrayList<?>, Iterator<?>>() {

        @Override
        public Iterator<?> convert(ArrayList<?> source) {
            return source.iterator();
        }
    });
    processor.setConversionService(conversionService);
    when(messageGroupMock.getMessages()).thenReturn(messagesUpForProcessing);
    Object result = processor.processMessageGroup(messageGroupMock);
    assertThat(((Message<?>) result).getPayload(), is(7));
}
Also used : Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) Test(org.junit.Test)

Example 54 with DefaultConversionService

use of org.springframework.core.convert.support.DefaultConversionService in project spring-integration by spring-projects.

the class GatewayProxyFactoryBeanTests method testRequestReplyWithAnonymousChannelConvertedTypeViaConversionService.

@Test
public void testRequestReplyWithAnonymousChannelConvertedTypeViaConversionService() throws Exception {
    QueueChannel requestChannel = new QueueChannel();
    startResponder(requestChannel);
    GenericConversionService cs = new DefaultConversionService();
    Converter<String, byte[]> stringToByteConverter = new Converter<String, byte[]>() {

        @Override
        public byte[] convert(String source) {
            return source.getBytes();
        }
    };
    stringToByteConverter = spy(stringToByteConverter);
    cs.addConverter(stringToByteConverter);
    GatewayProxyFactoryBean proxyFactory = new GatewayProxyFactoryBean();
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.registerSingleton(IntegrationUtils.INTEGRATION_CONVERSION_SERVICE_BEAN_NAME, cs);
    proxyFactory.setBeanFactory(bf);
    proxyFactory.setDefaultRequestChannel(requestChannel);
    proxyFactory.setServiceInterface(TestService.class);
    proxyFactory.setBeanName("testGateway");
    proxyFactory.afterPropertiesSet();
    TestService service = (TestService) proxyFactory.getObject();
    byte[] result = service.requestReplyInBytes("foo");
    assertEquals(6, result.length);
    Mockito.verify(stringToByteConverter, Mockito.times(1)).convert(Mockito.any(String.class));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Converter(org.springframework.core.convert.converter.Converter) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) Matchers.containsString(org.hamcrest.Matchers.containsString) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) Test(org.junit.Test)

Example 55 with DefaultConversionService

use of org.springframework.core.convert.support.DefaultConversionService in project spring-integration by spring-projects.

the class DefaultHttpHeaderMapperFromMessageInboundTests method validateCustomHeadersWithNonStringValuesAndDefaultConverterWithCustomConverter.

@Test
public void validateCustomHeadersWithNonStringValuesAndDefaultConverterWithCustomConverter() throws Exception {
    DefaultHttpHeaderMapper mapper = new DefaultHttpHeaderMapper();
    mapper.setOutboundHeaderNames(new String[] { "customHeader*" });
    GenericConversionService cs = new DefaultConversionService();
    cs.addConverter(new TestClassConverter());
    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    beanFactory.registerSingleton("integrationConversionService", cs);
    mapper.setBeanFactory(beanFactory);
    mapper.afterPropertiesSet();
    HttpHeaders headers = new HttpHeaders();
    Map<String, Object> messageHeaders = new HashMap<String, Object>();
    messageHeaders.put("customHeaderA", 123);
    messageHeaders.put("customHeaderB", new TestClass());
    mapper.fromHeaders(new MessageHeaders(messageHeaders), headers);
    assertNotNull(headers.get("customHeaderA"));
    assertEquals("123", headers.get("customHeaderA").get(0));
    assertNotNull(headers.get("customHeaderB"));
    assertEquals("TestClass.class", headers.get("customHeaderB").get(0));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HashMap(java.util.HashMap) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) MessageHeaders(org.springframework.messaging.MessageHeaders) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) Test(org.junit.Test)

Aggregations

DefaultConversionService (org.springframework.core.convert.support.DefaultConversionService)84 Test (org.junit.jupiter.api.Test)37 Test (org.junit.Test)29 ConversionService (org.springframework.core.convert.ConversionService)13 MethodParameter (org.springframework.core.MethodParameter)12 GenericConversionService (org.springframework.core.convert.support.GenericConversionService)12 ConfigurableWebBindingInitializer (org.springframework.web.bind.support.ConfigurableWebBindingInitializer)12 WebDataBinderFactory (org.springframework.web.bind.support.WebDataBinderFactory)12 DefaultDataBinderFactory (org.springframework.web.bind.support.DefaultDataBinderFactory)11 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)9 RequestParam (org.springframework.web.bind.annotation.RequestParam)9 Optional (java.util.Optional)8 BeforeEach (org.junit.jupiter.api.BeforeEach)6 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)6 HashMap (java.util.HashMap)5 DefaultDatatypeChannelMessageConverter (org.springframework.integration.support.converter.DefaultDatatypeChannelMessageConverter)5 GenericMessage (org.springframework.messaging.support.GenericMessage)5 Before (org.junit.Before)4 ContentConverter (com.synopsys.integration.alert.common.ContentConverter)3 ConfigurationModelConfigurationAccessor (com.synopsys.integration.alert.common.persistence.accessor.ConfigurationModelConfigurationAccessor)3