Search in sources :

Example 21 with InputDestination

use of org.springframework.cloud.stream.binder.test.InputDestination in project spring-cloud-stream by spring-cloud.

the class ContentTypeTckTests method byteArrayMessageToStringJsonMessageStreamListener.

@Test
public void byteArrayMessageToStringJsonMessageStreamListener() {
    ApplicationContext context = new SpringApplicationBuilder(ByteArrayMessageToStringJsonMessageStreamListener.class).web(WebApplicationType.NONE).run("--spring.jmx.enabled=false");
    InputDestination source = context.getBean(InputDestination.class);
    OutputDestination target = context.getBean(OutputDestination.class);
    String jsonPayload = "{\"name\":\"oleg\"}";
    source.send(new GenericMessage<>(jsonPayload.getBytes()));
    Message<byte[]> outputMessage = target.receive();
    assertEquals(MimeTypeUtils.APPLICATION_JSON, outputMessage.getHeaders().get(MessageHeaders.CONTENT_TYPE));
    assertEquals("{\"name\":\"bob\"}", new String(outputMessage.getPayload(), StandardCharsets.UTF_8));
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) OutputDestination(org.springframework.cloud.stream.binder.test.OutputDestination) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) InputDestination(org.springframework.cloud.stream.binder.test.InputDestination) Test(org.junit.Test)

Example 22 with InputDestination

use of org.springframework.cloud.stream.binder.test.InputDestination in project spring-cloud-stream by spring-cloud.

the class ContentTypeTckTests method _toStringDefaultContentTypePropertyUnknownContentType.

@Test
public void _toStringDefaultContentTypePropertyUnknownContentType() {
    ApplicationContext context = new SpringApplicationBuilder(StringToStringStreamListener.class).web(WebApplicationType.NONE).run("--spring.cloud.stream.default.contentType=foo/bar", "--spring.jmx.enabled=false");
    InputDestination source = context.getBean(InputDestination.class);
    TestChannelBinder binder = context.getBean(TestChannelBinder.class);
    String jsonPayload = "{\"name\":\"oleg\"}";
    source.send(new GenericMessage<>(jsonPayload.getBytes()));
    assertTrue(binder.getLastError().getPayload() instanceof MessageConversionException);
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) MessageConversionException(org.springframework.messaging.converter.MessageConversionException) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) TestChannelBinder(org.springframework.cloud.stream.binder.test.TestChannelBinder) InputDestination(org.springframework.cloud.stream.binder.test.InputDestination) Test(org.junit.Test)

Example 23 with InputDestination

use of org.springframework.cloud.stream.binder.test.InputDestination in project spring-cloud-stream by spring-cloud.

the class ContentTypeTckTests method byteArrayToPojoInboundContentTypeBinding.

@Test
public void byteArrayToPojoInboundContentTypeBinding() {
    ApplicationContext context = new SpringApplicationBuilder(ByteArrayToPojoStreamListener.class).web(WebApplicationType.NONE).run("--spring.cloud.stream.bindings.input.contentType=text/plain", "--spring.jmx.enabled=false");
    InputDestination source = context.getBean(InputDestination.class);
    OutputDestination target = context.getBean(OutputDestination.class);
    String jsonPayload = "{\"name\":\"oleg\"}";
    source.send(new GenericMessage<>(jsonPayload.getBytes()));
    Message<byte[]> outputMessage = target.receive();
    assertEquals(MimeTypeUtils.APPLICATION_JSON, outputMessage.getHeaders().get(MessageHeaders.CONTENT_TYPE));
    assertEquals(jsonPayload, new String(outputMessage.getPayload(), StandardCharsets.UTF_8));
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) OutputDestination(org.springframework.cloud.stream.binder.test.OutputDestination) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) InputDestination(org.springframework.cloud.stream.binder.test.InputDestination) Test(org.junit.Test)

Example 24 with InputDestination

use of org.springframework.cloud.stream.binder.test.InputDestination in project spring-cloud-stream by spring-cloud.

the class ContentTypeTckTests method pojoMessageToStringMessage.

@Test
public void pojoMessageToStringMessage() {
    ApplicationContext context = new SpringApplicationBuilder(PojoMessageToStringMessageStreamListener.class).web(WebApplicationType.NONE).run("--spring.jmx.enabled=false");
    InputDestination source = context.getBean(InputDestination.class);
    OutputDestination target = context.getBean(OutputDestination.class);
    String jsonPayload = "{\"name\":\"oleg\"}";
    source.send(new GenericMessage<>(jsonPayload.getBytes()));
    Message<byte[]> outputMessage = target.receive();
    assertEquals(MimeTypeUtils.TEXT_PLAIN, outputMessage.getHeaders().get(MessageHeaders.CONTENT_TYPE));
    assertEquals("oleg", new String(outputMessage.getPayload(), StandardCharsets.UTF_8));
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) OutputDestination(org.springframework.cloud.stream.binder.test.OutputDestination) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) InputDestination(org.springframework.cloud.stream.binder.test.InputDestination) Test(org.junit.Test)

Example 25 with InputDestination

use of org.springframework.cloud.stream.binder.test.InputDestination in project spring-cloud-stream by spring-cloud.

the class ContentTypeTckTests method kryo_pojoToPojo.

@Test
@SuppressWarnings("deprecation")
public void kryo_pojoToPojo() {
    ApplicationContext context = new SpringApplicationBuilder(PojoToPojoStreamListener.class).web(WebApplicationType.NONE).run("--spring.cloud.stream.default.contentType=application/x-java-object", "--spring.jmx.enabled=false");
    InputDestination source = context.getBean(InputDestination.class);
    OutputDestination target = context.getBean(OutputDestination.class);
    KryoMessageConverter converter = new KryoMessageConverter(null, true);
    @SuppressWarnings("unchecked") Message<byte[]> message = (Message<byte[]>) converter.toMessage(new Person("oleg"), new MessageHeaders(Collections.singletonMap(MessageHeaders.CONTENT_TYPE, MessageConverterUtils.X_JAVA_OBJECT)));
    source.send(new GenericMessage<>(message.getPayload()));
    Message<byte[]> outputMessage = target.receive();
    assertNotNull(outputMessage);
    MimeType contentType = (MimeType) outputMessage.getHeaders().get(MessageHeaders.CONTENT_TYPE);
    assertEquals("x-java-object", contentType.getSubtype());
    assertEquals(Person.class.getName(), contentType.getParameters().get("type"));
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) OutputDestination(org.springframework.cloud.stream.binder.test.OutputDestination) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) MessageHeaders(org.springframework.messaging.MessageHeaders) InputDestination(org.springframework.cloud.stream.binder.test.InputDestination) MimeType(org.springframework.util.MimeType) KryoMessageConverter(org.springframework.cloud.stream.converter.KryoMessageConverter) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)26 SpringApplicationBuilder (org.springframework.boot.builder.SpringApplicationBuilder)26 InputDestination (org.springframework.cloud.stream.binder.test.InputDestination)26 ApplicationContext (org.springframework.context.ApplicationContext)26 OutputDestination (org.springframework.cloud.stream.binder.test.OutputDestination)24 MessageHeaders (org.springframework.messaging.MessageHeaders)4 TestChannelBinder (org.springframework.cloud.stream.binder.test.TestChannelBinder)2 KryoMessageConverter (org.springframework.cloud.stream.converter.KryoMessageConverter)2 Message (org.springframework.messaging.Message)2 MessageConversionException (org.springframework.messaging.converter.MessageConversionException)2 GenericMessage (org.springframework.messaging.support.GenericMessage)2 MimeType (org.springframework.util.MimeType)2