Search in sources :

Example 1 with Processor

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

the class StreamListenerHandlerMethodTests method testMethodWithMultipleOutputParameters.

@Test
public void testMethodWithMultipleOutputParameters() throws Exception {
    ConfigurableApplicationContext context = SpringApplication.run(TestMethodWithMultipleOutputParameters.class, "--server.port=0", "--spring.jmx.enabled=false");
    Processor processor = context.getBean(Processor.class);
    StreamListenerTestUtils.FooOutboundChannel1 source2 = context.getBean(StreamListenerTestUtils.FooOutboundChannel1.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("testing"), "Assert failed");
            Assert.isTrue(message.getHeaders().get("output").equals("output2"), "Assert failed");
            latch.countDown();
        }
    });
    ((SubscribableChannel) source2.output()).subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            Assert.isTrue(message.getPayload().equals("TESTING"), "Assert failed");
            Assert.isTrue(message.getHeaders().get("output").equals("output1"), "Assert failed");
            latch.countDown();
        }
    });
    processor.input().send(MessageBuilder.withPayload("testING").setHeader("output", "output1").build());
    processor.input().send(MessageBuilder.withPayload("TESTing").setHeader("output", "output2").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 2 with Processor

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

the class StreamListenerHandlerMethodTests method testMethodHeadersNotPropagatged.

@SuppressWarnings("unchecked")
@Test
public void testMethodHeadersNotPropagatged() throws Exception {
    ConfigurableApplicationContext context = SpringApplication.run(TestMethodHeadersNotPropagated.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")).isNull();
    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 3 with Processor

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

the class StreamListenerMessageArgumentTests method testMessageArgument.

@Test
@SuppressWarnings("unchecked")
public void testMessageArgument() throws Exception {
    ConfigurableApplicationContext context = SpringApplication.run(this.configClass, "--server.port=0", "--spring.cloud.stream.bindings.output.contentType=text/plain", "--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("barbar" + id).setHeader("contentType", "text/plain").build());
    TestPojoWithMessageArgument testPojoWithMessageArgument = context.getBean(TestPojoWithMessageArgument.class);
    assertThat(testPojoWithMessageArgument.receivedMessages).hasSize(1);
    assertThat(testPojoWithMessageArgument.receivedMessages.get(0).getPayload()).isEqualTo("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)

Example 4 with Processor

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

the class StreamListenerMethodWithReturnValueTests method testReturn.

@Test
@SuppressWarnings("unchecked")
public void testReturn() 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());
    Message<String> message = (Message<String>) collector.forChannel(processor.output()).poll(1, TimeUnit.SECONDS);
    TestStringProcessor testStringProcessor = context.getBean(TestStringProcessor.class);
    Assertions.assertThat(testStringProcessor.receivedPojos).hasSize(1);
    Assertions.assertThat(testStringProcessor.receivedPojos.get(0)).hasFieldOrPropertyWithValue("foo", "barbar" + id);
    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)

Example 5 with Processor

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

the class AggregationTest method testBindableProxyFactoryCaching.

@Test
public void testBindableProxyFactoryCaching() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MockBinderRegistryConfiguration.class, TestSource2.class, TestProcessor.class);
    Map<String, BindableProxyFactory> factories = context.getBeansOfType(BindableProxyFactory.class);
    assertThat(factories).hasSize(2);
    Map<String, Source> sources = context.getBeansOfType(Source.class);
    assertThat(sources).hasSize(1);
    for (Source source : sources.values()) {
        source.output();
    }
    Map<String, FooSource> fooSources = context.getBeansOfType(FooSource.class);
    assertThat(fooSources).hasSize(1);
    for (FooSource source : fooSources.values()) {
        source.output();
    }
    Map<String, Processor> processors = context.getBeansOfType(Processor.class);
    assertThat(processors).hasSize(1);
    for (Processor processor : processors.values()) {
        processor.input();
        processor.output();
    }
    for (BindableProxyFactory factory : factories.values()) {
        Field field = ReflectionUtils.findField(BindableProxyFactory.class, "targetCache");
        ReflectionUtils.makeAccessible(field);
        Map<?, ?> targetCache = (Map<?, ?>) ReflectionUtils.getField(field, factory);
        if (factory.getObjectType() == Source.class) {
            assertThat(targetCache).hasSize(1);
        }
        if (factory.getObjectType() == FooSource.class) {
            assertThat(targetCache).hasSize(1);
        } else if (factory.getObjectType() == Processor.class) {
            assertThat(targetCache).hasSize(2);
        } else {
            Assert.fail("Found unexpected type");
        }
    }
    context.close();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Processor(org.springframework.cloud.stream.messaging.Processor) Source(org.springframework.cloud.stream.messaging.Source) Field(java.lang.reflect.Field) BindableProxyFactory(org.springframework.cloud.stream.binding.BindableProxyFactory) Map(java.util.Map) 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