org.springframework.cloud.stream.test.binder.MessageCollector Java Examples

The following examples show how to use org.springframework.cloud.stream.test.binder.MessageCollector. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StreamListenerMessageArgumentTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testMessageArgument() throws Exception {
	ConfigurableApplicationContext context = SpringApplication.run(this.configClass,
			"--server.port=0",
			"--spring.cloud.stream.bindings.output.contentType=text/plain",
			"--spring.jmx.enabled=false");
	MessageCollector collector = context.getBean(MessageCollector.class);
	Processor processor = context.getBean(Processor.class);
	String id = UUID.randomUUID().toString();
	processor.input().send(MessageBuilder.withPayload("barbar" + id)
			.setHeader("contentType", "text/plain").build());
	TestPojoWithMessageArgument testPojoWithMessageArgument = context
			.getBean(TestPojoWithMessageArgument.class);
	assertThat(testPojoWithMessageArgument.receivedMessages).hasSize(1);
	assertThat(testPojoWithMessageArgument.receivedMessages.get(0).getPayload())
			.isEqualTo("barbar" + id);
	Message<String> message = (Message<String>) collector
			.forChannel(processor.output()).poll(1, TimeUnit.SECONDS);
	assertThat(message).isNotNull();
	assertThat(message.getPayload()).contains("barbar" + id);
	context.close();
}
 
Example #2
Source File: StreamListenerMethodReturnWithConversionTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testReturnNoConversion() throws Exception {
	ConfigurableApplicationContext context = SpringApplication.run(
			this.configClass, "--server.port=0", "--spring.jmx.enabled=false");
	MessageCollector collector = context.getBean(MessageCollector.class);
	Processor processor = context.getBean(Processor.class);
	String id = UUID.randomUUID().toString();
	processor.input()
			.send(MessageBuilder.withPayload("{\"foo\":\"barbar" + id + "\"}")
					.setHeader("contentType", "application/json").build());
	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);
	assertThat(message).isNotNull();
	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);
	context.close();
}
 
Example #3
Source File: StreamListenerHandlerBeanTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testHandlerBean() throws Exception {
	ConfigurableApplicationContext context = SpringApplication.run(this.configClass,
			"--spring.cloud.stream.bindings.output.contentType=application/json",
			"--server.port=0");
	MessageCollector collector = context.getBean(MessageCollector.class);
	Processor processor = context.getBean(Processor.class);
	String id = UUID.randomUUID().toString();
	processor.input()
			.send(MessageBuilder.withPayload("{\"foo\":\"barbar" + id + "\"}")
					.setHeader("contentType", "application/json").build());
	HandlerBean handlerBean = context.getBean(HandlerBean.class);
	Assertions.assertThat(handlerBean.receivedPojos).hasSize(1);
	Assertions.assertThat(handlerBean.receivedPojos.get(0))
			.hasFieldOrPropertyWithValue("foo", "barbar" + id);
	Message<String> message = (Message<String>) collector
			.forChannel(processor.output()).poll(1, TimeUnit.SECONDS);
	assertThat(message).isNotNull();
	assertThat(message.getPayload()).isEqualTo("{\"bar\":\"barbar" + id + "\"}");
	assertThat(message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class)
			.includes(MimeTypeUtils.APPLICATION_JSON));
	context.close();
}
 
Example #4
Source File: StreamListenerHandlerMethodTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@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");
	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);
	MessageCollector messageCollector = context.getBean(MessageCollector.class);
	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());
	context.close();
}
 
Example #5
Source File: StreamListenerHandlerMethodTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testMethodHeadersNotPropagatged() throws Exception {
	ConfigurableApplicationContext context = SpringApplication.run(
			TestMethodHeadersNotPropagated.class, "--server.port=0",
			"--spring.jmx.enabled=false",
			"--spring.cloud.stream.bindings.input.contentType=text/plain",
			"--spring.cloud.stream.bindings.output.contentType=text/plain");
	Processor processor = context.getBean(Processor.class);
	final String testMessage = "testing";
	processor.input().send(
			MessageBuilder.withPayload(testMessage).setHeader("foo", "bar").build());
	MessageCollector messageCollector = context.getBean(MessageCollector.class);
	Message<String> result = (Message<String>) messageCollector
			.forChannel(processor.output()).poll(1000, TimeUnit.MILLISECONDS);
	assertThat(result).isNotNull();
	assertThat(result.getPayload()).isEqualTo(testMessage.toUpperCase());
	assertThat(result.getHeaders().get("foo")).isNull();
	context.close();
}
 
Example #6
Source File: StreamListenerHandlerMethodTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@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.
 */
public void testMethodHeadersPropagatged() throws Exception {
	ConfigurableApplicationContext context = SpringApplication.run(
			TestMethodHeadersPropagated.class, "--server.port=0",
			"--spring.jmx.enabled=false",
			"--spring.cloud.stream.bindings.input.contentType=text/plain",
			"--spring.cloud.stream.bindings.output.contentType=text/plain");
	Processor processor = context.getBean(Processor.class);
	final String testMessage = "testing";
	processor.input().send(
			MessageBuilder.withPayload(testMessage).setHeader("foo", "bar").build());
	MessageCollector messageCollector = context.getBean(MessageCollector.class);
	Message<String> result = (Message<String>) messageCollector
			.forChannel(processor.output()).poll(1000, TimeUnit.MILLISECONDS);
	assertThat(result).isNotNull();
	assertThat(result.getPayload()).isEqualTo(testMessage.toUpperCase());
	assertThat(result.getHeaders().get("foo")).isEqualTo("bar");
	context.close();
}
 
Example #7
Source File: StreamListenerHandlerMethodTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testMethodWithObjectAsMethodArgument() throws Exception {
	ConfigurableApplicationContext context = SpringApplication.run(
			TestMethodWithObjectAsMethodArgument.class, "--server.port=0",
			"--spring.jmx.enabled=false",
			"--spring.cloud.stream.bindings.input.contentType=text/plain",
			"--spring.cloud.stream.bindings.output.contentType=text/plain");
	Processor processor = context.getBean(Processor.class);
	final String testMessage = "testing";
	processor.input().send(MessageBuilder.withPayload(testMessage).build());
	MessageCollector messageCollector = context.getBean(MessageCollector.class);
	Message<String> result = (Message<String>) messageCollector
			.forChannel(processor.output()).poll(1000, TimeUnit.MILLISECONDS);
	assertThat(result).isNotNull();
	assertThat(result.getPayload()).isEqualTo(testMessage.toUpperCase());
	context.close();
}
 
Example #8
Source File: StreamListenerMethodWithReturnMessageTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testReturnMessage() throws Exception {
	ConfigurableApplicationContext context = SpringApplication.run(this.configClass,
			"--server.port=0", "--spring.jmx.enabled=false");
	MessageCollector collector = context.getBean(MessageCollector.class);
	Processor processor = context.getBean(Processor.class);
	String id = UUID.randomUUID().toString();
	processor.input()
			.send(MessageBuilder.withPayload("{\"foo\":\"barbar" + id + "\"}")
					.setHeader("contentType", "application/json").build());
	TestPojoWithMessageReturn testPojoWithMessageReturn = context
			.getBean(TestPojoWithMessageReturn.class);
	Assertions.assertThat(testPojoWithMessageReturn.receivedPojos).hasSize(1);
	Assertions.assertThat(testPojoWithMessageReturn.receivedPojos.get(0))
			.hasFieldOrPropertyWithValue("foo", "barbar" + id);
	Message<String> message = (Message<String>) collector
			.forChannel(processor.output()).poll(1, TimeUnit.SECONDS);
	assertThat(message).isNotNull();
	assertThat(message.getPayload()).contains("barbar" + id);
	context.close();
}
 
Example #9
Source File: ContentTypeTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendStringType() throws Exception {
	try (ConfigurableApplicationContext context = SpringApplication.run(
			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);
		User user = new User("Alice");
		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.getPayload()).isEqualTo(user.toString());
	}
}
 
Example #10
Source File: ContentTypeTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendBinaryDataWithContentTypeUsingHeaders() throws Exception {
	try (ConfigurableApplicationContext context = SpringApplication.run(
			SourceApplication.class, "--server.port=0",
			"--spring.jmx.enabled=false")) {
		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.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.getPayload()).isEqualTo(data);
	}
}
 
Example #11
Source File: ContentTypeTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendBynaryData() throws Exception {
	try (ConfigurableApplicationContext context = SpringApplication.run(
			SourceApplication.class, "--server.port=0",
			"--spring.jmx.enabled=false")) {

		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());
		Message<byte[]> message = (Message<byte[]>) collector
				.forChannel(source.output()).poll(1, TimeUnit.SECONDS);
		assertThat(
				message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class)
						.includes(MimeTypeUtils.APPLICATION_OCTET_STREAM));
		assertThat(message.getPayload()).isEqualTo(data);
	}
}
 
Example #12
Source File: ContentTypeTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendJsonString() throws Exception {
	try (ConfigurableApplicationContext context = SpringApplication.run(
			SourceApplication.class, "--server.port=0",
			"--spring.jmx.enabled=false")) {
		MessageCollector collector = context.getBean(MessageCollector.class);
		Source source = context.getBean(Source.class);
		source.output().send(MessageBuilder.withPayload("foo").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.APPLICATION_JSON));
		assertThat("foo").isEqualTo(message.getPayload());
	}
}
 
Example #13
Source File: ContentTypeTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendJsonAsString() throws Exception {
	try (ConfigurableApplicationContext context = SpringApplication.run(
			SourceApplication.class, "--server.port=0",
			"--spring.jmx.enabled=false")) {
		MessageCollector collector = context.getBean(MessageCollector.class);
		Source source = context.getBean(Source.class);
		User user = new User("Alice");
		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);
		assertThat(
				message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class)
						.includes(MimeTypeUtils.APPLICATION_JSON));
		assertThat(json).isEqualTo(message.getPayload());
	}
}
 
Example #14
Source File: ContentTypeTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendWithDefaultContentType() throws Exception {
	try (ConfigurableApplicationContext context = SpringApplication.run(
			SourceApplication.class, "--server.port=0",
			"--spring.jmx.enabled=false")) {

		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<String> message = (Message<String>) collector
				.forChannel(source.output()).poll(1, TimeUnit.SECONDS);
		User received = this.mapper.readValue(message.getPayload(), User.class);
		assertThat(
				message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class)
						.includes(MimeTypeUtils.APPLICATION_JSON));
		assertThat(user.getName()).isEqualTo(received.getName());
	}
}
 
Example #15
Source File: StreamListenerMethodWithReturnValueTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testReturn() throws Exception {
	ConfigurableApplicationContext context = SpringApplication.run(this.configClass,
			"--server.port=0", "--spring.jmx.enabled=false");
	MessageCollector collector = context.getBean(MessageCollector.class);
	Processor processor = context.getBean(Processor.class);
	String id = UUID.randomUUID().toString();
	processor.input()
			.send(MessageBuilder.withPayload("{\"foo\":\"barbar" + id + "\"}")
					.setHeader("contentType", "application/json").build());
	Message<String> message = (Message<String>) collector
			.forChannel(processor.output()).poll(1, TimeUnit.SECONDS);
	TestStringProcessor testStringProcessor = context
			.getBean(TestStringProcessor.class);
	Assertions.assertThat(testStringProcessor.receivedPojos).hasSize(1);
	Assertions.assertThat(testStringProcessor.receivedPojos.get(0))
			.hasFieldOrPropertyWithValue("foo", "barbar" + id);
	assertThat(message).isNotNull();
	assertThat(message.getPayload()).contains("barbar" + id);
	context.close();
}
 
Example #16
Source File: ContentTypeTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendBinaryDataWithContentType() throws Exception {
	try (ConfigurableApplicationContext context = SpringApplication.run(
			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());
		Message<byte[]> message = (Message<byte[]>) collector
				.forChannel(source.output()).poll(1, TimeUnit.SECONDS);
		assertThat(message.getPayload()).isEqualTo(data);
	}
}
 
Example #17
Source File: StreamListenerMethodReturnWithConversionTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@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");
	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);
	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);
	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));
	context.close();
}
 
Example #18
Source File: SpringCloudStreamAwsApplicationsITCase.java    From spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
@Test
public void testS3Source() throws IOException, InterruptedException {
	String bucket = "TestBucket";
	String key = "foo";
	String content = "Spring Cloud Stream AWS S3 Source test";

	this.applicationContext = SpringApplication.run(S3SourceBootConfiguration.class,
			"--s3.remoteDir=" + bucket, "--file.consumer.mode=lines", "--file.consumer.with-markers=false");

	ResourceIdResolver resourceIdResolver = this.applicationContext.getBean(ResourceIdResolver.class);

	AmazonS3 amazonS3 = this.applicationContext.getBean(AmazonS3.class);
	ObjectMetadata objectMetadata = new ObjectMetadata();
	objectMetadata.setContentLength(content.length());
	String bucketName = resourceIdResolver.resolveToPhysicalResourceId(bucket);
	amazonS3.putObject(bucketName, key, new ByteArrayInputStream(content.getBytes("UTF-8")), objectMetadata);

	try {
		Source source = this.applicationContext.getBean(Source.class);
		MessageCollector messageCollector = this.applicationContext.getBean(MessageCollector.class);
		Message<?> received = messageCollector.forChannel(source.output()).poll(10, TimeUnit.SECONDS);
		assertNotNull(received);
		assertThat(received, hasHeader(FileHeaders.FILENAME, key));
		assertThat(received, hasPayload("Spring Cloud Stream AWS S3 Source test"));
	}
	finally {
		amazonS3.deleteObject(bucketName, key);
	}
}
 
Example #19
Source File: StreamListenerWithAnnotatedInputOutputArgsTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void sendMessageAndValidate(ConfigurableApplicationContext context)
		throws InterruptedException {
	Processor processor = context.getBean(Processor.class);
	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);
	assertThat(result).isNotNull();
	assertThat(result.getPayload()).isEqualTo("HELLO");
	context.close();
}