Example usage for org.springframework.messaging.support MessageBuilder withPayload

List of usage examples for org.springframework.messaging.support MessageBuilder withPayload

Introduction

In this page you can find the example usage for org.springframework.messaging.support MessageBuilder withPayload.

Prototype

public static <T> MessageBuilder<T> withPayload(T payload) 

Source Link

Document

Create a new builder for a message with the given payload.

Usage

From source file:org.springframework.cloud.aws.messaging.support.converter.NotificationRequestConverterTest.java

@Test
public void testWrongTypeSupplied() throws Exception {
    this.expectedException.expect(MessageConversionException.class);
    this.expectedException.expectMessage("is not a valid notification");
    ObjectNode jsonObject = JsonNodeFactory.instance.objectNode();
    jsonObject.put("Type", "Subscription");
    jsonObject.put("Message", "Hello World!");
    String payload = jsonObject.toString();
    new NotificationRequestConverter().fromMessage(MessageBuilder.withPayload(payload).build(), null);
}

From source file:com.codeveo.lago.bot.stomp.client.LagoJsonMessageConverter.java

@Override
public Message<?> toMessage(Object payload, MessageHeaders header) {
    try {//from   w  w w  .  j  a  v a 2s.co  m
        if (payload == null) {
            throw new Exception("Message must not be null");
        }

        if (!(payload instanceof RClientMessage)) {
            throw new Exception("Payload is not type of '" + RClientMessage.class.getName() + "' but '"
                    + payload.getClass().getName() + "'");
        }

        String json = mapper.writeValueAsString((RClientMessage) payload);
        /* NOTE: Message that goes to STOMP broker */
        Message<?> message = MessageBuilder.withPayload(json.getBytes()).build();

        return message;
    } catch (Exception e) {
        LOG.error("Error occured during message serialization", e);
    }

    return null;
}

From source file:org.springframework.cloud.aws.messaging.support.NotificationMessageArgumentResolverTest.java

@Test
public void resolveArgument_withValidMessagePayload_shouldReturnNotificationMessage() throws Exception {
    // Arrange//from   ww w . j a va2  s  . co m
    NotificationMessageArgumentResolver notificationMessageArgumentResolver = new NotificationMessageArgumentResolver();
    Method methodWithNotificationMessageArgument = this.getClass()
            .getDeclaredMethod("methodWithNotificationMessageArgument", String.class);
    MethodParameter methodParameter = new MethodParameter(methodWithNotificationMessageArgument, 0);

    ObjectNode jsonObject = JsonNodeFactory.instance.objectNode();
    jsonObject.put("Type", "Notification");
    jsonObject.put("Message", "Hello World!");
    String payload = jsonObject.toString();
    Message<String> message = MessageBuilder.withPayload(payload).build();

    // Act
    Object result = notificationMessageArgumentResolver.resolveArgument(methodParameter, message);

    // Assert
    assertTrue(String.class.isInstance(result));
    assertEquals("Hello World!", result);
}

From source file:org.springframework.cloud.aws.messaging.support.NotificationSubjectArgumentResolverTest.java

@Test
public void resolveArgument_withValidRequestPayload_shouldReturnNotificationSubject() throws Exception {
    // Arrange/*  w ww.j  a va 2s  .c  o  m*/
    NotificationSubjectArgumentResolver notificationSubjectArgumentResolver = new NotificationSubjectArgumentResolver();
    Method methodWithNotificationSubjectArgument = this.getClass()
            .getDeclaredMethod("methodWithNotificationSubjectArgument", String.class);
    MethodParameter methodParameter = new MethodParameter(methodWithNotificationSubjectArgument, 0);

    ObjectNode jsonObject = JsonNodeFactory.instance.objectNode();
    jsonObject.put("Type", "Notification");
    jsonObject.put("Subject", "My subject!");
    jsonObject.put("Message", "message");
    String payload = jsonObject.toString();
    Message<String> message = MessageBuilder.withPayload(payload).build();

    // Act
    Object result = notificationSubjectArgumentResolver.resolveArgument(methodParameter, message);

    // Assert
    assertTrue(String.class.isInstance(result));
    assertEquals("My subject!", result);
}

From source file:io.jmnarloch.spring.cloud.stream.binder.hermes.Demo.java

@Test
public void shouldPublishByteHermesMessage() {

    // given/*  w ww  .j  a  va2s. c  o m*/
    final Message<byte[]> message = MessageBuilder.withPayload("Hello Hermes!".getBytes(UTF_8)).build();

    // when
    events.purchases().send(message);

    // then
    given().ignoreExceptions().await().atMost(5, SECONDS)
            .until(() -> wireMock.verify(1, postRequestedFor(urlEqualTo(PURCHASES_TOPIC_PATH))));
}

From source file:org.springframework.samples.portfolio.web.standalone.StandalonePortfolioControllerTests.java

@Test
public void getPositions() throws Exception {

    StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SUBSCRIBE);
    headers.setSubscriptionId("0");
    headers.setDestination("/app/positions");
    headers.setSessionId("0");
    headers.setUser(new TestPrincipal("fabrice"));
    headers.setSessionAttributes(new HashMap<String, Object>());
    Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();

    this.annotationMethodMessageHandler.handleMessage(message);

    assertEquals(1, this.clientOutboundChannel.getMessages().size());
    Message<?> reply = this.clientOutboundChannel.getMessages().get(0);

    StompHeaderAccessor replyHeaders = StompHeaderAccessor.wrap(reply);
    assertEquals("0", replyHeaders.getSessionId());
    assertEquals("0", replyHeaders.getSubscriptionId());
    assertEquals("/app/positions", replyHeaders.getDestination());

    String json = new String((byte[]) reply.getPayload(), Charset.forName("UTF-8"));
    new JsonPathExpectationsHelper("$[0].company").assertValue(json, "Citrix Systems, Inc.");
    new JsonPathExpectationsHelper("$[1].company").assertValue(json, "Dell Inc.");
    new JsonPathExpectationsHelper("$[2].company").assertValue(json, "Microsoft");
    new JsonPathExpectationsHelper("$[3].company").assertValue(json, "Oracle");
}

From source file:org.springframework.cloud.aws.messaging.support.converter.NotificationRequestConverterTest.java

@Test
public void testNoMessageAvailableSupplied() throws Exception {
    this.expectedException.expect(MessageConversionException.class);
    this.expectedException.expectMessage("does not contain a message");
    ObjectNode jsonObject = JsonNodeFactory.instance.objectNode();
    jsonObject.put("Type", "Notification");
    jsonObject.put("Subject", "Hello World!");
    String payload = jsonObject.toString();
    new NotificationRequestConverter().fromMessage(MessageBuilder.withPayload(payload).build(), null);
}

From source file:io.jmnarloch.spring.cloud.stream.binder.hermes.Demo.java

@Test
public void shouldPublishAvroMessage() {

    // given/*  w w  w .  java2 s .c  om*/
    final Message<byte[]> message = MessageBuilder.withPayload(new byte[0])
            .setHeader(MessageHeaders.CONTENT_TYPE, "avro/binary").setHeader("Schema-Version", 1).build();

    // when
    events.returns().send(message);

    // then
    given().ignoreExceptions().await().atMost(5, SECONDS)
            .until(() -> wireMock.verify(1,
                    postRequestedFor(urlEqualTo(RETURNS_TOPIC_PATH))
                            .withHeader("Content-Type", equalTo("avro/binary"))
                            .withHeader("Schema-Version", equalTo("1"))));
}

From source file:multibinder.TwoKafkaBindersApplicationTest.java

@Test
public void messagingWorks() {
    DirectChannel dataProducer = new DirectChannel();
    ((KafkaMessageChannelBinder) binderFactory.getBinder("kafka1")).bindProducer("dataIn", dataProducer,
            new ExtendedProducerProperties<>(new KafkaProducerProperties()));

    QueueChannel dataConsumer = new QueueChannel();
    ((KafkaMessageChannelBinder) binderFactory.getBinder("kafka2")).bindConsumer("dataOut",
            UUID.randomUUID().toString(), dataConsumer,
            new ExtendedConsumerProperties<>(new KafkaConsumerProperties()));

    String testPayload = "testFoo" + UUID.randomUUID().toString();
    dataProducer.send(MessageBuilder.withPayload(testPayload).build());

    Message<?> receive = dataConsumer.receive(5000);
    Assert.assertThat(receive, Matchers.notNullValue());
    Assert.assertThat(receive.getPayload(), CoreMatchers.equalTo(testPayload));
}

From source file:org.springframework.cloud.aws.messaging.support.converter.NotificationRequestConverterTest.java

@Test
public void testNoValidJson() throws Exception {
    this.expectedException.expect(MessageConversionException.class);
    this.expectedException.expectMessage("Could not read JSON");
    String message = "foo";
    new NotificationRequestConverter().fromMessage(MessageBuilder.withPayload(message).build(), null);
}