Search in sources :

Example 11 with Source

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

the class AvroSchemaMessageConverterTests method testSendMessageWithLocation.

@Test
public void testSendMessageWithLocation() throws Exception {
    ConfigurableApplicationContext sourceContext = SpringApplication.run(AvroSourceApplication.class, "--server.port=0", "--spring.jmx.enabled=false", "--schemaLocation=classpath:schemas/users_v1.schema", "--spring.cloud.stream.schemaRegistryClient.enabled=false", "--spring.cloud.stream.bindings.output.contentType=avro/bytes");
    Source source = sourceContext.getBean(Source.class);
    User1 firstOutboundFoo = new User1();
    firstOutboundFoo.setName("foo" + UUID.randomUUID().toString());
    firstOutboundFoo.setFavoriteColor("foo" + UUID.randomUUID().toString());
    source.output().send(MessageBuilder.withPayload(firstOutboundFoo).build());
    MessageCollector sourceMessageCollector = sourceContext.getBean(MessageCollector.class);
    Message<?> outboundMessage = sourceMessageCollector.forChannel(source.output()).poll(1000, TimeUnit.MILLISECONDS);
    ConfigurableApplicationContext barSourceContext = SpringApplication.run(AvroSourceApplication.class, "--server.port=0", "--spring.jmx.enabled=false", "--schemaLocation=classpath:schemas/users_v1.schema", "--spring.cloud.stream.schemaRegistryClient.enabled=false", "--spring.cloud.stream.bindings.output.contentType=avro/bytes");
    Source barSource = barSourceContext.getBean(Source.class);
    User2 firstOutboundUser2 = new User2();
    firstOutboundUser2.setFavoriteColor("foo" + UUID.randomUUID().toString());
    firstOutboundUser2.setFavoritePlace("foo" + UUID.randomUUID().toString());
    firstOutboundUser2.setName("foo" + UUID.randomUUID().toString());
    barSource.output().send(MessageBuilder.withPayload(firstOutboundUser2).build());
    MessageCollector barSourceMessageCollector = barSourceContext.getBean(MessageCollector.class);
    Message<?> barOutboundMessage = barSourceMessageCollector.forChannel(barSource.output()).poll(1000, TimeUnit.MILLISECONDS);
    assertThat(barOutboundMessage).isNotNull();
    User2 secondUser2OutboundPojo = new User2();
    secondUser2OutboundPojo.setFavoriteColor("foo" + UUID.randomUUID().toString());
    secondUser2OutboundPojo.setFavoritePlace("foo" + UUID.randomUUID().toString());
    secondUser2OutboundPojo.setName("foo" + UUID.randomUUID().toString());
    source.output().send(MessageBuilder.withPayload(secondUser2OutboundPojo).build());
    Message<?> secondBarOutboundMessage = sourceMessageCollector.forChannel(source.output()).poll(1000, TimeUnit.MILLISECONDS);
    ConfigurableApplicationContext sinkContext = SpringApplication.run(AvroSinkApplication.class, "--server.port=0", "--spring.jmx.enabled=false", "--spring.cloud.stream.schemaRegistryClient.enabled=false", "--schemaLocation=classpath:schemas/users_v1.schema");
    Sink sink = sinkContext.getBean(Sink.class);
    sink.input().send(outboundMessage);
    sink.input().send(barOutboundMessage);
    sink.input().send(secondBarOutboundMessage);
    List<User1> receivedUsers = sinkContext.getBean(AvroSinkApplication.class).receivedUsers;
    assertThat(receivedUsers).hasSize(3);
    assertThat(receivedUsers.get(0)).isNotSameAs(firstOutboundFoo);
    assertThat(receivedUsers.get(0).getFavoriteColor()).isEqualTo(firstOutboundFoo.getFavoriteColor());
    assertThat(receivedUsers.get(0).getName()).isEqualTo(firstOutboundFoo.getName());
    assertThat(receivedUsers.get(1)).isNotSameAs(firstOutboundUser2);
    assertThat(receivedUsers.get(1).getFavoriteColor()).isEqualTo(firstOutboundUser2.getFavoriteColor());
    assertThat(receivedUsers.get(1).getName()).isEqualTo(firstOutboundUser2.getName());
    assertThat(receivedUsers.get(2)).isNotSameAs(secondUser2OutboundPojo);
    assertThat(receivedUsers.get(2).getFavoriteColor()).isEqualTo(secondUser2OutboundPojo.getFavoriteColor());
    assertThat(receivedUsers.get(2).getName()).isEqualTo(secondUser2OutboundPojo.getName());
    sourceContext.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Sink(org.springframework.cloud.stream.messaging.Sink) MessageCollector(org.springframework.cloud.stream.test.binder.MessageCollector) Source(org.springframework.cloud.stream.messaging.Source) Test(org.junit.Test)

Example 12 with Source

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

the class AvroSchemaRegistryClientMessageConverterTests method testSendMessage.

@Test
public void testSendMessage() throws Exception {
    ConfigurableApplicationContext schemaRegistryServerContext = SpringApplication.run(SchemaRegistryServerApplication.class);
    ConfigurableApplicationContext sourceContext = SpringApplication.run(AvroSourceApplication.class, "--server.port=0", "--spring.jmx.enabled=false", "--spring.cloud.stream.bindings.output.contentType=application/*+avro", "--spring.cloud.stream.schema.avro.dynamicSchemaGenerationEnabled=true");
    Source source = sourceContext.getBean(Source.class);
    User1 firstOutboundFoo = new User1();
    firstOutboundFoo.setFavoriteColor("foo" + UUID.randomUUID().toString());
    firstOutboundFoo.setName("foo" + UUID.randomUUID().toString());
    source.output().send(MessageBuilder.withPayload(firstOutboundFoo).build());
    MessageCollector sourceMessageCollector = sourceContext.getBean(MessageCollector.class);
    Message<?> outboundMessage = sourceMessageCollector.forChannel(source.output()).poll(1000, TimeUnit.MILLISECONDS);
    ConfigurableApplicationContext barSourceContext = SpringApplication.run(AvroSourceApplication.class, "--server.port=0", "--spring.jmx.enabled=false", "--spring.cloud.stream.bindings.output.contentType=application/vnd.user1.v1+avro", "--spring.cloud.stream.schema.avro.dynamicSchemaGenerationEnabled=true");
    Source barSource = barSourceContext.getBean(Source.class);
    User2 firstOutboundUser2 = new User2();
    firstOutboundUser2.setFavoriteColor("foo" + UUID.randomUUID().toString());
    firstOutboundUser2.setName("foo" + UUID.randomUUID().toString());
    barSource.output().send(MessageBuilder.withPayload(firstOutboundUser2).build());
    MessageCollector barSourceMessageCollector = barSourceContext.getBean(MessageCollector.class);
    Message<?> barOutboundMessage = barSourceMessageCollector.forChannel(barSource.output()).poll(1000, TimeUnit.MILLISECONDS);
    assertThat(barOutboundMessage).isNotNull();
    User2 secondBarOutboundPojo = new User2();
    secondBarOutboundPojo.setFavoriteColor("foo" + UUID.randomUUID().toString());
    secondBarOutboundPojo.setName("foo" + UUID.randomUUID().toString());
    source.output().send(MessageBuilder.withPayload(secondBarOutboundPojo).build());
    Message<?> secondBarOutboundMessage = sourceMessageCollector.forChannel(source.output()).poll(1000, TimeUnit.MILLISECONDS);
    ConfigurableApplicationContext sinkContext = SpringApplication.run(AvroSinkApplication.class, "--server.port=0", "--spring.jmx.enabled=false");
    Sink sink = sinkContext.getBean(Sink.class);
    sink.input().send(outboundMessage);
    sink.input().send(barOutboundMessage);
    sink.input().send(secondBarOutboundMessage);
    List<User2> receivedPojos = sinkContext.getBean(AvroSinkApplication.class).receivedPojos;
    assertThat(receivedPojos).hasSize(3);
    assertThat(receivedPojos.get(0)).isNotSameAs(firstOutboundFoo);
    assertThat(receivedPojos.get(0).getFavoriteColor()).isEqualTo(firstOutboundFoo.getFavoriteColor());
    assertThat(receivedPojos.get(0).getName()).isEqualTo(firstOutboundFoo.getName());
    assertThat(receivedPojos.get(0).getFavoritePlace()).isEqualTo("NYC");
    assertThat(receivedPojos.get(1)).isNotSameAs(firstOutboundUser2);
    assertThat(receivedPojos.get(1).getFavoriteColor()).isEqualTo(firstOutboundUser2.getFavoriteColor());
    assertThat(receivedPojos.get(1).getName()).isEqualTo(firstOutboundUser2.getName());
    assertThat(receivedPojos.get(1).getFavoritePlace()).isEqualTo("Boston");
    assertThat(receivedPojos.get(2)).isNotSameAs(secondBarOutboundPojo);
    assertThat(receivedPojos.get(2).getFavoriteColor()).isEqualTo(secondBarOutboundPojo.getFavoriteColor());
    assertThat(receivedPojos.get(2).getName()).isEqualTo(secondBarOutboundPojo.getName());
    assertThat(receivedPojos.get(2).getFavoritePlace()).isEqualTo(secondBarOutboundPojo.getFavoritePlace());
    sinkContext.close();
    barSourceContext.close();
    sourceContext.close();
    schemaRegistryServerContext.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Sink(org.springframework.cloud.stream.messaging.Sink) MessageCollector(org.springframework.cloud.stream.test.binder.MessageCollector) Source(org.springframework.cloud.stream.messaging.Source) Test(org.junit.Test)

Example 13 with Source

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

the class AvroStubSchemaRegistryClientMessageConverterTests method testSendMessage.

@Test
public void testSendMessage() throws Exception {
    ConfigurableApplicationContext sourceContext = SpringApplication.run(AvroSourceApplication.class, "--server.port=0", "--debug", "--spring.jmx.enabled=false", "--spring.cloud.stream.bindings.output.contentType=application/*+avro", "--spring.cloud.stream.schema.avro.dynamicSchemaGenerationEnabled=true");
    Source source = sourceContext.getBean(Source.class);
    User1 firstOutboundFoo = new User1();
    firstOutboundFoo.setFavoriteColor("foo" + UUID.randomUUID().toString());
    firstOutboundFoo.setName("foo" + UUID.randomUUID().toString());
    source.output().send(MessageBuilder.withPayload(firstOutboundFoo).build());
    MessageCollector sourceMessageCollector = sourceContext.getBean(MessageCollector.class);
    Message<?> outboundMessage = sourceMessageCollector.forChannel(source.output()).poll(1000, TimeUnit.MILLISECONDS);
    ConfigurableApplicationContext barSourceContext = SpringApplication.run(AvroSourceApplication.class, "--server.port=0", "--spring.jmx.enabled=false", "--spring.cloud.stream.bindings.output.contentType=application/vnd.user1.v1+avro", "--spring.cloud.stream.schema.avro.dynamicSchemaGenerationEnabled=true");
    Source barSource = barSourceContext.getBean(Source.class);
    User2 firstOutboundUser2 = new User2();
    firstOutboundUser2.setFavoriteColor("foo" + UUID.randomUUID().toString());
    firstOutboundUser2.setName("foo" + UUID.randomUUID().toString());
    barSource.output().send(MessageBuilder.withPayload(firstOutboundUser2).build());
    MessageCollector barSourceMessageCollector = barSourceContext.getBean(MessageCollector.class);
    Message<?> barOutboundMessage = barSourceMessageCollector.forChannel(barSource.output()).poll(1000, TimeUnit.MILLISECONDS);
    assertThat(barOutboundMessage).isNotNull();
    User2 secondBarOutboundPojo = new User2();
    secondBarOutboundPojo.setFavoriteColor("foo" + UUID.randomUUID().toString());
    secondBarOutboundPojo.setName("foo" + UUID.randomUUID().toString());
    source.output().send(MessageBuilder.withPayload(secondBarOutboundPojo).build());
    Message<?> secondBarOutboundMessage = sourceMessageCollector.forChannel(source.output()).poll(1000, TimeUnit.MILLISECONDS);
    ConfigurableApplicationContext sinkContext = SpringApplication.run(AvroSinkApplication.class, "--server.port=0", "--spring.jmx.enabled=false");
    Sink sink = sinkContext.getBean(Sink.class);
    sink.input().send(outboundMessage);
    sink.input().send(barOutboundMessage);
    sink.input().send(secondBarOutboundMessage);
    List<User2> receivedPojos = sinkContext.getBean(AvroSinkApplication.class).receivedPojos;
    assertThat(receivedPojos).hasSize(3);
    assertThat(receivedPojos.get(0)).isNotSameAs(firstOutboundFoo);
    assertThat(receivedPojos.get(0).getFavoriteColor()).isEqualTo(firstOutboundFoo.getFavoriteColor());
    assertThat(receivedPojos.get(0).getName()).isEqualTo(firstOutboundFoo.getName());
    assertThat(receivedPojos.get(0).getFavoritePlace()).isEqualTo("NYC");
    assertThat(receivedPojos.get(1)).isNotSameAs(firstOutboundUser2);
    assertThat(receivedPojos.get(1).getFavoriteColor()).isEqualTo(firstOutboundUser2.getFavoriteColor());
    assertThat(receivedPojos.get(1).getName()).isEqualTo(firstOutboundUser2.getName());
    assertThat(receivedPojos.get(1).getFavoritePlace()).isEqualTo("Boston");
    assertThat(receivedPojos.get(2)).isNotSameAs(secondBarOutboundPojo);
    assertThat(receivedPojos.get(2).getFavoriteColor()).isEqualTo(secondBarOutboundPojo.getFavoriteColor());
    assertThat(receivedPojos.get(2).getName()).isEqualTo(secondBarOutboundPojo.getName());
    assertThat(receivedPojos.get(2).getFavoritePlace()).isEqualTo(secondBarOutboundPojo.getFavoritePlace());
    sourceContext.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Sink(org.springframework.cloud.stream.messaging.Sink) MessageCollector(org.springframework.cloud.stream.test.binder.MessageCollector) Source(org.springframework.cloud.stream.messaging.Source) Test(org.junit.Test)

Example 14 with Source

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

the class SubjectNamingStrategyTest method testCustomNamingStrategy.

@Test
public void testCustomNamingStrategy() throws Exception {
    ConfigurableApplicationContext sourceContext = SpringApplication.run(AvroSourceApplication.class, "--server.port=0", "--debug", "--spring.jmx.enabled=false", "--spring.cloud.stream.bindings.output.contentType=application/*+avro", "--spring.cloud.stream.schema.avro.subjectNamingStrategy=org.springframework.cloud.schema.avro.CustomSubjectNamingStrategy", "--spring.cloud.stream.schema.avro.dynamicSchemaGenerationEnabled=true");
    Source source = sourceContext.getBean(Source.class);
    User1 user1 = new User1();
    user1.setFavoriteColor("foo" + UUID.randomUUID().toString());
    user1.setName("foo" + UUID.randomUUID().toString());
    source.output().send(MessageBuilder.withPayload(user1).build());
    MessageCollector barSourceMessageCollector = sourceContext.getBean(MessageCollector.class);
    Message<?> message = barSourceMessageCollector.forChannel(source.output()).poll(1000, TimeUnit.MILLISECONDS);
    assertThat(message.getHeaders().get("contentType")).isEqualTo(MimeType.valueOf("application/vnd.org.springframework.cloud.schema.avro.User1.v1+avro"));
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) MessageCollector(org.springframework.cloud.stream.test.binder.MessageCollector) Source(org.springframework.cloud.stream.messaging.Source) Test(org.junit.Test)

Example 15 with Source

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

the class ContentTypeTests method testSendStringType.

@Test
public void testSendStringType() throws Exception {
    try (ConfigurableApplicationContext context = SpringApplication.run(SourceApplication.class, "--server.port=0", "--spring.jmx.enabled=false", "--spring.cloud.stream.bindings.output.contentType=text/plain")) {
        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);
        assertThat(message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class).includes(MimeTypeUtils.TEXT_PLAIN));
        assertThat(message.getPayload()).isEqualTo(user.toString());
    }
}
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