Search in sources :

Example 16 with Source

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

the class ContentTypeTests method testSendWithDefaultContentType.

@Test
public void testSendWithDefaultContentType() 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");
        source.output().send(MessageBuilder.withPayload(user).build());
        Message<String> message = (Message<String>) collector.forChannel(source.output()).poll(1, TimeUnit.SECONDS);
        User received = mapper.readValue(message.getPayload(), User.class);
        assertThat(message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class).includes(MimeTypeUtils.APPLICATION_JSON));
        assertThat(user.getName()).isEqualTo(received.getName());
    }
}
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 17 with Source

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

the class ContentTypeTests method testSendJsonString.

@Test
public void testSendJsonString() 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);
        source.output().send(MessageBuilder.withPayload("foo").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("foo").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)

Example 18 with Source

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

the class ContentTypeTests method testSendJavaSerializable.

@Test
public void testSendJavaSerializable() throws Exception {
    try (ConfigurableApplicationContext context = SpringApplication.run(SourceApplication.class, "--server.port=0", "--spring.jmx.enabled=false", "--spring.cloud.stream.bindings.output.contentType=application/x-java-serialized-object")) {
        MessageCollector collector = context.getBean(MessageCollector.class);
        Source source = context.getBean(Source.class);
        User user = new User("Alice");
        source.output().send(MessageBuilder.withPayload(user).build());
        Message<User> message = (Message<User>) collector.forChannel(source.output()).poll(1, TimeUnit.SECONDS);
        assertThat(message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class).includes(MessageConverterUtils.X_JAVA_SERIALIZED_OBJECT));
        User received = message.getPayload();
        assertThat(user.getName()).isEqualTo(received.getName());
    }
}
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 19 with Source

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

the class ContentTypeTests method testSendKryoSerialized.

@Test
public void testSendKryoSerialized() throws Exception {
    try (ConfigurableApplicationContext context = SpringApplication.run(SourceApplication.class, "--server.port=0", "--spring.jmx.enabled=false", "--spring.cloud.stream.bindings.output.contentType=application/x-java-object")) {
        MessageCollector collector = context.getBean(MessageCollector.class);
        Source source = context.getBean(Source.class);
        User user = new User("Alice");
        source.output().send(MessageBuilder.withPayload(user).build());
        Message<User> message = (Message<User>) collector.forChannel(source.output()).poll(1, TimeUnit.SECONDS);
        User received = message.getPayload();
        assertThat(message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class).includes(MimeType.valueOf(KryoMessageConverter.KRYO_MIME_TYPE)));
        assertThat(user.getName()).isEqualTo(received.getName());
    }
}
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 20 with Source

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

the class CustomPartitionedProducerTest method testCustomPartitionedProducerByName.

@Test
public void testCustomPartitionedProducerByName() {
    ApplicationContext context = SpringApplication.run(CustomPartitionedProducerTest.TestSource.class, "--spring.jmx.enabled=false", "--spring.main.web-application-type=none", "--spring.cloud.stream.bindings.output.producer.partitionKeyExtractorName=customPartitionKeyExtractor", "--spring.cloud.stream.bindings.output.producer.partitionSelectorName=customPartitionSelector");
    Source testSource = context.getBean(Source.class);
    DirectChannel messageChannel = (DirectChannel) testSource.output();
    for (ChannelInterceptor channelInterceptor : messageChannel.getChannelInterceptors()) {
        if (channelInterceptor instanceof MessageConverterConfigurer.PartitioningInterceptor) {
            Field partitionHandlerField = ReflectionUtils.findField(MessageConverterConfigurer.PartitioningInterceptor.class, "partitionHandler");
            ReflectionUtils.makeAccessible(partitionHandlerField);
            PartitionHandler partitionHandler = (PartitionHandler) ReflectionUtils.getField(partitionHandlerField, channelInterceptor);
            Field partitonKeyExtractorField = ReflectionUtils.findField(PartitionHandler.class, "partitionKeyExtractorStrategy");
            ReflectionUtils.makeAccessible(partitonKeyExtractorField);
            Field partitonSelectorField = ReflectionUtils.findField(PartitionHandler.class, "partitionSelectorStrategy");
            ReflectionUtils.makeAccessible(partitonSelectorField);
            Assert.assertTrue(((PartitionKeyExtractorStrategy) ReflectionUtils.getField(partitonKeyExtractorField, partitionHandler)).getClass().equals(CustomPartitionKeyExtractorClass.class));
            Assert.assertTrue(((PartitionSelectorStrategy) ReflectionUtils.getField(partitonSelectorField, partitionHandler)).getClass().equals(CustomPartitionSelectorClass.class));
        }
    }
}
Also used : PartitionSelectorStrategy(org.springframework.cloud.stream.binder.PartitionSelectorStrategy) DirectChannel(org.springframework.integration.channel.DirectChannel) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) PartitionHandler(org.springframework.cloud.stream.binder.PartitionHandler) CustomPartitionSelectorClass(org.springframework.cloud.stream.partitioning.CustomPartitionSelectorClass) PropertySource(org.springframework.context.annotation.PropertySource) MessageSource(org.springframework.integration.core.MessageSource) Source(org.springframework.cloud.stream.messaging.Source) Field(java.lang.reflect.Field) PartitionKeyExtractorStrategy(org.springframework.cloud.stream.binder.PartitionKeyExtractorStrategy) CustomPartitionKeyExtractorClass(org.springframework.cloud.stream.partitioning.CustomPartitionKeyExtractorClass) ApplicationContext(org.springframework.context.ApplicationContext) 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