Search in sources :

Example 16 with MessageCollector

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

the class AggregateWithMainTest method testAggregateApplication.

@SuppressWarnings("unchecked")
@Test
public void testAggregateApplication() throws InterruptedException {
    // emulate a main method
    ConfigurableApplicationContext context = new AggregateApplicationBuilder(MainConfiguration.class).web(false).from(UppercaseProcessor.class).namespace("upper").to(SuffixProcessor.class).namespace("suffix").run("--spring.cloud.stream.bindings.input.contentType=text/plain", "--spring.cloud.stream.bindings.output.contentType=text/plain");
    AggregateApplication aggregateAccessor = context.getBean(AggregateApplication.class);
    MessageCollector messageCollector = context.getBean(MessageCollector.class);
    Processor uppercaseProcessor = aggregateAccessor.getBinding(Processor.class, "upper");
    Processor suffixProcessor = aggregateAccessor.getBinding(Processor.class, "suffix");
    uppercaseProcessor.input().send(MessageBuilder.withPayload("Hello").build());
    Message<String> receivedMessage = (Message<String>) messageCollector.forChannel(suffixProcessor.output()).poll(1, TimeUnit.SECONDS);
    assertThat(receivedMessage).isNotNull();
    assertThat(receivedMessage.getPayload()).isEqualTo("HELLO WORLD!");
    context.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Processor(org.springframework.cloud.stream.messaging.Processor) Message(org.springframework.messaging.Message) MessageCollector(org.springframework.cloud.stream.test.binder.MessageCollector) AggregateApplicationBuilder(org.springframework.cloud.stream.aggregate.AggregateApplicationBuilder) AggregateApplication(org.springframework.cloud.stream.aggregate.AggregateApplication) Test(org.junit.Test)

Example 17 with MessageCollector

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

the class StreamListenerHandlerBeanTests method testHandlerBean.

@Test
@SuppressWarnings("unchecked")
public void testHandlerBean() throws Exception {
    ConfigurableApplicationContext context = SpringApplication.run(this.configClass, "--spring.cloud.stream.bindings.output.contentType=application/json", "--server.port=0");
    MessageCollector collector = context.getBean(MessageCollector.class);
    Processor processor = context.getBean(Processor.class);
    String id = UUID.randomUUID().toString();
    processor.input().send(MessageBuilder.withPayload("{\"foo\":\"barbar" + id + "\"}").setHeader("contentType", "application/json").build());
    HandlerBean handlerBean = context.getBean(HandlerBean.class);
    Assertions.assertThat(handlerBean.receivedPojos).hasSize(1);
    Assertions.assertThat(handlerBean.receivedPojos.get(0)).hasFieldOrPropertyWithValue("foo", "barbar" + id);
    Message<String> message = (Message<String>) collector.forChannel(processor.output()).poll(1, TimeUnit.SECONDS);
    assertThat(message).isNotNull();
    assertThat(message.getPayload()).isEqualTo("{\"bar\":\"barbar" + id + "\"}");
    assertThat(message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class).includes(MimeTypeUtils.APPLICATION_JSON));
    context.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Processor(org.springframework.cloud.stream.messaging.Processor) Message(org.springframework.messaging.Message) MessageCollector(org.springframework.cloud.stream.test.binder.MessageCollector) Test(org.junit.Test)

Example 18 with MessageCollector

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

the class StreamListenerHandlerMethodTests method testStreamListenerMethodWithTargetBeanFromOutside.

// TODO: Handle dynamic destinations and contentType
@SuppressWarnings("unchecked")
public void testStreamListenerMethodWithTargetBeanFromOutside() throws Exception {
    ConfigurableApplicationContext context = SpringApplication.run(TestStreamListenerMethodWithTargetBeanFromOutside.class, "--server.port=0", "--spring.jmx.enabled=false", "--spring.cloud.stream.bindings.input.contentType=text/plain", "--spring.cloud.stream.bindings.output.contentType=text/plain");
    Sink sink = context.getBean(Sink.class);
    final String testMessageToSend = "testing";
    sink.input().send(MessageBuilder.withPayload(testMessageToSend).build());
    DirectChannel directChannel = (DirectChannel) context.getBean(testMessageToSend.toUpperCase(), MessageChannel.class);
    MessageCollector messageCollector = context.getBean(MessageCollector.class);
    Message<String> result = (Message<String>) messageCollector.forChannel(directChannel).poll(1000, TimeUnit.MILLISECONDS);
    sink.input().send(MessageBuilder.withPayload(testMessageToSend).build());
    assertThat(result).isNotNull();
    assertThat(result.getPayload()).isEqualTo(testMessageToSend.toUpperCase());
    context.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Sink(org.springframework.cloud.stream.messaging.Sink) MessageChannel(org.springframework.messaging.MessageChannel) Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) MessageCollector(org.springframework.cloud.stream.test.binder.MessageCollector)

Example 19 with MessageCollector

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

the class StreamListenerHandlerMethodTests method testMethodWithObjectAsMethodArgument.

@SuppressWarnings("unchecked")
@Test
public void testMethodWithObjectAsMethodArgument() throws Exception {
    ConfigurableApplicationContext context = SpringApplication.run(TestMethodWithObjectAsMethodArgument.class, "--server.port=0", "--spring.jmx.enabled=false", "--spring.cloud.stream.bindings.input.contentType=text/plain", "--spring.cloud.stream.bindings.output.contentType=text/plain");
    Processor processor = context.getBean(Processor.class);
    final String testMessage = "testing";
    processor.input().send(MessageBuilder.withPayload(testMessage).build());
    MessageCollector messageCollector = context.getBean(MessageCollector.class);
    Message<String> result = (Message<String>) messageCollector.forChannel(processor.output()).poll(1000, TimeUnit.MILLISECONDS);
    assertThat(result).isNotNull();
    assertThat(result.getPayload()).isEqualTo(testMessage.toUpperCase());
    context.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Processor(org.springframework.cloud.stream.messaging.Processor) Message(org.springframework.messaging.Message) MessageCollector(org.springframework.cloud.stream.test.binder.MessageCollector) Test(org.junit.Test)

Example 20 with MessageCollector

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

the class StreamListenerHandlerMethodTests method testMethodHeadersPropagatged.

@SuppressWarnings("unchecked")
@Test
public /**
 * @since 2.0 : This test is an example of the new behavior of 2.0 when it comes to contentType handling.
 * The default contentType being JSON in order to be able to check a message without quotes the user needs to set the input/output contentType accordingly
 * Also, received messages are always of Message<byte[]> now.
 */
void testMethodHeadersPropagatged() throws Exception {
    ConfigurableApplicationContext context = SpringApplication.run(TestMethodHeadersPropagated.class, "--server.port=0", "--spring.jmx.enabled=false", "--spring.cloud.stream.bindings.input.contentType=text/plain", "--spring.cloud.stream.bindings.output.contentType=text/plain");
    Processor processor = context.getBean(Processor.class);
    final String testMessage = "testing";
    processor.input().send(MessageBuilder.withPayload(testMessage).setHeader("foo", "bar").build());
    MessageCollector messageCollector = context.getBean(MessageCollector.class);
    Message<String> result = (Message<String>) messageCollector.forChannel(processor.output()).poll(1000, TimeUnit.MILLISECONDS);
    assertThat(result).isNotNull();
    assertThat(result.getPayload()).isEqualTo(testMessage.toUpperCase());
    assertThat(result.getHeaders().get("foo")).isEqualTo("bar");
    context.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Processor(org.springframework.cloud.stream.messaging.Processor) Message(org.springframework.messaging.Message) MessageCollector(org.springframework.cloud.stream.test.binder.MessageCollector) Test(org.junit.Test)

Aggregations

MessageCollector (org.springframework.cloud.stream.test.binder.MessageCollector)38 Message (org.springframework.messaging.Message)30 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)25 Test (org.junit.Test)24 Processor (org.springframework.cloud.stream.messaging.Processor)19 Source (org.springframework.cloud.stream.messaging.Source)16 Sink (org.springframework.cloud.stream.messaging.Sink)5 ArrayList (java.util.ArrayList)3 AggregateApplication (org.springframework.cloud.stream.aggregate.AggregateApplication)1 AggregateApplicationBuilder (org.springframework.cloud.stream.aggregate.AggregateApplicationBuilder)1 DirectChannel (org.springframework.integration.channel.DirectChannel)1 MessageChannel (org.springframework.messaging.MessageChannel)1 Tuple (org.springframework.tuple.Tuple)1