com.amazonaws.services.sqs.model.SendMessageResult Java Examples

The following examples show how to use com.amazonaws.services.sqs.model.SendMessageResult. 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: QueueMessageChannelTest.java    From spring-cloud-aws with Apache License 2.0 7 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
void sendMessage_withExecutionExceptionWhileSendingAsyncMessage_throwMessageDeliveryException()
		throws Exception {
	// Arrange
	Future<SendMessageResult> future = mock(Future.class);
	when(future.get(1000, TimeUnit.MILLISECONDS))
			.thenThrow(new ExecutionException(new Exception()));
	AmazonSQSAsync amazonSqs = mock(AmazonSQSAsync.class);
	when(amazonSqs.sendMessageAsync(any(SendMessageRequest.class)))
			.thenReturn(future);
	QueueMessageChannel queueMessageChannel = new QueueMessageChannel(amazonSqs,
			"http://testQueue");

	// Assert
	assertThatThrownBy(() -> queueMessageChannel
			.send(MessageBuilder.withPayload("Hello").build(), 1000))
					.isInstanceOf(MessageDeliveryException.class);
}
 
Example #2
Source File: SqsExecutor.java    From spring-integration-aws with MIT License 6 votes vote down vote up
/**
 * Executes the outbound Sqs Operation.
 * 
 */
public Object executeOutboundOperation(final Message<?> message) {

	try {
		String serializedMessage = messageMarshaller.serialize(message);
		if (queue == null) {
			SendMessageRequest request = new SendMessageRequest(queueUrl,
					serializedMessage);
			SendMessageResult result = sqsClient.sendMessage(request);
			log.debug("Message sent, Id:" + result.getMessageId());
		} else {
			queue.add(serializedMessage);
		}
	} catch (MessageMarshallerException e) {
		log.error(e.getMessage(), e);
		throw new MessagingException(e.getMessage(), e.getCause());
	}

	return message.getPayload();
}
 
Example #3
Source File: SqsDaoImpl.java    From herd with Apache License 2.0 6 votes vote down vote up
@Override
public SendMessageResult sendMessage(AwsParamsDto awsParamsDto, String queueName, String messageText, List<MessageHeader> messageHeaders)
{
    Map<String, MessageAttributeValue> messageAttributes = null;

    if (CollectionUtils.isNotEmpty(messageHeaders))
    {
        messageAttributes = new HashMap<>();

        for (MessageHeader messageHeader : messageHeaders)
        {
            messageAttributes.put(messageHeader.getKey(), new MessageAttributeValue().withDataType("String").withStringValue(messageHeader.getValue()));
        }
    }

    return sqsOperations.sendMessage(queueName, messageText, messageAttributes, awsClientFactory.getAmazonSQSClient(awsParamsDto));
}
 
Example #4
Source File: MockSqsOperationsImpl.java    From herd with Apache License 2.0 6 votes vote down vote up
@Override
public SendMessageResult sendMessage(String queueName, String messageText, Map<String, MessageAttributeValue> messageAttributes, AmazonSQS amazonSQS)
{
    // Throw a throttling exception for a specific queue name for testing purposes.
    if (queueName.equals(MockAwsOperationsHelper.AMAZON_THROTTLING_EXCEPTION))
    {
        AmazonServiceException throttlingException = new AmazonServiceException("test throttling exception");
        throttlingException.setErrorCode("ThrottlingException");
        throw throttlingException;
    }

    // Throw an illegal state exception for a specific queue name for testing purposes.
    if (queueName.equals(MOCK_SQS_QUEUE_NOT_FOUND_NAME))
    {
        throw new IllegalStateException(String.format("AWS SQS queue with \"%s\" name not found.", queueName));
    }

    // Nothing else to do in the normal case since our unit tests aren't reading messages once they have been published.
    return new SendMessageResult().withMessageId(AbstractDaoTest.MESSAGE_ID);
}
 
Example #5
Source File: SqsDaoTest.java    From herd with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendMessage()
{
    // Send an SQS message without specifying HTTP proxy settings.
    assertEquals(new SendMessageResult().withMessageId(MESSAGE_ID), sqsDao.sendMessage(
        new AwsParamsDto(NO_AWS_ACCESS_KEY, NO_AWS_SECRET_KEY, NO_SESSION_TOKEN, NO_HTTP_PROXY_HOST, NO_HTTP_PROXY_PORT, AWS_REGION_NAME_US_EAST_1),
        AWS_SQS_QUEUE_NAME, MESSAGE_TEXT, NO_MESSAGE_HEADERS));

    // Send an SQS message using proxy settings.
    assertEquals(new SendMessageResult().withMessageId(MESSAGE_ID), sqsDao
        .sendMessage(new AwsParamsDto(NO_AWS_ACCESS_KEY, NO_AWS_SECRET_KEY, NO_SESSION_TOKEN, HTTP_PROXY_HOST, HTTP_PROXY_PORT, AWS_REGION_NAME_US_EAST_1),
            AWS_SQS_QUEUE_NAME, MESSAGE_TEXT, NO_MESSAGE_HEADERS));

    // Publish an SQS message with message headers.
    assertEquals(new SendMessageResult().withMessageId(MESSAGE_ID), sqsDao.sendMessage(
        new AwsParamsDto(NO_AWS_ACCESS_KEY, NO_AWS_SECRET_KEY, NO_SESSION_TOKEN, NO_HTTP_PROXY_HOST, NO_HTTP_PROXY_PORT, AWS_REGION_NAME_US_EAST_1),
        AWS_SNS_TOPIC_ARN, MESSAGE_TEXT, Collections.singletonList(new MessageHeader(KEY, VALUE))));
}
 
Example #6
Source File: QueueMessageChannel.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
private void sendMessageAndWaitForResult(SendMessageRequest sendMessageRequest,
		long timeout) throws ExecutionException, TimeoutException {
	if (timeout > 0) {
		Future<SendMessageResult> sendMessageFuture = this.amazonSqs
				.sendMessageAsync(sendMessageRequest);

		try {
			sendMessageFuture.get(timeout, TimeUnit.MILLISECONDS);
		}
		catch (InterruptedException e) {
			Thread.currentThread().interrupt();
		}
	}
	else {
		this.amazonSqs.sendMessage(sendMessageRequest);
	}
}
 
Example #7
Source File: QueueMessageChannelTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void sendMessage_validTextMessage_returnsTrue() throws Exception {
	// Arrange
	AmazonSQSAsync amazonSqs = mock(AmazonSQSAsync.class);
	ArgumentCaptor<SendMessageRequest> sendMessageRequestArgumentCaptor = ArgumentCaptor
			.forClass(SendMessageRequest.class);
	when(amazonSqs.sendMessage(sendMessageRequestArgumentCaptor.capture()))
			.thenReturn(new SendMessageResult());

	Message<String> stringMessage = MessageBuilder.withPayload("message content")
			.build();
	MessageChannel messageChannel = new QueueMessageChannel(amazonSqs,
			"http://testQueue");

	// Act
	boolean sent = messageChannel.send(stringMessage);

	// Assert
	verify(amazonSqs, only()).sendMessage(any(SendMessageRequest.class));
	assertThat(sendMessageRequestArgumentCaptor.getValue().getMessageBody())
			.isEqualTo("message content");
	assertThat(sent).isTrue();
}
 
Example #8
Source File: QueueMessageChannelTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void sendMessage_withMimeTypeAsStringHeader_shouldPassItAsMessageAttribute()
		throws Exception {
	// Arrange
	AmazonSQSAsync amazonSqs = mock(AmazonSQSAsync.class);
	QueueMessageChannel messageChannel = new QueueMessageChannel(amazonSqs,
			"http://testQueue");
	String mimeTypeAsString = new MimeType("test", "plain", Charset.forName("UTF-8"))
			.toString();
	Message<String> message = MessageBuilder.withPayload("Hello")
			.setHeader(MessageHeaders.CONTENT_TYPE, mimeTypeAsString).build();

	ArgumentCaptor<SendMessageRequest> sendMessageRequestArgumentCaptor = ArgumentCaptor
			.forClass(SendMessageRequest.class);
	when(amazonSqs.sendMessage(sendMessageRequestArgumentCaptor.capture()))
			.thenReturn(new SendMessageResult());

	// Act
	boolean sent = messageChannel.send(message);

	// Assert
	assertThat(sent).isTrue();
	assertThat(sendMessageRequestArgumentCaptor.getValue().getMessageAttributes()
			.get(MessageHeaders.CONTENT_TYPE).getStringValue())
					.isEqualTo(mimeTypeAsString);
}
 
Example #9
Source File: QueueMessageChannelTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void sendMessage_withMimeTypeHeader_shouldPassItAsMessageAttribute()
		throws Exception {
	// Arrange
	AmazonSQSAsync amazonSqs = mock(AmazonSQSAsync.class);
	QueueMessageChannel messageChannel = new QueueMessageChannel(amazonSqs,
			"http://testQueue");
	MimeType mimeType = new MimeType("test", "plain", Charset.forName("UTF-8"));
	Message<String> message = MessageBuilder.withPayload("Hello")
			.setHeader(MessageHeaders.CONTENT_TYPE, mimeType).build();

	ArgumentCaptor<SendMessageRequest> sendMessageRequestArgumentCaptor = ArgumentCaptor
			.forClass(SendMessageRequest.class);
	when(amazonSqs.sendMessage(sendMessageRequestArgumentCaptor.capture()))
			.thenReturn(new SendMessageResult());

	// Act
	boolean sent = messageChannel.send(message);

	// Assert
	assertThat(sent).isTrue();
	assertThat(sendMessageRequestArgumentCaptor.getValue().getMessageAttributes()
			.get(MessageHeaders.CONTENT_TYPE).getStringValue())
					.isEqualTo(mimeType.toString());
}
 
Example #10
Source File: QueueMessageChannelTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void sendMessage_withUuidAsId_shouldConvertUuidToString() throws Exception {
	// Arrange
	AmazonSQSAsync amazonSqs = mock(AmazonSQSAsync.class);
	QueueMessageChannel messageChannel = new QueueMessageChannel(amazonSqs,
			"http://testQueue");
	Message<String> message = MessageBuilder.withPayload("Hello").build();
	UUID uuid = (UUID) message.getHeaders().get(MessageHeaders.ID);

	ArgumentCaptor<SendMessageRequest> sendMessageRequestArgumentCaptor = ArgumentCaptor
			.forClass(SendMessageRequest.class);
	when(amazonSqs.sendMessage(sendMessageRequestArgumentCaptor.capture()))
			.thenReturn(new SendMessageResult());

	// Act
	boolean sent = messageChannel.send(message);

	// Assert
	assertThat(sent).isTrue();
	assertThat(sendMessageRequestArgumentCaptor.getValue().getMessageAttributes()
			.get(MessageHeaders.ID).getStringValue()).isEqualTo(uuid.toString());
}
 
Example #11
Source File: QueueMessageChannelTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
void sendMessage_withTimeout_sendsMessageAsyncAndReturnsTrueOnceFutureCompleted()
		throws Exception {
	// Arrange
	Future<SendMessageResult> future = mock(Future.class);
	when(future.get(1000, TimeUnit.MILLISECONDS)).thenReturn(new SendMessageResult());
	AmazonSQSAsync amazonSqs = mock(AmazonSQSAsync.class);
	when(amazonSqs.sendMessageAsync(any(SendMessageRequest.class)))
			.thenReturn(future);
	QueueMessageChannel queueMessageChannel = new QueueMessageChannel(amazonSqs,
			"http://testQueue");

	// Act
	boolean result = queueMessageChannel
			.send(MessageBuilder.withPayload("Hello").build(), 1000);

	// Assert
	assertThat(result).isTrue();
	verify(amazonSqs, only()).sendMessageAsync(any(SendMessageRequest.class));
}
 
Example #12
Source File: QueueMessageChannelTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
void sendMessage_withSendMessageAsyncTakingMoreTimeThanSpecifiedTimeout_returnsFalse()
		throws Exception {
	// Arrange
	Future<SendMessageResult> future = mock(Future.class);
	when(future.get(1000, TimeUnit.MILLISECONDS)).thenThrow(new TimeoutException());
	AmazonSQSAsync amazonSqs = mock(AmazonSQSAsync.class);
	when(amazonSqs.sendMessageAsync(any(SendMessageRequest.class)))
			.thenReturn(future);
	QueueMessageChannel queueMessageChannel = new QueueMessageChannel(amazonSqs,
			"http://testQueue");

	// Act
	boolean result = queueMessageChannel
			.send(MessageBuilder.withPayload("Hello").build(), 1000);

	// Assert
	assertThat(result).isFalse();
}
 
Example #13
Source File: QueueMessageChannelTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void sendMessage_withoutDelayHeader_shouldNotSetDelayOnSendMessageRequestAndNotSetHeaderAsMessageAttribute()
		throws Exception {
	// Arrange
	AmazonSQSAsync amazonSqs = mock(AmazonSQSAsync.class);

	ArgumentCaptor<SendMessageRequest> sendMessageRequestArgumentCaptor = ArgumentCaptor
			.forClass(SendMessageRequest.class);
	when(amazonSqs.sendMessage(sendMessageRequestArgumentCaptor.capture()))
			.thenReturn(new SendMessageResult());

	QueueMessageChannel queueMessageChannel = new QueueMessageChannel(amazonSqs,
			"http://testQueue");
	Message<String> message = MessageBuilder.withPayload("Hello").build();

	// Act
	queueMessageChannel.send(message);

	// Assert
	SendMessageRequest sendMessageRequest = sendMessageRequestArgumentCaptor
			.getValue();
	assertThat(sendMessageRequest.getDelaySeconds()).isNull();
	assertThat(sendMessageRequest.getMessageAttributes()
			.containsKey(SqsMessageHeaders.SQS_DELAY_HEADER)).isFalse();
}
 
Example #14
Source File: QueueMessageChannelTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void sendMessage_withGroupIdHeader_shouldSetGroupIdOnSendMessageRequestAndNotSetItAsHeaderAsMessageAttribute()
		throws Exception {
	// Arrange
	AmazonSQSAsync amazonSqs = mock(AmazonSQSAsync.class);

	ArgumentCaptor<SendMessageRequest> sendMessageRequestArgumentCaptor = ArgumentCaptor
			.forClass(SendMessageRequest.class);
	when(amazonSqs.sendMessage(sendMessageRequestArgumentCaptor.capture()))
			.thenReturn(new SendMessageResult());

	QueueMessageChannel queueMessageChannel = new QueueMessageChannel(amazonSqs,
			"http://testQueue");
	Message<String> message = MessageBuilder.withPayload("Hello")
			.setHeader(SqsMessageHeaders.SQS_GROUP_ID_HEADER, "id-5").build();

	// Act
	queueMessageChannel.send(message);

	// Assert
	SendMessageRequest sendMessageRequest = sendMessageRequestArgumentCaptor
			.getValue();
	assertThat(sendMessageRequest.getMessageGroupId()).isEqualTo("id-5");
	assertThat(sendMessageRequest.getMessageAttributes()
			.containsKey(SqsMessageHeaders.SQS_GROUP_ID_HEADER)).isFalse();
}
 
Example #15
Source File: SQSResourcesIntegrationTest.java    From aws-sdk-java-resources with Apache License 2.0 6 votes vote down vote up
/**
 * Tests a simple send, receive and delete of a message from the queue
 * resource. Asserts the message contents and its associated attributes.
 */
@Test
@Ignore
public void testSendReceiveDelete() throws InterruptedException {
    SendMessageResult sendMessageResult = queue.sendMessage(TEST_MESSAGE);
    assertNotNull(sendMessageResult);
    assertNotNull(sendMessageResult.getMessageId());

    List<Message> messages = waitForMessagesFromQueue(null);

    assertNotNull(messages);
    assertEquals(1, messages.size());
    Message message = messages.get(0);
    assertMessage(TEST_MESSAGE, sendMessageResult.getMessageId(),
            sendMessageResult.getMD5OfMessageBody(), message);

    queue.deleteMessages(new DeleteMessageBatchRequest()
            .withEntries(new DeleteMessageBatchRequestEntry("msg1", message
                    .getReceiptHandle())));
}
 
Example #16
Source File: SQSMessageProducerTest.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetDeliveryDelay() throws JMSException {
    assertEquals(0, producer.getDeliveryDelay());
    
    producer.setDeliveryDelay(2000);
    
    assertEquals(2000, producer.getDeliveryDelay());
    
    ArgumentCaptor<SendMessageRequest> requestCaptor = ArgumentCaptor.forClass(SendMessageRequest.class);
    when(amazonSQSClient.sendMessage(requestCaptor.capture()))
        .thenReturn(new SendMessageResult().withMessageId(MESSAGE_ID_1));

    SQSTextMessage msg = new SQSTextMessage("Sorry I'm late!");
    producer.send(msg);
    
    assertEquals(2, requestCaptor.getValue().getDelaySeconds().intValue());
}
 
Example #17
Source File: SQSMessageProducerFifoTest.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 6 votes vote down vote up
/**
 * Test sendInternal input with SQSTextMessage
 */
@Test
public void testSendInternalSQSTextMessage() throws JMSException {

    String messageBody = "MyText1";
    SQSTextMessage msg = spy(new SQSTextMessage(messageBody));
    msg.setStringProperty(SQSMessagingClientConstants.JMSX_GROUP_ID, GROUP_ID);
    msg.setStringProperty(SQSMessagingClientConstants.JMS_SQS_DEDUPLICATION_ID, DEDUP_ID);

    when(amazonSQSClient.sendMessage(any(SendMessageRequest.class)))
            .thenReturn(new SendMessageResult().withMessageId(MESSAGE_ID).withSequenceNumber(SEQ_NUMBER));

    producer.sendInternal(destination, msg);

    verify(amazonSQSClient).sendMessage(argThat(new sendMessageRequestMatcher(QUEUE_URL, messageBody, SQSMessage.TEXT_MESSAGE_TYPE, GROUP_ID, DEDUP_ID)));
    verify(msg).setJMSDestination(destination);
    verify(msg).setJMSMessageID("ID:" + MESSAGE_ID);
    verify(msg).setSQSMessageId(MESSAGE_ID);
    verify(msg).setSequenceNumber(SEQ_NUMBER);
}
 
Example #18
Source File: SQSMessageProducerFifoTest.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 6 votes vote down vote up
/**
 * Test sendInternal input with SQSObjectMessage
 */
@Test
public void testSendInternalSQSObjectMessage() throws JMSException {

    HashSet<String> set = new HashSet<String>();
    set.add("data1");

    SQSObjectMessage msg = spy(new SQSObjectMessage(set));
    msg.setStringProperty(SQSMessagingClientConstants.JMSX_GROUP_ID, GROUP_ID);
    msg.setStringProperty(SQSMessagingClientConstants.JMS_SQS_DEDUPLICATION_ID, DEDUP_ID);
    String msgBody = msg.getMessageBody();

    when(amazonSQSClient.sendMessage(any(SendMessageRequest.class)))
            .thenReturn(new SendMessageResult().withMessageId(MESSAGE_ID).withSequenceNumber(SEQ_NUMBER));

    producer.sendInternal(destination, msg);

    verify(amazonSQSClient).sendMessage(argThat(new sendMessageRequestMatcher(QUEUE_URL, msgBody, SQSMessage.OBJECT_MESSAGE_TYPE, GROUP_ID, DEDUP_ID)));
    verify(msg).setJMSDestination(destination);
    verify(msg).setJMSMessageID("ID:" + MESSAGE_ID);
    verify(msg).setSQSMessageId(MESSAGE_ID);
    verify(msg).setSequenceNumber(SEQ_NUMBER);
}
 
Example #19
Source File: SqsClient.java    From kafka-connect-sqs with Apache License 2.0 6 votes vote down vote up
/**
 * Send a message to an SQS queue.
 *
 * @param url       SQS queue url.
 * @param body      The message to send.
 * @param groupId   Optional group identifier (fifo queues only).
 * @param messageId Optional message identifier (fifo queues only).
 * @return
 */
public String send(final String url, final String body, final String groupId, final String messageId) {
  log.debug(".send: queue={}, gid={}, mid={}", url, groupId, messageId);

  Guard.verifyValidUrl(url);
  // Guard.verifyNotNullOrEmpty( body, "message body" ) ;
  if (!isValidState()) {
    throw new IllegalStateException("AmazonSQS client is not initialized");
  }
  final boolean fifo = isFifo(url);

  final SendMessageRequest request = new SendMessageRequest(url, body);
  if (fifo) {
    Guard.verifyNotNullOrEmpty(groupId, "groupId");
    Guard.verifyNotNullOrEmpty(messageId, "messageId");
    request.setMessageGroupId(groupId);
    request.setMessageDeduplicationId(messageId);
  }

  final SendMessageResult result = client.sendMessage(request);

  log.debug(".send-message.OK: queue={}, result={}", url, result);

  return fifo ? result.getSequenceNumber() : result.getMessageId();
}
 
Example #20
Source File: SQSMessageProducerFifoTest.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 6 votes vote down vote up
/**
 * Test sendInternal input with SQSByteMessage
 */
@Test
public void testSendInternalSQSByteMessage() throws JMSException {

    SQSBytesMessage msg = spy(new SQSBytesMessage());
    msg.setStringProperty(SQSMessagingClientConstants.JMSX_GROUP_ID, GROUP_ID);
    msg.setStringProperty(SQSMessagingClientConstants.JMS_SQS_DEDUPLICATION_ID, DEDUP_ID);
    msg.writeByte((byte)0);
    msg.reset();

    when(amazonSQSClient.sendMessage(any(SendMessageRequest.class)))
            .thenReturn(new SendMessageResult().withMessageId(MESSAGE_ID).withSequenceNumber(SEQ_NUMBER));

    producer.sendInternal(destination, msg);

    String messageBody = "AA==";
    verify(amazonSQSClient).sendMessage(argThat(new sendMessageRequestMatcher(QUEUE_URL, messageBody, SQSMessage.BYTE_MESSAGE_TYPE, GROUP_ID, DEDUP_ID)));

    verify(msg).setJMSDestination(destination);
    verify(msg).setJMSMessageID("ID:" + MESSAGE_ID);
    verify(msg).setSQSMessageId(MESSAGE_ID);
    verify(msg).setSequenceNumber(SEQ_NUMBER);
}
 
Example #21
Source File: ITDeleteSQS.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimpleDelete() throws IOException {
    // Setup - put one message in queue
    SendMessageResult sendMessageResult = sqsClient.sendMessage(TEST_QUEUE_URL, "Test message");
    assertEquals(200, sendMessageResult.getSdkHttpMetadata().getHttpStatusCode());

    // Setup - receive message to get receipt handle
    ReceiveMessageResult receiveMessageResult = sqsClient.receiveMessage(TEST_QUEUE_URL);
    assertEquals(200, receiveMessageResult.getSdkHttpMetadata().getHttpStatusCode());
    Message deleteMessage = receiveMessageResult.getMessages().get(0);
    String receiptHandle = deleteMessage.getReceiptHandle();

    // Test - delete message with DeleteSQS
    final TestRunner runner = TestRunners.newTestRunner(new DeleteSQS());
    runner.setProperty(DeleteSQS.CREDENTIALS_FILE, CREDENTIALS_FILE);
    runner.setProperty(DeleteSQS.QUEUE_URL, TEST_QUEUE_URL);
    runner.setProperty(DeleteSQS.REGION, TEST_REGION);
    final Map<String, String> ffAttributes = new HashMap<>();
    ffAttributes.put("filename", "1.txt");
    ffAttributes.put("sqs.receipt.handle", receiptHandle);
    runner.enqueue("TestMessageBody", ffAttributes);

    runner.run(1);

    runner.assertAllFlowFilesTransferred(DeleteSQS.REL_SUCCESS, 1);
}
 
Example #22
Source File: QueueMessageChannelTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void sendMessage_withDelayHeader_shouldSetDelayOnSendMessageRequestAndNotSetItAsHeaderAsMessageAttribute()
		throws Exception {
	// Arrange
	AmazonSQSAsync amazonSqs = mock(AmazonSQSAsync.class);

	ArgumentCaptor<SendMessageRequest> sendMessageRequestArgumentCaptor = ArgumentCaptor
			.forClass(SendMessageRequest.class);
	when(amazonSqs.sendMessage(sendMessageRequestArgumentCaptor.capture()))
			.thenReturn(new SendMessageResult());

	QueueMessageChannel queueMessageChannel = new QueueMessageChannel(amazonSqs,
			"http://testQueue");
	Message<String> message = MessageBuilder.withPayload("Hello")
			.setHeader(SqsMessageHeaders.SQS_DELAY_HEADER, 15).build();

	// Act
	queueMessageChannel.send(message);

	// Assert
	SendMessageRequest sendMessageRequest = sendMessageRequestArgumentCaptor
			.getValue();
	assertThat(sendMessageRequest.getDelaySeconds()).isEqualTo(new Integer(15));
	assertThat(sendMessageRequest.getMessageAttributes()
			.containsKey(SqsMessageHeaders.SQS_DELAY_HEADER)).isFalse();
}
 
Example #23
Source File: SQSMessageProducerTest.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Test sendInternal input with SQSByteMessage
 */
@Test
public void testSendInternalSQSByteMessage() throws JMSException {

    SQSBytesMessage msg = spy(new SQSBytesMessage());
    msg.writeByte((byte)0);
    msg.reset();

    Map<String, MessageAttributeValue> messageAttributes = createMessageAttribute("byte");

    String messageId = "MessageId";
    when(amazonSQSClient.sendMessage(any(SendMessageRequest.class)))
            .thenReturn(new SendMessageResult().withMessageId(MESSAGE_ID_1))
            .thenReturn(new SendMessageResult().withMessageId(MESSAGE_ID_2));

    producer.sendInternal(destination, msg);

    /*
     * Re send the message
     */
    msg.clearBody();
    msg.writeInt(42);
    producer.sendInternal(destination, msg);

    List<String> messagesBody = Arrays.asList("AA==", "AAAAKg==");
    verify(amazonSQSClient, times(2)).sendMessage(argThat(new sendMessageRequestMatcher(QUEUE_URL, messagesBody,
                                                                                        messageAttributes)));

    verify(msg, times(2)).setJMSDestination(destination);
    verify(msg).setJMSMessageID("ID:" + MESSAGE_ID_1);
    verify(msg).setJMSMessageID("ID:" + MESSAGE_ID_2);
    verify(msg).setSQSMessageId(MESSAGE_ID_1);
    verify(msg).setSQSMessageId(MESSAGE_ID_2);
}
 
Example #24
Source File: SQSMessageProducerTest.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Test sendInternal input with SQSByteMessage
 */
@Test
public void testSendInternalSQSByteMessageFromReceivedMessage() throws JMSException, IOException {
    
    /*
     * Set up non JMS sqs message
     */
    Map<String,MessageAttributeValue> mapMessageAttributes = new HashMap<String, MessageAttributeValue>();
    MessageAttributeValue messageAttributeValue = new MessageAttributeValue();
    messageAttributeValue.setStringValue(SQSMessage.BYTE_MESSAGE_TYPE);
    messageAttributeValue.setDataType(SQSMessagingClientConstants.STRING);
    mapMessageAttributes.put(SQSMessage.JMS_SQS_MESSAGE_TYPE, messageAttributeValue);

    Map<String, String> mapAttributes = new HashMap<String, String>();
    mapAttributes.put(SQSMessagingClientConstants.APPROXIMATE_RECEIVE_COUNT, "1");

    byte[] byteArray = new byte[] { 1, 0, 'a', 65 };
    String messageBody = Base64.encodeAsString(byteArray);
    com.amazonaws.services.sqs.model.Message message =
            new com.amazonaws.services.sqs.model.Message()
                    .withMessageAttributes(mapMessageAttributes)
                    .withAttributes(mapAttributes)
                    .withBody(messageBody);

    SQSObjectMessage msg = spy(new SQSObjectMessage(acknowledger, QUEUE_URL, message));

    Map<String, MessageAttributeValue> messageAttributes = createMessageAttribute("object");

    when(amazonSQSClient.sendMessage(any(SendMessageRequest.class)))
            .thenReturn(new SendMessageResult().withMessageId(MESSAGE_ID_1))
            .thenReturn(new SendMessageResult().withMessageId(MESSAGE_ID_2));

    producer.sendInternal(destination, msg);

    verify(amazonSQSClient).sendMessage(argThat(new sendMessageRequestMatcher(QUEUE_URL, Arrays.asList(messageBody),
            messageAttributes)));
    verify(msg).setJMSDestination(destination);
    verify(msg).setJMSMessageID("ID:" + MESSAGE_ID_1);
    verify(msg).setSQSMessageId(MESSAGE_ID_1);
}
 
Example #25
Source File: SQSMessageProducerTest.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Test sendInternal input with SQSObjectMessage
 */
@Test
public void testSendInternalSQSObjectMessage() throws JMSException {

    HashSet<String> set1 = new HashSet<String>();
    set1.add("data1");
    HashSet<String> set2 = new HashSet<String>();
    set2.add("data2");

    SQSObjectMessage msg = spy(new SQSObjectMessage(set1));
    String megBody1 = msg.getMessageBody();

    Map<String, MessageAttributeValue> messageAttributes = createMessageAttribute("object");

    when(amazonSQSClient.sendMessage(any(SendMessageRequest.class)))
            .thenReturn(new SendMessageResult().withMessageId(MESSAGE_ID_1))
            .thenReturn(new SendMessageResult().withMessageId(MESSAGE_ID_2));

    producer.sendInternal(destination, msg);

    /*
     * Re send the message
     */
    msg.clearBody();
    msg.setObject(set2);
    String megBody2 = msg.getMessageBody();
    producer.sendInternal(destination, msg);
    
    ArgumentCaptor<SendMessageRequest> argumentCaptor = ArgumentCaptor.forClass(SendMessageRequest.class);
    verify(amazonSQSClient, times(2)).sendMessage(argumentCaptor.capture());
    
    assertEquals(megBody1, argumentCaptor.getAllValues().get(0).getMessageBody());
    assertEquals(megBody2, argumentCaptor.getAllValues().get(1).getMessageBody());
    verify(msg, times(2)).setJMSDestination(destination);
    verify(msg).setJMSMessageID("ID:" + MESSAGE_ID_1);
    verify(msg).setJMSMessageID("ID:" + MESSAGE_ID_2);
    verify(msg).setSQSMessageId(MESSAGE_ID_1);
    verify(msg).setSQSMessageId(MESSAGE_ID_2);
}
 
Example #26
Source File: SQSMessageProducerFifoTest.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Test sendInternal input with SQSTextMessage
 */
@Test
public void testSendInternalSQSTextMessageFromReceivedMessage() throws JMSException {

    /*
     * Set up non JMS sqs message
     */
    Map<String,MessageAttributeValue> mapMessageAttributes = new HashMap<String, MessageAttributeValue>();
    MessageAttributeValue messageAttributeValue = new MessageAttributeValue();
    messageAttributeValue.setStringValue(SQSMessage.TEXT_MESSAGE_TYPE);
    messageAttributeValue.setDataType(SQSMessagingClientConstants.STRING);
    mapMessageAttributes.put(SQSMessage.JMS_SQS_MESSAGE_TYPE, messageAttributeValue);

    Map<String, String> mapAttributes = new HashMap<String, String>();
    mapAttributes.put(SQSMessagingClientConstants.APPROXIMATE_RECEIVE_COUNT, "1");
    mapAttributes.put(SQSMessagingClientConstants.MESSAGE_GROUP_ID, GROUP_ID);
    mapAttributes.put(SQSMessagingClientConstants.MESSAGE_DEDUPLICATION_ID, DEDUP_ID);
    mapAttributes.put(SQSMessagingClientConstants.SEQUENCE_NUMBER, SEQ_NUMBER);

    com.amazonaws.services.sqs.model.Message message =
            new com.amazonaws.services.sqs.model.Message()
                        .withMessageAttributes(mapMessageAttributes)
                        .withAttributes(mapAttributes)
                        .withBody("MessageBody");

    SQSTextMessage msg = spy(new SQSTextMessage(acknowledger, QUEUE_URL, message));

    when(amazonSQSClient.sendMessage(any(SendMessageRequest.class)))
            .thenReturn(new SendMessageResult().withMessageId(MESSAGE_ID).withSequenceNumber(SEQ_NUMBER_2));

    producer.sendInternal(destination, msg);

    verify(amazonSQSClient).sendMessage(argThat(new sendMessageRequestMatcher(QUEUE_URL, "MessageBody", SQSMessage.TEXT_MESSAGE_TYPE, GROUP_ID, DEDUP_ID)));
    verify(msg).setJMSDestination(destination);
    verify(msg).setJMSMessageID("ID:" + MESSAGE_ID);
    verify(msg).setSQSMessageId(MESSAGE_ID);
    verify(msg).setSequenceNumber(SEQ_NUMBER_2);
}
 
Example #27
Source File: QueueImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public SendMessageResult sendMessage(String messageBody,
        ResultCapture<SendMessageResult> extractor) {

    SendMessageRequest request = new SendMessageRequest()
        .withMessageBody(messageBody);
    return sendMessage(request, extractor);
}
 
Example #28
Source File: QueueImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public SendMessageResult sendMessage(SendMessageRequest request,
        ResultCapture<SendMessageResult> extractor) {

    ActionResult result = resource.performAction("SendMessage", request,
            extractor);

    if (result == null) return null;
    return (SendMessageResult) result.getData();
}
 
Example #29
Source File: QueueMessageChannelTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void sendMessage_withDeduplicationIdHeader_shouldSetDeduplicationIdOnSendMessageRequestAndNotSetItAsHeaderAsMessageAttribute()
		throws Exception {
	// @checkstyle:on
	// Arrange
	AmazonSQSAsync amazonSqs = mock(AmazonSQSAsync.class);

	ArgumentCaptor<SendMessageRequest> sendMessageRequestArgumentCaptor = ArgumentCaptor
			.forClass(SendMessageRequest.class);
	when(amazonSqs.sendMessage(sendMessageRequestArgumentCaptor.capture()))
			.thenReturn(new SendMessageResult());

	QueueMessageChannel queueMessageChannel = new QueueMessageChannel(amazonSqs,
			"http://testQueue");
	Message<String> message = MessageBuilder.withPayload("Hello")
			.setHeader(SqsMessageHeaders.SQS_DEDUPLICATION_ID_HEADER, "id-5").build();

	// Act
	queueMessageChannel.send(message);

	// Assert
	SendMessageRequest sendMessageRequest = sendMessageRequestArgumentCaptor
			.getValue();
	assertThat(sendMessageRequest.getMessageDeduplicationId()).isEqualTo("id-5");
	assertThat(sendMessageRequest.getMessageAttributes()
			.containsKey(SqsMessageHeaders.SQS_DEDUPLICATION_ID_HEADER)).isFalse();
}
 
Example #30
Source File: SQSMessageProducerTest.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Test sendInternal input with SQSTextMessage
 */
@Test
public void testSendInternalSQSTextMessage() throws JMSException {

    String messageBody1 = "MyText1";
    String messageBody2 = "MyText2";
    SQSTextMessage msg = spy(new SQSTextMessage(messageBody1));

    Map<String, MessageAttributeValue> messageAttributes = createMessageAttribute("text");

    when(amazonSQSClient.sendMessage(any(SendMessageRequest.class)))
            .thenReturn(new SendMessageResult().withMessageId(MESSAGE_ID_1))
            .thenReturn(new SendMessageResult().withMessageId(MESSAGE_ID_2));

    producer.sendInternal(destination, msg);

    /*
     * Re send the message
     */
    msg.setText(messageBody2);
    producer.sendInternal(destination, msg);

    List<String> messagesBody = Arrays.asList(messageBody1, messageBody2);
    verify(amazonSQSClient, times(2)).sendMessage(argThat(new sendMessageRequestMatcher(QUEUE_URL, messagesBody, messageAttributes)));
    verify(msg, times(2)).setJMSDestination(destination);
    verify(msg).setJMSMessageID("ID:" + MESSAGE_ID_1);
    verify(msg).setJMSMessageID("ID:" + MESSAGE_ID_2);
    verify(msg).setSQSMessageId(MESSAGE_ID_1);
    verify(msg).setSQSMessageId(MESSAGE_ID_2);
}