Search in sources :

Example 11 with Processor

use of org.springframework.cloud.stream.messaging.Processor 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 12 with Processor

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

the class StreamListenerHandlerMethodTests method testMethodWithMultipleInputParameters.

@Test
public void testMethodWithMultipleInputParameters() throws Exception {
    ConfigurableApplicationContext context = SpringApplication.run(TestMethodWithMultipleInputParameters.class, "--server.port=0", "--spring.jmx.enabled=false");
    Processor processor = context.getBean(Processor.class);
    StreamListenerTestUtils.FooInboundChannel1 inboundChannel2 = context.getBean(StreamListenerTestUtils.FooInboundChannel1.class);
    final CountDownLatch latch = new CountDownLatch(2);
    ((SubscribableChannel) processor.output()).subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            Assert.isTrue(message.getPayload().equals("footesting") || message.getPayload().equals("BARTESTING"), "Assert failed");
            latch.countDown();
        }
    });
    processor.input().send(MessageBuilder.withPayload("{\"foo\":\"fooTESTing\"}").setHeader("contentType", "application/json").build());
    inboundChannel2.input().send(MessageBuilder.withPayload("{\"bar\":\"bartestING\"}").setHeader("contentType", "application/json").build());
    assertThat(latch.await(1, TimeUnit.SECONDS));
    context.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Processor(org.springframework.cloud.stream.messaging.Processor) MessageHandler(org.springframework.messaging.MessageHandler) MessagingException(org.springframework.messaging.MessagingException) CountDownLatch(java.util.concurrent.CountDownLatch) SubscribableChannel(org.springframework.messaging.SubscribableChannel) Test(org.junit.Test)

Example 13 with Processor

use of org.springframework.cloud.stream.messaging.Processor 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 14 with Processor

use of org.springframework.cloud.stream.messaging.Processor 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)

Example 15 with Processor

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

the class StreamListenerMethodWithReturnMessageTests method testReturnMessage.

@Test
@SuppressWarnings("unchecked")
public void testReturnMessage() throws Exception {
    ConfigurableApplicationContext context = SpringApplication.run(this.configClass, "--server.port=0", "--spring.jmx.enabled=false");
    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());
    TestPojoWithMessageReturn testPojoWithMessageReturn = context.getBean(TestPojoWithMessageReturn.class);
    Assertions.assertThat(testPojoWithMessageReturn.receivedPojos).hasSize(1);
    Assertions.assertThat(testPojoWithMessageReturn.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()).contains("barbar" + id);
    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

Processor (org.springframework.cloud.stream.messaging.Processor)26 Message (org.springframework.messaging.Message)20 MessageCollector (org.springframework.cloud.stream.test.binder.MessageCollector)19 Test (org.junit.Test)14 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)12 CountDownLatch (java.util.concurrent.CountDownLatch)2 MessageHandler (org.springframework.messaging.MessageHandler)2 MessagingException (org.springframework.messaging.MessagingException)2 SubscribableChannel (org.springframework.messaging.SubscribableChannel)2 Field (java.lang.reflect.Field)1 Map (java.util.Map)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 AggregateApplication (org.springframework.cloud.stream.aggregate.AggregateApplication)1 AggregateApplicationBuilder (org.springframework.cloud.stream.aggregate.AggregateApplicationBuilder)1 BindableProxyFactory (org.springframework.cloud.stream.binding.BindableProxyFactory)1 Source (org.springframework.cloud.stream.messaging.Source)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1