Align StreamListener method/parameter mappings
- Use the same mapping of method/parameters for both declarative and message handler Stream Listeners
- Declarative mode of the StreamListener method is determined if at least one of the the method parameters is annotated with @Input or @Output with either bound elements (e.g. channels) or conversion targets from bound elements via a registered StreamListenerParameterAdapter
- If the method is non-declarative then it is considered to be in message handler mode
- Declarative mode now accepts @Output annotation at method level as well along with @SendTo
- Declarative mode also accepts @Input annotation value at the method level via @StreamListener valuue.
- Message handler mode accepts @Input annotation value at the method parameter level and @Output annotation at the method annotation level
- Both @Output and @SendTo annotations are supported to specify the outbound target value while @SendTo is allowed only as a method level annotation
- Add assertions on allowable use of method and parameter annotations
- Add parameterized tests to cover all possible cases
- Message handler tests
- Reactor and RxJava tests
Resolves #664
Add more error handling and simplify the usage patterns
Address review comments
- Support @Input/@Output only for declarative StreamListener methods
- Support multiple @Output only when there is no return type in the StreamListener method
- Add/Update tests
WIP
Refactor PR based on the review comments
This commit is contained in:
committed by
Marius Bogoevici
parent
8595d094c9
commit
453a9b0c10
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.cloud.stream.config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.cloud.stream.annotation.EnableBinding;
|
||||
import org.springframework.cloud.stream.annotation.Input;
|
||||
import org.springframework.cloud.stream.annotation.StreamListener;
|
||||
import org.springframework.cloud.stream.messaging.Processor;
|
||||
import org.springframework.cloud.stream.messaging.Sink;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.messaging.handler.annotation.Header;
|
||||
import org.springframework.messaging.handler.annotation.Headers;
|
||||
import org.springframework.messaging.handler.annotation.Payload;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.springframework.cloud.stream.binding.StreamListenerErrorMessages.INVALID_MESSAGE_HANDLER_METHOD_PARAMS;
|
||||
|
||||
/**
|
||||
* @author Marius Bogoevici
|
||||
* @author Ilayaperumal Gopinathan
|
||||
*/
|
||||
public class StreamListenerAnnotatedMethodArgumentsTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testAnnotatedArguments() throws Exception {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(TestPojoWithAnnotatedArguments1.class,
|
||||
"--server.port=0");
|
||||
|
||||
TestPojoWithAnnotatedArguments testPojoWithAnnotatedArguments = context
|
||||
.getBean(TestPojoWithAnnotatedArguments.class);
|
||||
Sink sink = context.getBean(Sink.class);
|
||||
String id = UUID.randomUUID().toString();
|
||||
sink.input().send(MessageBuilder.withPayload("{\"foo\":\"barbar" + id + "\"}")
|
||||
.setHeader("contentType", "application/json").setHeader("testHeader", "testValue").build());
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedArguments).hasSize(3);
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedArguments.get(0)).isInstanceOf(StreamListenerTestInterfaces.FooPojo.class);
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedArguments.get(0)).hasFieldOrPropertyWithValue("foo",
|
||||
"barbar" + id);
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedArguments.get(1)).isInstanceOf(Map.class);
|
||||
assertThat((Map<String, String>) testPojoWithAnnotatedArguments.receivedArguments.get(1))
|
||||
.containsEntry(MessageHeaders.CONTENT_TYPE, "application/json");
|
||||
assertThat((Map<String, String>) testPojoWithAnnotatedArguments.receivedArguments.get(1))
|
||||
.containsEntry("testHeader", "testValue");
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedArguments.get(2)).isEqualTo("application/json");
|
||||
context.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInputAnnotationAtMethodParameter() throws Exception {
|
||||
try {
|
||||
SpringApplication.run(TestPojoWithAnnotatedArguments2.class, "--server.port=0");
|
||||
fail("Exception expected: "+ INVALID_MESSAGE_HANDLER_METHOD_PARAMS);
|
||||
}
|
||||
catch (BeanCreationException e) {
|
||||
assertThat(e.getCause().getMessage()).contains(INVALID_MESSAGE_HANDLER_METHOD_PARAMS);
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@EnableAutoConfiguration
|
||||
public static class TestPojoWithAnnotatedArguments1 extends TestPojoWithAnnotatedArguments {
|
||||
|
||||
@StreamListener(Processor.INPUT)
|
||||
public void receive(@Payload StreamListenerTestInterfaces.FooPojo fooPojo,
|
||||
@Headers Map<String, Object> headers,
|
||||
@Header(MessageHeaders.CONTENT_TYPE) String contentType) {
|
||||
this.receivedArguments.add(fooPojo);
|
||||
this.receivedArguments.add(headers);
|
||||
this.receivedArguments.add(contentType);
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@EnableAutoConfiguration
|
||||
public static class TestPojoWithAnnotatedArguments2 extends TestPojoWithAnnotatedArguments {
|
||||
|
||||
@StreamListener
|
||||
public void receive(@Input(Processor.INPUT) @Payload StreamListenerTestInterfaces.FooPojo fooPojo,
|
||||
@Headers Map<String, Object> headers,
|
||||
@Header(MessageHeaders.CONTENT_TYPE) String contentType) {
|
||||
this.receivedArguments.add(fooPojo);
|
||||
this.receivedArguments.add(headers);
|
||||
this.receivedArguments.add(contentType);
|
||||
}
|
||||
}
|
||||
|
||||
public static class TestPojoWithAnnotatedArguments {
|
||||
List<Object> receivedArguments = new ArrayList<>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.cloud.stream.config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.cloud.stream.annotation.EnableBinding;
|
||||
import org.springframework.cloud.stream.annotation.StreamListener;
|
||||
import org.springframework.cloud.stream.messaging.Sink;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Marius Bogoevici
|
||||
* @author Ilayaperumal Gopinathan
|
||||
*/
|
||||
public class StreamListenerContentTypeConversionTests {
|
||||
|
||||
@Test
|
||||
public void testContentTypeConversion() throws Exception {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(TestSink1.class,
|
||||
"--server.port=0");
|
||||
@SuppressWarnings("unchecked")
|
||||
TestSink1 testSink = context.getBean(TestSink1.class);
|
||||
Sink sink = context.getBean(Sink.class);
|
||||
String id = UUID.randomUUID().toString();
|
||||
sink.input().send(
|
||||
MessageBuilder.withPayload("{\"foo\":\"barbar" + id + "\"}")
|
||||
.setHeader("contentType", "application/json").build());
|
||||
assertThat(testSink.latch.await(10, TimeUnit.SECONDS));
|
||||
assertThat(testSink.receivedArguments).hasSize(1);
|
||||
assertThat(testSink.receivedArguments.get(0)).hasFieldOrPropertyWithValue("foo",
|
||||
"barbar" + id);
|
||||
context.close();
|
||||
}
|
||||
|
||||
@EnableBinding(Sink.class)
|
||||
@EnableAutoConfiguration
|
||||
public static class TestSink1 {
|
||||
|
||||
List<StreamListenerTestInterfaces.FooPojo> receivedArguments = new ArrayList<>();
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
|
||||
@StreamListener(Sink.INPUT)
|
||||
public void receive(StreamListenerTestInterfaces.FooPojo fooPojo) {
|
||||
this.receivedArguments.add(fooPojo);
|
||||
this.latch.countDown();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.cloud.stream.config;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.cloud.stream.annotation.EnableBinding;
|
||||
import org.springframework.cloud.stream.annotation.StreamListener;
|
||||
import org.springframework.cloud.stream.messaging.Processor;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.messaging.Message;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* @author Marius Bogoevici
|
||||
* @author Ilayaperumal Gopinathan
|
||||
*/
|
||||
public class StreamListenerDuplicateMappingTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testDuplicateMapping() throws Exception {
|
||||
try {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(TestDuplicateMapping1.class,
|
||||
"--server.port=0");
|
||||
fail("Exception expected on duplicate mapping");
|
||||
}
|
||||
catch (BeanCreationException e) {
|
||||
assertThat(e.getCause().getMessage()).startsWith("Duplicate @StreamListener mapping");
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@EnableAutoConfiguration
|
||||
public static class TestDuplicateMapping1 {
|
||||
|
||||
@StreamListener(Processor.INPUT)
|
||||
public void receive(Message<String> fooMessage) {
|
||||
}
|
||||
|
||||
@StreamListener(Processor.INPUT)
|
||||
public void receive2(Message<String> fooMessage) {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.cloud.stream.config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.cloud.stream.annotation.EnableBinding;
|
||||
import org.springframework.cloud.stream.annotation.Output;
|
||||
import org.springframework.cloud.stream.annotation.StreamListener;
|
||||
import org.springframework.cloud.stream.messaging.Processor;
|
||||
import org.springframework.cloud.stream.test.binder.MessageCollector;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.messaging.handler.annotation.SendTo;
|
||||
import org.springframework.util.MimeType;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Marius Bogoevici
|
||||
* @author Ilayaperumal Gopinathan
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class StreamListenerHandlerBeanTests {
|
||||
|
||||
private Class<?> configClass;
|
||||
|
||||
public StreamListenerHandlerBeanTests(Class<?> configClass) {
|
||||
this.configClass = configClass;
|
||||
}
|
||||
|
||||
@Parameterized.Parameters
|
||||
public static Collection InputConfigs() {
|
||||
return Arrays.asList(new Class[] { TestHandlerBean1.class, TestHandlerBean2.class });
|
||||
}
|
||||
|
||||
@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);
|
||||
assertThat(handlerBean.receivedPojos).hasSize(1);
|
||||
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();
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@EnableAutoConfiguration
|
||||
public static class TestHandlerBean1 {
|
||||
|
||||
@Bean
|
||||
public HandlerBean1 handlerBean() {
|
||||
return new HandlerBean1();
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@EnableAutoConfiguration
|
||||
public static class TestHandlerBean2 {
|
||||
|
||||
@Bean
|
||||
public HandlerBean2 handlerBean() {
|
||||
return new HandlerBean2();
|
||||
}
|
||||
}
|
||||
|
||||
public static class HandlerBean1 extends HandlerBean {
|
||||
|
||||
@StreamListener(Processor.INPUT)
|
||||
@SendTo(Processor.OUTPUT)
|
||||
public StreamListenerTestInterfaces.BarPojo receive(StreamListenerTestInterfaces.FooPojo fooMessage) {
|
||||
this.receivedPojos.add(fooMessage);
|
||||
StreamListenerTestInterfaces.BarPojo barPojo = new StreamListenerTestInterfaces.BarPojo();
|
||||
barPojo.setBar(fooMessage.getFoo());
|
||||
return barPojo;
|
||||
}
|
||||
}
|
||||
|
||||
public static class HandlerBean2 extends HandlerBean {
|
||||
|
||||
@StreamListener(Processor.INPUT)
|
||||
@Output(Processor.OUTPUT)
|
||||
public StreamListenerTestInterfaces.BarPojo receive(StreamListenerTestInterfaces.FooPojo fooMessage) {
|
||||
this.receivedPojos.add(fooMessage);
|
||||
StreamListenerTestInterfaces.BarPojo barPojo = new StreamListenerTestInterfaces.BarPojo();
|
||||
barPojo.setBar(fooMessage.getFoo());
|
||||
return barPojo;
|
||||
}
|
||||
}
|
||||
|
||||
public static class HandlerBean {
|
||||
|
||||
List<StreamListenerTestInterfaces.FooPojo> receivedPojos = new ArrayList<>();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,407 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.stream.config;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.cloud.stream.annotation.EnableBinding;
|
||||
import org.springframework.cloud.stream.annotation.Input;
|
||||
import org.springframework.cloud.stream.annotation.Output;
|
||||
import org.springframework.cloud.stream.annotation.StreamListener;
|
||||
import org.springframework.cloud.stream.messaging.Processor;
|
||||
import org.springframework.cloud.stream.messaging.Sink;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.MessageHandler;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
import org.springframework.messaging.SubscribableChannel;
|
||||
import org.springframework.messaging.handler.annotation.Payload;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.springframework.cloud.stream.binding.StreamListenerErrorMessages.AMBIGUOUS_MESSAGE_HANDLER_METHOD_ARGUMENTS;
|
||||
import static org.springframework.cloud.stream.binding.StreamListenerErrorMessages.INPUT_AT_STREAM_LISTENER;
|
||||
import static org.springframework.cloud.stream.binding.StreamListenerErrorMessages.INVALID_INBOUND_NAME;
|
||||
import static org.springframework.cloud.stream.binding.StreamListenerErrorMessages.INVALID_MESSAGE_HANDLER_METHOD_PARAMS;
|
||||
import static org.springframework.cloud.stream.binding.StreamListenerErrorMessages.INVALID_OUTBOUND_NAME;
|
||||
import static org.springframework.cloud.stream.binding.StreamListenerErrorMessages.INVALID_OUTPUT_VALUES;
|
||||
import static org.springframework.cloud.stream.binding.StreamListenerErrorMessages.NO_INPUT_DESTINATION;
|
||||
import static org.springframework.cloud.stream.binding.StreamListenerErrorMessages.RETURN_TYPE_MULTIPLE_OUTBOUND_SPECIFIED;
|
||||
import static org.springframework.cloud.stream.binding.StreamListenerErrorMessages.RETURN_TYPE_NO_OUTBOUND_SPECIFIED;
|
||||
import static org.springframework.cloud.stream.binding.StreamListenerErrorMessages.TARGET_BEAN_NOT_EXISTS;
|
||||
|
||||
/**
|
||||
* @author Marius Bogoevici
|
||||
* @author Ilayaperumal Gopinathan
|
||||
*/
|
||||
public class StreamListenerHandlerMethodTests {
|
||||
|
||||
@Test
|
||||
public void testMethodWIthInputOnStreamListener() throws Exception {
|
||||
try {
|
||||
SpringApplication.run(TestMethodWIthInputOnStreamListener.class, "--server.port=0");
|
||||
fail("Exception expected: "+ INPUT_AT_STREAM_LISTENER);
|
||||
}
|
||||
catch (BeanCreationException e) {
|
||||
assertThat(e.getCause().getMessage()).contains(INPUT_AT_STREAM_LISTENER);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReturnTypeWithMultipleOutput() throws Exception {
|
||||
try {
|
||||
SpringApplication.run(TestReturnTypeWithMultipleOutput.class, "--server.port=0");
|
||||
fail("Exception expected: "+ RETURN_TYPE_MULTIPLE_OUTBOUND_SPECIFIED);
|
||||
}
|
||||
catch (BeanCreationException e) {
|
||||
assertThat(e.getCause().getMessage()).contains(RETURN_TYPE_MULTIPLE_OUTBOUND_SPECIFIED);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReturnTypeWithNoOutput() throws Exception {
|
||||
try {
|
||||
SpringApplication.run(TestReturnTypeWithNoOutput.class, "--server.port=0");
|
||||
fail("Exception expected: " + RETURN_TYPE_NO_OUTBOUND_SPECIFIED);
|
||||
}
|
||||
catch (BeanCreationException e) {
|
||||
assertThat(e.getCause().getMessage()).contains(RETURN_TYPE_NO_OUTBOUND_SPECIFIED);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMethodInputAnnotationWithNoValue() throws Exception {
|
||||
try {
|
||||
SpringApplication.run(TestMethodInputAnnotationWithNoValue.class, "--server.port=0");
|
||||
fail("Exception expected: "+ INVALID_INBOUND_NAME);
|
||||
}
|
||||
catch (BeanCreationException e) {
|
||||
assertThat(e.getCause().getMessage()).contains(INVALID_INBOUND_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMethodOutputAnnotationWithNoValue() throws Exception {
|
||||
try {
|
||||
SpringApplication.run(TestMethodOutputAnnotationWithNoValue.class, "--server.port=0");
|
||||
fail("Exception expected: "+ INVALID_OUTBOUND_NAME);
|
||||
}
|
||||
catch (BeanCreationException e) {
|
||||
assertThat(e.getCause().getMessage()).contains(INVALID_OUTBOUND_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMethodInvalidInboundName() throws Exception {
|
||||
try {
|
||||
SpringApplication.run(TestMethodInvalidInboundName.class, "--server.port=0");
|
||||
fail("Exception expected on using invalid inbound name");
|
||||
}
|
||||
catch (BeanCreationException e) {
|
||||
assertThat(e.getCause().getMessage()).contains(TARGET_BEAN_NOT_EXISTS + ": invalid");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMethodInvalidOutboundName() throws Exception {
|
||||
try {
|
||||
SpringApplication.run(TestMethodInvalidOutboundName.class, "--server.port=0");
|
||||
fail("Exception expected on using invalid outbound name");
|
||||
}
|
||||
catch (BeanCreationException e) {
|
||||
assertThat(e.getCause().getMessage()).contains(TARGET_BEAN_NOT_EXISTS + ": invalid");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAmbiguousMethodArguments1() throws Exception {
|
||||
try {
|
||||
SpringApplication.run(TestAmbiguousMethodArguments1.class, "--server.port=0");
|
||||
fail("Exception expected: "+ AMBIGUOUS_MESSAGE_HANDLER_METHOD_ARGUMENTS);
|
||||
}
|
||||
catch (BeanCreationException e) {
|
||||
assertThat(e.getCause().getMessage()).contains(AMBIGUOUS_MESSAGE_HANDLER_METHOD_ARGUMENTS);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAmbiguousMethodArguments2() throws Exception {
|
||||
try {
|
||||
SpringApplication.run(TestAmbiguousMethodArguments2.class, "--server.port=0");
|
||||
fail("Exception expected:" + AMBIGUOUS_MESSAGE_HANDLER_METHOD_ARGUMENTS);
|
||||
}
|
||||
catch (BeanCreationException e) {
|
||||
assertThat(e.getCause().getMessage()).contains(AMBIGUOUS_MESSAGE_HANDLER_METHOD_ARGUMENTS);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMethodWithInputAsMethodAndParameter() throws Exception {
|
||||
try {
|
||||
SpringApplication.run(TestMethodWithInputAsMethodAndParameter.class, "--server.port=0");
|
||||
fail("Exception expected: " + INVALID_MESSAGE_HANDLER_METHOD_PARAMS);
|
||||
}
|
||||
catch (BeanCreationException e) {
|
||||
assertThat(e.getCause().getMessage()).contains(INVALID_MESSAGE_HANDLER_METHOD_PARAMS);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMethodWithOutputAsMethodAndParameter() throws Exception {
|
||||
try {
|
||||
SpringApplication.run(TestMethodWithOutputAsMethodAndParameter.class, "--server.port=0");
|
||||
fail("Exception expected:" + INVALID_OUTPUT_VALUES);
|
||||
}
|
||||
catch (BeanCreationException e) {
|
||||
assertThat(e.getCause().getMessage()).startsWith(INVALID_OUTPUT_VALUES);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMethodWithoutInput() throws Exception {
|
||||
try {
|
||||
SpringApplication.run(TestMethodWithoutInput.class, "--server.port=0");
|
||||
fail("Exception expected when inbound target is not set");
|
||||
}
|
||||
catch (BeanCreationException e) {
|
||||
assertThat(e.getCause().getMessage()).contains(NO_INPUT_DESTINATION);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMethodWithMultipleInputParameters() throws Exception {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(TestMethodWithMultipleInputParameters.class, "--server.port=0");
|
||||
Processor processor = context.getBean(Processor.class);
|
||||
StreamListenerTestInterfaces.FooInboundChannel1 inboundChannel2 = context.getBean(StreamListenerTestInterfaces.FooInboundChannel1.class);
|
||||
String id = UUID.randomUUID().toString();
|
||||
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"));
|
||||
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();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMethodWithMultipleOutputParameters() throws Exception {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(TestMethodWithMultipleOutputParameters.class, "--server.port=0");
|
||||
Processor processor = context.getBean(Processor.class);
|
||||
String id = UUID.randomUUID().toString();
|
||||
StreamListenerTestInterfaces.FooOutboundChannel1 source2 = context.getBean(StreamListenerTestInterfaces.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.isTrue(message.getHeaders().get("output").equals("output2"));
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
((SubscribableChannel) source2.output()).subscribe(new MessageHandler() {
|
||||
@Override
|
||||
public void handleMessage(Message<?> message) throws MessagingException {
|
||||
Assert.isTrue(message.getPayload().equals("TESTING"));
|
||||
Assert.isTrue(message.getHeaders().get("output").equals("output1"));
|
||||
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();
|
||||
}
|
||||
|
||||
@EnableBinding({Processor.class, StreamListenerTestInterfaces.FooOutboundChannel1.class})
|
||||
@EnableAutoConfiguration
|
||||
public static class TestMethodWithMultipleOutputParameters {
|
||||
|
||||
@StreamListener
|
||||
public void receive(@Input(Processor.INPUT) SubscribableChannel input, @Output(Processor.OUTPUT) final MessageChannel output1,
|
||||
@Output(StreamListenerTestInterfaces.FooOutboundChannel1.OUTPUT) final MessageChannel output2) {
|
||||
input.subscribe(new MessageHandler() {
|
||||
@Override
|
||||
public void handleMessage(Message<?> message) throws MessagingException {
|
||||
if (message.getHeaders().get("output").equals("output1")) {
|
||||
output1.send(org.springframework.messaging.support.MessageBuilder.withPayload(message.getPayload().toString().toUpperCase()).build());
|
||||
}
|
||||
else if (message.getHeaders().get("output").equals("output2")) {
|
||||
output2.send(org.springframework.messaging.support.MessageBuilder.withPayload(message.getPayload().toString().toLowerCase()).build());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding({Sink.class})
|
||||
@EnableAutoConfiguration
|
||||
public static class TestMethodWithoutInput {
|
||||
|
||||
@StreamListener
|
||||
public void receive(StreamListenerTestInterfaces.FooPojo fooPojo) {
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding({Sink.class})
|
||||
@EnableAutoConfiguration
|
||||
public static class TestMethodWIthInputOnStreamListener {
|
||||
|
||||
@StreamListener
|
||||
@Input(Sink.INPUT)
|
||||
public void receive(StreamListenerTestInterfaces.FooPojo fooPojo) {
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding({Sink.class})
|
||||
@EnableAutoConfiguration
|
||||
public static class TestAmbiguousMethodArguments1 {
|
||||
|
||||
@StreamListener(Processor.INPUT)
|
||||
public void receive(@Payload StreamListenerTestInterfaces.FooPojo fooPojo, String value) {
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding({Sink.class})
|
||||
@EnableAutoConfiguration
|
||||
public static class TestAmbiguousMethodArguments2 {
|
||||
|
||||
@StreamListener(Processor.INPUT)
|
||||
public void receive(@Payload StreamListenerTestInterfaces.FooPojo fooPojo, @Payload StreamListenerTestInterfaces.BarPojo barPojo) {
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding({Processor.class, StreamListenerTestInterfaces.FooOutboundChannel1.class})
|
||||
@EnableAutoConfiguration
|
||||
public static class TestReturnTypeWithMultipleOutput {
|
||||
|
||||
@StreamListener
|
||||
public String receive(@Input(Processor.INPUT) SubscribableChannel input1, @Output(Processor.OUTPUT) MessageChannel output1,
|
||||
@Output(StreamListenerTestInterfaces.FooOutboundChannel1.OUTPUT) MessageChannel output2) {
|
||||
return "foo";
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding({Processor.class, StreamListenerTestInterfaces.FooOutboundChannel1.class})
|
||||
@EnableAutoConfiguration
|
||||
public static class TestReturnTypeWithNoOutput {
|
||||
|
||||
@StreamListener
|
||||
public String receive(@Input(Processor.INPUT) SubscribableChannel input1) {
|
||||
return "foo";
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding({Processor.class})
|
||||
@EnableAutoConfiguration
|
||||
public static class TestMethodInputAnnotationWithNoValue {
|
||||
|
||||
@StreamListener
|
||||
public void receive(@Input SubscribableChannel input) {
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding({Processor.class})
|
||||
@EnableAutoConfiguration
|
||||
public static class TestMethodOutputAnnotationWithNoValue {
|
||||
|
||||
@StreamListener
|
||||
public void receive(@Input(Processor.OUTPUT) SubscribableChannel input, @Output MessageChannel output) {
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding({Sink.class})
|
||||
@EnableAutoConfiguration
|
||||
public static class TestMethodInvalidInboundName {
|
||||
|
||||
@StreamListener
|
||||
public void receive(@Input("invalid") SubscribableChannel input) {
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding({Processor.class})
|
||||
@EnableAutoConfiguration
|
||||
public static class TestMethodInvalidOutboundName {
|
||||
|
||||
@StreamListener
|
||||
public void receive(@Input(Processor.INPUT) SubscribableChannel input, @Output("invalid") MessageChannel output) {
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding({Sink.class})
|
||||
@EnableAutoConfiguration
|
||||
public static class TestMethodWithInputAsMethodAndParameter {
|
||||
|
||||
@StreamListener
|
||||
public void receive(@Input(Sink.INPUT) StreamListenerTestInterfaces.FooPojo fooPojo) {
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding({Processor.class, StreamListenerTestInterfaces.FooOutboundChannel1.class})
|
||||
@EnableAutoConfiguration
|
||||
public static class TestMethodWithOutputAsMethodAndParameter {
|
||||
|
||||
@StreamListener
|
||||
@Output(StreamListenerTestInterfaces.FooOutboundChannel1.OUTPUT)
|
||||
public void receive(@Input(Processor.INPUT) SubscribableChannel input, @Output(Processor.OUTPUT) final MessageChannel output1) {
|
||||
input.subscribe(new MessageHandler() {
|
||||
@Override
|
||||
public void handleMessage(Message<?> message) throws MessagingException {
|
||||
output1.send(org.springframework.messaging.support.MessageBuilder.withPayload(message.getPayload().toString().toUpperCase()).build());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding({Processor.class, StreamListenerTestInterfaces.FooInboundChannel1.class})
|
||||
@EnableAutoConfiguration
|
||||
public static class TestMethodWithMultipleInputParameters {
|
||||
|
||||
@StreamListener
|
||||
public void receive(@Input(Processor.INPUT) SubscribableChannel input1, @Input(StreamListenerTestInterfaces.FooInboundChannel1.INPUT) SubscribableChannel input2,
|
||||
final @Output(Processor.OUTPUT) MessageChannel output) {
|
||||
input1.subscribe(new MessageHandler() {
|
||||
@Override
|
||||
public void handleMessage(Message<?> message) throws MessagingException {
|
||||
output.send(org.springframework.messaging.support.MessageBuilder.withPayload(message.getPayload().toString().toUpperCase()).build());
|
||||
}
|
||||
});
|
||||
input2.subscribe(new MessageHandler() {
|
||||
@Override
|
||||
public void handleMessage(Message<?> message) throws MessagingException {
|
||||
output.send(org.springframework.messaging.support.MessageBuilder.withPayload(message.getPayload().toString().toUpperCase()).build());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.cloud.stream.config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.cloud.stream.annotation.EnableBinding;
|
||||
import org.springframework.cloud.stream.annotation.Output;
|
||||
import org.springframework.cloud.stream.annotation.StreamListener;
|
||||
import org.springframework.cloud.stream.messaging.Processor;
|
||||
import org.springframework.cloud.stream.test.binder.MessageCollector;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.handler.annotation.SendTo;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Marius Bogoevici
|
||||
* @author Ilayaperumal Gopinathan
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class StreamListenerMessageArgumentTests {
|
||||
|
||||
private Class<?> configClass;
|
||||
|
||||
public StreamListenerMessageArgumentTests(Class<?> configClass) {
|
||||
this.configClass = configClass;
|
||||
}
|
||||
|
||||
@Parameterized.Parameters
|
||||
public static Collection InputConfigs() {
|
||||
return Arrays.asList(new Class[] {TestPojoWithMessageArgument1.class, TestPojoWithMessageArgument2.class});
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testMessageArgument() throws Exception {
|
||||
ConfigurableApplicationContext context = SpringApplication
|
||||
.run(this.configClass, "--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("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<StreamListenerTestInterfaces.BarPojo> message = (Message<StreamListenerTestInterfaces.BarPojo>) collector
|
||||
.forChannel(processor.output()).poll(1, TimeUnit.SECONDS);
|
||||
assertThat(message).isNotNull();
|
||||
assertThat(message.getPayload().getBar()).isEqualTo("barbar" + id);
|
||||
context.close();
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@EnableAutoConfiguration
|
||||
public static class TestPojoWithMessageArgument1 extends TestPojoWithMessageArgument {
|
||||
|
||||
@StreamListener(Processor.INPUT)
|
||||
@SendTo(Processor.OUTPUT)
|
||||
public StreamListenerTestInterfaces.BarPojo receive(Message<String> fooMessage) {
|
||||
this.receivedMessages.add(fooMessage);
|
||||
StreamListenerTestInterfaces.BarPojo barPojo = new StreamListenerTestInterfaces.BarPojo();
|
||||
barPojo.setBar(fooMessage.getPayload());
|
||||
return barPojo;
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@EnableAutoConfiguration
|
||||
public static class TestPojoWithMessageArgument2 extends TestPojoWithMessageArgument {
|
||||
|
||||
@StreamListener(Processor.INPUT)
|
||||
@Output(Processor.OUTPUT)
|
||||
public StreamListenerTestInterfaces.BarPojo receive(Message<String> fooMessage) {
|
||||
this.receivedMessages.add(fooMessage);
|
||||
StreamListenerTestInterfaces.BarPojo barPojo = new StreamListenerTestInterfaces.BarPojo();
|
||||
barPojo.setBar(fooMessage.getPayload());
|
||||
return barPojo;
|
||||
}
|
||||
}
|
||||
|
||||
public static class TestPojoWithMessageArgument {
|
||||
List<Message<String>> receivedMessages = new ArrayList<>();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.cloud.stream.config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Suite;
|
||||
import org.junit.runners.model.InitializationError;
|
||||
import org.junit.runners.model.RunnerBuilder;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.cloud.stream.annotation.EnableBinding;
|
||||
import org.springframework.cloud.stream.annotation.Output;
|
||||
import org.springframework.cloud.stream.annotation.StreamListener;
|
||||
import org.springframework.cloud.stream.messaging.Processor;
|
||||
import org.springframework.cloud.stream.test.binder.MessageCollector;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.messaging.handler.annotation.SendTo;
|
||||
import org.springframework.util.MimeType;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Marius Bogoevici
|
||||
* @author Ilayaperumal Gopinathan
|
||||
*/
|
||||
@RunWith(StreamListenerMethodReturnWithConversionTests.class)
|
||||
@Suite.SuiteClasses({StreamListenerMethodReturnWithConversionTests.TestReturnConversion.class,
|
||||
StreamListenerMethodReturnWithConversionTests.TestReturnNoConversion.class})
|
||||
public class StreamListenerMethodReturnWithConversionTests extends Suite {
|
||||
|
||||
public StreamListenerMethodReturnWithConversionTests(Class<?> klass, RunnerBuilder builder) throws InitializationError {
|
||||
super(klass, builder);
|
||||
}
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
public static class TestReturnConversion {
|
||||
|
||||
private Class<?> configClass;
|
||||
|
||||
public TestReturnConversion(Class<?> configClass) {
|
||||
this.configClass = configClass;
|
||||
}
|
||||
|
||||
@Parameterized.Parameters
|
||||
public static Collection InputConfigs() {
|
||||
return Arrays.asList(new Class[] {TestPojoWithMimeType1.class, TestPojoWithMimeType2.class});
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testReturnConversion() 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());
|
||||
TestPojoWithMimeType testPojoWithMimeType = context.getBean(TestPojoWithMimeType.class);
|
||||
assertThat(testPojoWithMimeType.receivedPojos).hasSize(1);
|
||||
assertThat(testPojoWithMimeType.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();
|
||||
}
|
||||
}
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
public static class TestReturnNoConversion {
|
||||
|
||||
private Class<?> configClass;
|
||||
|
||||
public TestReturnNoConversion(Class<?> configClass) {
|
||||
this.configClass = configClass;
|
||||
}
|
||||
|
||||
@Parameterized.Parameters
|
||||
public static Collection InputConfigs() {
|
||||
return Arrays.asList(new Class[] {TestPojoWithMimeType1.class, TestPojoWithMimeType2.class});
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testReturnNoConversion() throws Exception {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(this.configClass, "--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());
|
||||
TestPojoWithMimeType testPojoWithMimeType = context.getBean(TestPojoWithMimeType.class);
|
||||
assertThat(testPojoWithMimeType.receivedPojos).hasSize(1);
|
||||
assertThat(testPojoWithMimeType.receivedPojos.get(0)).hasFieldOrPropertyWithValue("foo", "barbar" + id);
|
||||
Message<StreamListenerTestInterfaces.BarPojo> message = (Message<StreamListenerTestInterfaces.BarPojo>) collector.forChannel(processor.output()).poll(1,
|
||||
TimeUnit.SECONDS);
|
||||
assertThat(message).isNotNull();
|
||||
assertThat(message.getPayload().getBar()).isEqualTo("barbar" + id);
|
||||
assertThat(message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class) == null);
|
||||
context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@EnableAutoConfiguration
|
||||
public static class TestPojoWithMimeType1 extends TestPojoWithMimeType {
|
||||
|
||||
@StreamListener(Processor.INPUT)
|
||||
@SendTo(Processor.OUTPUT)
|
||||
public StreamListenerTestInterfaces.BarPojo receive(StreamListenerTestInterfaces.FooPojo fooPojo) {
|
||||
this.receivedPojos.add(fooPojo);
|
||||
StreamListenerTestInterfaces.BarPojo barPojo = new StreamListenerTestInterfaces.BarPojo();
|
||||
barPojo.setBar(fooPojo.getFoo());
|
||||
return barPojo;
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@EnableAutoConfiguration
|
||||
public static class TestPojoWithMimeType2 extends TestPojoWithMimeType {
|
||||
|
||||
@StreamListener(Processor.INPUT)
|
||||
@Output(Processor.OUTPUT)
|
||||
public StreamListenerTestInterfaces.BarPojo receive(StreamListenerTestInterfaces.FooPojo fooPojo) {
|
||||
this.receivedPojos.add(fooPojo);
|
||||
StreamListenerTestInterfaces.BarPojo barPojo = new StreamListenerTestInterfaces.BarPojo();
|
||||
barPojo.setBar(fooPojo.getFoo());
|
||||
return barPojo;
|
||||
}
|
||||
}
|
||||
|
||||
public static class TestPojoWithMimeType {
|
||||
List<StreamListenerTestInterfaces.FooPojo> receivedPojos = new ArrayList<>();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.cloud.stream.config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.cloud.stream.annotation.EnableBinding;
|
||||
import org.springframework.cloud.stream.annotation.Output;
|
||||
import org.springframework.cloud.stream.annotation.StreamListener;
|
||||
import org.springframework.cloud.stream.messaging.Processor;
|
||||
import org.springframework.cloud.stream.test.binder.MessageCollector;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.handler.annotation.SendTo;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Marius Bogoevici
|
||||
* @author Ilayaperumal Gopinathan
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class StreamListenerMethodWithReturnMessageTests {
|
||||
|
||||
private Class<?> configClass;
|
||||
|
||||
public StreamListenerMethodWithReturnMessageTests(Class<?> configClass) {
|
||||
this.configClass = configClass;
|
||||
}
|
||||
|
||||
@Parameterized.Parameters
|
||||
public static Collection InputConfigs() {
|
||||
return Arrays.asList(new Class[]{TestPojoWithMessageReturn1.class, TestPojoWithMessageReturn2.class});
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testReturnMessage() throws Exception {
|
||||
ConfigurableApplicationContext context = SpringApplication
|
||||
.run(this.configClass, "--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());
|
||||
TestPojoWithMessageReturn testPojoWithMessageReturn = context
|
||||
.getBean(TestPojoWithMessageReturn.class);
|
||||
assertThat(testPojoWithMessageReturn.receivedPojos).hasSize(1);
|
||||
assertThat(testPojoWithMessageReturn.receivedPojos.get(0)).hasFieldOrPropertyWithValue("foo", "barbar" + id);
|
||||
Message<StreamListenerTestInterfaces.BarPojo> message = (Message<StreamListenerTestInterfaces.BarPojo>) collector
|
||||
.forChannel(processor.output()).poll(1, TimeUnit.SECONDS);
|
||||
assertThat(message).isNotNull();
|
||||
assertThat(message.getPayload().getBar()).isEqualTo("barbar" + id);
|
||||
context.close();
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@EnableAutoConfiguration
|
||||
public static class TestPojoWithMessageReturn1 extends TestPojoWithMessageReturn {
|
||||
|
||||
@StreamListener(Processor.INPUT)
|
||||
@SendTo(Processor.OUTPUT)
|
||||
public Message<?> receive(StreamListenerTestInterfaces.FooPojo fooPojo) {
|
||||
this.receivedPojos.add(fooPojo);
|
||||
StreamListenerTestInterfaces.BarPojo barPojo = new StreamListenerTestInterfaces.BarPojo();
|
||||
barPojo.setBar(fooPojo.getFoo());
|
||||
return MessageBuilder.withPayload(barPojo).setHeader("foo", "bar").build();
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@EnableAutoConfiguration
|
||||
public static class TestPojoWithMessageReturn2 extends TestPojoWithMessageReturn {
|
||||
|
||||
@StreamListener(Processor.INPUT)
|
||||
@Output(Processor.OUTPUT)
|
||||
public Message<?> receive(StreamListenerTestInterfaces.FooPojo fooPojo) {
|
||||
this.receivedPojos.add(fooPojo);
|
||||
StreamListenerTestInterfaces.BarPojo bazPojo = new StreamListenerTestInterfaces.BarPojo();
|
||||
bazPojo.setBar(fooPojo.getFoo());
|
||||
return MessageBuilder.withPayload(bazPojo).setHeader("foo", "bar").build();
|
||||
}
|
||||
}
|
||||
|
||||
public static class TestPojoWithMessageReturn {
|
||||
List<StreamListenerTestInterfaces.FooPojo> receivedPojos = new ArrayList<>();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.cloud.stream.config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.cloud.stream.annotation.EnableBinding;
|
||||
import org.springframework.cloud.stream.annotation.Output;
|
||||
import org.springframework.cloud.stream.annotation.StreamListener;
|
||||
import org.springframework.cloud.stream.messaging.Processor;
|
||||
import org.springframework.cloud.stream.test.binder.MessageCollector;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.handler.annotation.SendTo;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Marius Bogoevici
|
||||
* @author Ilayaperumal Gopinathan
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class StreamListenerMethodWithReturnValueTests {
|
||||
|
||||
private Class<?> configClass;
|
||||
|
||||
public StreamListenerMethodWithReturnValueTests(Class<?> configClass) {
|
||||
this.configClass = configClass;
|
||||
}
|
||||
|
||||
@Parameterized.Parameters
|
||||
public static Collection InputConfigs() {
|
||||
return Arrays.asList(new Class[]{TestStringProcessor1.class, TestStringProcessor2.class});
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testReturn() throws Exception {
|
||||
ConfigurableApplicationContext context = SpringApplication
|
||||
.run(this.configClass, "--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());
|
||||
Message<String> message = (Message<String>) collector
|
||||
.forChannel(processor.output()).poll(1, TimeUnit.SECONDS);
|
||||
TestStringProcessor testStringProcessor = context
|
||||
.getBean(TestStringProcessor.class);
|
||||
assertThat(testStringProcessor.receivedPojos).hasSize(1);
|
||||
assertThat(testStringProcessor.receivedPojos.get(0)).hasFieldOrPropertyWithValue("foo", "barbar" + id);
|
||||
assertThat(message).isNotNull();
|
||||
assertThat(message.getPayload()).isEqualTo("barbar" + id);
|
||||
context.close();
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@EnableAutoConfiguration
|
||||
public static class TestStringProcessor1 extends TestStringProcessor {
|
||||
|
||||
@StreamListener(Processor.INPUT)
|
||||
@SendTo(Processor.OUTPUT)
|
||||
public String receive(StreamListenerTestInterfaces.FooPojo fooPojo) {
|
||||
this.receivedPojos.add(fooPojo);
|
||||
return fooPojo.getFoo();
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@EnableAutoConfiguration
|
||||
public static class TestStringProcessor2 extends TestStringProcessor {
|
||||
|
||||
@StreamListener(Processor.INPUT)
|
||||
@Output(Processor.OUTPUT)
|
||||
public String receive(StreamListenerTestInterfaces.FooPojo fooPojo) {
|
||||
this.receivedPojos.add(fooPojo);
|
||||
return fooPojo.getFoo();
|
||||
}
|
||||
}
|
||||
|
||||
public static class TestStringProcessor {
|
||||
List<StreamListenerTestInterfaces.FooPojo> receivedPojos = new ArrayList<>();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.cloud.stream.config;
|
||||
|
||||
import org.springframework.cloud.stream.annotation.Input;
|
||||
import org.springframework.cloud.stream.annotation.Output;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.SubscribableChannel;
|
||||
|
||||
/**
|
||||
* @author Ilayaperumal Gopinathan
|
||||
*/
|
||||
public class StreamListenerTestInterfaces {
|
||||
|
||||
public static class FooPojo {
|
||||
|
||||
private String foo;
|
||||
|
||||
public String getFoo() {
|
||||
return this.foo;
|
||||
}
|
||||
|
||||
public void setFoo(String foo) {
|
||||
this.foo = foo;
|
||||
}
|
||||
}
|
||||
|
||||
public static class BarPojo {
|
||||
|
||||
private String bar;
|
||||
|
||||
public String getBar() {
|
||||
return this.bar;
|
||||
}
|
||||
|
||||
public void setBar(String bar) {
|
||||
this.bar = bar;
|
||||
}
|
||||
}
|
||||
|
||||
public interface FooInboundChannel1 {
|
||||
|
||||
public String INPUT = "foo1-input";
|
||||
|
||||
@Input(FooInboundChannel1.INPUT)
|
||||
SubscribableChannel input();
|
||||
|
||||
}
|
||||
|
||||
public interface FooOutboundChannel1 {
|
||||
|
||||
String OUTPUT = "foo1-output";
|
||||
|
||||
@Output(FooOutboundChannel1.OUTPUT)
|
||||
MessageChannel output();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -37,11 +37,15 @@ import org.springframework.messaging.SubscribableChannel;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.springframework.cloud.stream.binding.StreamListenerErrorMessages.INVALID_DECLARATIVE_METHOD_PARAMETERS;
|
||||
import static org.springframework.cloud.stream.binding.StreamListenerErrorMessages.TARGET_BEAN_NOT_EXISTS;
|
||||
|
||||
/**
|
||||
* @author Marius Bogoevici
|
||||
* @author Ilayaperumal Gopinathan
|
||||
*/
|
||||
public class StreamListenerWithAnnotatedArgsTests {
|
||||
public class StreamListenerWithAnnotatedInputOutputArgsTests {
|
||||
|
||||
@Test
|
||||
public void testInputOutputArgs() throws Exception {
|
||||
@@ -49,6 +53,34 @@ public class StreamListenerWithAnnotatedArgsTests {
|
||||
sendMessageAndValidate(context);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInputOutputArgsWithMoreParameters() {
|
||||
try {
|
||||
SpringApplication.run(TestInputOutputArgsWithMoreParameters.class, "--server.port=0");
|
||||
fail("Expected exception: "+ INVALID_DECLARATIVE_METHOD_PARAMETERS);
|
||||
}
|
||||
catch (Exception e) {
|
||||
assertThat(e.getMessage()).contains(INVALID_DECLARATIVE_METHOD_PARAMETERS);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInputOutputArgsWithInvalidBindableTarget() {
|
||||
try {
|
||||
SpringApplication.run(TestInputOutputArgsWithInvalidBindableTarget.class, "--server.port=0");
|
||||
fail("Exception expected on using invalid bindable target as method parameter");
|
||||
}
|
||||
catch (Exception e) {
|
||||
assertThat(e.getMessage()).contains(TARGET_BEAN_NOT_EXISTS + ": invalid");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInputOutputArgsWithParameterOrderChanged() throws Exception {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(TestInputOutputArgsWithParameterOrderChanged.class, "--server.port=0");
|
||||
sendMessageAndValidate(context);
|
||||
}
|
||||
|
||||
private void sendMessageAndValidate(ConfigurableApplicationContext context) throws InterruptedException {
|
||||
@SuppressWarnings("unchecked")
|
||||
Processor processor = context.getBean(Processor.class);
|
||||
@@ -76,4 +108,50 @@ public class StreamListenerWithAnnotatedArgsTests {
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@EnableAutoConfiguration
|
||||
public static class TestInputOutputArgsWithMoreParameters {
|
||||
|
||||
@StreamListener
|
||||
public void receive(@Input(Processor.INPUT) SubscribableChannel input, @Output(Processor.OUTPUT) final MessageChannel output,
|
||||
String someArg) {
|
||||
input.subscribe(new MessageHandler() {
|
||||
@Override
|
||||
public void handleMessage(Message<?> message) throws MessagingException {
|
||||
output.send(MessageBuilder.withPayload(message.getPayload().toString().toUpperCase()).build());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@EnableAutoConfiguration
|
||||
public static class TestInputOutputArgsWithInvalidBindableTarget {
|
||||
|
||||
@StreamListener
|
||||
public void receive(@Input("invalid") SubscribableChannel input, @Output(Processor.OUTPUT) final MessageChannel output) {
|
||||
input.subscribe(new MessageHandler() {
|
||||
@Override
|
||||
public void handleMessage(Message<?> message) throws MessagingException {
|
||||
output.send(MessageBuilder.withPayload(message.getPayload().toString().toUpperCase()).build());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@EnableAutoConfiguration
|
||||
public static class TestInputOutputArgsWithParameterOrderChanged {
|
||||
|
||||
@StreamListener
|
||||
public void receive(@Output(Processor.OUTPUT) final MessageChannel output, @Input("input") SubscribableChannel input) {
|
||||
input.subscribe(new MessageHandler() {
|
||||
@Override
|
||||
public void handleMessage(Message<?> message) throws MessagingException {
|
||||
output.send(MessageBuilder.withPayload(message.getPayload().toString().toUpperCase()).build());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,388 +0,0 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.stream.config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.cloud.stream.annotation.EnableBinding;
|
||||
import org.springframework.cloud.stream.annotation.StreamListener;
|
||||
import org.springframework.cloud.stream.messaging.Processor;
|
||||
import org.springframework.cloud.stream.messaging.Sink;
|
||||
import org.springframework.cloud.stream.test.binder.MessageCollector;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.messaging.handler.annotation.Header;
|
||||
import org.springframework.messaging.handler.annotation.Headers;
|
||||
import org.springframework.messaging.handler.annotation.Payload;
|
||||
import org.springframework.messaging.handler.annotation.SendTo;
|
||||
import org.springframework.util.MimeType;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* @author Marius Bogoevici
|
||||
*/
|
||||
public class StreamListenerWithHandlerTests {
|
||||
|
||||
@Test
|
||||
public void testContentTypeConversion() throws Exception {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(TestSink.class, "--server.port=0");
|
||||
@SuppressWarnings("unchecked")
|
||||
TestSink testSink = context.getBean(TestSink.class);
|
||||
Sink sink = context.getBean(Sink.class);
|
||||
String id = UUID.randomUUID().toString();
|
||||
sink.input().send(MessageBuilder.withPayload("{\"bar\":\"barbar" + id + "\"}")
|
||||
.setHeader("contentType", "application/json").build());
|
||||
assertThat(testSink.latch.await(10, TimeUnit.SECONDS));
|
||||
assertThat(testSink.receivedArguments).hasSize(1);
|
||||
assertThat(testSink.receivedArguments.get(0)).hasFieldOrPropertyWithValue("bar", "barbar" + id);
|
||||
context.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testAnnotatedArguments() throws Exception {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(TestPojoWithAnnotatedArguments.class,
|
||||
"--server.port=0");
|
||||
|
||||
TestPojoWithAnnotatedArguments testPojoWithAnnotatedArguments = context
|
||||
.getBean(TestPojoWithAnnotatedArguments.class);
|
||||
Sink sink = context.getBean(Sink.class);
|
||||
String id = UUID.randomUUID().toString();
|
||||
sink.input().send(MessageBuilder.withPayload("{\"bar\":\"barbar" + id + "\"}")
|
||||
.setHeader("contentType", "application/json").setHeader("testHeader", "testValue").build());
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedArguments).hasSize(3);
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedArguments.get(0)).isInstanceOf(FooPojo.class);
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedArguments.get(0)).hasFieldOrPropertyWithValue("bar",
|
||||
"barbar" + id);
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedArguments.get(1)).isInstanceOf(Map.class);
|
||||
assertThat((Map<String, String>) testPojoWithAnnotatedArguments.receivedArguments.get(1))
|
||||
.containsEntry(MessageHeaders.CONTENT_TYPE, "application/json");
|
||||
assertThat((Map<String, String>) testPojoWithAnnotatedArguments.receivedArguments.get(1))
|
||||
.containsEntry("testHeader", "testValue");
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedArguments.get(2)).isEqualTo("application/json");
|
||||
context.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testReturn() throws Exception {
|
||||
ConfigurableApplicationContext context = SpringApplication
|
||||
.run(TestStringProcessor.class, "--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("{\"bar\":\"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);
|
||||
assertThat(testStringProcessor.receivedPojos).hasSize(1);
|
||||
assertThat(testStringProcessor.receivedPojos.get(0)).hasFieldOrPropertyWithValue("bar", "barbar" + id);
|
||||
assertThat(message).isNotNull();
|
||||
assertThat(message.getPayload()).isEqualTo("barbar" + id);
|
||||
context.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testReturnConversion() throws Exception {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(TestPojoWithMimeType.class,
|
||||
"--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("{\"bar\":\"barbar" + id + "\"}")
|
||||
.setHeader("contentType", "application/json").build());
|
||||
TestPojoWithMimeType testPojoWithMimeType = context.getBean(TestPojoWithMimeType.class);
|
||||
assertThat(testPojoWithMimeType.receivedPojos).hasSize(1);
|
||||
assertThat(testPojoWithMimeType.receivedPojos.get(0)).hasFieldOrPropertyWithValue("bar", "barbar" + id);
|
||||
Message<String> message = (Message<String>) collector.forChannel(processor.output()).poll(1, TimeUnit.SECONDS);
|
||||
assertThat(message).isNotNull();
|
||||
assertThat(message.getPayload()).isEqualTo("{\"qux\":\"barbar" + id + "\"}");
|
||||
assertThat(message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class).includes(MimeTypeUtils.APPLICATION_JSON));
|
||||
context.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testReturnNoConversion() throws Exception {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(TestPojoWithMimeType.class, "--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("{\"bar\":\"barbar" + id + "\"}")
|
||||
.setHeader("contentType", "application/json").build());
|
||||
TestPojoWithMimeType testPojoWithMimeType = context.getBean(TestPojoWithMimeType.class);
|
||||
assertThat(testPojoWithMimeType.receivedPojos).hasSize(1);
|
||||
assertThat(testPojoWithMimeType.receivedPojos.get(0)).hasFieldOrPropertyWithValue("bar", "barbar" + id);
|
||||
Message<BazPojo> message = (Message<BazPojo>) collector.forChannel(processor.output()).poll(1,
|
||||
TimeUnit.SECONDS);
|
||||
assertThat(message).isNotNull();
|
||||
assertThat(message.getPayload().getQux()).isEqualTo("barbar" + id);
|
||||
context.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testReturnMessage() throws Exception {
|
||||
ConfigurableApplicationContext context = SpringApplication
|
||||
.run(TestPojoWithMessageReturn.class, "--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("{\"bar\":\"barbar" + id + "\"}")
|
||||
.setHeader("contentType", "application/json").build());
|
||||
TestPojoWithMessageReturn testPojoWithMessageReturn = context
|
||||
.getBean(TestPojoWithMessageReturn.class);
|
||||
assertThat(testPojoWithMessageReturn.receivedPojos).hasSize(1);
|
||||
assertThat(testPojoWithMessageReturn.receivedPojos.get(0)).hasFieldOrPropertyWithValue("bar", "barbar" + id);
|
||||
Message<BazPojo> message = (Message<BazPojo>) collector
|
||||
.forChannel(processor.output()).poll(1, TimeUnit.SECONDS);
|
||||
assertThat(message).isNotNull();
|
||||
assertThat(message.getPayload().getQux()).isEqualTo("barbar" + id);
|
||||
context.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testMessageArgument() throws Exception {
|
||||
ConfigurableApplicationContext context = SpringApplication
|
||||
.run(TestPojoWithMessageArgument.class, "--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("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<BazPojo> message = (Message<BazPojo>) collector
|
||||
.forChannel(processor.output()).poll(1, TimeUnit.SECONDS);
|
||||
assertThat(message).isNotNull();
|
||||
assertThat(message.getPayload().getQux()).isEqualTo("barbar" + id);
|
||||
context.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testDuplicateMapping() throws Exception {
|
||||
try {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(TestDuplicateMapping.class,
|
||||
"--server.port=0");
|
||||
fail("Exception expected on duplicate mapping");
|
||||
}
|
||||
catch (BeanCreationException e) {
|
||||
assertThat(e.getCause().getMessage()).startsWith("Duplicate @StreamListener mapping");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testHandlerBean() throws Exception {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(TestHandlerBean.class,
|
||||
"--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("{\"bar\":\"barbar" + id + "\"}")
|
||||
.setHeader("contentType", "application/json").build());
|
||||
HandlerBean handlerBean = context.getBean(HandlerBean.class);
|
||||
assertThat(handlerBean.receivedPojos).hasSize(1);
|
||||
assertThat(handlerBean.receivedPojos.get(0)).hasFieldOrPropertyWithValue("bar", "barbar" + id);
|
||||
Message<String> message = (Message<String>) collector.forChannel(processor.output()).poll(1, TimeUnit.SECONDS);
|
||||
assertThat(message).isNotNull();
|
||||
assertThat(message.getPayload()).isEqualTo("{\"qux\":\"barbar" + id + "\"}");
|
||||
assertThat(message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class).includes(MimeTypeUtils.APPLICATION_JSON));
|
||||
context.close();
|
||||
}
|
||||
|
||||
@EnableBinding(Sink.class)
|
||||
@EnableAutoConfiguration
|
||||
public static class TestSink {
|
||||
|
||||
List<FooPojo> receivedArguments = new ArrayList<>();
|
||||
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
|
||||
@StreamListener(Sink.INPUT)
|
||||
public void receive(FooPojo fooPojo) {
|
||||
this.receivedArguments.add(fooPojo);
|
||||
this.latch.countDown();
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@EnableAutoConfiguration
|
||||
public static class TestStringProcessor {
|
||||
|
||||
List<FooPojo> receivedPojos = new ArrayList<>();
|
||||
|
||||
@StreamListener(Processor.INPUT)
|
||||
@SendTo(Processor.OUTPUT)
|
||||
public String receive(FooPojo fooPojo) {
|
||||
this.receivedPojos.add(fooPojo);
|
||||
return fooPojo.getBar();
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@EnableAutoConfiguration
|
||||
public static class TestPojoWithMimeType {
|
||||
|
||||
List<FooPojo> receivedPojos = new ArrayList<>();
|
||||
|
||||
@StreamListener(Processor.INPUT)
|
||||
@SendTo(Processor.OUTPUT)
|
||||
public BazPojo receive(FooPojo fooPojo) {
|
||||
this.receivedPojos.add(fooPojo);
|
||||
BazPojo bazPojo = new BazPojo();
|
||||
bazPojo.setQux(fooPojo.getBar());
|
||||
return bazPojo;
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@EnableAutoConfiguration
|
||||
public static class TestPojoWithAnnotatedArguments {
|
||||
|
||||
List<Object> receivedArguments = new ArrayList<>();
|
||||
|
||||
@StreamListener(Processor.INPUT)
|
||||
public void receive(@Payload FooPojo fooPojo,
|
||||
@Headers Map<String, Object> headers,
|
||||
@Header(MessageHeaders.CONTENT_TYPE) String contentType) {
|
||||
this.receivedArguments.add(fooPojo);
|
||||
this.receivedArguments.add(headers);
|
||||
this.receivedArguments.add(contentType);
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@EnableAutoConfiguration
|
||||
public static class TestPojoWithMessageReturn {
|
||||
|
||||
List<FooPojo> receivedPojos = new ArrayList<>();
|
||||
|
||||
@StreamListener(Processor.INPUT)
|
||||
@SendTo(Processor.OUTPUT)
|
||||
public Message<?> receive(FooPojo fooPojo) {
|
||||
this.receivedPojos.add(fooPojo);
|
||||
BazPojo bazPojo = new BazPojo();
|
||||
bazPojo.setQux(fooPojo.getBar());
|
||||
return MessageBuilder.withPayload(bazPojo).setHeader("foo", "bar").build();
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@EnableAutoConfiguration
|
||||
public static class TestPojoWithMessageArgument {
|
||||
|
||||
List<Message<String>> receivedMessages = new ArrayList<>();
|
||||
|
||||
@StreamListener(Processor.INPUT)
|
||||
@SendTo(Processor.OUTPUT)
|
||||
public BazPojo receive(Message<String> fooMessage) {
|
||||
this.receivedMessages.add(fooMessage);
|
||||
BazPojo bazPojo = new BazPojo();
|
||||
bazPojo.setQux(fooMessage.getPayload());
|
||||
return bazPojo;
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@EnableAutoConfiguration
|
||||
public static class TestDuplicateMapping {
|
||||
|
||||
@StreamListener(Processor.INPUT)
|
||||
public void receive(Message<String> fooMessage) {
|
||||
}
|
||||
|
||||
@StreamListener(Processor.INPUT)
|
||||
public void receive2(Message<String> fooMessage) {
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@EnableAutoConfiguration
|
||||
public static class TestHandlerBean {
|
||||
|
||||
@Bean
|
||||
public HandlerBean handlerBean() {
|
||||
return new HandlerBean();
|
||||
}
|
||||
}
|
||||
|
||||
public static class HandlerBean {
|
||||
|
||||
List<FooPojo> receivedPojos = new ArrayList<>();
|
||||
|
||||
@StreamListener(Processor.INPUT)
|
||||
@SendTo(Processor.OUTPUT)
|
||||
public BazPojo receive(FooPojo fooMessage) {
|
||||
this.receivedPojos.add(fooMessage);
|
||||
BazPojo bazPojo = new BazPojo();
|
||||
bazPojo.setQux(fooMessage.getBar());
|
||||
return bazPojo;
|
||||
}
|
||||
}
|
||||
|
||||
public static class FooPojo {
|
||||
|
||||
private String bar;
|
||||
|
||||
public String getBar() {
|
||||
return this.bar;
|
||||
}
|
||||
|
||||
public void setBar(String bar) {
|
||||
this.bar = bar;
|
||||
}
|
||||
}
|
||||
|
||||
public static class BazPojo {
|
||||
|
||||
private String qux;
|
||||
|
||||
public String getQux() {
|
||||
return this.qux;
|
||||
}
|
||||
|
||||
public void setQux(String qux) {
|
||||
this.qux = qux;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user