Search in sources :

Example 1 with TestChannelBinder

use of org.springframework.cloud.stream.binder.test.TestChannelBinder in project spring-cloud-stream by spring-cloud.

the class PollableConsumerTests method testSimple.

@Test
public void testSimple() {
    TestChannelBinder binder = createBinder();
    MessageConverterConfigurer configurer = context.getBean(MessageConverterConfigurer.class);
    DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource(this.messageConverter);
    configurer.configurePolledMessageSource(pollableSource, "foo");
    pollableSource.addInterceptor(new ChannelInterceptorAdapter() {

        @Override
        public Message<?> preSend(Message<?> message, MessageChannel channel) {
            return MessageBuilder.withPayload(((String) message.getPayload()).toUpperCase()).copyHeaders(message.getHeaders()).build();
        }
    });
    ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(null);
    properties.setMaxAttempts(2);
    properties.setBackOffInitialInterval(0);
    binder.bindPollableConsumer("foo", "bar", pollableSource, properties);
    final AtomicInteger count = new AtomicInteger();
    assertThat(pollableSource.poll(received -> {
        assertThat(received.getPayload()).isEqualTo("POLLED DATA");
        assertThat(received.getHeaders().get(MessageHeaders.CONTENT_TYPE)).isEqualTo(MimeType.valueOf("text/plain"));
        if (count.incrementAndGet() == 1) {
            throw new RuntimeException("test retry");
        }
    })).isTrue();
    assertThat(count.get()).isEqualTo(2);
}
Also used : ChannelInterceptorAdapter(org.springframework.messaging.support.ChannelInterceptorAdapter) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageChannel(org.springframework.messaging.MessageChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageConverterConfigurer(org.springframework.cloud.stream.binding.MessageConverterConfigurer) TestChannelBinder(org.springframework.cloud.stream.binder.test.TestChannelBinder) Test(org.junit.Test)

Example 2 with TestChannelBinder

use of org.springframework.cloud.stream.binder.test.TestChannelBinder in project spring-cloud-stream by spring-cloud.

the class PollableConsumerTests method testErrorsNoRetry.

@Test
public void testErrorsNoRetry() {
    TestChannelBinder binder = createBinder();
    MessageConverterConfigurer configurer = context.getBean(MessageConverterConfigurer.class);
    DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource(this.messageConverter);
    configurer.configurePolledMessageSource(pollableSource, "foo");
    pollableSource.addInterceptor(new ChannelInterceptorAdapter() {

        @Override
        public Message<?> preSend(Message<?> message, MessageChannel channel) {
            return MessageBuilder.withPayload(((String) message.getPayload()).toUpperCase()).copyHeaders(message.getHeaders()).build();
        }
    });
    ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(null);
    properties.setMaxAttempts(1);
    binder.bindPollableConsumer("foo", "bar", pollableSource, properties);
    final CountDownLatch latch = new CountDownLatch(1);
    context.getBean(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME, SubscribableChannel.class).subscribe(m -> {
        latch.countDown();
    });
    final AtomicInteger count = new AtomicInteger();
    assertThat(pollableSource.poll(received -> {
        count.incrementAndGet();
        throw new RuntimeException("test recoverer");
    })).isTrue();
    assertThat(count.get()).isEqualTo(1);
}
Also used : ChannelInterceptorAdapter(org.springframework.messaging.support.ChannelInterceptorAdapter) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) CountDownLatch(java.util.concurrent.CountDownLatch) MessageChannel(org.springframework.messaging.MessageChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageConverterConfigurer(org.springframework.cloud.stream.binding.MessageConverterConfigurer) TestChannelBinder(org.springframework.cloud.stream.binder.test.TestChannelBinder) SubscribableChannel(org.springframework.messaging.SubscribableChannel) Test(org.junit.Test)

Example 3 with TestChannelBinder

use of org.springframework.cloud.stream.binder.test.TestChannelBinder in project spring-cloud-stream by spring-cloud.

the class PollableConsumerTests method testConvertList.

@Test
public void testConvertList() {
    TestChannelBinder binder = createBinder();
    MessageConverterConfigurer configurer = context.getBean(MessageConverterConfigurer.class);
    binder.setMessageSourceDelegate(() -> new GenericMessage<>("[{\"foo\":\"bar\"},{\"foo\":\"baz\"}]".getBytes()));
    DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource(this.messageConverter);
    configurer.configurePolledMessageSource(pollableSource, "foo");
    ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(null);
    properties.setMaxAttempts(1);
    properties.setBackOffInitialInterval(0);
    binder.bindPollableConsumer("foo", "bar", pollableSource, properties);
    final AtomicReference<Object> payload = new AtomicReference<>();
    assertThat(pollableSource.poll(received -> {
        payload.set(received.getPayload());
    }, new ParameterizedTypeReference<List<Foo>>() {
    })).isTrue();
    @SuppressWarnings("unchecked") List<Foo> list = (List<Foo>) payload.get();
    assertThat(list.size()).isEqualTo(2);
    assertThat(list.get(0).getFoo()).isEqualTo("bar");
    assertThat(list.get(1).getFoo()).isEqualTo("baz");
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) MessageConverterConfigurer(org.springframework.cloud.stream.binding.MessageConverterConfigurer) List(java.util.List) TestChannelBinder(org.springframework.cloud.stream.binder.test.TestChannelBinder) Test(org.junit.Test)

Example 4 with TestChannelBinder

use of org.springframework.cloud.stream.binder.test.TestChannelBinder in project spring-cloud-stream by spring-cloud.

the class PollableConsumerTests method testConvertMap.

@Test
public void testConvertMap() {
    TestChannelBinder binder = createBinder();
    MessageConverterConfigurer configurer = context.getBean(MessageConverterConfigurer.class);
    binder.setMessageSourceDelegate(() -> new GenericMessage<>("{\"qux\":{\"foo\":\"bar\"}}".getBytes()));
    DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource(this.messageConverter);
    configurer.configurePolledMessageSource(pollableSource, "foo");
    ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(null);
    properties.setMaxAttempts(1);
    properties.setBackOffInitialInterval(0);
    binder.bindPollableConsumer("foo", "bar", pollableSource, properties);
    final AtomicReference<Object> payload = new AtomicReference<>();
    assertThat(pollableSource.poll(received -> {
        payload.set(received.getPayload());
    }, new ParameterizedTypeReference<Map<String, Foo>>() {
    })).isTrue();
    @SuppressWarnings("unchecked") Map<String, Foo> map = (Map<String, Foo>) payload.get();
    assertThat(map.size()).isEqualTo(1);
    assertThat(map.get("qux").getFoo()).isEqualTo("bar");
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) MessageConverterConfigurer(org.springframework.cloud.stream.binding.MessageConverterConfigurer) TestChannelBinder(org.springframework.cloud.stream.binder.test.TestChannelBinder) Map(java.util.Map) Test(org.junit.Test)

Example 5 with TestChannelBinder

use of org.springframework.cloud.stream.binder.test.TestChannelBinder in project spring-cloud-stream by spring-cloud.

the class PollableConsumerTests method testConvertSimpler.

@Test
public void testConvertSimpler() {
    TestChannelBinder binder = createBinder();
    MessageConverterConfigurer configurer = context.getBean(MessageConverterConfigurer.class);
    BindingServiceProperties bsps = this.context.getBean(BindingServiceProperties.class);
    BindingProperties props = new BindingProperties();
    props.setContentType("text/plain");
    bsps.setBindings(Collections.singletonMap("foo", props));
    binder.setMessageSourceDelegate(() -> new GenericMessage<>("foo".getBytes()));
    DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource(this.messageConverter);
    configurer.configurePolledMessageSource(pollableSource, "foo");
    ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(null);
    properties.setMaxAttempts(1);
    properties.setBackOffInitialInterval(0);
    binder.bindPollableConsumer("foo", "bar", pollableSource, properties);
    final AtomicReference<Object> payload = new AtomicReference<>();
    assertThat(pollableSource.poll(received -> {
        payload.set(received.getPayload());
    }, new ParameterizedTypeReference<String>() {
    })).isTrue();
    assertThat(payload.get()).isInstanceOf(String.class);
    assertThat(payload.get()).isEqualTo("foo");
    // test the cache for coverage
    assertThat(pollableSource.poll(received -> {
        payload.set(received.getPayload());
    }, new ParameterizedTypeReference<String>() {
    })).isTrue();
    assertThat(payload.get()).isInstanceOf(String.class);
    assertThat(payload.get()).isEqualTo("foo");
}
Also used : BindingProperties(org.springframework.cloud.stream.config.BindingProperties) AtomicReference(java.util.concurrent.atomic.AtomicReference) MessageConverterConfigurer(org.springframework.cloud.stream.binding.MessageConverterConfigurer) TestChannelBinder(org.springframework.cloud.stream.binder.test.TestChannelBinder) BindingServiceProperties(org.springframework.cloud.stream.config.BindingServiceProperties) Test(org.junit.Test)

Aggregations

TestChannelBinder (org.springframework.cloud.stream.binder.test.TestChannelBinder)11 Test (org.junit.Test)10 MessageConverterConfigurer (org.springframework.cloud.stream.binding.MessageConverterConfigurer)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Message (org.springframework.messaging.Message)4 MessageChannel (org.springframework.messaging.MessageChannel)4 ChannelInterceptorAdapter (org.springframework.messaging.support.ChannelInterceptorAdapter)4 GenericMessage (org.springframework.messaging.support.GenericMessage)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 SpringApplicationBuilder (org.springframework.boot.builder.SpringApplicationBuilder)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 InputDestination (org.springframework.cloud.stream.binder.test.InputDestination)2 ApplicationContext (org.springframework.context.ApplicationContext)2 SubscribableChannel (org.springframework.messaging.SubscribableChannel)2 MessageConversionException (org.springframework.messaging.converter.MessageConversionException)2 List (java.util.List)1 Map (java.util.Map)1 BindingProperties (org.springframework.cloud.stream.config.BindingProperties)1 BindingServiceProperties (org.springframework.cloud.stream.config.BindingServiceProperties)1