Added checkstyle
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2015 the original author or authors.
|
||||
* Copyright 2015-2019 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.
|
||||
@@ -53,11 +53,13 @@ public class ContentTypeOutboundSourceTests {
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testMessageHeaderWhenNoExplicitContentTypeOnMessage() throws Exception {
|
||||
testSource.output().send(MessageBuilder.withPayload("{\"message\":\"Hi\"}").setHeader(MessageHeaders.CONTENT_TYPE,"text/plain").build());
|
||||
Message<String> received = (Message<String>) ((TestSupportBinder) binderFactory.getBinder(null,
|
||||
MessageChannel.class))
|
||||
.messageCollector().forChannel(testSource.output()).poll();
|
||||
assertThat(received.getHeaders().get(MessageHeaders.CONTENT_TYPE).toString()).contains("text/plain");
|
||||
this.testSource.output().send(MessageBuilder.withPayload("{\"message\":\"Hi\"}")
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE, "text/plain").build());
|
||||
Message<String> received = (Message<String>) ((TestSupportBinder) this.binderFactory
|
||||
.getBinder(null, MessageChannel.class)).messageCollector()
|
||||
.forChannel(this.testSource.output()).poll();
|
||||
assertThat(received.getHeaders().get(MessageHeaders.CONTENT_TYPE).toString())
|
||||
.contains("text/plain");
|
||||
assertThat("{\"message\":\"Hi\"}").isEqualTo(received.getPayload());
|
||||
}
|
||||
|
||||
@@ -67,4 +69,5 @@ public class ContentTypeOutboundSourceTests {
|
||||
public static class TestSource {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
* Copyright 2017-2019 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.
|
||||
@@ -42,11 +42,13 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* @author Oleg Zhurakousky
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = CustomHeaderPropagationTests.HeaderPropagationProcessor.class,
|
||||
webEnvironment = SpringBootTest.WebEnvironment.NONE,
|
||||
properties = {"spring.cloud.stream.integration.messageHandlerNotPropagatedHeaders=bar,contentType"})
|
||||
// @checkstyle:off
|
||||
@SpringBootTest(classes = CustomHeaderPropagationTests.HeaderPropagationProcessor.class, webEnvironment = SpringBootTest.WebEnvironment.NONE, properties = {
|
||||
"spring.cloud.stream.integration.messageHandlerNotPropagatedHeaders=bar,contentType" })
|
||||
public class CustomHeaderPropagationTests {
|
||||
|
||||
// @checkstyle:on
|
||||
|
||||
@Autowired
|
||||
private Processor testProcessor;
|
||||
|
||||
@@ -55,19 +57,19 @@ public class CustomHeaderPropagationTests {
|
||||
|
||||
@Test
|
||||
/**
|
||||
* @since 2.0 The behavior of content type handling has changed.
|
||||
* All input/output channels have a default content type of application/json
|
||||
* When a processor or a source returns a String, and if the content type is json it will be quoted
|
||||
* @since 2.0 The behavior of content type handling has changed. All input/output
|
||||
* channels have a default content type of application/json When a processor or a
|
||||
* source returns a String, and if the content type is json it will be quoted
|
||||
*/
|
||||
public void testCustomHeaderPropagation() throws Exception {
|
||||
testProcessor.input().send(MessageBuilder.withPayload("{'name':'foo'}")
|
||||
this.testProcessor.input().send(MessageBuilder.withPayload("{'name':'foo'}")
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE, "application/json")
|
||||
.setHeader("foo", "fooValue")
|
||||
.setHeader("bar", "barValue")
|
||||
.build());
|
||||
.setHeader("foo", "fooValue").setHeader("bar", "barValue").build());
|
||||
@SuppressWarnings("unchecked")
|
||||
Message<String> received = (Message<String>) ((TestSupportBinder) binderFactory.getBinder(null, MessageChannel.class))
|
||||
.messageCollector().forChannel(testProcessor.output()).poll(10, TimeUnit.SECONDS);
|
||||
Message<String> received = (Message<String>) ((TestSupportBinder) this.binderFactory
|
||||
.getBinder(null, MessageChannel.class)).messageCollector()
|
||||
.forChannel(this.testProcessor.output())
|
||||
.poll(10, TimeUnit.SECONDS);
|
||||
assertThat(received).isNotNull();
|
||||
assertThat(received.getHeaders()).containsEntry("foo", "fooValue");
|
||||
assertThat(received.getHeaders()).doesNotContainKey("bar");
|
||||
@@ -81,9 +83,12 @@ public class CustomHeaderPropagationTests {
|
||||
|
||||
@ServiceActivator(inputChannel = "input", outputChannel = "output")
|
||||
public Message<String> consume(String data) {
|
||||
//if we don't force content to be String, it will be quoted on the outbound channel
|
||||
return MessageBuilder.withPayload(data).setHeader(MessageHeaders.CONTENT_TYPE,"text/plain").build();
|
||||
// if we don't force content to be String, it will be quoted on the outbound
|
||||
// channel
|
||||
return MessageBuilder.withPayload(data)
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE, "text/plain").build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,7 +19,6 @@ package org.springframework.cloud.stream.config;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@@ -44,7 +43,6 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.util.MimeType;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
|
||||
/**
|
||||
* @author Ilayaperumal Gopinathan
|
||||
@@ -66,16 +64,17 @@ public class CustomMessageConverterTests {
|
||||
|
||||
@Test
|
||||
public void testCustomMessageConverter() throws Exception {
|
||||
assertThat(customMessageConverters).hasSize(2);
|
||||
assertThat(customMessageConverters).extracting("class").contains(FooConverter.class,
|
||||
BarConverter.class);
|
||||
testSource.output().send(MessageBuilder.withPayload(new Foo("hi")).build());
|
||||
assertThat(this.customMessageConverters).hasSize(2);
|
||||
assertThat(this.customMessageConverters).extracting("class")
|
||||
.contains(FooConverter.class, BarConverter.class);
|
||||
this.testSource.output().send(MessageBuilder.withPayload(new Foo("hi")).build());
|
||||
@SuppressWarnings("unchecked")
|
||||
Message<String> received = (Message<String>) ((TestSupportBinder) binderFactory.getBinder(null,
|
||||
MessageChannel.class))
|
||||
.messageCollector().forChannel(testSource.output()).poll(1, TimeUnit.SECONDS);
|
||||
Assert.assertThat(received, notNullValue());
|
||||
assertThat(received.getHeaders().get(MessageHeaders.CONTENT_TYPE)).isEqualTo(MimeType.valueOf("test/foo"));
|
||||
Message<String> received = (Message<String>) ((TestSupportBinder) this.binderFactory
|
||||
.getBinder(null, MessageChannel.class)).messageCollector()
|
||||
.forChannel(this.testSource.output()).poll(1, TimeUnit.SECONDS);
|
||||
assertThat(received).isNotNull();
|
||||
assertThat(received.getHeaders().get(MessageHeaders.CONTENT_TYPE))
|
||||
.isEqualTo(MimeType.valueOf("test/foo"));
|
||||
}
|
||||
|
||||
@EnableBinding(Source.class)
|
||||
@@ -95,6 +94,7 @@ public class CustomMessageConverterTests {
|
||||
public MessageConverter barConverter() {
|
||||
return new BarConverter();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class FooConverter extends AbstractMessageConverter {
|
||||
@@ -109,7 +109,8 @@ public class CustomMessageConverterTests {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object convertToInternal(Object payload, MessageHeaders headers, Object conversionHint) {
|
||||
protected Object convertToInternal(Object payload, MessageHeaders headers,
|
||||
Object conversionHint) {
|
||||
Object result = null;
|
||||
try {
|
||||
if (payload instanceof Foo) {
|
||||
@@ -118,11 +119,12 @@ public class CustomMessageConverterTests {
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
this.logger.error(e.getMessage(), e);
|
||||
return null;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class BarConverter extends AbstractMessageConverter {
|
||||
@@ -137,7 +139,8 @@ public class CustomMessageConverterTests {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object convertToInternal(Object payload, MessageHeaders headers, Object conversionHint) {
|
||||
protected Object convertToInternal(Object payload, MessageHeaders headers,
|
||||
Object conversionHint) {
|
||||
Object result = null;
|
||||
try {
|
||||
if (payload instanceof Bar) {
|
||||
@@ -146,11 +149,12 @@ public class CustomMessageConverterTests {
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
this.logger.error(e.getMessage(), e);
|
||||
return null;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Foo {
|
||||
@@ -170,5 +174,7 @@ public class CustomMessageConverterTests {
|
||||
public Bar(String testing) {
|
||||
this.testing = testing;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
* Copyright 2017-2019 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.
|
||||
@@ -42,10 +42,11 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* @author Oleg Zhurakousky
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = DefaultHeaderPropagationTests.HeaderPropagationProcessor.class,
|
||||
webEnvironment = SpringBootTest.WebEnvironment.NONE)
|
||||
// @checkstyle:off
|
||||
@SpringBootTest(classes = DefaultHeaderPropagationTests.HeaderPropagationProcessor.class, webEnvironment = SpringBootTest.WebEnvironment.NONE)
|
||||
public class DefaultHeaderPropagationTests {
|
||||
|
||||
// @checkstyle:on
|
||||
@Autowired
|
||||
private Processor testProcessor;
|
||||
|
||||
@@ -54,14 +55,14 @@ public class DefaultHeaderPropagationTests {
|
||||
|
||||
@Test
|
||||
public void testDefaultHeaderPropagation() throws Exception {
|
||||
testProcessor.input().send(MessageBuilder.withPayload("{'name':'foo'}")
|
||||
this.testProcessor.input().send(MessageBuilder.withPayload("{'name':'foo'}")
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE, "application/json")
|
||||
.setHeader("foo", "fooValue")
|
||||
.setHeader("bar", "barValue")
|
||||
.build());
|
||||
.setHeader("foo", "fooValue").setHeader("bar", "barValue").build());
|
||||
@SuppressWarnings("unchecked")
|
||||
Message<String> received = (Message<String>) ((TestSupportBinder) binderFactory.getBinder(null, MessageChannel.class))
|
||||
.messageCollector().forChannel(testProcessor.output()).poll(1, TimeUnit.SECONDS);
|
||||
Message<String> received = (Message<String>) ((TestSupportBinder) this.binderFactory
|
||||
.getBinder(null, MessageChannel.class)).messageCollector()
|
||||
.forChannel(this.testProcessor.output())
|
||||
.poll(1, TimeUnit.SECONDS);
|
||||
assertThat(received).isNotNull();
|
||||
assertThat(received.getHeaders()).containsEntry("foo", "fooValue");
|
||||
assertThat(received.getHeaders()).containsEntry("bar", "barValue");
|
||||
@@ -75,8 +76,10 @@ public class DefaultHeaderPropagationTests {
|
||||
|
||||
@ServiceActivator(inputChannel = "input", outputChannel = "output")
|
||||
public Message<String> consume(String data) {
|
||||
return MessageBuilder.withPayload(data).setHeader(MessageHeaders.CONTENT_TYPE,"text/plain").build();
|
||||
return MessageBuilder.withPayload(data)
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE, "text/plain").build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -35,17 +35,19 @@ import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Marius Bogoevici
|
||||
* @author Oleg Zhurakousky
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = DefaultHeaderPropagationWithApplicationProvidedHeaderTests.HeaderPropagationProcessor.class,
|
||||
webEnvironment = SpringBootTest.WebEnvironment.NONE)
|
||||
// @checkstyle:off
|
||||
@SpringBootTest(classes = DefaultHeaderPropagationWithApplicationProvidedHeaderTests.HeaderPropagationProcessor.class, webEnvironment = SpringBootTest.WebEnvironment.NONE)
|
||||
public class DefaultHeaderPropagationWithApplicationProvidedHeaderTests {
|
||||
|
||||
// @checkstyle:on
|
||||
|
||||
@Autowired
|
||||
private Processor testProcessor;
|
||||
|
||||
@@ -54,16 +56,16 @@ public class DefaultHeaderPropagationWithApplicationProvidedHeaderTests {
|
||||
|
||||
@Test
|
||||
public void testHeaderPropagationIfSetByApplication() throws Exception {
|
||||
testProcessor.input().send(MessageBuilder.withPayload("{'name':'foo'}")
|
||||
this.testProcessor.input().send(MessageBuilder.withPayload("{'name':'foo'}")
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE, "application/json")
|
||||
.setHeader("foo", "fooValue")
|
||||
.setHeader("bar", "barValue")
|
||||
.build());
|
||||
.setHeader("foo", "fooValue").setHeader("bar", "barValue").build());
|
||||
@SuppressWarnings("unchecked")
|
||||
Message<String> received = (Message<String>) ((TestSupportBinder) binderFactory.getBinder(null, MessageChannel.class))
|
||||
.messageCollector().forChannel(testProcessor.output()).poll(1, TimeUnit.SECONDS);
|
||||
assertEquals("fooValue", received.getHeaders().get("foo"));
|
||||
assertEquals("barValue", received.getHeaders().get("bar"));
|
||||
Message<String> received = (Message<String>) ((TestSupportBinder) this.binderFactory
|
||||
.getBinder(null, MessageChannel.class)).messageCollector()
|
||||
.forChannel(this.testProcessor.output())
|
||||
.poll(1, TimeUnit.SECONDS);
|
||||
assertThat(received.getHeaders().get("foo")).isEqualTo("fooValue");
|
||||
assertThat(received.getHeaders().get("bar")).isEqualTo("barValue");
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@@ -72,8 +74,10 @@ public class DefaultHeaderPropagationWithApplicationProvidedHeaderTests {
|
||||
|
||||
@ServiceActivator(inputChannel = "input", outputChannel = "output")
|
||||
public Message<?> consume(String data) {
|
||||
return MessageBuilder.withPayload(data).setHeader(MessageHeaders.CONTENT_TYPE, "text/plain").build();
|
||||
return MessageBuilder.withPayload(data)
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE, "text/plain").build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -54,11 +54,13 @@ public class DeserializeJSONToJavaTypeTests {
|
||||
|
||||
@Test
|
||||
public void testMessageDeserialized() throws Exception {
|
||||
testProcessor.input().send(
|
||||
MessageBuilder.withPayload("{\"name\":\"Bar\"}").setHeader("contentType", "application/json").build());
|
||||
this.testProcessor.input().send(MessageBuilder.withPayload("{\"name\":\"Bar\"}")
|
||||
.setHeader("contentType", "application/json").build());
|
||||
@SuppressWarnings("unchecked")
|
||||
Message<String> received = (Message<String>) ((TestSupportBinder) binderFactory.getBinder(null, MessageChannel.class))
|
||||
.messageCollector().forChannel(testProcessor.output()).poll(1, TimeUnit.SECONDS);
|
||||
Message<String> received = (Message<String>) ((TestSupportBinder) this.binderFactory
|
||||
.getBinder(null, MessageChannel.class)).messageCollector()
|
||||
.forChannel(this.testProcessor.output())
|
||||
.poll(1, TimeUnit.SECONDS);
|
||||
assertThat(received).isNotNull();
|
||||
assertThat(received.getPayload()).isEqualTo("{\"name\":\"Bar\"}");
|
||||
}
|
||||
@@ -73,6 +75,7 @@ public class DeserializeJSONToJavaTypeTests {
|
||||
public Foo consume(Foo foo) {
|
||||
return foo;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Foo {
|
||||
@@ -80,11 +83,13 @@ public class DeserializeJSONToJavaTypeTests {
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
* Copyright 2017-2019 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.
|
||||
@@ -56,14 +56,17 @@ public class InboundJsonToTupleConversionTest {
|
||||
|
||||
@Test
|
||||
public void testInboundJsonTupleConversion() throws Exception {
|
||||
testProcessor.input().send(MessageBuilder.withPayload("{'name':'foo'}")
|
||||
.build());
|
||||
this.testProcessor.input()
|
||||
.send(MessageBuilder.withPayload("{'name':'foo'}").build());
|
||||
@SuppressWarnings("unchecked")
|
||||
Message<byte[]> received = (Message<byte[]>) ((TestSupportBinder) binderFactory.getBinder(null, MessageChannel.class))
|
||||
.messageCollector().forChannel(testProcessor.output()).poll(1, TimeUnit.SECONDS);
|
||||
Message<byte[]> received = (Message<byte[]>) ((TestSupportBinder) this.binderFactory
|
||||
.getBinder(null, MessageChannel.class)).messageCollector()
|
||||
.forChannel(this.testProcessor.output())
|
||||
.poll(1, TimeUnit.SECONDS);
|
||||
assertThat(received).isNotNull();
|
||||
String payload = new String(received.getPayload(), StandardCharsets.UTF_8);
|
||||
assertThat(TupleBuilder.fromString(payload)).isEqualTo(TupleBuilder.tuple().of("name", "foo"));
|
||||
assertThat(TupleBuilder.fromString(payload))
|
||||
.isEqualTo(TupleBuilder.tuple().of("name", "foo"));
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
|
||||
@@ -43,30 +43,35 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* @author Oleg Zhurakousky
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = { LegacyContentTypeTests.LegacyTestSink.class})
|
||||
@SpringBootTest(classes = { LegacyContentTypeTests.LegacyTestSink.class })
|
||||
public class LegacyContentTypeTests {
|
||||
|
||||
@Autowired
|
||||
private Sink testSink;
|
||||
|
||||
@Test
|
||||
public void testOriginalContentTypeIsRetrievedForLegacyContentHeaderType() throws Exception {
|
||||
public void testOriginalContentTypeIsRetrievedForLegacyContentHeaderType()
|
||||
throws Exception {
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
MessageHandler messageHandler = new MessageHandler() {
|
||||
@Override
|
||||
public void handleMessage(Message<?> message) throws MessagingException {
|
||||
assertThat(message.getPayload()).isInstanceOf(byte[].class);
|
||||
assertThat(((byte[])message.getPayload())).isEqualTo("{\"message\":\"Hi\"}".getBytes(StandardCharsets.UTF_8));
|
||||
assertThat(message.getHeaders().get(MessageHeaders.CONTENT_TYPE).toString()).isEqualTo("application/json");
|
||||
assertThat(((byte[]) message.getPayload())).isEqualTo(
|
||||
"{\"message\":\"Hi\"}".getBytes(StandardCharsets.UTF_8));
|
||||
assertThat(
|
||||
message.getHeaders().get(MessageHeaders.CONTENT_TYPE).toString())
|
||||
.isEqualTo("application/json");
|
||||
latch.countDown();
|
||||
}
|
||||
};
|
||||
testSink.input().subscribe(messageHandler);
|
||||
testSink.input().send(MessageBuilder.withPayload("{\"message\":\"Hi\"}".getBytes())
|
||||
.setHeader(BinderHeaders.BINDER_ORIGINAL_CONTENT_TYPE, "application/json")
|
||||
.build());
|
||||
this.testSink.input().subscribe(messageHandler);
|
||||
this.testSink.input().send(MessageBuilder
|
||||
.withPayload("{\"message\":\"Hi\"}".getBytes())
|
||||
.setHeader(BinderHeaders.BINDER_ORIGINAL_CONTENT_TYPE, "application/json")
|
||||
.build());
|
||||
assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
|
||||
testSink.input().unsubscribe(messageHandler);
|
||||
this.testSink.input().unsubscribe(messageHandler);
|
||||
}
|
||||
|
||||
@EnableBinding(Sink.class)
|
||||
@@ -74,4 +79,5 @@ public class LegacyContentTypeTests {
|
||||
public static class LegacyTestSink {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -45,7 +45,6 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
/**
|
||||
* @author Ilayaperumal Gopinathan
|
||||
@@ -54,7 +53,8 @@ import static org.junit.Assert.assertNull;
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = { MessageChannelConfigurerTests.TestSink.class,
|
||||
MessageChannelConfigurerTests.TestSource.class, SpelExpressionConverterConfiguration.class})
|
||||
MessageChannelConfigurerTests.TestSource.class,
|
||||
SpelExpressionConverterConfiguration.class })
|
||||
public class MessageChannelConfigurerTests {
|
||||
|
||||
@Autowired
|
||||
@@ -71,8 +71,10 @@ public class MessageChannelConfigurerTests {
|
||||
|
||||
@Test
|
||||
public void testChannelTypes() throws Exception {
|
||||
DirectWithAttributesChannel inputChannel = (DirectWithAttributesChannel) testSink.input();
|
||||
DirectWithAttributesChannel outputChannel = (DirectWithAttributesChannel) testSource.output();
|
||||
DirectWithAttributesChannel inputChannel = (DirectWithAttributesChannel) this.testSink
|
||||
.input();
|
||||
DirectWithAttributesChannel outputChannel = (DirectWithAttributesChannel) this.testSource
|
||||
.output();
|
||||
assertThat(inputChannel.getAttribute("type")).isEqualTo(Sink.INPUT);
|
||||
assertThat(outputChannel.getAttribute("type")).isEqualTo(Source.OUTPUT);
|
||||
}
|
||||
@@ -85,42 +87,48 @@ public class MessageChannelConfigurerTests {
|
||||
assertThat(message.getPayload()).isEqualTo("{\"message\":\"Hi\"}".getBytes());
|
||||
latch.countDown();
|
||||
};
|
||||
testSink.input().subscribe(messageHandler);
|
||||
testSink.input().send(MessageBuilder.withPayload("{\"message\":\"Hi\"}".getBytes()).build());
|
||||
this.testSink.input().subscribe(messageHandler);
|
||||
this.testSink.input().send(
|
||||
MessageBuilder.withPayload("{\"message\":\"Hi\"}".getBytes()).build());
|
||||
assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
|
||||
testSink.input().unsubscribe(messageHandler);
|
||||
this.testSink.input().unsubscribe(messageHandler);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testObjectMapperConfig() throws Exception {
|
||||
CompositeMessageConverter converters = (CompositeMessageConverter) messageConverterFactory
|
||||
CompositeMessageConverter converters = (CompositeMessageConverter) this.messageConverterFactory
|
||||
.getMessageConverterForType(MimeTypeUtils.APPLICATION_JSON);
|
||||
for (MessageConverter converter : converters.getConverters()) {
|
||||
DirectFieldAccessor converterAccessor = new DirectFieldAccessor(converter);
|
||||
ObjectMapper objectMapper = (ObjectMapper) converterAccessor.getPropertyValue("objectMapper");
|
||||
ObjectMapper objectMapper = (ObjectMapper) converterAccessor
|
||||
.getPropertyValue("objectMapper");
|
||||
// assert that the ObjectMapper used by the converters is compliant with the
|
||||
// Boot configuration
|
||||
assertThat(!objectMapper.getSerializationConfig().isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS))
|
||||
.withFailMessage("SerializationFeature 'WRITE_DATES_AS_TIMESTAMPS' should be disabled");
|
||||
assertThat(!objectMapper.getSerializationConfig().isEnabled(
|
||||
SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)).withFailMessage(
|
||||
"SerializationFeature 'WRITE_DATES_AS_TIMESTAMPS' should be disabled");
|
||||
// assert that the globally set bean is used by the converters
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPartitionHeader() throws Exception {
|
||||
this.testSource.output().send(MessageBuilder.withPayload("{\"message\":\"Hi\"}").build());
|
||||
Message<?> message = this.messageCollector.forChannel(testSource.output()).poll(1, TimeUnit.SECONDS);
|
||||
this.testSource.output()
|
||||
.send(MessageBuilder.withPayload("{\"message\":\"Hi\"}").build());
|
||||
Message<?> message = this.messageCollector.forChannel(this.testSource.output())
|
||||
.poll(1, TimeUnit.SECONDS);
|
||||
assertThat(message.getHeaders().get(BinderHeaders.PARTITION_HEADER).equals(0));
|
||||
assertNull(message.getHeaders().get(BinderHeaders.PARTITION_OVERRIDE));
|
||||
assertThat(message.getHeaders().get(BinderHeaders.PARTITION_OVERRIDE)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPartitionHeaderWithPartitionOverride() throws Exception {
|
||||
this.testSource.output().send(MessageBuilder.withPayload("{\"message\":\"Hi\"}")
|
||||
.setHeader(BinderHeaders.PARTITION_OVERRIDE, 123).build());
|
||||
Message<?> message = this.messageCollector.forChannel(testSource.output()).poll(1, TimeUnit.SECONDS);
|
||||
Message<?> message = this.messageCollector.forChannel(this.testSource.output())
|
||||
.poll(1, TimeUnit.SECONDS);
|
||||
assertThat(message.getHeaders().get(BinderHeaders.PARTITION_HEADER).equals(123));
|
||||
assertNull(message.getHeaders().get(BinderHeaders.PARTITION_OVERRIDE));
|
||||
assertThat(message.getHeaders().get(BinderHeaders.PARTITION_OVERRIDE)).isNull();
|
||||
}
|
||||
|
||||
@EnableBinding(Sink.class)
|
||||
@@ -136,4 +144,5 @@ public class MessageChannelConfigurerTests {
|
||||
public static class TestSource {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -41,19 +41,21 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* @author Soby Chacko
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = { MessageChannelWithNativeDecodingTests.NativeDecodingSink.class})
|
||||
@SpringBootTest(classes = {
|
||||
MessageChannelWithNativeDecodingTests.NativeDecodingSink.class })
|
||||
public class MessageChannelWithNativeDecodingTests {
|
||||
|
||||
@Autowired
|
||||
private Sink nativeDecodingSink;
|
||||
|
||||
@Test
|
||||
public void testMessageConverterInterceptorsAreSkippedWhenNativeDecodingIsEnabled() throws Exception {
|
||||
public void testMessageConverterInterceptorsAreSkippedWhenNativeDecodingIsEnabled()
|
||||
throws Exception {
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
|
||||
byte[] serializedData;
|
||||
ObjectOutput out;
|
||||
try(ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
|
||||
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
|
||||
out = new ObjectOutputStream(bos);
|
||||
out.writeObject(123);
|
||||
out.flush();
|
||||
@@ -61,17 +63,18 @@ public class MessageChannelWithNativeDecodingTests {
|
||||
}
|
||||
|
||||
MessageHandler messageHandler = message -> {
|
||||
//ensure that the data is not deserialized becasue of native decoding
|
||||
//and the content type set in the properties file didn't take any effect
|
||||
// ensure that the data is not deserialized becasue of native decoding
|
||||
// and the content type set in the properties file didn't take any effect
|
||||
assertThat(message.getPayload()).isInstanceOf(byte[].class);
|
||||
assertThat(message.getPayload()).isEqualTo(serializedData);
|
||||
latch.countDown();
|
||||
};
|
||||
nativeDecodingSink.input().subscribe(messageHandler);
|
||||
this.nativeDecodingSink.input().subscribe(messageHandler);
|
||||
|
||||
nativeDecodingSink.input().send(MessageBuilder.withPayload(serializedData).build());
|
||||
this.nativeDecodingSink.input()
|
||||
.send(MessageBuilder.withPayload(serializedData).build());
|
||||
assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
|
||||
nativeDecodingSink.input().unsubscribe(messageHandler);
|
||||
this.nativeDecodingSink.input().unsubscribe(messageHandler);
|
||||
}
|
||||
|
||||
@EnableBinding(Sink.class)
|
||||
@@ -80,4 +83,5 @@ public class MessageChannelWithNativeDecodingTests {
|
||||
public static class NativeDecodingSink {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -39,7 +39,8 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* @author Soby Chacko
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = { MessageChannelWithNativeEncodingTests.NativeEncodingSource.class})
|
||||
@SpringBootTest(classes = {
|
||||
MessageChannelWithNativeEncodingTests.NativeEncodingSource.class })
|
||||
public class MessageChannelWithNativeEncodingTests {
|
||||
|
||||
@Autowired
|
||||
@@ -49,11 +50,15 @@ public class MessageChannelWithNativeEncodingTests {
|
||||
private MessageCollector messageCollector;
|
||||
|
||||
@Test
|
||||
public void testOutboundContentTypeInterceptorIsSkippedWhenNativeEncodingIsEnabled() throws Exception {
|
||||
this.nativeEncodingSource.output().send(MessageBuilder.withPayload("hello foobar!").build());
|
||||
Message<?> message = this.messageCollector.forChannel(this.nativeEncodingSource.output()).poll(1, TimeUnit.SECONDS);
|
||||
//should not convert the payload to byte[] even though we set a contentType on the channel.
|
||||
//This is becasue, we are using native encoding.
|
||||
public void testOutboundContentTypeInterceptorIsSkippedWhenNativeEncodingIsEnabled()
|
||||
throws Exception {
|
||||
this.nativeEncodingSource.output()
|
||||
.send(MessageBuilder.withPayload("hello foobar!").build());
|
||||
Message<?> message = this.messageCollector
|
||||
.forChannel(this.nativeEncodingSource.output()).poll(1, TimeUnit.SECONDS);
|
||||
// should not convert the payload to byte[] even though we set a contentType on
|
||||
// the channel.
|
||||
// This is becasue, we are using native encoding.
|
||||
assertThat(message.getPayload()).isInstanceOf(String.class);
|
||||
assertThat(message.getPayload()).isEqualTo("hello foobar!");
|
||||
assertThat(message.getHeaders().get(MessageHeaders.CONTENT_TYPE)).isNull();
|
||||
@@ -65,4 +70,5 @@ public class MessageChannelWithNativeEncodingTests {
|
||||
public static class NativeEncodingSource {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ import org.springframework.messaging.handler.annotation.support.MethodArgumentNo
|
||||
import org.springframework.util.MimeType;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.springframework.cloud.stream.binding.StreamListenerErrorMessages.INVALID_DECLARATIVE_METHOD_PARAMETERS;
|
||||
|
||||
/**
|
||||
@@ -63,33 +63,39 @@ public class StreamListenerAnnotatedMethodArgumentsTests {
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testAnnotatedArguments() {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(TestPojoWithAnnotatedArguments.class,
|
||||
"--server.port=0");
|
||||
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("{\"foo\":\"barbar" + id + "\"}")
|
||||
.setHeader("contentType", MimeType.valueOf("application/json")).setHeader("testHeader", "testValue").build());
|
||||
sink.input()
|
||||
.send(MessageBuilder.withPayload("{\"foo\":\"barbar" + id + "\"}")
|
||||
.setHeader("contentType", MimeType.valueOf("application/json"))
|
||||
.setHeader("testHeader", "testValue").build());
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedArguments).hasSize(3);
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedArguments.get(0))
|
||||
.isInstanceOf(StreamListenerTestUtils.FooPojo.class);
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedArguments.get(0)).hasFieldOrPropertyWithValue("foo",
|
||||
"barbar" + id);
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedArguments.get(1)).isInstanceOf(Map.class);
|
||||
assertThat((Map<String, Object>) testPojoWithAnnotatedArguments.receivedArguments.get(1))
|
||||
.containsEntry(MessageHeaders.CONTENT_TYPE, MimeType.valueOf("application/json"));
|
||||
assertThat((Map<String, String>) testPojoWithAnnotatedArguments.receivedArguments.get(1))
|
||||
.containsEntry("testHeader", "testValue");
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedArguments.get(2)).isEqualTo("application/json");
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedArguments.get(0))
|
||||
.hasFieldOrPropertyWithValue("foo", "barbar" + id);
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedArguments.get(1))
|
||||
.isInstanceOf(Map.class);
|
||||
assertThat((Map<String, Object>) testPojoWithAnnotatedArguments.receivedArguments
|
||||
.get(1)).containsEntry(MessageHeaders.CONTENT_TYPE,
|
||||
MimeType.valueOf("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() {
|
||||
try {
|
||||
SpringApplication.run(TestPojoWithInvalidInputAnnotatedArgument.class, "--server.port=0");
|
||||
SpringApplication.run(TestPojoWithInvalidInputAnnotatedArgument.class,
|
||||
"--server.port=0");
|
||||
fail("Exception expected: " + INVALID_DECLARATIVE_METHOD_PARAMETERS);
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
@@ -99,30 +105,36 @@ public class StreamListenerAnnotatedMethodArgumentsTests {
|
||||
|
||||
@Test
|
||||
public void testValidAnnotationAtMethodParameterWithPojoThatPassesValidation() {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(TestPojoWithValidAnnotationThatPassesValidation.class,
|
||||
"--server.port=0");
|
||||
ConfigurableApplicationContext context = SpringApplication.run(
|
||||
TestPojoWithValidAnnotationThatPassesValidation.class, "--server.port=0");
|
||||
|
||||
TestPojoWithValidAnnotationThatPassesValidation testPojoWithValidAnnotationThatPassesValidation = context.getBean(TestPojoWithValidAnnotationThatPassesValidation.class);
|
||||
TestPojoWithValidAnnotationThatPassesValidation testPojoWithValidAnnotationThatPassesValidation = context
|
||||
.getBean(TestPojoWithValidAnnotationThatPassesValidation.class);
|
||||
Sink sink = context.getBean(Sink.class);
|
||||
String id = UUID.randomUUID().toString();
|
||||
sink.input().send(MessageBuilder.withPayload("{\"foo\":\"" + id + "\"}")
|
||||
.setHeader("contentType", MimeType.valueOf("application/json")).build());
|
||||
assertThat(testPojoWithValidAnnotationThatPassesValidation.receivedArguments.get(0)).hasFieldOrPropertyWithValue("foo", id);
|
||||
assertThat(
|
||||
testPojoWithValidAnnotationThatPassesValidation.receivedArguments.get(0))
|
||||
.hasFieldOrPropertyWithValue("foo", id);
|
||||
context.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidAnnotationAtMethodParameterWithPojoThatFailsValidation() {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(TestPojoWithValidAnnotationThatPassesValidation.class,
|
||||
"--server.port=0");
|
||||
ConfigurableApplicationContext context = SpringApplication.run(
|
||||
TestPojoWithValidAnnotationThatPassesValidation.class, "--server.port=0");
|
||||
|
||||
Sink sink = context.getBean(Sink.class);
|
||||
try {
|
||||
sink.input().send(MessageBuilder.withPayload("{\"foo\":\"\"}")
|
||||
.setHeader("contentType", MimeType.valueOf("application/json")).build());
|
||||
.setHeader("contentType", MimeType.valueOf("application/json"))
|
||||
.build());
|
||||
fail("Exception expected: MethodArgumentNotValidException!");
|
||||
} catch(MethodArgumentNotValidException e) {
|
||||
assertThat(e.getMessage()).contains("default message [foo]]; default message [must not be blank]]");
|
||||
}
|
||||
catch (MethodArgumentNotValidException e) {
|
||||
assertThat(e.getMessage()).contains(
|
||||
"default message [foo]]; default message [must not be blank]]");
|
||||
}
|
||||
context.close();
|
||||
}
|
||||
@@ -141,6 +153,7 @@ public class StreamListenerAnnotatedMethodArgumentsTests {
|
||||
this.receivedArguments.add(headers);
|
||||
this.receivedArguments.add(contentType);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding(Sink.class)
|
||||
@@ -158,6 +171,7 @@ public class StreamListenerAnnotatedMethodArgumentsTests {
|
||||
this.receivedArguments.add(headers);
|
||||
this.receivedArguments.add(contentType);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@@ -167,9 +181,11 @@ public class StreamListenerAnnotatedMethodArgumentsTests {
|
||||
List<Object> receivedArguments = new ArrayList<>();
|
||||
|
||||
@StreamListener(Processor.INPUT)
|
||||
public void receive(@Valid StreamListenerTestUtils.PojoWithValidation pojoWithValidation) {
|
||||
public void receive(
|
||||
@Valid StreamListenerTestUtils.PojoWithValidation pojoWithValidation) {
|
||||
this.receivedArguments.add(pojoWithValidation);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -49,22 +49,22 @@ public class StreamListenerAnnotationBeanPostProcessorOverrideTest {
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testOverrideStreamListenerAnnotationBeanPostProcessor() throws Exception {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(TestPojoWithAnnotatedArguments.class,
|
||||
"--server.port=0");
|
||||
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("{\"foo\":\"barbar" + id + "\"}")
|
||||
.setHeader("contentType", "application/json").setHeader("testHeader", "testValue")
|
||||
.setHeader("type", "foo").build());
|
||||
.setHeader("contentType", "application/json")
|
||||
.setHeader("testHeader", "testValue").setHeader("type", "foo").build());
|
||||
sink.input().send(MessageBuilder.withPayload("{\"bar\":\"foofoo" + id + "\"}")
|
||||
.setHeader("contentType", "application/json").setHeader("testHeader", "testValue")
|
||||
.setHeader("type", "bar").build());
|
||||
.setHeader("contentType", "application/json")
|
||||
.setHeader("testHeader", "testValue").setHeader("type", "bar").build());
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedFoo).hasSize(1);
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedFoo.get(0)).hasFieldOrPropertyWithValue("foo",
|
||||
"barbar" + id);
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedFoo.get(0))
|
||||
.hasFieldOrPropertyWithValue("foo", "barbar" + id);
|
||||
context.close();
|
||||
}
|
||||
|
||||
@@ -82,12 +82,14 @@ public class StreamListenerAnnotationBeanPostProcessorOverrideTest {
|
||||
public static StreamListenerAnnotationBeanPostProcessor streamListenerAnnotationBeanPostProcessor() {
|
||||
return new StreamListenerAnnotationBeanPostProcessor() {
|
||||
@Override
|
||||
protected StreamListener postProcessAnnotation(StreamListener originalAnnotation,
|
||||
Method annotatedMethod) {
|
||||
protected StreamListener postProcessAnnotation(
|
||||
StreamListener originalAnnotation, Method annotatedMethod) {
|
||||
Map<String, Object> attributes = new HashMap<>(
|
||||
AnnotationUtils.getAnnotationAttributes(originalAnnotation));
|
||||
attributes.put("condition", "headers['type']=='" + originalAnnotation.condition() + "'");
|
||||
return AnnotationUtils.synthesizeAnnotation(attributes, StreamListener.class, annotatedMethod);
|
||||
attributes.put("condition",
|
||||
"headers['type']=='" + originalAnnotation.condition() + "'");
|
||||
return AnnotationUtils.synthesizeAnnotation(attributes,
|
||||
StreamListener.class, annotatedMethod);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -96,5 +98,7 @@ public class StreamListenerAnnotationBeanPostProcessorOverrideTest {
|
||||
public void receive(@Payload StreamListenerTestUtils.FooPojo fooPojo) {
|
||||
this.receivedFoo.add(fooPojo);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
* Copyright 2017-2019 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.
|
||||
@@ -46,6 +46,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
@Documented
|
||||
@StreamListener
|
||||
@interface EventHandler {
|
||||
|
||||
/**
|
||||
* The name of the binding target (e.g. channel) that the method subscribes to.
|
||||
* @return the name of the binding target.
|
||||
@@ -76,37 +77,37 @@ public class StreamListenerAsMetaAnnotationTests {
|
||||
|
||||
@Test
|
||||
public void testCustomAnnotation() {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(TestPojoWithCustomAnnotatedArguments.class,
|
||||
"--server.port=0");
|
||||
ConfigurableApplicationContext context = SpringApplication
|
||||
.run(TestPojoWithCustomAnnotatedArguments.class, "--server.port=0");
|
||||
|
||||
TestPojoWithCustomAnnotatedArguments testPojoWithAnnotatedArguments = context
|
||||
.getBean(TestPojoWithCustomAnnotatedArguments.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")
|
||||
.setHeader("type", "foo").build());
|
||||
.setHeader("contentType", "application/json")
|
||||
.setHeader("testHeader", "testValue").setHeader("type", "foo").build());
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedFoo).hasSize(1);
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedFoo.get(0)).hasFieldOrPropertyWithValue("foo",
|
||||
"barbar" + id);
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedFoo.get(0))
|
||||
.hasFieldOrPropertyWithValue("foo", "barbar" + id);
|
||||
context.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAnnotation() {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(TestPojoWithAnnotatedArguments.class,
|
||||
"--server.port=0");
|
||||
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("{\"foo\":\"barbar" + id + "\"}")
|
||||
.setHeader("contentType", "application/json").setHeader("testHeader", "testValue")
|
||||
.setHeader("type", "foo").build());
|
||||
.setHeader("contentType", "application/json")
|
||||
.setHeader("testHeader", "testValue").setHeader("type", "foo").build());
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedFoo).hasSize(1);
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedFoo.get(0)).hasFieldOrPropertyWithValue("foo",
|
||||
"barbar" + id);
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedFoo.get(0))
|
||||
.hasFieldOrPropertyWithValue("foo", "barbar" + id);
|
||||
context.close();
|
||||
}
|
||||
|
||||
@@ -122,6 +123,7 @@ public class StreamListenerAsMetaAnnotationTests {
|
||||
public void receive(@Payload StreamListenerTestUtils.FooPojo fooPojo) {
|
||||
this.receivedFoo.add(fooPojo);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding(Sink.class)
|
||||
@@ -136,5 +138,7 @@ public class StreamListenerAsMetaAnnotationTests {
|
||||
public void receive(@Payload StreamListenerTestUtils.FooPojo fooPojo) {
|
||||
this.receivedFoo.add(fooPojo);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
* Copyright 2016-2019 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.
|
||||
@@ -42,15 +42,15 @@ public class StreamListenerContentTypeConversionTests {
|
||||
|
||||
@Test
|
||||
public void testContentTypeConversion() throws Exception {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(TestSinkWithContentTypeConversion.class,
|
||||
"--server.port=0");
|
||||
ConfigurableApplicationContext context = SpringApplication
|
||||
.run(TestSinkWithContentTypeConversion.class, "--server.port=0");
|
||||
@SuppressWarnings("unchecked")
|
||||
TestSinkWithContentTypeConversion testSink = context.getBean(TestSinkWithContentTypeConversion.class);
|
||||
TestSinkWithContentTypeConversion testSink = context
|
||||
.getBean(TestSinkWithContentTypeConversion.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());
|
||||
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",
|
||||
@@ -71,6 +71,7 @@ public class StreamListenerContentTypeConversionTests {
|
||||
this.receivedArguments.add(fooPojo);
|
||||
this.latch.countDown();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.handler.annotation.SendTo;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
|
||||
/**
|
||||
* @author Marius Bogoevici
|
||||
@@ -44,11 +44,13 @@ public class StreamListenerDuplicateMappingTests {
|
||||
public void testMultipleMappingsWithReturnValue() {
|
||||
ConfigurableApplicationContext context = null;
|
||||
try {
|
||||
context = SpringApplication.run(TestMultipleMappingsWithReturnValue.class, "--server.port=0");
|
||||
context = SpringApplication.run(TestMultipleMappingsWithReturnValue.class,
|
||||
"--server.port=0");
|
||||
fail("Exception expected on duplicate mapping");
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
assertThat(e.getMessage()).startsWith(StreamListenerErrorMessages.MULTIPLE_VALUE_RETURNING_METHODS);
|
||||
assertThat(e.getMessage()).startsWith(
|
||||
StreamListenerErrorMessages.MULTIPLE_VALUE_RETURNING_METHODS);
|
||||
}
|
||||
finally {
|
||||
if (context != null) {
|
||||
@@ -61,11 +63,14 @@ public class StreamListenerDuplicateMappingTests {
|
||||
public void testDuplicateMappingFromAbstractMethod() {
|
||||
ConfigurableApplicationContext context = null;
|
||||
try {
|
||||
context = SpringApplication.run(TestDuplicateMappingFromAbstractMethod.class, "--server.port=0");
|
||||
context = SpringApplication.run(TestDuplicateMappingFromAbstractMethod.class,
|
||||
"--server.port=0");
|
||||
}
|
||||
catch (BeanCreationException e) {
|
||||
String errorMessage = e.getCause().getMessage().startsWith("Duplicate @StreamListener mapping")
|
||||
? "Duplicate mapping exception is not expected" : "Test failed with exception";
|
||||
String errorMessage = e.getCause().getMessage()
|
||||
.startsWith("Duplicate @StreamListener mapping")
|
||||
? "Duplicate mapping exception is not expected"
|
||||
: "Test failed with exception";
|
||||
fail(errorMessage + ": " + e.getMessage());
|
||||
}
|
||||
finally {
|
||||
@@ -76,7 +81,9 @@ public class StreamListenerDuplicateMappingTests {
|
||||
}
|
||||
|
||||
public interface GenericSink<T extends Base> {
|
||||
|
||||
void testMethod(T msg);
|
||||
|
||||
}
|
||||
|
||||
public interface Base {
|
||||
@@ -98,16 +105,19 @@ public class StreamListenerDuplicateMappingTests {
|
||||
public String receiveDuplicateMapping(Message<String> fooMessage) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding(Sink.class)
|
||||
@EnableAutoConfiguration
|
||||
public static class TestDuplicateMappingFromAbstractMethod implements GenericSink<TestBase> {
|
||||
public static class TestDuplicateMappingFromAbstractMethod
|
||||
implements GenericSink<TestBase> {
|
||||
|
||||
@Override
|
||||
@StreamListener(Sink.INPUT)
|
||||
public void testMethod(TestBase msg) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class TestBase implements Base {
|
||||
|
||||
@@ -75,15 +75,15 @@ public class StreamListenerHandlerBeanTests {
|
||||
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 + "\"}")
|
||||
processor.input()
|
||||
.send(MessageBuilder.withPayload("{\"foo\":\"barbar" + id + "\"}")
|
||||
.setHeader("contentType", "application/json").build());
|
||||
HandlerBean handlerBean = context.getBean(HandlerBean.class);
|
||||
Assertions.assertThat(handlerBean.receivedPojos).hasSize(1);
|
||||
Assertions.assertThat(handlerBean.receivedPojos.get(0)).hasFieldOrPropertyWithValue("foo",
|
||||
"barbar" + id);
|
||||
Message<String> message = (Message<String>) collector.forChannel(
|
||||
processor.output()).poll(1, TimeUnit.SECONDS);
|
||||
Assertions.assertThat(handlerBean.receivedPojos.get(0))
|
||||
.hasFieldOrPropertyWithValue("foo", "barbar" + id);
|
||||
Message<String> message = (Message<String>) collector
|
||||
.forChannel(processor.output()).poll(1, TimeUnit.SECONDS);
|
||||
assertThat(message).isNotNull();
|
||||
assertThat(message.getPayload()).isEqualTo("{\"bar\":\"barbar" + id + "\"}");
|
||||
assertThat(message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class)
|
||||
@@ -99,6 +99,7 @@ public class StreamListenerHandlerBeanTests {
|
||||
public HandlerBeanWithSendTo handlerBean() {
|
||||
return new HandlerBeanWithSendTo();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@@ -109,30 +110,35 @@ public class StreamListenerHandlerBeanTests {
|
||||
public HandlerBeanWithOutput handlerBean() {
|
||||
return new HandlerBeanWithOutput();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class HandlerBeanWithSendTo extends HandlerBean {
|
||||
|
||||
@StreamListener(Processor.INPUT)
|
||||
@SendTo(Processor.OUTPUT)
|
||||
public StreamListenerTestUtils.BarPojo receive(StreamListenerTestUtils.FooPojo fooMessage) {
|
||||
public StreamListenerTestUtils.BarPojo receive(
|
||||
StreamListenerTestUtils.FooPojo fooMessage) {
|
||||
this.receivedPojos.add(fooMessage);
|
||||
StreamListenerTestUtils.BarPojo barPojo = new StreamListenerTestUtils.BarPojo();
|
||||
barPojo.setBar(fooMessage.getFoo());
|
||||
return barPojo;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class HandlerBeanWithOutput extends HandlerBean {
|
||||
|
||||
@StreamListener(Processor.INPUT)
|
||||
@Output(Processor.OUTPUT)
|
||||
public StreamListenerTestUtils.BarPojo receive(StreamListenerTestUtils.FooPojo fooMessage) {
|
||||
public StreamListenerTestUtils.BarPojo receive(
|
||||
StreamListenerTestUtils.FooPojo fooMessage) {
|
||||
this.receivedPojos.add(fooMessage);
|
||||
StreamListenerTestUtils.BarPojo barPojo = new StreamListenerTestUtils.BarPojo();
|
||||
barPojo.setBar(fooMessage.getFoo());
|
||||
return barPojo;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class HandlerBean {
|
||||
|
||||
@@ -47,7 +47,7 @@ import org.springframework.messaging.handler.annotation.SendTo;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.assertj.core.api.Assertions.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_DECLARATIVE_METHOD_PARAMETERS;
|
||||
@@ -82,8 +82,8 @@ public class StreamListenerHandlerMethodTests {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testMethodWithObjectAsMethodArgument() throws Exception {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(TestMethodWithObjectAsMethodArgument.class,
|
||||
"--server.port=0",
|
||||
ConfigurableApplicationContext context = SpringApplication.run(
|
||||
TestMethodWithObjectAsMethodArgument.class, "--server.port=0",
|
||||
"--spring.jmx.enabled=false",
|
||||
"--spring.cloud.stream.bindings.input.contentType=text/plain",
|
||||
"--spring.cloud.stream.bindings.output.contentType=text/plain");
|
||||
@@ -91,7 +91,8 @@ public class StreamListenerHandlerMethodTests {
|
||||
final String testMessage = "testing";
|
||||
processor.input().send(MessageBuilder.withPayload(testMessage).build());
|
||||
MessageCollector messageCollector = context.getBean(MessageCollector.class);
|
||||
Message<String> result = (Message<String>) messageCollector.forChannel(processor.output()).poll(1000, TimeUnit.MILLISECONDS);
|
||||
Message<String> result = (Message<String>) messageCollector
|
||||
.forChannel(processor.output()).poll(1000, TimeUnit.MILLISECONDS);
|
||||
assertThat(result).isNotNull();
|
||||
assertThat(result.getPayload()).isEqualTo(testMessage.toUpperCase());
|
||||
context.close();
|
||||
@@ -100,23 +101,24 @@ public class StreamListenerHandlerMethodTests {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
/**
|
||||
* @since 2.0 : This test is an example of the new behavior of 2.0 when it comes to contentType handling.
|
||||
* The default contentType being JSON in order to be able to check a message without quotes the user needs to set the input/output contentType accordingly
|
||||
* Also, received messages are always of Message<byte[]> now.
|
||||
* @since 2.0 : This test is an example of the new behavior of 2.0 when it comes to
|
||||
* contentType handling. The default contentType being JSON in order to be able to
|
||||
* check a message without quotes the user needs to set the input/output contentType
|
||||
* accordingly Also, received messages are always of Message<byte[]> now.
|
||||
*/
|
||||
public void testMethodHeadersPropagatged() throws Exception {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(TestMethodHeadersPropagated.class,
|
||||
"--server.port=0",
|
||||
ConfigurableApplicationContext context = SpringApplication.run(
|
||||
TestMethodHeadersPropagated.class, "--server.port=0",
|
||||
"--spring.jmx.enabled=false",
|
||||
"--spring.cloud.stream.bindings.input.contentType=text/plain",
|
||||
"--spring.cloud.stream.bindings.output.contentType=text/plain");
|
||||
Processor processor = context.getBean(Processor.class);
|
||||
final String testMessage = "testing";
|
||||
processor.input().send(MessageBuilder.withPayload(testMessage)
|
||||
.setHeader("foo", "bar")
|
||||
.build());
|
||||
processor.input().send(
|
||||
MessageBuilder.withPayload(testMessage).setHeader("foo", "bar").build());
|
||||
MessageCollector messageCollector = context.getBean(MessageCollector.class);
|
||||
Message<String> result = (Message<String>) messageCollector.forChannel(processor.output()).poll(1000, TimeUnit.MILLISECONDS);
|
||||
Message<String> result = (Message<String>) messageCollector
|
||||
.forChannel(processor.output()).poll(1000, TimeUnit.MILLISECONDS);
|
||||
assertThat(result).isNotNull();
|
||||
assertThat(result.getPayload()).isEqualTo(testMessage.toUpperCase());
|
||||
assertThat(result.getHeaders().get("foo")).isEqualTo("bar");
|
||||
@@ -126,40 +128,40 @@ public class StreamListenerHandlerMethodTests {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testMethodHeadersNotPropagatged() throws Exception {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(TestMethodHeadersNotPropagated.class,
|
||||
"--server.port=0",
|
||||
ConfigurableApplicationContext context = SpringApplication.run(
|
||||
TestMethodHeadersNotPropagated.class, "--server.port=0",
|
||||
"--spring.jmx.enabled=false",
|
||||
"--spring.cloud.stream.bindings.input.contentType=text/plain",
|
||||
"--spring.cloud.stream.bindings.output.contentType=text/plain");
|
||||
Processor processor = context.getBean(Processor.class);
|
||||
final String testMessage = "testing";
|
||||
processor.input().send(MessageBuilder.withPayload(testMessage)
|
||||
.setHeader("foo", "bar")
|
||||
.build());
|
||||
processor.input().send(
|
||||
MessageBuilder.withPayload(testMessage).setHeader("foo", "bar").build());
|
||||
MessageCollector messageCollector = context.getBean(MessageCollector.class);
|
||||
Message<String> result = (Message<String>) messageCollector.forChannel(processor.output()).poll(1000, TimeUnit.MILLISECONDS);
|
||||
Message<String> result = (Message<String>) messageCollector
|
||||
.forChannel(processor.output()).poll(1000, TimeUnit.MILLISECONDS);
|
||||
assertThat(result).isNotNull();
|
||||
assertThat(result.getPayload()).isEqualTo(testMessage.toUpperCase());
|
||||
assertThat(result.getHeaders().get("foo")).isNull();
|
||||
context.close();
|
||||
}
|
||||
|
||||
|
||||
//TODO: Handle dynamic destinations and contentType
|
||||
// TODO: Handle dynamic destinations and contentType
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testStreamListenerMethodWithTargetBeanFromOutside() throws Exception {
|
||||
ConfigurableApplicationContext context = SpringApplication
|
||||
.run(TestStreamListenerMethodWithTargetBeanFromOutside.class, "--server.port=0",
|
||||
"--spring.jmx.enabled=false",
|
||||
"--spring.cloud.stream.bindings.input.contentType=text/plain",
|
||||
"--spring.cloud.stream.bindings.output.contentType=text/plain");
|
||||
ConfigurableApplicationContext context = SpringApplication.run(
|
||||
TestStreamListenerMethodWithTargetBeanFromOutside.class,
|
||||
"--server.port=0", "--spring.jmx.enabled=false",
|
||||
"--spring.cloud.stream.bindings.input.contentType=text/plain",
|
||||
"--spring.cloud.stream.bindings.output.contentType=text/plain");
|
||||
Sink sink = context.getBean(Sink.class);
|
||||
final String testMessageToSend = "testing";
|
||||
sink.input().send(MessageBuilder.withPayload(testMessageToSend).build());
|
||||
DirectChannel directChannel = (DirectChannel) context.getBean(testMessageToSend.toUpperCase(),
|
||||
MessageChannel.class);
|
||||
DirectChannel directChannel = (DirectChannel) context
|
||||
.getBean(testMessageToSend.toUpperCase(), MessageChannel.class);
|
||||
MessageCollector messageCollector = context.getBean(MessageCollector.class);
|
||||
Message<String> result = (Message<String>) messageCollector.forChannel(directChannel).poll(1000, TimeUnit.MILLISECONDS);
|
||||
Message<String> result = (Message<String>) messageCollector
|
||||
.forChannel(directChannel).poll(1000, TimeUnit.MILLISECONDS);
|
||||
sink.input().send(MessageBuilder.withPayload(testMessageToSend).build());
|
||||
assertThat(result).isNotNull();
|
||||
assertThat(result.getPayload()).isEqualTo(testMessageToSend.toUpperCase());
|
||||
@@ -169,8 +171,8 @@ public class StreamListenerHandlerMethodTests {
|
||||
@Test
|
||||
public void testInvalidReturnTypeWithSendToAndOutput() throws Exception {
|
||||
try {
|
||||
SpringApplication.run(TestReturnTypeWithMultipleOutput.class, "--server.port=0",
|
||||
"--spring.jmx.enabled=false");
|
||||
SpringApplication.run(TestReturnTypeWithMultipleOutput.class,
|
||||
"--server.port=0", "--spring.jmx.enabled=false");
|
||||
fail("Exception expected: " + RETURN_TYPE_MULTIPLE_OUTBOUND_SPECIFIED);
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
@@ -181,8 +183,8 @@ public class StreamListenerHandlerMethodTests {
|
||||
@Test
|
||||
public void testInvalidReturnTypeWithNoOutput() throws Exception {
|
||||
try {
|
||||
SpringApplication.run(TestInvalidReturnTypeWithNoOutput.class, "--server.port=0",
|
||||
"--spring.jmx.enabled=false");
|
||||
SpringApplication.run(TestInvalidReturnTypeWithNoOutput.class,
|
||||
"--server.port=0", "--spring.jmx.enabled=false");
|
||||
fail("Exception expected: " + RETURN_TYPE_NO_OUTBOUND_SPECIFIED);
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
@@ -193,8 +195,8 @@ public class StreamListenerHandlerMethodTests {
|
||||
@Test
|
||||
public void testInvalidInputAnnotationWithNoValue() throws Exception {
|
||||
try {
|
||||
SpringApplication.run(TestInvalidInputAnnotationWithNoValue.class, "--server.port=0",
|
||||
"--spring.jmx.enabled=false");
|
||||
SpringApplication.run(TestInvalidInputAnnotationWithNoValue.class,
|
||||
"--server.port=0", "--spring.jmx.enabled=false");
|
||||
fail("Exception expected: " + INVALID_INBOUND_NAME);
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
@@ -205,8 +207,8 @@ public class StreamListenerHandlerMethodTests {
|
||||
@Test
|
||||
public void testInvalidOutputAnnotationWithNoValue() throws Exception {
|
||||
try {
|
||||
SpringApplication.run(TestInvalidOutputAnnotationWithNoValue.class, "--server.port=0",
|
||||
"--spring.jmx.enabled=false");
|
||||
SpringApplication.run(TestInvalidOutputAnnotationWithNoValue.class,
|
||||
"--server.port=0", "--spring.jmx.enabled=false");
|
||||
fail("Exception expected: " + INVALID_OUTBOUND_NAME);
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
@@ -222,7 +224,8 @@ public class StreamListenerHandlerMethodTests {
|
||||
fail("Exception expected on using invalid inbound name");
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
assertThat(e.getMessage()).contains(StreamListenerErrorMessages.INVALID_DECLARATIVE_METHOD_PARAMETERS);
|
||||
assertThat(e.getMessage()).contains(
|
||||
StreamListenerErrorMessages.INVALID_DECLARATIVE_METHOD_PARAMETERS);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -246,7 +249,8 @@ public class StreamListenerHandlerMethodTests {
|
||||
fail("Exception expected: " + AMBIGUOUS_MESSAGE_HANDLER_METHOD_ARGUMENTS);
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
assertThat(e.getMessage()).contains(AMBIGUOUS_MESSAGE_HANDLER_METHOD_ARGUMENTS);
|
||||
assertThat(e.getMessage())
|
||||
.contains(AMBIGUOUS_MESSAGE_HANDLER_METHOD_ARGUMENTS);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -258,15 +262,16 @@ public class StreamListenerHandlerMethodTests {
|
||||
fail("Exception expected:" + AMBIGUOUS_MESSAGE_HANDLER_METHOD_ARGUMENTS);
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
assertThat(e.getMessage()).contains(AMBIGUOUS_MESSAGE_HANDLER_METHOD_ARGUMENTS);
|
||||
assertThat(e.getMessage())
|
||||
.contains(AMBIGUOUS_MESSAGE_HANDLER_METHOD_ARGUMENTS);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMethodWithInputAsMethodAndParameter() throws Exception {
|
||||
try {
|
||||
SpringApplication.run(TestMethodWithInputAsMethodAndParameter.class, "--server.port=0",
|
||||
"--spring.jmx.enabled=false");
|
||||
SpringApplication.run(TestMethodWithInputAsMethodAndParameter.class,
|
||||
"--server.port=0", "--spring.jmx.enabled=false");
|
||||
fail("Exception expected: " + INVALID_DECLARATIVE_METHOD_PARAMETERS);
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
@@ -277,8 +282,8 @@ public class StreamListenerHandlerMethodTests {
|
||||
@Test
|
||||
public void testMethodWithOutputAsMethodAndParameter() throws Exception {
|
||||
try {
|
||||
SpringApplication.run(TestMethodWithOutputAsMethodAndParameter.class, "--server.port=0",
|
||||
"--spring.jmx.enabled=false");
|
||||
SpringApplication.run(TestMethodWithOutputAsMethodAndParameter.class,
|
||||
"--server.port=0", "--spring.jmx.enabled=false");
|
||||
fail("Exception expected:" + INVALID_OUTPUT_VALUES);
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
@@ -300,8 +305,8 @@ public class StreamListenerHandlerMethodTests {
|
||||
|
||||
@Test
|
||||
public void testMethodWithMultipleInputParameters() throws Exception {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(TestMethodWithMultipleInputParameters.class,
|
||||
"--server.port=0",
|
||||
ConfigurableApplicationContext context = SpringApplication.run(
|
||||
TestMethodWithMultipleInputParameters.class, "--server.port=0",
|
||||
"--spring.jmx.enabled=false");
|
||||
Processor processor = context.getBean(Processor.class);
|
||||
StreamListenerTestUtils.FooInboundChannel1 inboundChannel2 = context
|
||||
@@ -310,22 +315,26 @@ public class StreamListenerHandlerMethodTests {
|
||||
((SubscribableChannel) processor.output()).subscribe(new MessageHandler() {
|
||||
@Override
|
||||
public void handleMessage(Message<?> message) throws MessagingException {
|
||||
Assert.isTrue(message.getPayload().equals("footesting") || message.getPayload().equals("BARTESTING"), "Assert failed");
|
||||
Assert.isTrue(
|
||||
message.getPayload().equals("footesting")
|
||||
|| message.getPayload().equals("BARTESTING"),
|
||||
"Assert failed");
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
processor.input().send(MessageBuilder.withPayload("{\"foo\":\"fooTESTing\"}")
|
||||
.setHeader("contentType", "application/json").build());
|
||||
inboundChannel2.input().send(MessageBuilder.withPayload("{\"bar\":\"bartestING\"}")
|
||||
.setHeader("contentType", "application/json").build());
|
||||
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",
|
||||
ConfigurableApplicationContext context = SpringApplication.run(
|
||||
TestMethodWithMultipleOutputParameters.class, "--server.port=0",
|
||||
"--spring.jmx.enabled=false");
|
||||
Processor processor = context.getBean(Processor.class);
|
||||
StreamListenerTestUtils.FooOutboundChannel1 source2 = context
|
||||
@@ -335,7 +344,8 @@ public class StreamListenerHandlerMethodTests {
|
||||
@Override
|
||||
public void handleMessage(Message<?> message) throws MessagingException {
|
||||
Assert.isTrue(message.getPayload().equals("testing"), "Assert failed");
|
||||
Assert.isTrue(message.getHeaders().get("output").equals("output2"), "Assert failed");
|
||||
Assert.isTrue(message.getHeaders().get("output").equals("output2"),
|
||||
"Assert failed");
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
@@ -343,12 +353,15 @@ public class StreamListenerHandlerMethodTests {
|
||||
@Override
|
||||
public void handleMessage(Message<?> message) throws MessagingException {
|
||||
Assert.isTrue(message.getPayload().equals("TESTING"), "Assert failed");
|
||||
Assert.isTrue(message.getHeaders().get("output").equals("output1"), "Assert failed");
|
||||
Assert.isTrue(message.getHeaders().get("output").equals("output1"),
|
||||
"Assert failed");
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
processor.input().send(MessageBuilder.withPayload("testING").setHeader("output", "output1").build());
|
||||
processor.input().send(MessageBuilder.withPayload("TESTing").setHeader("output", "output2").build());
|
||||
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();
|
||||
}
|
||||
@@ -366,15 +379,20 @@ public class StreamListenerHandlerMethodTests {
|
||||
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());
|
||||
.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());
|
||||
.withPayload(
|
||||
message.getPayload().toString().toLowerCase())
|
||||
.build());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding({ Sink.class })
|
||||
@@ -384,6 +402,7 @@ public class StreamListenerHandlerMethodTests {
|
||||
@StreamListener
|
||||
public void receive(StreamListenerTestUtils.FooPojo fooPojo) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding({ Processor.class })
|
||||
@@ -395,6 +414,7 @@ public class StreamListenerHandlerMethodTests {
|
||||
public String receive(Object received) {
|
||||
return received.toString().toUpperCase();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding({ Processor.class })
|
||||
@@ -430,13 +450,15 @@ public class StreamListenerHandlerMethodTests {
|
||||
@StreamListener(Sink.INPUT)
|
||||
@SendTo(ROUTER_QUEUE)
|
||||
public Message<String> convertMessageBody(Message<String> message) {
|
||||
return new DefaultMessageBuilderFactory().withPayload(message.getPayload().toUpperCase()).build();
|
||||
return new DefaultMessageBuilderFactory()
|
||||
.withPayload(message.getPayload().toUpperCase()).build();
|
||||
}
|
||||
|
||||
@Router(inputChannel = ROUTER_QUEUE)
|
||||
public String route(String message) {
|
||||
return message.toUpperCase();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding({ Sink.class })
|
||||
@@ -447,6 +469,7 @@ public class StreamListenerHandlerMethodTests {
|
||||
@Input(Sink.INPUT)
|
||||
public void receive(StreamListenerTestUtils.FooPojo fooPojo) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding({ Sink.class })
|
||||
@@ -454,8 +477,10 @@ public class StreamListenerHandlerMethodTests {
|
||||
public static class TestAmbiguousMethodArguments1 {
|
||||
|
||||
@StreamListener(Processor.INPUT)
|
||||
public void receive(@Payload StreamListenerTestUtils.FooPojo fooPojo, String value) {
|
||||
public void receive(@Payload StreamListenerTestUtils.FooPojo fooPojo,
|
||||
String value) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding({ Sink.class })
|
||||
@@ -466,6 +491,7 @@ public class StreamListenerHandlerMethodTests {
|
||||
public void receive(@Payload StreamListenerTestUtils.FooPojo fooPojo,
|
||||
@Payload StreamListenerTestUtils.BarPojo barPojo) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding({ Processor.class, StreamListenerTestUtils.FooOutboundChannel1.class })
|
||||
@@ -478,6 +504,7 @@ public class StreamListenerHandlerMethodTests {
|
||||
@Output(StreamListenerTestUtils.FooOutboundChannel1.OUTPUT) MessageChannel output2) {
|
||||
return "foo";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding({ Processor.class, StreamListenerTestUtils.FooOutboundChannel1.class })
|
||||
@@ -488,6 +515,7 @@ public class StreamListenerHandlerMethodTests {
|
||||
public String receive(@Input(Processor.INPUT) SubscribableChannel input1) {
|
||||
return "foo";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding({ Processor.class })
|
||||
@@ -497,6 +525,7 @@ public class StreamListenerHandlerMethodTests {
|
||||
@StreamListener
|
||||
public void receive(@Input SubscribableChannel input) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding({ Processor.class })
|
||||
@@ -504,8 +533,10 @@ public class StreamListenerHandlerMethodTests {
|
||||
public static class TestInvalidOutputAnnotationWithNoValue {
|
||||
|
||||
@StreamListener
|
||||
public void receive(@Input(Processor.OUTPUT) SubscribableChannel input, @Output MessageChannel output) {
|
||||
public void receive(@Input(Processor.OUTPUT) SubscribableChannel input,
|
||||
@Output MessageChannel output) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding({ Sink.class })
|
||||
@@ -515,6 +546,7 @@ public class StreamListenerHandlerMethodTests {
|
||||
@StreamListener
|
||||
public void receive(@Input("invalid") SubscribableChannel input) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding({ Processor.class })
|
||||
@@ -525,6 +557,7 @@ public class StreamListenerHandlerMethodTests {
|
||||
public void receive(@Input(Processor.INPUT) SubscribableChannel input,
|
||||
@Output("invalid") MessageChannel output) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding({ Sink.class })
|
||||
@@ -534,6 +567,7 @@ public class StreamListenerHandlerMethodTests {
|
||||
@StreamListener
|
||||
public void receive(@Input(Sink.INPUT) StreamListenerTestUtils.FooPojo fooPojo) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding({ Processor.class, StreamListenerTestUtils.FooOutboundChannel1.class })
|
||||
@@ -548,10 +582,12 @@ public class StreamListenerHandlerMethodTests {
|
||||
@Override
|
||||
public void handleMessage(Message<?> message) throws MessagingException {
|
||||
output1.send(org.springframework.messaging.support.MessageBuilder
|
||||
.withPayload(message.getPayload().toString().toUpperCase()).build());
|
||||
.withPayload(message.getPayload().toString().toUpperCase())
|
||||
.build());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding({ Processor.class, StreamListenerTestUtils.FooInboundChannel1.class })
|
||||
@@ -566,17 +602,20 @@ public class StreamListenerHandlerMethodTests {
|
||||
@Override
|
||||
public void handleMessage(Message<?> message) throws MessagingException {
|
||||
output.send(org.springframework.messaging.support.MessageBuilder
|
||||
.withPayload(message.getPayload().toString().toUpperCase()).build());
|
||||
.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());
|
||||
.withPayload(message.getPayload().toString().toUpperCase())
|
||||
.build());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -58,14 +58,17 @@ public class StreamListenerMessageArgumentTests {
|
||||
|
||||
@Parameterized.Parameters
|
||||
public static Collection<?> InputConfigs() {
|
||||
return Arrays.asList(new Class[] { TestPojoWithMessageArgument1.class, TestPojoWithMessageArgument2.class });
|
||||
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", "--spring.cloud.stream.bindings.output.contentType=text/plain","--spring.jmx.enabled=false");
|
||||
ConfigurableApplicationContext context = SpringApplication.run(this.configClass,
|
||||
"--server.port=0",
|
||||
"--spring.cloud.stream.bindings.output.contentType=text/plain",
|
||||
"--spring.jmx.enabled=false");
|
||||
MessageCollector collector = context.getBean(MessageCollector.class);
|
||||
Processor processor = context.getBean(Processor.class);
|
||||
String id = UUID.randomUUID().toString();
|
||||
@@ -74,7 +77,8 @@ public class StreamListenerMessageArgumentTests {
|
||||
TestPojoWithMessageArgument testPojoWithMessageArgument = context
|
||||
.getBean(TestPojoWithMessageArgument.class);
|
||||
assertThat(testPojoWithMessageArgument.receivedMessages).hasSize(1);
|
||||
assertThat(testPojoWithMessageArgument.receivedMessages.get(0).getPayload()).isEqualTo("barbar" + id);
|
||||
assertThat(testPojoWithMessageArgument.receivedMessages.get(0).getPayload())
|
||||
.isEqualTo("barbar" + id);
|
||||
Message<String> message = (Message<String>) collector
|
||||
.forChannel(processor.output()).poll(1, TimeUnit.SECONDS);
|
||||
assertThat(message).isNotNull();
|
||||
@@ -94,6 +98,7 @@ public class StreamListenerMessageArgumentTests {
|
||||
barPojo.setBar(fooMessage.getPayload());
|
||||
return barPojo;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@@ -108,10 +113,13 @@ public class StreamListenerMessageArgumentTests {
|
||||
barPojo.setBar(fooMessage.getPayload());
|
||||
return barPojo;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class TestPojoWithMessageArgument {
|
||||
|
||||
List<Message<String>> receivedMessages = new ArrayList<>();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -52,8 +52,8 @@ public class StreamListenerMethodRegisteredOnlyOnceTest {
|
||||
|
||||
@Test
|
||||
public void should_handleSomeMessage() {
|
||||
sink.channel().send(new GenericMessage<>("Payload"));
|
||||
verify(handler).handleMessage(); //should only be invoked once.
|
||||
this.sink.channel().send(new GenericMessage<>("Payload"));
|
||||
verify(this.handler).handleMessage(); // should only be invoked once.
|
||||
}
|
||||
|
||||
public interface SomeSink {
|
||||
@@ -72,4 +72,5 @@ public class StreamListenerMethodRegisteredOnlyOnceTest {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -57,12 +57,13 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*
|
||||
*/
|
||||
@RunWith(StreamListenerMethodReturnWithConversionTests.class)
|
||||
@Suite.SuiteClasses({ StreamListenerMethodReturnWithConversionTests.TestReturnConversion.class,
|
||||
@Suite.SuiteClasses({
|
||||
StreamListenerMethodReturnWithConversionTests.TestReturnConversion.class,
|
||||
StreamListenerMethodReturnWithConversionTests.TestReturnNoConversion.class })
|
||||
public class StreamListenerMethodReturnWithConversionTests extends Suite {
|
||||
|
||||
public StreamListenerMethodReturnWithConversionTests(Class<?> klass, RunnerBuilder builder)
|
||||
throws InitializationError {
|
||||
public StreamListenerMethodReturnWithConversionTests(Class<?> klass,
|
||||
RunnerBuilder builder) throws InitializationError {
|
||||
super(klass, builder);
|
||||
}
|
||||
|
||||
@@ -77,30 +78,39 @@ public class StreamListenerMethodReturnWithConversionTests extends Suite {
|
||||
|
||||
@Parameterized.Parameters
|
||||
public static Collection<?> InputConfigs() {
|
||||
return Arrays.asList(new Class[] { TestPojoWithMimeType1.class, TestPojoWithMimeType2.class });
|
||||
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","--spring.jmx.enabled=false");
|
||||
ConfigurableApplicationContext context = SpringApplication.run(
|
||||
this.configClass,
|
||||
"--spring.cloud.stream.bindings.output.contentType=application/json",
|
||||
"--server.port=0", "--spring.jmx.enabled=false");
|
||||
MessageCollector collector = context.getBean(MessageCollector.class);
|
||||
Processor processor = context.getBean(Processor.class);
|
||||
String id = UUID.randomUUID().toString();
|
||||
processor.input().send(MessageBuilder.withPayload("{\"foo\":\"barbar" + id + "\"}")
|
||||
.setHeader("contentType", "application/json").build());
|
||||
TestPojoWithMimeType testPojoWithMimeType = context.getBean(TestPojoWithMimeType.class);
|
||||
processor.input()
|
||||
.send(MessageBuilder.withPayload("{\"foo\":\"barbar" + id + "\"}")
|
||||
.setHeader("contentType", "application/json").build());
|
||||
TestPojoWithMimeType testPojoWithMimeType = context
|
||||
.getBean(TestPojoWithMimeType.class);
|
||||
Assertions.assertThat(testPojoWithMimeType.receivedPojos).hasSize(1);
|
||||
Assertions.assertThat(testPojoWithMimeType.receivedPojos.get(0)).hasFieldOrPropertyWithValue("foo", "barbar" + id);
|
||||
Message<String> message = (Message<String>) collector.forChannel(processor.output()).poll(1,
|
||||
TimeUnit.SECONDS);
|
||||
Assertions.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(new String(message.getPayload())).isEqualTo("{\"bar\":\"barbar" + id + "\"}");
|
||||
assertThat(message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class)
|
||||
.includes(MimeTypeUtils.APPLICATION_JSON));
|
||||
assertThat(new String(message.getPayload()))
|
||||
.isEqualTo("{\"bar\":\"barbar" + id + "\"}");
|
||||
assertThat(
|
||||
message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class)
|
||||
.includes(MimeTypeUtils.APPLICATION_JSON));
|
||||
context.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
@@ -116,30 +126,37 @@ public class StreamListenerMethodReturnWithConversionTests extends Suite {
|
||||
|
||||
@Parameterized.Parameters
|
||||
public static Collection<?> InputConfigs() {
|
||||
return Arrays.asList(new Class[] { TestPojoWithMimeType1.class, TestPojoWithMimeType2.class });
|
||||
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","--spring.jmx.enabled=false");
|
||||
ConfigurableApplicationContext context = SpringApplication.run(
|
||||
this.configClass, "--server.port=0", "--spring.jmx.enabled=false");
|
||||
MessageCollector collector = context.getBean(MessageCollector.class);
|
||||
Processor processor = context.getBean(Processor.class);
|
||||
String id = UUID.randomUUID().toString();
|
||||
processor.input().send(MessageBuilder.withPayload("{\"foo\":\"barbar" + id + "\"}")
|
||||
.setHeader("contentType", "application/json").build());
|
||||
TestPojoWithMimeType testPojoWithMimeType = context.getBean(TestPojoWithMimeType.class);
|
||||
processor.input()
|
||||
.send(MessageBuilder.withPayload("{\"foo\":\"barbar" + id + "\"}")
|
||||
.setHeader("contentType", "application/json").build());
|
||||
TestPojoWithMimeType testPojoWithMimeType = context
|
||||
.getBean(TestPojoWithMimeType.class);
|
||||
Assertions.assertThat(testPojoWithMimeType.receivedPojos).hasSize(1);
|
||||
Assertions.assertThat(testPojoWithMimeType.receivedPojos.get(0)).hasFieldOrPropertyWithValue("foo", "barbar" + id);
|
||||
Assertions.assertThat(testPojoWithMimeType.receivedPojos.get(0))
|
||||
.hasFieldOrPropertyWithValue("foo", "barbar" + id);
|
||||
Message<String> message = (Message<String>) collector
|
||||
.forChannel(processor.output()).poll(1,
|
||||
TimeUnit.SECONDS);
|
||||
.forChannel(processor.output()).poll(1, TimeUnit.SECONDS);
|
||||
assertThat(message).isNotNull();
|
||||
StreamListenerTestUtils.BarPojo barPojo = mapper.readValue(message.getPayload(),StreamListenerTestUtils.BarPojo.class);
|
||||
StreamListenerTestUtils.BarPojo barPojo = this.mapper.readValue(
|
||||
message.getPayload(), StreamListenerTestUtils.BarPojo.class);
|
||||
assertThat(barPojo.getBar()).isEqualTo("barbar" + id);
|
||||
assertThat(message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class) != null);
|
||||
assertThat(message.getHeaders().get(MessageHeaders.CONTENT_TYPE,
|
||||
MimeType.class) != null);
|
||||
context.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@@ -148,12 +165,14 @@ public class StreamListenerMethodReturnWithConversionTests extends Suite {
|
||||
|
||||
@StreamListener(Processor.INPUT)
|
||||
@SendTo(Processor.OUTPUT)
|
||||
public StreamListenerTestUtils.BarPojo receive(StreamListenerTestUtils.FooPojo fooPojo) {
|
||||
public StreamListenerTestUtils.BarPojo receive(
|
||||
StreamListenerTestUtils.FooPojo fooPojo) {
|
||||
this.receivedPojos.add(fooPojo);
|
||||
StreamListenerTestUtils.BarPojo barPojo = new StreamListenerTestUtils.BarPojo();
|
||||
barPojo.setBar(fooPojo.getFoo());
|
||||
return barPojo;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@@ -162,16 +181,20 @@ public class StreamListenerMethodReturnWithConversionTests extends Suite {
|
||||
|
||||
@StreamListener(Processor.INPUT)
|
||||
@Output(Processor.OUTPUT)
|
||||
public StreamListenerTestUtils.BarPojo receive(StreamListenerTestUtils.FooPojo fooPojo) {
|
||||
public StreamListenerTestUtils.BarPojo receive(
|
||||
StreamListenerTestUtils.FooPojo fooPojo) {
|
||||
this.receivedPojos.add(fooPojo);
|
||||
StreamListenerTestUtils.BarPojo barPojo = new StreamListenerTestUtils.BarPojo();
|
||||
barPojo.setBar(fooPojo.getFoo());
|
||||
return barPojo;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class TestPojoWithMimeType {
|
||||
|
||||
List<StreamListenerTestUtils.FooPojo> receivedPojos = new ArrayList<>();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2018 the original author or authors.
|
||||
* Copyright 2018-2019 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.
|
||||
@@ -69,32 +69,43 @@ public class StreamListenerMethodSetupOrchestratorTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testCustomStreamListenerOrchestratorAndDefaultTogetherInSameContext() throws Exception {
|
||||
public void testCustomStreamListenerOrchestratorAndDefaultTogetherInSameContext()
|
||||
throws Exception {
|
||||
|
||||
//Two StreamListener methods, so 2 invocations
|
||||
verify(customOrchestrator, times(2)).supports(any());
|
||||
// Two StreamListener methods, so 2 invocations
|
||||
verify(this.customOrchestrator, times(2)).supports(any());
|
||||
|
||||
Method method = multipleStreamListenerProcessor.getClass().getMethod("handleMessage");
|
||||
StreamListener streamListener = AnnotatedElementUtils.findMergedAnnotation(method, StreamListener.class);
|
||||
//verify that the invocation happened on the custom Orchestrator
|
||||
verify(customOrchestrator).orchestrateStreamListenerSetupMethod(streamListener, method, multipleStreamListenerProcessor);
|
||||
Method method = this.multipleStreamListenerProcessor.getClass()
|
||||
.getMethod("handleMessage");
|
||||
StreamListener streamListener = AnnotatedElementUtils.findMergedAnnotation(method,
|
||||
StreamListener.class);
|
||||
// verify that the invocation happened on the custom Orchestrator
|
||||
verify(this.customOrchestrator).orchestrateStreamListenerSetupMethod(
|
||||
streamListener, method, this.multipleStreamListenerProcessor);
|
||||
|
||||
Method method1 = multipleStreamListenerProcessor.getClass().getMethod("produceString");
|
||||
StreamListener streamListener1 = AnnotatedElementUtils.findMergedAnnotation(method, StreamListener.class);
|
||||
Method method1 = this.multipleStreamListenerProcessor.getClass()
|
||||
.getMethod("produceString");
|
||||
StreamListener streamListener1 = AnnotatedElementUtils
|
||||
.findMergedAnnotation(method, StreamListener.class);
|
||||
|
||||
//Verify that the invocation did not happen on the custom orchestrator
|
||||
verify(customOrchestrator, never()).orchestrateStreamListenerSetupMethod(streamListener1, method1, multipleStreamListenerProcessor);
|
||||
// Verify that the invocation did not happen on the custom orchestrator
|
||||
verify(this.customOrchestrator, never()).orchestrateStreamListenerSetupMethod(
|
||||
streamListener1, method1, this.multipleStreamListenerProcessor);
|
||||
|
||||
Field field = ReflectionUtils.findField(streamListenerAnnotationBeanPostProcessor.getClass(), "streamListenerSetupMethodOrchestrators");
|
||||
Field field = ReflectionUtils.findField(
|
||||
this.streamListenerAnnotationBeanPostProcessor.getClass(),
|
||||
"streamListenerSetupMethodOrchestrators");
|
||||
ReflectionUtils.makeAccessible(field);
|
||||
|
||||
Set<StreamListenerSetupMethodOrchestrator> field1 =
|
||||
(LinkedHashSet<StreamListenerSetupMethodOrchestrator>)ReflectionUtils.getField(field, streamListenerAnnotationBeanPostProcessor);
|
||||
Set<StreamListenerSetupMethodOrchestrator> field1;
|
||||
field1 = (LinkedHashSet<StreamListenerSetupMethodOrchestrator>) ReflectionUtils
|
||||
.getField(field, this.streamListenerAnnotationBeanPostProcessor);
|
||||
List<StreamListenerSetupMethodOrchestrator> list = new ArrayList<>(field1);
|
||||
|
||||
//Ensure that the custom orchestrator did not support this request
|
||||
// Ensure that the custom orchestrator did not support this request
|
||||
assertThat(list.get(0).supports(method1)).isEqualTo(false);
|
||||
//Ensure that we are using the default Orchestrator in StreamListenerAnnoatationBeanPostProcessor
|
||||
// Ensure that we are using the default Orchestrator in
|
||||
// StreamListenerAnnoatationBeanPostProcessor
|
||||
assertThat(list.get(1).supports(method1)).isEqualTo(true);
|
||||
}
|
||||
|
||||
@@ -121,7 +132,7 @@ public class StreamListenerMethodSetupOrchestratorTests {
|
||||
|
||||
@StreamListener("foobar")
|
||||
@SendTo("output")
|
||||
public String produceString(){
|
||||
public String produceString() {
|
||||
return "foobar";
|
||||
}
|
||||
|
||||
@@ -140,8 +151,11 @@ public class StreamListenerMethodSetupOrchestratorTests {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void orchestrateStreamListenerSetupMethod(StreamListener streamListener, Method method, Object bean) {
|
||||
//stub method
|
||||
public void orchestrateStreamListenerSetupMethod(StreamListener streamListener,
|
||||
Method method, Object bean) {
|
||||
// stub method
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -59,14 +59,15 @@ public class StreamListenerMethodWithReturnMessageTests {
|
||||
|
||||
@Parameterized.Parameters
|
||||
public static Collection<?> InputConfigs() {
|
||||
return Arrays.asList(new Class[] { TestPojoWithMessageReturn1.class, TestPojoWithMessageReturn2.class });
|
||||
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","--spring.jmx.enabled=false");
|
||||
ConfigurableApplicationContext context = SpringApplication.run(this.configClass,
|
||||
"--server.port=0", "--spring.jmx.enabled=false");
|
||||
MessageCollector collector = context.getBean(MessageCollector.class);
|
||||
Processor processor = context.getBean(Processor.class);
|
||||
String id = UUID.randomUUID().toString();
|
||||
@@ -76,7 +77,8 @@ public class StreamListenerMethodWithReturnMessageTests {
|
||||
TestPojoWithMessageReturn testPojoWithMessageReturn = context
|
||||
.getBean(TestPojoWithMessageReturn.class);
|
||||
Assertions.assertThat(testPojoWithMessageReturn.receivedPojos).hasSize(1);
|
||||
Assertions.assertThat(testPojoWithMessageReturn.receivedPojos.get(0)).hasFieldOrPropertyWithValue("foo", "barbar" + id);
|
||||
Assertions.assertThat(testPojoWithMessageReturn.receivedPojos.get(0))
|
||||
.hasFieldOrPropertyWithValue("foo", "barbar" + id);
|
||||
Message<String> message = (Message<String>) collector
|
||||
.forChannel(processor.output()).poll(1, TimeUnit.SECONDS);
|
||||
assertThat(message).isNotNull();
|
||||
@@ -96,6 +98,7 @@ public class StreamListenerMethodWithReturnMessageTests {
|
||||
barPojo.setBar(fooPojo.getFoo());
|
||||
return MessageBuilder.withPayload(barPojo).setHeader("foo", "bar").build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@@ -110,10 +113,13 @@ public class StreamListenerMethodWithReturnMessageTests {
|
||||
bazPojo.setBar(fooPojo.getFoo());
|
||||
return MessageBuilder.withPayload(bazPojo).setHeader("foo", "bar").build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class TestPojoWithMessageReturn {
|
||||
|
||||
List<StreamListenerTestUtils.FooPojo> receivedPojos = new ArrayList<>();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -58,14 +58,15 @@ public class StreamListenerMethodWithReturnValueTests {
|
||||
|
||||
@Parameterized.Parameters
|
||||
public static Collection<?> InputConfigs() {
|
||||
return Arrays.asList(new Class[] { TestStringProcessor1.class, TestStringProcessor2.class });
|
||||
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","--spring.jmx.enabled=false");
|
||||
ConfigurableApplicationContext context = SpringApplication.run(this.configClass,
|
||||
"--server.port=0", "--spring.jmx.enabled=false");
|
||||
MessageCollector collector = context.getBean(MessageCollector.class);
|
||||
Processor processor = context.getBean(Processor.class);
|
||||
String id = UUID.randomUUID().toString();
|
||||
@@ -77,7 +78,8 @@ public class StreamListenerMethodWithReturnValueTests {
|
||||
TestStringProcessor testStringProcessor = context
|
||||
.getBean(TestStringProcessor.class);
|
||||
Assertions.assertThat(testStringProcessor.receivedPojos).hasSize(1);
|
||||
Assertions.assertThat(testStringProcessor.receivedPojos.get(0)).hasFieldOrPropertyWithValue("foo", "barbar" + id);
|
||||
Assertions.assertThat(testStringProcessor.receivedPojos.get(0))
|
||||
.hasFieldOrPropertyWithValue("foo", "barbar" + id);
|
||||
assertThat(message).isNotNull();
|
||||
assertThat(message.getPayload()).contains("barbar" + id);
|
||||
context.close();
|
||||
@@ -93,6 +95,7 @@ public class StreamListenerMethodWithReturnValueTests {
|
||||
this.receivedPojos.add(fooPojo);
|
||||
return fooPojo.getFoo();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@@ -105,10 +108,13 @@ public class StreamListenerMethodWithReturnValueTests {
|
||||
this.receivedPojos.add(fooPojo);
|
||||
return fooPojo.getFoo();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class TestStringProcessor {
|
||||
|
||||
List<StreamListenerTestUtils.FooPojo> receivedPojos = new ArrayList<>();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
* Copyright 2016-2019 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.
|
||||
@@ -23,8 +23,6 @@ import org.springframework.cloud.stream.annotation.Output;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.SubscribableChannel;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @author Ilayaperumal Gopinathan
|
||||
*/
|
||||
@@ -63,10 +61,11 @@ public class StreamListenerTestUtils {
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuffer sb = new StringBuffer("FooPojo{");
|
||||
sb.append("foo='").append(foo).append('\'');
|
||||
sb.append("foo='").append(this.foo).append('\'');
|
||||
sb.append('}');
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class BarPojo {
|
||||
@@ -84,10 +83,11 @@ public class StreamListenerTestUtils {
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuffer sb = new StringBuffer("BarPojo{");
|
||||
sb.append("bar='").append(bar).append('\'');
|
||||
sb.append("bar='").append(this.bar).append('\'');
|
||||
sb.append('}');
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class PojoWithValidation {
|
||||
@@ -95,9 +95,13 @@ public class StreamListenerTestUtils {
|
||||
@NotBlank
|
||||
private String foo;
|
||||
|
||||
public String getFoo() { return this.foo; }
|
||||
public String getFoo() {
|
||||
return this.foo;
|
||||
}
|
||||
|
||||
public void setFoo(String foo) { this.foo = foo; }
|
||||
public void setFoo(String foo) {
|
||||
this.foo = foo;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -51,14 +51,18 @@ public class StreamListenerWithAnnotatedInputOutputArgsTests {
|
||||
|
||||
@Test
|
||||
public void testInputOutputArgs() throws Exception {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(TestInputOutputArgs.class, "--server.port=0", "--spring.cloud.stream.bindings.output.contentType=text/plain", "--spring.jmx.enabled=false");
|
||||
ConfigurableApplicationContext context = SpringApplication.run(
|
||||
TestInputOutputArgs.class, "--server.port=0",
|
||||
"--spring.cloud.stream.bindings.output.contentType=text/plain",
|
||||
"--spring.jmx.enabled=false");
|
||||
sendMessageAndValidate(context);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInputOutputArgsWithMoreParameters() {
|
||||
try {
|
||||
SpringApplication.run(TestInputOutputArgsWithMoreParameters.class, "--server.port=0");
|
||||
SpringApplication.run(TestInputOutputArgsWithMoreParameters.class,
|
||||
"--server.port=0");
|
||||
fail("Expected exception: " + INVALID_DECLARATIVE_METHOD_PARAMETERS);
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
@@ -69,27 +73,34 @@ public class StreamListenerWithAnnotatedInputOutputArgsTests {
|
||||
@Test
|
||||
public void testInputOutputArgsWithInvalidBindableTarget() {
|
||||
try {
|
||||
SpringApplication.run(TestInputOutputArgsWithInvalidBindableTarget.class, "--server.port=0","--spring.jmx.enabled=false");
|
||||
SpringApplication.run(TestInputOutputArgsWithInvalidBindableTarget.class,
|
||||
"--server.port=0", "--spring.jmx.enabled=false");
|
||||
fail("Exception expected on using invalid bindable target as method parameter");
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
assertThat(e.getMessage()).contains(StreamListenerErrorMessages.INVALID_DECLARATIVE_METHOD_PARAMETERS);
|
||||
assertThat(e.getMessage()).contains(
|
||||
StreamListenerErrorMessages.INVALID_DECLARATIVE_METHOD_PARAMETERS);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInputOutputArgsWithParameterOrderChanged() throws Exception {
|
||||
ConfigurableApplicationContext context = SpringApplication
|
||||
.run(TestInputOutputArgsWithParameterOrderChanged.class, "--server.port=0", "--spring.cloud.stream.bindings.output.contentType=text/plain","--spring.jmx.enabled=false");
|
||||
ConfigurableApplicationContext context = SpringApplication.run(
|
||||
TestInputOutputArgsWithParameterOrderChanged.class, "--server.port=0",
|
||||
"--spring.cloud.stream.bindings.output.contentType=text/plain",
|
||||
"--spring.jmx.enabled=false");
|
||||
sendMessageAndValidate(context);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void sendMessageAndValidate(ConfigurableApplicationContext context) throws InterruptedException {
|
||||
private void sendMessageAndValidate(ConfigurableApplicationContext context)
|
||||
throws InterruptedException {
|
||||
Processor processor = context.getBean(Processor.class);
|
||||
processor.input().send(MessageBuilder.withPayload("hello").setHeader("contentType", "text/plain").build());
|
||||
processor.input().send(MessageBuilder.withPayload("hello")
|
||||
.setHeader("contentType", "text/plain").build());
|
||||
MessageCollector messageCollector = context.getBean(MessageCollector.class);
|
||||
Message<String> result = (Message<String>) messageCollector.forChannel(processor.output()).poll(1000, TimeUnit.MILLISECONDS);
|
||||
Message<String> result = (Message<String>) messageCollector
|
||||
.forChannel(processor.output()).poll(1000, TimeUnit.MILLISECONDS);
|
||||
assertThat(result).isNotNull();
|
||||
assertThat(result.getPayload()).isEqualTo("HELLO");
|
||||
context.close();
|
||||
@@ -105,10 +116,13 @@ public class StreamListenerWithAnnotatedInputOutputArgsTests {
|
||||
input.subscribe(new MessageHandler() {
|
||||
@Override
|
||||
public void handleMessage(Message<?> message) throws MessagingException {
|
||||
output.send(MessageBuilder.withPayload(message.getPayload().toString().toUpperCase()).build());
|
||||
output.send(MessageBuilder
|
||||
.withPayload(message.getPayload().toString().toUpperCase())
|
||||
.build());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@@ -117,15 +131,17 @@ public class StreamListenerWithAnnotatedInputOutputArgsTests {
|
||||
|
||||
@StreamListener
|
||||
public void receive(@Input(Processor.INPUT) SubscribableChannel input,
|
||||
@Output(Processor.OUTPUT) final MessageChannel output,
|
||||
String someArg) {
|
||||
@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());
|
||||
output.send(MessageBuilder
|
||||
.withPayload(message.getPayload().toString().toUpperCase())
|
||||
.build());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@@ -138,10 +154,13 @@ public class StreamListenerWithAnnotatedInputOutputArgsTests {
|
||||
input.subscribe(new MessageHandler() {
|
||||
@Override
|
||||
public void handleMessage(Message<?> message) throws MessagingException {
|
||||
output.send(MessageBuilder.withPayload(message.getPayload().toString().toUpperCase()).build());
|
||||
output.send(MessageBuilder
|
||||
.withPayload(message.getPayload().toString().toUpperCase())
|
||||
.build());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
@@ -154,10 +173,13 @@ public class StreamListenerWithAnnotatedInputOutputArgsTests {
|
||||
input.subscribe(new MessageHandler() {
|
||||
@Override
|
||||
public void handleMessage(Message<?> message) throws MessagingException {
|
||||
output.send(MessageBuilder.withPayload(message.getPayload().toString().toUpperCase()).build());
|
||||
output.send(MessageBuilder
|
||||
.withPayload(message.getPayload().toString().toUpperCase())
|
||||
.build());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -44,28 +44,28 @@ public class StreamListenerWithConditionsTest {
|
||||
|
||||
@Test
|
||||
public void testAnnotatedArgumentsWithConditionalClass() throws Exception {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(TestPojoWithAnnotatedArguments.class,
|
||||
"--server.port=0");
|
||||
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("{\"foo\":\"barbar" + id + "\"}")
|
||||
.setHeader("contentType", "application/json").setHeader("testHeader", "testValue")
|
||||
.setHeader("type", "foo").build());
|
||||
.setHeader("contentType", "application/json")
|
||||
.setHeader("testHeader", "testValue").setHeader("type", "foo").build());
|
||||
sink.input().send(MessageBuilder.withPayload("{\"bar\":\"foofoo" + id + "\"}")
|
||||
.setHeader("contentType", "application/json").setHeader("testHeader", "testValue")
|
||||
.setHeader("type", "bar").build());
|
||||
.setHeader("contentType", "application/json")
|
||||
.setHeader("testHeader", "testValue").setHeader("type", "bar").build());
|
||||
sink.input().send(MessageBuilder.withPayload("{\"bar\":\"foofoo" + id + "\"}")
|
||||
.setHeader("contentType", "application/json").setHeader("testHeader", "testValue")
|
||||
.setHeader("type", "qux").build());
|
||||
.setHeader("contentType", "application/json")
|
||||
.setHeader("testHeader", "testValue").setHeader("type", "qux").build());
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedFoo).hasSize(1);
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedFoo.get(0)).hasFieldOrPropertyWithValue("foo",
|
||||
"barbar" + id);
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedFoo.get(0))
|
||||
.hasFieldOrPropertyWithValue("foo", "barbar" + id);
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedBar).hasSize(1);
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedBar.get(0)).hasFieldOrPropertyWithValue("bar",
|
||||
"foofoo" + id);
|
||||
assertThat(testPojoWithAnnotatedArguments.receivedBar.get(0))
|
||||
.hasFieldOrPropertyWithValue("bar", "foofoo" + id);
|
||||
context.close();
|
||||
}
|
||||
|
||||
@@ -73,13 +73,13 @@ public class StreamListenerWithConditionsTest {
|
||||
public void testConditionalFailsWithReturnValue() throws Exception {
|
||||
try {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(
|
||||
TestConditionalOnMethodWithReturnValueFails.class,
|
||||
"--server.port=0");
|
||||
TestConditionalOnMethodWithReturnValueFails.class, "--server.port=0");
|
||||
context.close();
|
||||
fail("Context creation failure expected");
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
assertThat(e.getMessage()).contains(StreamListenerErrorMessages.CONDITION_ON_METHOD_RETURNING_VALUE);
|
||||
assertThat(e.getMessage()).contains(
|
||||
StreamListenerErrorMessages.CONDITION_ON_METHOD_RETURNING_VALUE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,13 +87,13 @@ public class StreamListenerWithConditionsTest {
|
||||
public void testConditionalFailsWithDeclarativeMethod() throws Exception {
|
||||
try {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(
|
||||
TestConditionalOnDeclarativeMethodFails.class,
|
||||
"--server.port=0");
|
||||
TestConditionalOnDeclarativeMethodFails.class, "--server.port=0");
|
||||
context.close();
|
||||
fail("Context creation failure expected");
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
assertThat(e.getMessage()).contains(StreamListenerErrorMessages.CONDITION_ON_DECLARATIVE_METHOD);
|
||||
assertThat(e.getMessage()).contains(
|
||||
StreamListenerErrorMessages.CONDITION_ON_DECLARATIVE_METHOD);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,6 +125,7 @@ public class StreamListenerWithConditionsTest {
|
||||
public void receive(@Input("input") MessageChannel input) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding(Sink.class)
|
||||
@@ -135,5 +136,7 @@ public class StreamListenerWithConditionsTest {
|
||||
public String receive(String value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
* Copyright 2017-2019 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.
|
||||
@@ -40,12 +40,12 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
/**
|
||||
* @author Marius Bogoevici
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
* @since 1.2
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = TextPlainConversionTest.FooProcessor.class,
|
||||
webEnvironment = SpringBootTest.WebEnvironment.NONE)
|
||||
// @checkstyle:off
|
||||
@SpringBootTest(classes = TextPlainConversionTest.FooProcessor.class, webEnvironment = SpringBootTest.WebEnvironment.NONE)
|
||||
// @checkstyle:on
|
||||
public class TextPlainConversionTest {
|
||||
|
||||
@Autowired
|
||||
@@ -56,30 +56,38 @@ public class TextPlainConversionTest {
|
||||
|
||||
@Test
|
||||
public void testTextPlainConversionOnOutput() throws Exception {
|
||||
testProcessor.input().send(MessageBuilder.withPayload("Bar").build());
|
||||
this.testProcessor.input().send(MessageBuilder.withPayload("Bar").build());
|
||||
@SuppressWarnings("unchecked")
|
||||
Message<String> received = (Message<String>) ((TestSupportBinder) binderFactory.getBinder(null, MessageChannel.class))
|
||||
.messageCollector().forChannel(testProcessor.output()).poll(1, TimeUnit.SECONDS);
|
||||
Message<String> received = (Message<String>) ((TestSupportBinder) this.binderFactory
|
||||
.getBinder(null, MessageChannel.class)).messageCollector()
|
||||
.forChannel(this.testProcessor.output())
|
||||
.poll(1, TimeUnit.SECONDS);
|
||||
assertThat(received).isNotNull();
|
||||
assertThat(received.getPayload()).isEqualTo("Foo{name='Bar'}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testByteArrayConversionOnOutput() throws Exception {
|
||||
testProcessor.output().send(MessageBuilder.withPayload("Bar".getBytes()).build());
|
||||
this.testProcessor.output()
|
||||
.send(MessageBuilder.withPayload("Bar".getBytes()).build());
|
||||
@SuppressWarnings("unchecked")
|
||||
Message<String> received = (Message<String>)((TestSupportBinder) binderFactory.getBinder(null, MessageChannel.class))
|
||||
.messageCollector().forChannel(testProcessor.output()).poll(1, TimeUnit.SECONDS);
|
||||
Message<String> received = (Message<String>) ((TestSupportBinder) this.binderFactory
|
||||
.getBinder(null, MessageChannel.class)).messageCollector()
|
||||
.forChannel(this.testProcessor.output())
|
||||
.poll(1, TimeUnit.SECONDS);
|
||||
assertThat(received).isNotNull();
|
||||
assertThat(received.getPayload()).isEqualTo("Bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTextPlainConversionOnInputAndOutput() throws Exception {
|
||||
testProcessor.input().send(MessageBuilder.withPayload(new Foo("Bar")).build());
|
||||
this.testProcessor.input()
|
||||
.send(MessageBuilder.withPayload(new Foo("Bar")).build());
|
||||
@SuppressWarnings("unchecked")
|
||||
Message<String> received = (Message<String>) ((TestSupportBinder) binderFactory.getBinder(null, MessageChannel.class))
|
||||
.messageCollector().forChannel(testProcessor.output()).poll(1, TimeUnit.SECONDS);
|
||||
Message<String> received = (Message<String>) ((TestSupportBinder) this.binderFactory
|
||||
.getBinder(null, MessageChannel.class)).messageCollector()
|
||||
.forChannel(this.testProcessor.output())
|
||||
.poll(1, TimeUnit.SECONDS);
|
||||
assertThat(received).isNotNull();
|
||||
assertThat(received.getPayload()).isEqualTo("Foo{name='Foo{name='Bar'}'}");
|
||||
}
|
||||
@@ -105,7 +113,7 @@ public class TextPlainConversionTest {
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
@@ -114,7 +122,7 @@ public class TextPlainConversionTest {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Foo{name='" + name + "'}";
|
||||
return "Foo{name='" + this.name + "'}";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -47,9 +47,9 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* @since 1.2
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = TextPlainToJsonConversionTest.FooProcessor.class,
|
||||
webEnvironment = SpringBootTest.WebEnvironment.NONE
|
||||
)
|
||||
// @checkstyle:off
|
||||
@SpringBootTest(classes = TextPlainToJsonConversionTest.FooProcessor.class, webEnvironment = SpringBootTest.WebEnvironment.NONE)
|
||||
// @checkstyle:on
|
||||
public class TextPlainToJsonConversionTest {
|
||||
|
||||
@Autowired
|
||||
@@ -63,21 +63,24 @@ public class TextPlainToJsonConversionTest {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testNoContentTypeToJsonConversionOnInput() throws Exception {
|
||||
testProcessor.input().send(MessageBuilder.withPayload("{\"name\":\"Bar\"}").build());
|
||||
Message<String> received = (Message<String>) ((TestSupportBinder) binderFactory.getBinder(null, MessageChannel.class))
|
||||
.messageCollector().forChannel(testProcessor.output()).poll(1, TimeUnit.SECONDS);
|
||||
this.testProcessor.input()
|
||||
.send(MessageBuilder.withPayload("{\"name\":\"Bar\"}").build());
|
||||
Message<String> received = (Message<String>) ((TestSupportBinder) this.binderFactory
|
||||
.getBinder(null, MessageChannel.class)).messageCollector()
|
||||
.forChannel(this.testProcessor.output())
|
||||
.poll(1, TimeUnit.SECONDS);
|
||||
assertThat(received).isNotNull();
|
||||
Foo foo = mapper.readValue(received.getPayload(),Foo.class);
|
||||
Foo foo = this.mapper.readValue(received.getPayload(), Foo.class);
|
||||
assertThat(foo.getName()).isEqualTo("transformed-Bar");
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 2.0: Conversion from text/plain -> json is no longer supported. Strict contentType only.
|
||||
* @throws Exception
|
||||
* @since 2.0: Conversion from text/plain -> json is no longer supported. Strict
|
||||
* contentType only.
|
||||
*/
|
||||
@Test(expected = MessagingException.class)
|
||||
public void testTextPlainToJsonConversionOnInput() throws Exception {
|
||||
testProcessor.input().send(MessageBuilder.withPayload("{\"name\":\"Bar\"}")
|
||||
public void testTextPlainToJsonConversionOnInput() {
|
||||
this.testProcessor.input().send(MessageBuilder.withPayload("{\"name\":\"Bar\"}")
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE, "text/plain").build());
|
||||
}
|
||||
|
||||
@@ -103,7 +106,7 @@ public class TextPlainToJsonConversionTest {
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
@@ -112,7 +115,7 @@ public class TextPlainToJsonConversionTest {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Foo{name='" + name + "'}";
|
||||
return "Foo{name='" + this.name + "'}";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.cloud.stream.config.aggregate;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
@@ -32,7 +31,7 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Ilayaperumal Gopinathan
|
||||
@@ -44,14 +43,15 @@ public class AggregateApplicationTests {
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testAggregateApplication() throws Exception {
|
||||
ConfigurableApplicationContext context = new AggregateApplicationBuilder(
|
||||
AggregateApplicationTestConfig.class).web(false).from(TestSource.class).to(TestProcessor.class).run();
|
||||
TestSupportBinder testSupportBinder = (TestSupportBinder) context.getBean(BinderFactory.class).getBinder(null,
|
||||
MessageChannel.class);
|
||||
AggregateApplicationTestConfig.class).web(false).from(TestSource.class)
|
||||
.to(TestProcessor.class).run();
|
||||
TestSupportBinder testSupportBinder = (TestSupportBinder) context
|
||||
.getBean(BinderFactory.class).getBinder(null, MessageChannel.class);
|
||||
MessageChannel processorOutput = testSupportBinder.getChannelForName("output");
|
||||
Message<String> received = (Message<String>) (testSupportBinder.messageCollector().forChannel(processorOutput)
|
||||
.poll(5, TimeUnit.SECONDS));
|
||||
Assert.assertThat(received, notNullValue());
|
||||
Assert.assertTrue(received.getPayload().endsWith("processed"));
|
||||
Message<String> received = (Message<String>) (testSupportBinder.messageCollector()
|
||||
.forChannel(processorOutput).poll(5, TimeUnit.SECONDS));
|
||||
assertThat(received).isNotNull();
|
||||
assertThat(received.getPayload().endsWith("processed")).isTrue();
|
||||
|
||||
context.close();
|
||||
}
|
||||
@@ -61,4 +61,5 @@ public class AggregateApplicationTests {
|
||||
static class AggregateApplicationTestConfig {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
* Copyright 2017-2019 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.
|
||||
@@ -37,6 +37,8 @@ public class TestProcessor {
|
||||
@StreamListener(Processor.INPUT)
|
||||
@SendTo(Processor.OUTPUT)
|
||||
public Message<String> process(String message) {
|
||||
return MessageBuilder.withPayload(message + " processed").setHeader(MessageHeaders.CONTENT_TYPE,"text/plain").build();
|
||||
return MessageBuilder.withPayload(message + " processed")
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE, "text/plain").build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
* Copyright 2017-2019 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.
|
||||
@@ -44,8 +44,11 @@ public class TestSource {
|
||||
return new MessageSource<String>() {
|
||||
@Override
|
||||
public Message<String> receive() {
|
||||
return MessageBuilder.withPayload(new SimpleDateFormat("DDMMMYYYY").format(new Date())).setHeader(MessageHeaders.CONTENT_TYPE,"text/plain").build();
|
||||
return MessageBuilder
|
||||
.withPayload(new SimpleDateFormat("DDMMMYYYY").format(new Date()))
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE, "text/plain").build();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,7 +25,6 @@ import java.util.concurrent.TimeUnit;
|
||||
import com.esotericsoftware.kryo.Kryo;
|
||||
import com.esotericsoftware.kryo.io.Output;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -72,7 +71,7 @@ public class ContentTypeTests {
|
||||
source.output().send(MessageBuilder.withPayload(user).build());
|
||||
Message<String> message = (Message<String>) collector
|
||||
.forChannel(source.output()).poll(1, TimeUnit.SECONDS);
|
||||
User received = mapper.readValue(message.getPayload(), User.class);
|
||||
User received = this.mapper.readValue(message.getPayload(), User.class);
|
||||
assertThat(
|
||||
message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class)
|
||||
.includes(MimeTypeUtils.APPLICATION_JSON));
|
||||
@@ -88,7 +87,7 @@ public class ContentTypeTests {
|
||||
MessageCollector collector = context.getBean(MessageCollector.class);
|
||||
Source source = context.getBean(Source.class);
|
||||
User user = new User("Alice");
|
||||
String json = mapper.writeValueAsString(user);
|
||||
String json = this.mapper.writeValueAsString(user);
|
||||
source.output().send(MessageBuilder.withPayload(user).build());
|
||||
Message<String> message = (Message<String>) collector
|
||||
.forChannel(source.output()).poll(1, TimeUnit.SECONDS);
|
||||
@@ -100,7 +99,7 @@ public class ContentTypeTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendJsonString() throws Exception{
|
||||
public void testSendJsonString() throws Exception {
|
||||
try (ConfigurableApplicationContext context = SpringApplication.run(
|
||||
SourceApplication.class, "--server.port=0",
|
||||
"--spring.jmx.enabled=false")) {
|
||||
@@ -125,7 +124,11 @@ public class ContentTypeTests {
|
||||
MessageCollector collector = context.getBean(MessageCollector.class);
|
||||
Source source = context.getBean(Source.class);
|
||||
byte[] data = new byte[] { 0, 1, 2, 3 };
|
||||
source.output().send(MessageBuilder.withPayload(data).setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_OCTET_STREAM).build());
|
||||
source.output()
|
||||
.send(MessageBuilder.withPayload(data)
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE,
|
||||
MimeTypeUtils.APPLICATION_OCTET_STREAM)
|
||||
.build());
|
||||
Message<byte[]> message = (Message<byte[]>) collector
|
||||
.forChannel(source.output()).poll(1, TimeUnit.SECONDS);
|
||||
assertThat(
|
||||
@@ -138,14 +141,12 @@ public class ContentTypeTests {
|
||||
@Test
|
||||
public void testSendBinaryDataWithContentType() throws Exception {
|
||||
try (ConfigurableApplicationContext context = SpringApplication.run(
|
||||
SourceApplication.class, "--server.port=0",
|
||||
"--spring.jmx.enabled=false",
|
||||
SourceApplication.class, "--server.port=0", "--spring.jmx.enabled=false",
|
||||
"--spring.cloud.stream.bindings.output.contentType=image/jpeg")) {
|
||||
MessageCollector collector = context.getBean(MessageCollector.class);
|
||||
Source source = context.getBean(Source.class);
|
||||
byte[] data = new byte[] { 0, 1, 2, 3 };
|
||||
source.output().send(MessageBuilder.withPayload(data)
|
||||
.build());
|
||||
source.output().send(MessageBuilder.withPayload(data).build());
|
||||
Message<byte[]> message = (Message<byte[]>) collector
|
||||
.forChannel(source.output()).poll(1, TimeUnit.SECONDS);
|
||||
assertThat(message.getPayload()).isEqualTo(data);
|
||||
@@ -161,12 +162,13 @@ public class ContentTypeTests {
|
||||
Source source = context.getBean(Source.class);
|
||||
byte[] data = new byte[] { 0, 1, 2, 3 };
|
||||
source.output().send(MessageBuilder.withPayload(data)
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE,MimeTypeUtils.IMAGE_JPEG)
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.IMAGE_JPEG)
|
||||
.build());
|
||||
Message<byte[]> message = (Message<byte[]>) collector
|
||||
.forChannel(source.output()).poll(1, TimeUnit.SECONDS);
|
||||
assertThat(message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class)
|
||||
.includes(MimeTypeUtils.IMAGE_JPEG));
|
||||
assertThat(
|
||||
message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class)
|
||||
.includes(MimeTypeUtils.IMAGE_JPEG));
|
||||
assertThat(message.getPayload()).isEqualTo(data);
|
||||
}
|
||||
}
|
||||
@@ -174,17 +176,17 @@ public class ContentTypeTests {
|
||||
@Test
|
||||
public void testSendJavaSerializable() throws Exception {
|
||||
try (ConfigurableApplicationContext context = SpringApplication.run(
|
||||
SourceApplication.class, "--server.port=0",
|
||||
"--spring.jmx.enabled=false",
|
||||
SourceApplication.class, "--server.port=0", "--spring.jmx.enabled=false",
|
||||
"--spring.cloud.stream.bindings.output.contentType=application/x-java-serialized-object")) {
|
||||
MessageCollector collector = context.getBean(MessageCollector.class);
|
||||
Source source = context.getBean(Source.class);
|
||||
User user = new User("Alice");
|
||||
source.output().send(MessageBuilder.withPayload(user).build());
|
||||
Message<User> message = (Message<User>) collector
|
||||
.forChannel(source.output()).poll(1, TimeUnit.SECONDS);
|
||||
assertThat(message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class)
|
||||
.includes(MessageConverterUtils.X_JAVA_SERIALIZED_OBJECT));
|
||||
Message<User> message = (Message<User>) collector.forChannel(source.output())
|
||||
.poll(1, TimeUnit.SECONDS);
|
||||
assertThat(
|
||||
message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class)
|
||||
.includes(MessageConverterUtils.X_JAVA_SERIALIZED_OBJECT));
|
||||
User received = message.getPayload();
|
||||
assertThat(user.getName()).isEqualTo(received.getName());
|
||||
}
|
||||
@@ -193,17 +195,17 @@ public class ContentTypeTests {
|
||||
@Test
|
||||
public void testSendKryoSerialized() throws Exception {
|
||||
try (ConfigurableApplicationContext context = SpringApplication.run(
|
||||
SourceApplication.class, "--server.port=0",
|
||||
"--spring.jmx.enabled=false",
|
||||
SourceApplication.class, "--server.port=0", "--spring.jmx.enabled=false",
|
||||
"--spring.cloud.stream.bindings.output.contentType=application/x-java-object")) {
|
||||
MessageCollector collector = context.getBean(MessageCollector.class);
|
||||
Source source = context.getBean(Source.class);
|
||||
User user = new User("Alice");
|
||||
source.output().send(MessageBuilder.withPayload(user).build());
|
||||
Message<User> message = (Message<User>) collector
|
||||
.forChannel(source.output()).poll(1, TimeUnit.SECONDS);
|
||||
Message<User> message = (Message<User>) collector.forChannel(source.output())
|
||||
.poll(1, TimeUnit.SECONDS);
|
||||
User received = message.getPayload();
|
||||
assertThat(message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class)
|
||||
assertThat(message.getHeaders()
|
||||
.get(MessageHeaders.CONTENT_TYPE, MimeType.class)
|
||||
.includes(MimeType.valueOf(KryoMessageConverter.KRYO_MIME_TYPE)));
|
||||
assertThat(user.getName()).isEqualTo(received.getName());
|
||||
|
||||
@@ -211,10 +213,9 @@ public class ContentTypeTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendStringType() throws Exception{
|
||||
public void testSendStringType() throws Exception {
|
||||
try (ConfigurableApplicationContext context = SpringApplication.run(
|
||||
SourceApplication.class, "--server.port=0",
|
||||
"--spring.jmx.enabled=false",
|
||||
SourceApplication.class, "--server.port=0", "--spring.jmx.enabled=false",
|
||||
"--spring.cloud.stream.bindings.output.contentType=text/plain")) {
|
||||
MessageCollector collector = context.getBean(MessageCollector.class);
|
||||
Source source = context.getBean(Source.class);
|
||||
@@ -222,8 +223,9 @@ public class ContentTypeTests {
|
||||
source.output().send(MessageBuilder.withPayload(user).build());
|
||||
Message<String> message = (Message<String>) collector
|
||||
.forChannel(source.output()).poll(1, TimeUnit.SECONDS);
|
||||
assertThat(message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class)
|
||||
.includes(MimeTypeUtils.TEXT_PLAIN));
|
||||
assertThat(
|
||||
message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class)
|
||||
.includes(MimeTypeUtils.TEXT_PLAIN));
|
||||
assertThat(message.getPayload()).isEqualTo(user.toString());
|
||||
}
|
||||
}
|
||||
@@ -231,34 +233,35 @@ public class ContentTypeTests {
|
||||
@Test
|
||||
public void testSendTuple() throws Exception {
|
||||
try (ConfigurableApplicationContext context = SpringApplication.run(
|
||||
SourceApplication.class, "--server.port=0",
|
||||
"--spring.jmx.enabled=false",
|
||||
SourceApplication.class, "--server.port=0", "--spring.jmx.enabled=false",
|
||||
"--spring.cloud.stream.bindings.output.contentType=application/x-spring-tuple")) {
|
||||
MessageCollector collector = context.getBean(MessageCollector.class);
|
||||
Source source = context.getBean(Source.class);
|
||||
Tuple tuple = TupleBuilder.tuple().of("foo","bar");
|
||||
Tuple tuple = TupleBuilder.tuple().of("foo", "bar");
|
||||
source.output().send(MessageBuilder.withPayload(tuple).build());
|
||||
Message<byte[]> message = (Message<byte[]>) collector
|
||||
.forChannel(source.output()).poll(1, TimeUnit.SECONDS);
|
||||
assertThat(message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class)
|
||||
.includes(MessageConverterUtils.X_SPRING_TUPLE));
|
||||
assertThat(TupleBuilder.fromString(new String(message.getPayload()))).isEqualTo(tuple);
|
||||
assertThat(
|
||||
message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class)
|
||||
.includes(MessageConverterUtils.X_SPRING_TUPLE));
|
||||
assertThat(TupleBuilder.fromString(new String(message.getPayload())))
|
||||
.isEqualTo(tuple);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReceiveWithDefaults() throws Exception {
|
||||
try (ConfigurableApplicationContext context = SpringApplication.run(
|
||||
SinkApplication.class, "--server.port=0",
|
||||
"--spring.jmx.enabled=false")) {
|
||||
SinkApplication.class, "--server.port=0", "--spring.jmx.enabled=false")) {
|
||||
TestSink testSink = context.getBean(TestSink.class);
|
||||
SinkApplication sourceApp = context.getBean(SinkApplication.class);
|
||||
User user = new User("Alice");
|
||||
testSink.pojo().send(MessageBuilder.withPayload(mapper.writeValueAsBytes(user)).build());
|
||||
Map<String,Object> headers = (Map<String, Object>) sourceApp.arguments.pop();
|
||||
User received = (User)sourceApp.arguments.pop();
|
||||
assertThat(((MimeType)headers.get(MessageHeaders.CONTENT_TYPE))
|
||||
.includes(MimeTypeUtils.APPLICATION_JSON));
|
||||
testSink.pojo().send(MessageBuilder
|
||||
.withPayload(this.mapper.writeValueAsBytes(user)).build());
|
||||
Map<String, Object> headers = (Map<String, Object>) sourceApp.arguments.pop();
|
||||
User received = (User) sourceApp.arguments.pop();
|
||||
assertThat(((MimeType) headers.get(MessageHeaders.CONTENT_TYPE))
|
||||
.includes(MimeTypeUtils.APPLICATION_JSON));
|
||||
assertThat(user.getName()).isEqualTo(received.getName());
|
||||
}
|
||||
}
|
||||
@@ -266,23 +269,22 @@ public class ContentTypeTests {
|
||||
@Test
|
||||
public void testReceiveRawWithDifferentContentTypes() {
|
||||
try (ConfigurableApplicationContext context = SpringApplication.run(
|
||||
SinkApplication.class, "--server.port=0",
|
||||
"--spring.jmx.enabled=false")) {
|
||||
SinkApplication.class, "--server.port=0", "--spring.jmx.enabled=false")) {
|
||||
TestSink testSink = context.getBean(TestSink.class);
|
||||
SinkApplication sourceApp = context.getBean(SinkApplication.class);
|
||||
testSink.raw().send(MessageBuilder.withPayload(new byte[4])
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE,MimeTypeUtils.IMAGE_JPEG)
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.IMAGE_JPEG)
|
||||
.build());
|
||||
testSink.raw().send(MessageBuilder.withPayload(new byte[4])
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE,MimeTypeUtils.IMAGE_GIF)
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.IMAGE_GIF)
|
||||
.build());
|
||||
Map<String,Object> headers = (Map<String, Object>) sourceApp.arguments.pop();
|
||||
Map<String, Object> headers = (Map<String, Object>) sourceApp.arguments.pop();
|
||||
sourceApp.arguments.pop();
|
||||
assertThat(((MimeType)headers.get(MessageHeaders.CONTENT_TYPE))
|
||||
assertThat(((MimeType) headers.get(MessageHeaders.CONTENT_TYPE))
|
||||
.includes(MimeTypeUtils.IMAGE_GIF));
|
||||
headers = (Map<String, Object>) sourceApp.arguments.pop();
|
||||
sourceApp.arguments.pop();
|
||||
assertThat(((MimeType)headers.get(MessageHeaders.CONTENT_TYPE))
|
||||
assertThat(((MimeType) headers.get(MessageHeaders.CONTENT_TYPE))
|
||||
.includes(MimeTypeUtils.IMAGE_JPEG));
|
||||
}
|
||||
}
|
||||
@@ -293,20 +295,20 @@ public class ContentTypeTests {
|
||||
try (ConfigurableApplicationContext context = SpringApplication.run(
|
||||
SinkApplication.class, "--server.port=0", "--debug",
|
||||
"--spring.jmx.enabled=false",
|
||||
"--spring.cloud.stream.bindings.pojo_input.contentType=application/x-java-object;type=org.springframework.cloud.stream.config.contentType.User"
|
||||
)) {
|
||||
"--spring.cloud.stream.bindings.pojo_input.contentType="
|
||||
+ "application/x-java-object;type=org.springframework.cloud.stream.config.contentType.User")) {
|
||||
TestSink testSink = context.getBean(TestSink.class);
|
||||
SinkApplication sourceApp = context.getBean(SinkApplication.class);
|
||||
Kryo kryo = new Kryo();
|
||||
User user = new User("Alice");
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
Output output = new Output(baos);
|
||||
kryo.writeObject(output,user);
|
||||
kryo.writeObject(output, user);
|
||||
output.close();
|
||||
testSink.pojo().send(MessageBuilder.withPayload(baos.toByteArray()).build());
|
||||
Map<String,Object> headers = (Map<String, Object>) sourceApp.arguments.pop();
|
||||
User received = (User)sourceApp.arguments.pop();
|
||||
assertThat(((MimeType)headers.get(MessageHeaders.CONTENT_TYPE))
|
||||
Map<String, Object> headers = (Map<String, Object>) sourceApp.arguments.pop();
|
||||
User received = (User) sourceApp.arguments.pop();
|
||||
assertThat(((MimeType) headers.get(MessageHeaders.CONTENT_TYPE))
|
||||
.includes(MimeType.valueOf(KryoMessageConverter.KRYO_MIME_TYPE)));
|
||||
assertThat(user.getName()).isEqualTo(received.getName());
|
||||
}
|
||||
@@ -316,23 +318,23 @@ public class ContentTypeTests {
|
||||
@SuppressWarnings("deprecation")
|
||||
public void testReceiveKryoWithHeadersOverridingDefault() {
|
||||
try (ConfigurableApplicationContext context = SpringApplication.run(
|
||||
SinkApplication.class, "--server.port=0",
|
||||
"--spring.jmx.enabled=false"
|
||||
)) {
|
||||
SinkApplication.class, "--server.port=0", "--spring.jmx.enabled=false")) {
|
||||
TestSink testSink = context.getBean(TestSink.class);
|
||||
SinkApplication sourceApp = context.getBean(SinkApplication.class);
|
||||
Kryo kryo = new Kryo();
|
||||
User user = new User("Alice");
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
Output output = new Output(baos);
|
||||
kryo.writeObject(output,user);
|
||||
kryo.writeObject(output, user);
|
||||
output.close();
|
||||
testSink.pojo().send(MessageBuilder.withPayload(baos.toByteArray())
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE, MimeType.valueOf(KryoMessageConverter.KRYO_MIME_TYPE))
|
||||
.build());
|
||||
Map<String,Object> headers = (Map<String, Object>) sourceApp.arguments.pop();
|
||||
User received = (User)sourceApp.arguments.pop();
|
||||
assertThat(((MimeType)headers.get(MessageHeaders.CONTENT_TYPE))
|
||||
testSink.pojo()
|
||||
.send(MessageBuilder.withPayload(baos.toByteArray())
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE,
|
||||
MimeType.valueOf(KryoMessageConverter.KRYO_MIME_TYPE))
|
||||
.build());
|
||||
Map<String, Object> headers = (Map<String, Object>) sourceApp.arguments.pop();
|
||||
User received = (User) sourceApp.arguments.pop();
|
||||
assertThat(((MimeType) headers.get(MessageHeaders.CONTENT_TYPE))
|
||||
.includes(MimeType.valueOf(KryoMessageConverter.KRYO_MIME_TYPE)));
|
||||
assertThat(user.getName()).isEqualTo(received.getName());
|
||||
}
|
||||
@@ -342,58 +344,23 @@ public class ContentTypeTests {
|
||||
@Ignore
|
||||
public void testReceiveJavaSerializable() throws Exception {
|
||||
try (ConfigurableApplicationContext context = SpringApplication.run(
|
||||
SinkApplication.class, "--server.port=0",
|
||||
"--spring.jmx.enabled=false",
|
||||
"--spring.cloud.stream.bindings.pojo_input.contentType=application/x-java-serialized-object"
|
||||
)) {
|
||||
SinkApplication.class, "--server.port=0", "--spring.jmx.enabled=false",
|
||||
"--spring.cloud.stream.bindings.pojo_input.contentType=application/x-java-serialized-object")) {
|
||||
TestSink testSink = context.getBean(TestSink.class);
|
||||
SinkApplication sourceApp = context.getBean(SinkApplication.class);
|
||||
User user = new User("Alice");
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
new ObjectOutputStream(baos).writeObject(user);
|
||||
testSink.pojo().send(MessageBuilder.withPayload(baos.toByteArray()).build());
|
||||
Map<String,Object> headers = (Map<String, Object>) sourceApp.arguments.pop();
|
||||
User received = (User)sourceApp.arguments.pop();
|
||||
assertThat(((MimeType)headers.get(MessageHeaders.CONTENT_TYPE))
|
||||
Map<String, Object> headers = (Map<String, Object>) sourceApp.arguments.pop();
|
||||
User received = (User) sourceApp.arguments.pop();
|
||||
assertThat(((MimeType) headers.get(MessageHeaders.CONTENT_TYPE))
|
||||
.includes(MessageConverterUtils.X_JAVA_SERIALIZED_OBJECT));
|
||||
assertThat(user.getName()).isEqualTo(received.getName());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@EnableBinding(Source.class)
|
||||
@SpringBootApplication
|
||||
public static class SourceApplication {
|
||||
}
|
||||
|
||||
@EnableBinding(TestSink.class)
|
||||
@SpringBootApplication
|
||||
public static class SinkApplication {
|
||||
|
||||
public LinkedList<? super Object> arguments = new LinkedList<>();
|
||||
|
||||
@StreamListener("POJO_INPUT")
|
||||
public void receive(User user, @Headers Map<String, Object> headers){
|
||||
arguments.push(user);
|
||||
arguments.push(headers);
|
||||
}
|
||||
|
||||
@StreamListener("TUPLE_INPUT")
|
||||
public void receive(Tuple tuple){
|
||||
}
|
||||
|
||||
@StreamListener("STRING_INPUT")
|
||||
public void receive(String string){
|
||||
}
|
||||
|
||||
@StreamListener("RAW_INPUT")
|
||||
public void receive(byte[] data, @Headers Map<String, Object> headers){
|
||||
arguments.push(data);
|
||||
arguments.push(headers);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public interface TestSink {
|
||||
|
||||
@Input("POJO_INPUT")
|
||||
@@ -409,4 +376,39 @@ public class ContentTypeTests {
|
||||
SubscribableChannel raw();
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding(Source.class)
|
||||
@SpringBootApplication
|
||||
public static class SourceApplication {
|
||||
|
||||
}
|
||||
|
||||
@EnableBinding(TestSink.class)
|
||||
@SpringBootApplication
|
||||
public static class SinkApplication {
|
||||
|
||||
public LinkedList<? super Object> arguments = new LinkedList<>();
|
||||
|
||||
@StreamListener("POJO_INPUT")
|
||||
public void receive(User user, @Headers Map<String, Object> headers) {
|
||||
this.arguments.push(user);
|
||||
this.arguments.push(headers);
|
||||
}
|
||||
|
||||
@StreamListener("TUPLE_INPUT")
|
||||
public void receive(Tuple tuple) {
|
||||
}
|
||||
|
||||
@StreamListener("STRING_INPUT")
|
||||
public void receive(String string) {
|
||||
}
|
||||
|
||||
@StreamListener("RAW_INPUT")
|
||||
public void receive(byte[] data, @Headers Map<String, Object> headers) {
|
||||
this.arguments.push(data);
|
||||
this.arguments.push(headers);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
* Copyright 2017-2019 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.
|
||||
@@ -30,7 +30,8 @@ public class User implements Serializable {
|
||||
|
||||
private String name;
|
||||
|
||||
public User(){}
|
||||
public User() {
|
||||
}
|
||||
|
||||
@JsonCreator
|
||||
public User(@JsonProperty("name") String name) {
|
||||
@@ -38,7 +39,7 @@ public class User implements Serializable {
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
@@ -48,8 +49,9 @@ public class User implements Serializable {
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuffer sb = new StringBuffer("User{");
|
||||
sb.append("name='").append(name).append('\'');
|
||||
sb.append("name='").append(this.name).append('\'');
|
||||
sb.append('}');
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user