Search in sources :

Example 16 with OutputDestination

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

the class ContentTypeTckTests method kryo_pojoToPojoContentTypeHeader.

@Test
@SuppressWarnings("deprecation")
public void kryo_pojoToPojoContentTypeHeader() {
    ApplicationContext context = new SpringApplicationBuilder(PojoToPojoStreamListener.class).web(WebApplicationType.NONE).run("--spring.jmx.enabled=false", "--spring.cloud.stream.bindings.output.contentType=application/x-java-object");
    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(message);
    Message<byte[]> outputMessage = target.receive();
    assertNotNull(outputMessage);
    MimeType contentType = (MimeType) outputMessage.getHeaders().get(MessageHeaders.CONTENT_TYPE);
    assertEquals("x-java-object", contentType.getSubtype());
}
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)

Example 17 with OutputDestination

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

the class ContentTypeTckTests method overrideMessageConverter_defaultContentTypeBinding.

/**
 * This test simply demonstrates how one can override an existing MessageConverter for a given contentType.
 * In this case we are demonstrating how Kryo converter can be overriden ('application/x-java-object' maps to Kryo).
 */
@Test
public void overrideMessageConverter_defaultContentTypeBinding() {
    ApplicationContext context = new SpringApplicationBuilder(StringToStringStreamListener.class, CustomConverters.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);
    String jsonPayload = "{\"name\":\"oleg\"}";
    source.send(new GenericMessage<>(jsonPayload.getBytes()));
    Message<byte[]> outputMessage = target.receive();
    assertNotNull(outputMessage);
    System.out.println(new String(outputMessage.getPayload(), StandardCharsets.UTF_8));
    assertEquals("AlwaysStringKryoMessageConverter", new String(outputMessage.getPayload(), StandardCharsets.UTF_8));
    assertEquals(MimeType.valueOf("application/x-java-object"), outputMessage.getHeaders().get(MessageHeaders.CONTENT_TYPE));
}
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 18 with OutputDestination

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

the class ContentTypeTckTests method toCollectionWithParameterizedType.

@Test
public void toCollectionWithParameterizedType() {
    ApplicationContext context = new SpringApplicationBuilder(CollectionWithParameterizedTypes.class).web(WebApplicationType.NONE).run("--spring.jmx.enabled=false");
    InputDestination source = context.getBean(InputDestination.class);
    OutputDestination target = context.getBean(OutputDestination.class);
    String jsonPayload = "[{\"person\":{\"name\":\"jon\"},\"id\":123},{\"person\":{\"name\":\"jane\"},\"id\":456}]";
    source.send(new GenericMessage<>(jsonPayload.getBytes()));
    Message<byte[]> outputMessage = target.receive();
    assertThat(outputMessage.getPayload()).isEqualTo(jsonPayload.getBytes());
}
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 19 with OutputDestination

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

the class ContentTypeTckTests method pojoToPojo.

@Test
public void pojoToPojo() {
    ApplicationContext context = new SpringApplicationBuilder(PojoToPojoStreamListener.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(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 20 with OutputDestination

use of org.springframework.cloud.stream.binder.test.OutputDestination 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)

Aggregations

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