diff --git a/spring-cloud-function-stream/src/test/java/org/springframework/cloud/function/stream/consumer/FluxPojoStreamingConsumerTests.java b/spring-cloud-function-stream/src/test/java/org/springframework/cloud/function/stream/consumer/FluxPojoStreamingConsumerTests.java new file mode 100644 index 000000000..c790e0413 --- /dev/null +++ b/spring-cloud-function-stream/src/test/java/org/springframework/cloud/function/stream/consumer/FluxPojoStreamingConsumerTests.java @@ -0,0 +1,73 @@ +/* + * Copyright 2017 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.function.stream.consumer; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.stream.messaging.Sink; +import org.springframework.context.annotation.Bean; +import org.springframework.messaging.support.MessageBuilder; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +import reactor.core.publisher.Flux; + +/** + * @author Marius Bogoevici + */ +@RunWith(SpringRunner.class) +@SpringBootTest(classes = FluxPojoStreamingConsumerTests.StreamingSinkTest.class, properties = { + "spring.cloud.stream.bindings.input.destination=data-in", + "spring.cloud.function.stream.endpoint=sinkConsumer" }) +public class FluxPojoStreamingConsumerTests { + + @Autowired + Sink sink; + + @Autowired + List sinkCollector; + + @Test + public void test() throws Exception { + sink.input().send(MessageBuilder.withPayload("foo").build()); + assertThat(sinkCollector).hasSize(1); + } + + @SpringBootApplication + public static class StreamingSinkTest { + + @Bean + public List sinkCollector() { + return new ArrayList<>(); + } + + @Bean + public Consumer> sinkConsumer(final List sinkCollector) { + return foos -> foos.doOnNext(s -> sinkCollector.add(s)); + } + } + +} diff --git a/spring-cloud-function-stream/src/test/java/org/springframework/cloud/function/stream/consumer/FluxStreamingConsumerTests.java b/spring-cloud-function-stream/src/test/java/org/springframework/cloud/function/stream/consumer/FluxStreamingConsumerTests.java new file mode 100644 index 000000000..8393067f9 --- /dev/null +++ b/spring-cloud-function-stream/src/test/java/org/springframework/cloud/function/stream/consumer/FluxStreamingConsumerTests.java @@ -0,0 +1,91 @@ +/* + * Copyright 2017 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.function.stream.consumer; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.stream.messaging.Sink; +import org.springframework.context.annotation.Bean; +import org.springframework.messaging.support.MessageBuilder; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +import reactor.core.publisher.Flux; + +/** + * @author Marius Bogoevici + */ +@RunWith(SpringRunner.class) +@SpringBootTest(classes = FluxStreamingConsumerTests.StreamingSinkTest.class, properties = { + "spring.cloud.stream.bindings.input.destination=data-in", + "spring.cloud.function.stream.endpoint=sinkConsumer" }) +public class FluxStreamingConsumerTests { + + @Autowired + Sink sink; + + @Autowired + List sinkCollector; + + @Test + public void test() throws Exception { + sink.input().send(MessageBuilder.withPayload(new Foo("foo")).build()); + assertThat(sinkCollector).hasSize(1); + } + + @SpringBootApplication + public static class StreamingSinkTest { + + @Bean + public List sinkCollector() { + return new ArrayList<>(); + } + + @Bean + public Consumer> sinkConsumer(final List sinkCollector) { + return foos -> foos.subscribe(s -> sinkCollector.add(s)); + } + } + + protected static class Foo { + private String name; + + Foo() { + } + + public Foo(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } +} diff --git a/spring-cloud-function-stream/src/test/java/org/springframework/cloud/function/stream/consumer/PojoStreamingConsumerTests.java b/spring-cloud-function-stream/src/test/java/org/springframework/cloud/function/stream/consumer/PojoStreamingConsumerTests.java new file mode 100644 index 000000000..b3574723f --- /dev/null +++ b/spring-cloud-function-stream/src/test/java/org/springframework/cloud/function/stream/consumer/PojoStreamingConsumerTests.java @@ -0,0 +1,89 @@ +/* + * Copyright 2017 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.function.stream.consumer; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.stream.messaging.Sink; +import org.springframework.context.annotation.Bean; +import org.springframework.messaging.support.MessageBuilder; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Marius Bogoevici + */ +@RunWith(SpringRunner.class) +@SpringBootTest(classes = PojoStreamingConsumerTests.StreamingSinkTest.class, properties = { + "spring.cloud.stream.bindings.input.destination=data-in", + "spring.cloud.function.stream.endpoint=sinkConsumer" }) +public class PojoStreamingConsumerTests { + + @Autowired + Sink sink; + + @Autowired + List sinkCollector; + + @Test + public void test() throws Exception { + sink.input().send(MessageBuilder.withPayload(new Foo("foo")).build()); + assertThat(sinkCollector).hasSize(1); + } + + @SpringBootApplication + public static class StreamingSinkTest { + + @Bean + public List sinkCollector() { + return new ArrayList<>(); + } + + @Bean + public Consumer sinkConsumer(final List sinkCollector) { + return s -> sinkCollector.add(s); + } + } + + protected static class Foo { + private String name; + + Foo() { + } + + public Foo(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } +} diff --git a/spring-cloud-function-stream/src/test/java/org/springframework/cloud/function/stream/consumer/StreamingConsumerTests.java b/spring-cloud-function-stream/src/test/java/org/springframework/cloud/function/stream/consumer/StreamingConsumerTests.java index c0efa9d2d..7564e46ac 100644 --- a/spring-cloud-function-stream/src/test/java/org/springframework/cloud/function/stream/consumer/StreamingConsumerTests.java +++ b/spring-cloud-function-stream/src/test/java/org/springframework/cloud/function/stream/consumer/StreamingConsumerTests.java @@ -18,21 +18,16 @@ package org.springframework.cloud.function.stream.consumer; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.TimeUnit; import java.util.function.Consumer; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.test.context.SpringBootTest; -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.annotation.Bean; -import org.springframework.messaging.Message; import org.springframework.messaging.support.MessageBuilder; import org.springframework.test.context.junit4.SpringRunner;