Search in sources :

Example 1 with Source

use of org.springframework.cloud.stream.messaging.Source in project spring-cloud-stream by spring-cloud.

the class ContentTypeTests method testSendTuple.

@Test
public void testSendTuple() throws Exception {
    try (ConfigurableApplicationContext context = SpringApplication.run(SourceApplication.class, "--server.port=0", "--spring.jmx.enabled=false", "--spring.cloud.stream.bindings.output.contentType=application/x-spring-tuple")) {
        MessageCollector collector = context.getBean(MessageCollector.class);
        Source source = context.getBean(Source.class);
        Tuple tuple = TupleBuilder.tuple().of("foo", "bar");
        source.output().send(MessageBuilder.withPayload(tuple).build());
        Message<byte[]> message = (Message<byte[]>) collector.forChannel(source.output()).poll(1, TimeUnit.SECONDS);
        assertThat(message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class).includes(MessageConverterUtils.X_SPRING_TUPLE));
        assertThat(TupleBuilder.fromString(new String(message.getPayload()))).isEqualTo(tuple);
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Message(org.springframework.messaging.Message) MessageCollector(org.springframework.cloud.stream.test.binder.MessageCollector) Source(org.springframework.cloud.stream.messaging.Source) Tuple(org.springframework.tuple.Tuple) Test(org.junit.Test)

Example 2 with Source

use of org.springframework.cloud.stream.messaging.Source in project spring-cloud-stream by spring-cloud.

the class ContentTypeTests method testSendBinaryDataWithContentTypeUsingHeaders.

@Test
public void testSendBinaryDataWithContentTypeUsingHeaders() throws Exception {
    try (ConfigurableApplicationContext context = SpringApplication.run(SourceApplication.class, "--server.port=0", "--spring.jmx.enabled=false")) {
        MessageCollector collector = context.getBean(MessageCollector.class);
        Source source = context.getBean(Source.class);
        byte[] data = new byte[] { 0, 1, 2, 3 };
        source.output().send(MessageBuilder.withPayload(data).setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.IMAGE_JPEG).build());
        Message<byte[]> message = (Message<byte[]>) collector.forChannel(source.output()).poll(1, TimeUnit.SECONDS);
        assertThat(message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class).includes(MimeTypeUtils.IMAGE_JPEG));
        assertThat(message.getPayload()).isEqualTo(data);
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Message(org.springframework.messaging.Message) MessageCollector(org.springframework.cloud.stream.test.binder.MessageCollector) Source(org.springframework.cloud.stream.messaging.Source) Test(org.junit.Test)

Example 3 with Source

use of org.springframework.cloud.stream.messaging.Source in project spring-cloud-stream by spring-cloud.

the class ContentTypeTests method testSendBinaryDataWithContentType.

@Test
public void testSendBinaryDataWithContentType() throws Exception {
    try (ConfigurableApplicationContext context = SpringApplication.run(SourceApplication.class, "--server.port=0", "--spring.jmx.enabled=false", "--spring.cloud.stream.bindings.output.contentType=image/jpeg")) {
        MessageCollector collector = context.getBean(MessageCollector.class);
        Source source = context.getBean(Source.class);
        byte[] data = new byte[] { 0, 1, 2, 3 };
        source.output().send(MessageBuilder.withPayload(data).build());
        Message<byte[]> message = (Message<byte[]>) collector.forChannel(source.output()).poll(1, TimeUnit.SECONDS);
        assertThat(message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class).includes(MimeTypeUtils.IMAGE_JPEG));
        assertThat(message.getPayload()).isEqualTo(data);
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Message(org.springframework.messaging.Message) MessageCollector(org.springframework.cloud.stream.test.binder.MessageCollector) Source(org.springframework.cloud.stream.messaging.Source) Test(org.junit.Test)

Example 4 with Source

use of org.springframework.cloud.stream.messaging.Source in project spring-cloud-stream by spring-cloud.

the class ContentTypeTests method testSendBynaryData.

@Test
public void testSendBynaryData() throws Exception {
    try (ConfigurableApplicationContext context = SpringApplication.run(SourceApplication.class, "--server.port=0", "--spring.jmx.enabled=false")) {
        MessageCollector collector = context.getBean(MessageCollector.class);
        Source source = context.getBean(Source.class);
        byte[] data = new byte[] { 0, 1, 2, 3 };
        source.output().send(MessageBuilder.withPayload(data).setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_OCTET_STREAM).build());
        Message<byte[]> message = (Message<byte[]>) collector.forChannel(source.output()).poll(1, TimeUnit.SECONDS);
        assertThat(message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class).includes(MimeTypeUtils.APPLICATION_OCTET_STREAM));
        assertThat(message.getPayload()).isEqualTo(data);
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Message(org.springframework.messaging.Message) MessageCollector(org.springframework.cloud.stream.test.binder.MessageCollector) Source(org.springframework.cloud.stream.messaging.Source) Test(org.junit.Test)

Example 5 with Source

use of org.springframework.cloud.stream.messaging.Source in project spring-cloud-stream by spring-cloud.

the class ContentTypeTests method testSendJsonAsString.

@Test
public void testSendJsonAsString() throws Exception {
    try (ConfigurableApplicationContext context = SpringApplication.run(SourceApplication.class, "--server.port=0", "--spring.jmx.enabled=false")) {
        MessageCollector collector = context.getBean(MessageCollector.class);
        Source source = context.getBean(Source.class);
        User user = new User("Alice");
        String json = mapper.writeValueAsString(user);
        source.output().send(MessageBuilder.withPayload(user).build());
        Message<String> message = (Message<String>) collector.forChannel(source.output()).poll(1, TimeUnit.SECONDS);
        assertThat(message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class).includes(MimeTypeUtils.APPLICATION_JSON));
        assertThat(json).isEqualTo(message.getPayload());
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Message(org.springframework.messaging.Message) MessageCollector(org.springframework.cloud.stream.test.binder.MessageCollector) Source(org.springframework.cloud.stream.messaging.Source) Test(org.junit.Test)

Aggregations

Source (org.springframework.cloud.stream.messaging.Source)21 Test (org.junit.Test)19 MessageCollector (org.springframework.cloud.stream.test.binder.MessageCollector)16 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)15 Message (org.springframework.messaging.Message)10 Field (java.lang.reflect.Field)5 PartitionHandler (org.springframework.cloud.stream.binder.PartitionHandler)4 PartitionKeyExtractorStrategy (org.springframework.cloud.stream.binder.PartitionKeyExtractorStrategy)4 PartitionSelectorStrategy (org.springframework.cloud.stream.binder.PartitionSelectorStrategy)4 Sink (org.springframework.cloud.stream.messaging.Sink)4 CustomPartitionKeyExtractorClass (org.springframework.cloud.stream.partitioning.CustomPartitionKeyExtractorClass)4 CustomPartitionSelectorClass (org.springframework.cloud.stream.partitioning.CustomPartitionSelectorClass)4 ApplicationContext (org.springframework.context.ApplicationContext)4 PropertySource (org.springframework.context.annotation.PropertySource)4 DirectChannel (org.springframework.integration.channel.DirectChannel)4 MessageSource (org.springframework.integration.core.MessageSource)4 ChannelInterceptor (org.springframework.messaging.support.ChannelInterceptor)4 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 BindableProxyFactory (org.springframework.cloud.stream.binding.BindableProxyFactory)1