Java Code Examples for org.apache.qpid.proton.message.Message#setBody()

The following examples show how to use org.apache.qpid.proton.message.Message#setBody() . 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: AmqpJmsBytesMessageFacadeTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
@Test
public void testClearBodyWithExistingInputStream() throws Exception {
    byte[] bytes = "myBytes".getBytes();

    Message message = Message.Factory.create();
    message.setBody(new Data(new Binary(bytes)));
    AmqpJmsBytesMessageFacade amqpBytesMessageFacade = createReceivedBytesMessageFacade(createMockAmqpConsumer(), message);

    @SuppressWarnings("unused")
    InputStream unused = amqpBytesMessageFacade.getInputStream();

    amqpBytesMessageFacade.clearBody();

    assertEquals("Expected no data from facade, but got some", END_OF_STREAM, amqpBytesMessageFacade.getInputStream().read(new byte[1]));

    assertDataBodyAsExpected(amqpBytesMessageFacade.getBody(), 0);
}
 
Example 2
Source File: AmqpJmsMapMessageFacadeTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateWithPopulatedMap() throws Exception {
    Message message = Message.Factory.create();
    Map<String, Object> bodyMap = new HashMap<String, Object>();
    bodyMap.put("entry1", Boolean.TRUE);
    bodyMap.put("entry2", Boolean.FALSE);

    message.setBody(new AmqpValue(bodyMap));

    AmqpJmsMapMessageFacade amqpMapMessageFacade = createReceivedMapMessageFacade(createMockAmqpConsumer(), message);

    // Data should be preserved
    assertTrue(amqpMapMessageFacade.getMapNames().hasMoreElements());
    Object result = amqpMapMessageFacade.get("entry1");
    assertNotNull(result);
    assertTrue(result instanceof Boolean);
    assertTrue(amqpMapMessageFacade.hasBody());

    // Should be able to use the message, e.g clearing it and adding to it.
    amqpMapMessageFacade.clearBody();
    assertFalse(amqpMapMessageFacade.hasBody());
    amqpMapMessageFacade.put("entry", "value");
}
 
Example 3
Source File: AmqpJmsBytesMessageFacadeTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
@Test
public void testInputStreamUsingReceivedMessageWithAmqpValueSectionContainingBinary() throws Exception {
    byte[] bytes = "myBytes".getBytes();

    Message message = Message.Factory.create();
    message.setBody(new AmqpValue(new Binary(bytes)));

    AmqpJmsBytesMessageFacade amqpBytesMessageFacade = createReceivedBytesMessageFacade(createMockAmqpConsumer(), message);
    InputStream bytesStream = amqpBytesMessageFacade.getInputStream();

    // retrieve the expected bytes, check they match
    byte[] receivedBytes = new byte[bytes.length];
    bytesStream.read(receivedBytes);
    assertTrue(Arrays.equals(bytes, receivedBytes));

    // verify no more bytes remain, i.e EOS
    assertEquals("Expected input stream to be at end but data was returned", END_OF_STREAM, bytesStream.read(new byte[1]));
}
 
Example 4
Source File: CredentialsMessageFilterTest.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Verifies that a message containing a non Data section body
 * does not pass the filter.
 */
@Test
public void testVerifyFailsForNonDataSectionBody() {

    // GIVEN a message with an unsupported subject
    final Message msg = givenAValidMessageWithoutBody(CredentialsConstants.CredentialsAction.get);
    msg.setBody(new AmqpValue(BILLIE_HASHED_PASSWORD.encode()));
    msg.setContentType("application/json");

    // WHEN receiving the message via a link with any tenant
    final boolean filterResult = CredentialsMessageFilter.verify(target, msg);

    // THEN message validation fails
    assertFalse(filterResult);
}
 
Example 5
Source File: AmqpJmsMapMessageFacadeTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateWithAmqpValueBodySectionContainingUnexpectedValueThrowsISE() throws Exception {
    Message message = Message.Factory.create();
    message.setBody(new AmqpValue("not-a-map"));

    try {
        createReceivedMapMessageFacade(createMockAmqpConsumer(), message);
        fail("expected exception to be thrown");
    } catch (IllegalStateException ise) {
        // expected
    }
}
 
Example 6
Source File: AmqpJmsBytesMessageFacadeTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
@Test
public void testHasBodyAfterClear() throws Exception {
    byte[] bodyBytes = "myOrigBytes".getBytes();

    Message message = Message.Factory.create();
    message.setBody(new Data(new Binary(bodyBytes)));
    AmqpJmsBytesMessageFacade amqpBytesMessageFacade = createReceivedBytesMessageFacade(createMockAmqpConsumer(), message);

    assertTrue(amqpBytesMessageFacade.hasBody());

    amqpBytesMessageFacade.clearBody();

    assertFalse(amqpBytesMessageFacade.hasBody());
}
 
Example 7
Source File: AmqpJmsMapMessageFacadeTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateWithEmptyAmqpValueBodySection() throws Exception {
    Message message = Message.Factory.create();
    message.setBody(new AmqpValue(null));

    AmqpJmsMapMessageFacade amqpMapMessageFacade = createReceivedMapMessageFacade(createMockAmqpConsumer(), message);

    // Should be able to use the message, e.g clearing it and adding to it.
    amqpMapMessageFacade.clearBody();
    amqpMapMessageFacade.put("entry", "value");
    assertTrue(amqpMapMessageFacade.getMapNames().hasMoreElements());
}
 
Example 8
Source File: AmqpJmsBytesMessageFacadeTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
/**
 * Test that setting bytes on a received message results in the expected content in the body section
 * of the underlying message and returned by a new InputStream requested from the message.
 *
 * @throws Exception if an error occurs during the test.
 */
@Test
public void testSetGetBodyOnReceivedMessage() throws Exception {
    byte[] orig = "myOrigBytes".getBytes();
    byte[] replacement = "myReplacementBytes".getBytes();

    Message message = Message.Factory.create();
    message.setBody(new Data(new Binary(orig)));
    AmqpJmsBytesMessageFacade amqpBytesMessageFacade = createReceivedBytesMessageFacade(createMockAmqpConsumer(), message);

    OutputStream os = amqpBytesMessageFacade.getOutputStream();
    os.write(replacement);

    amqpBytesMessageFacade.reset();

    // Retrieve the new Binary from the underlying message, check they match
    // (the backing arrays may be different length so not checking arrayEquals)
    Data body = (Data) amqpBytesMessageFacade.getBody();
    assertEquals("Underlying message data section did not contain the expected bytes", new Binary(replacement), body.getValue());

    assertEquals("expected body length to match replacement bytes", replacement.length, amqpBytesMessageFacade.getBodyLength());

    // retrieve the new bytes via an InputStream, check they match expected
    byte[] receivedBytes = new byte[replacement.length];
    InputStream bytesStream = amqpBytesMessageFacade.getInputStream();
    bytesStream.read(receivedBytes);
    assertTrue("Retrieved bytes from input steam did not match expected bytes", Arrays.equals(replacement, receivedBytes));

    // verify no more bytes remain, i.e EOS
    assertEquals("Expected input stream to be at end but data was returned", END_OF_STREAM, bytesStream.read(new byte[1]));
}
 
Example 9
Source File: AmqpJmsTextMessageFacadeTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateWithNonEmptyAmqpValue() throws Exception {
    Message message = Message.Factory.create();
    message.setBody(new AmqpValue("TEST"));

    AmqpJmsTextMessageFacade amqpTextMessageFacade = createReceivedTextMessageFacade(createMockAmqpConsumer(), message);

    assertEquals("TEST", amqpTextMessageFacade.getText());

    // Should be able to use the message, e.g clearing it and adding to it.
    amqpTextMessageFacade.clearBody();
    amqpTextMessageFacade.setText("TEST-CLEARED");
    assertEquals("TEST-CLEARED", amqpTextMessageFacade.getText());
}
 
Example 10
Source File: AmqpJmsBytesMessageFacadeTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetBodyLengthUsingReceivedMessageWithAmqpValueSectionContainingNull() throws Exception {
    Message message = Message.Factory.create();
    message.setBody(new AmqpValue(null));
    AmqpJmsBytesMessageFacade amqpBytesMessageFacade = createReceivedBytesMessageFacade(createMockAmqpConsumer(), message);

    assertEquals("Message reports unexpected length", 0, amqpBytesMessageFacade.getBodyLength());
}
 
Example 11
Source File: MessageHelperTest.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Verifies that the helper does not throw an exception when reading
 * invalid UTF-8 from a message's payload.
 */
@Test
public void testGetPayloadAsStringHandlesNonCharacterPayload() {

    final Message msg = ProtonHelper.message();
    msg.setBody(new Data(new Binary(new byte[] { (byte) 0xc3, (byte) 0x28 })));
    assertThat(MessageHelper.getPayloadAsString(msg)).isNotNull();

    msg.setBody(new Data(new Binary(new byte[] { (byte) 0xf0, (byte) 0x28, (byte) 0x8c, (byte) 0xbc })));
    assertThat(MessageHelper.getPayloadAsString(msg)).isNotNull();
}
 
Example 12
Source File: AmqpJmsTextMessageFacadeTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetTextUsingReceivedMessageWithDataSectionContainingNothingReturnsEmptyString() throws Exception {
    Message message = Message.Factory.create();
    message.setBody(new Data(null));

    // This shouldn't happen with actual received messages, since Data sections can't really
    // have a null value in them, they would have an empty byte array, but just in case...
    AmqpJmsTextMessageFacade amqpTextMessageFacade = createReceivedTextMessageFacade(createMockAmqpConsumer(), message);

    assertEquals("expected zero-length string", "", amqpTextMessageFacade.getText());
}
 
Example 13
Source File: AmqpJmsStreamMessageFacadeTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
@Test
public void testRepeatedPeekAfterPopReturnsExpectedValue() throws Exception {
    Message message = Message.Factory.create();
    List<Object> list = new ArrayList<Object>();
    list.add(Boolean.FALSE);
    list.add(Boolean.TRUE);
    message.setBody(new AmqpSequence(list));

    AmqpJmsStreamMessageFacade amqpStreamMessageFacade = createReceivedStreamMessageFacade(createMockAmqpConsumer(), message);

    assertEquals("Unexpected value retrieved", Boolean.FALSE, amqpStreamMessageFacade.peek());
    amqpStreamMessageFacade.pop();
    assertEquals("Unexpected value retrieved", Boolean.TRUE, amqpStreamMessageFacade.peek());
}
 
Example 14
Source File: AmqpJmsObjectMessageFacadeTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetObjectUsingReceivedMessageWithDataSectionContainingNothingReturnsNull() throws Exception {
    Message message = Message.Factory.create();
    message.setContentType(AmqpMessageSupport.SERIALIZED_JAVA_OBJECT_CONTENT_TYPE.toString());
    message.setBody(new Data(null));

    AmqpJmsObjectMessageFacade amqpObjectMessageFacade = createReceivedObjectMessageFacade(createMockAmqpConsumer(), message);

    assertNull("Expected null object", amqpObjectMessageFacade.getObject());
}
 
Example 15
Source File: CredentialsMessageFilterTest.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Verifies that a valid message passes the filter.
 */
@Test
public void testVerifySucceedsForValidGetAction() {

    // GIVEN a credentials message for user billie
    final Message msg = givenAValidMessageWithoutBody(CredentialsConstants.CredentialsAction.get);
    msg.setBody(new Data(new Binary(BILLIE_HASHED_PASSWORD.toBuffer().getBytes())));
    msg.setContentType("application/json");

    // WHEN receiving the message via a link with any tenant
    final boolean filterResult = CredentialsMessageFilter.verify(target, msg);

    // THEN message validation succeeds
    assertTrue(filterResult);
}
 
Example 16
Source File: CredentialsMessageFilterTest.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Verifies that a message containing a subject that does not represent
 * a Credentials API operation does not pass the filter.
 */
@Test
public void testVerifyFailsForUnknownAction() {

    // GIVEN a message with an unsupported subject
    final Message msg = givenAValidMessageWithoutBody(CredentialsConstants.CredentialsAction.unknown);
    msg.setBody(new AmqpValue(BILLIE_HASHED_PASSWORD));
    msg.setContentType("application/json");

    // WHEN receiving the message via a link with any tenant
    final boolean filterResult = CredentialsMessageFilter.verify(target, msg);

    // THEN message validation fails
    assertFalse(filterResult);
}
 
Example 17
Source File: AmqpJmsStreamMessageFacadeTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateWithUnexpectedBodySectionTypeThrowsISE() throws Exception {
    Message message = Message.Factory.create();
    message.setBody(new Data(new Binary(new byte[0])));

    try {
        createReceivedStreamMessageFacade(createMockAmqpConsumer(), message);
        fail("expected exception to be thrown");
    } catch (IllegalStateException ise) {
        // expected
    }
}
 
Example 18
Source File: AmqpJmsStreamMessageFacadeTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateWithNoBodySection() throws Exception
{
    Message message = Message.Factory.create();
    message.setBody(null);

    AmqpJmsStreamMessageFacade amqpStreamMessageFacade = createReceivedStreamMessageFacade(createMockAmqpConsumer(), message);

    //Should be able to use the message, e.g clearing it and adding to it.
    amqpStreamMessageFacade.clearBody();
    amqpStreamMessageFacade.put("myString");
}
 
Example 19
Source File: AmqpSendReceiveTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
private void doTestBrokerRestartAndDurability(boolean durable, boolean enforceHeader, boolean explicitSetNonDurable) throws Exception {
   AmqpClient client = createAmqpClient();
   AmqpConnection connection = addConnection(client.connect());
   AmqpSession session = connection.createSession();

   AmqpSender sender = session.createSender(getQueueName());

   final Queue queueView1 = getProxyToQueue(getQueueName());

   Message protonMessage = Message.Factory.create();
   protonMessage.setMessageId("ID:Message:1");
   protonMessage.setBody(new AmqpValue("Test-Message -> " + (durable ? "durable" : "non-durable")));
   if (durable || enforceHeader) {
      Header header = new Header();
      if (durable) {
         header.setDurable(true);
      } else {
         if (explicitSetNonDurable) {
            header.setDurable(false);
         } else {
            // Set priority so the durable field gets defaulted
            header.setPriority(UnsignedByte.valueOf((byte) 5));
            assertNull(header.getDurable());
         }
      }

      protonMessage.setHeader(header);
   } else {
      assertNull("Should not have a header", protonMessage.getHeader());
   }

   AmqpMessage message = new AmqpMessage(protonMessage);

   sender.send(message);
   connection.close();

   Wait.assertEquals(1, queueView1::getMessageCount);

   // Restart the server and the Queue should be empty
   // if the message was non-durable
   server.stop();
   server.start();

   // Reconnect now
   connection = addConnection(client.connect());
   session = connection.createSession();
   AmqpReceiver receiver = session.createReceiver(getQueueName());

   final Queue queueView2 = getProxyToQueue(getQueueName());
   if (durable) {
      Wait.assertTrue("Message should not have returned", () -> queueView2.getMessageCount() == 1);
   } else {
      Wait.assertTrue("Message should have been restored", () -> queueView2.getMessageCount() == 0);
   }

   receiver.flow(1);
   message = receiver.receive(1, TimeUnit.SECONDS);

   if (durable) {
      assertNotNull("Should have read a message", message);
   } else {
      assertNull("Should not have read a message", message);
   }

   connection.close();
}
 
Example 20
Source File: LinkTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
private Delivery sendMessageToClient(String deliveryTag, String messageContent)
{
    byte[] tag = deliveryTag.getBytes(StandardCharsets.UTF_8);

    Message m = Proton.message();
    m.setBody(new AmqpValue(messageContent));

    byte[] encoded = new byte[BUFFER_SIZE];
    int len = m.encode(encoded, 0, BUFFER_SIZE);

    assertTrue("given array was too small", len < BUFFER_SIZE);

    Delivery serverDelivery = getServer().sender.delivery(tag);

    int sent = getServer().sender.send(encoded, 0, len);

    assertEquals("sender unable to send all data at once as assumed for simplicity", len, sent);

    boolean senderAdvanced = getServer().sender.advance();
    assertTrue("sender has not advanced", senderAdvanced);

    return serverDelivery;
}