com.amazonaws.services.sqs.AmazonSQSAsync Java Examples

The following examples show how to use com.amazonaws.services.sqs.AmazonSQSAsync. 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: QueueMessagingTemplateTest.java    From spring-cloud-aws with Apache License 2.0 7 votes vote down vote up
@Test
void send_withCustomDestinationResolveAndDestination_usesDestination() {
	AmazonSQSAsync amazonSqs = createAmazonSqs();
	QueueMessagingTemplate queueMessagingTemplate = new QueueMessagingTemplate(
			amazonSqs,
			(DestinationResolver<String>) name -> name.toUpperCase(Locale.ENGLISH),
			null);

	Message<String> stringMessage = MessageBuilder.withPayload("message content")
			.build();
	queueMessagingTemplate.send("myqueue", stringMessage);

	ArgumentCaptor<SendMessageRequest> sendMessageRequestArgumentCaptor = ArgumentCaptor
			.forClass(SendMessageRequest.class);
	verify(amazonSqs).sendMessage(sendMessageRequestArgumentCaptor.capture());
	assertThat(sendMessageRequestArgumentCaptor.getValue().getQueueUrl())
			.isEqualTo("MYQUEUE");
}
 
Example #3
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 #4
Source File: QueueMessagingTemplateTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
private AmazonSQSAsync createAmazonSqs() {
	AmazonSQSAsync amazonSqs = mock(AmazonSQSAsync.class);

	GetQueueUrlResult queueUrl = new GetQueueUrlResult();
	queueUrl.setQueueUrl("https://queue-url.com");
	when(amazonSqs.getQueueUrl(any(GetQueueUrlRequest.class))).thenReturn(queueUrl);

	ReceiveMessageResult receiveMessageResult = new ReceiveMessageResult();
	com.amazonaws.services.sqs.model.Message message = new com.amazonaws.services.sqs.model.Message();
	message.setBody("My message");
	receiveMessageResult.withMessages(message);
	when(amazonSqs.receiveMessage(any(ReceiveMessageRequest.class)))
			.thenReturn(receiveMessageResult);

	return amazonSqs;
}
 
Example #5
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 #6
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 #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: SqsConsumer.java    From datacollector with Apache License 2.0 6 votes vote down vote up
private AmazonSQSAsync buildAsyncClient() {
  final AmazonSQSAsyncClientBuilder builder = AmazonSQSAsyncClientBuilder.standard();
  if(conf.region == AwsRegion.OTHER) {
    builder.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(conf.endpoint, null));
  } else {
    builder.withRegion(conf.region.getId());
  }
  builder.setCredentials(credentials);
  builder.setClientConfiguration(clientConfiguration);

  return builder.build();
}
 
Example #9
Source File: QueueMessageChannelTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void sendMessage_serviceThrowsError_throwsMessagingException() throws Exception {
	// Arrange
	AmazonSQSAsync amazonSqs = mock(AmazonSQSAsync.class);

	Message<String> stringMessage = MessageBuilder.withPayload("message content")
			.build();
	MessageChannel messageChannel = new QueueMessageChannel(amazonSqs,
			"http://testQueue");
	when(amazonSqs
			.sendMessage(new SendMessageRequest("http://testQueue", "message content")
					.withDelaySeconds(0).withMessageAttributes(isNotNull())))
							.thenThrow(new AmazonServiceException("wanted error"));

	// Assert
	assertThatThrownBy(() -> messageChannel.send(stringMessage))
			.isInstanceOf(MessagingException.class)
			.hasMessageContaining("wanted error");
}
 
Example #10
Source File: MessageListenerContainerTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void testIsActive() throws Exception {
	AbstractMessageListenerContainer container = new StubAbstractMessageListenerContainer();

	AmazonSQSAsync mock = mock(AmazonSQSAsync.class, withSettings().stubOnly());
	container.setAmazonSqs(mock);
	container.setMessageHandler(mock(QueueMessageHandler.class));

	container.afterPropertiesSet();

	when(mock.getQueueUrl(new GetQueueUrlRequest().withQueueName("testQueue")))
			.thenReturn(new GetQueueUrlResult()
					.withQueueUrl("http://testQueue.amazonaws.com"));

	container.start();
	assertThat(container.isRunning()).isTrue();

	container.stop();
	assertThat(container.isRunning()).isFalse();

	// Container can still be active an restarted later (e.g. paused for a while)
	assertThat(container.isActive()).isTrue();
}
 
Example #11
Source File: QueueMessageChannelTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void receiveMessage_withoutTimeout_returnsTextMessage() throws Exception {
	// Arrange
	AmazonSQSAsync amazonSqs = mock(AmazonSQSAsync.class);
	when(amazonSqs.receiveMessage(new ReceiveMessageRequest("http://testQueue")
			.withWaitTimeSeconds(0).withMaxNumberOfMessages(1)
			.withAttributeNames(QueueMessageChannel.ATTRIBUTE_NAMES)
			.withMessageAttributeNames("All")))
					.thenReturn(new ReceiveMessageResult().withMessages(Collections
							.singleton(new com.amazonaws.services.sqs.model.Message()
									.withBody("content"))));

	PollableChannel messageChannel = new QueueMessageChannel(amazonSqs,
			"http://testQueue");

	// Act
	Message<?> receivedMessage = messageChannel.receive();

	// Assert
	assertThat(receivedMessage).isNotNull();
	assertThat(receivedMessage.getPayload()).isEqualTo("content");
}
 
Example #12
Source File: SqsConfiguration.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
private QueueMessageHandler getMessageHandler(AmazonSQSAsync amazonSqs) {
	if (this.queueMessageHandlerFactory.getAmazonSqs() == null) {
		this.queueMessageHandlerFactory.setAmazonSqs(amazonSqs);
	}

	if (CollectionUtils
			.isEmpty(this.queueMessageHandlerFactory.getMessageConverters())
			&& this.mappingJackson2MessageConverter != null) {
		this.queueMessageHandlerFactory.setMessageConverters(
				Arrays.asList(this.mappingJackson2MessageConverter));
	}

	this.queueMessageHandlerFactory.setBeanFactory(this.beanFactory);
	this.queueMessageHandlerFactory.setObjectMapper(this.objectMapper);

	return this.queueMessageHandlerFactory.createQueueMessageHandler();
}
 
Example #13
Source File: QueueMessageChannelTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void receiveMessage_withSpecifiedTimeout_returnsNull() throws Exception {
	// Arrange
	AmazonSQSAsync amazonSqs = mock(AmazonSQSAsync.class);
	when(amazonSqs.receiveMessage(new ReceiveMessageRequest("http://testQueue")
			.withWaitTimeSeconds(2).withMaxNumberOfMessages(1)
			.withAttributeNames(QueueMessageChannel.ATTRIBUTE_NAMES)
			.withMessageAttributeNames("All"))).thenReturn(
					new ReceiveMessageResult().withMessages(Collections.emptyList()));

	PollableChannel messageChannel = new QueueMessageChannel(amazonSqs,
			"http://testQueue");

	// Act
	Message<?> receivedMessage = messageChannel.receive(2);

	// Assert
	assertThat(receivedMessage).isNull();
}
 
Example #14
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 #15
Source File: QueueMessageChannelTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void receiveMessage_withoutDefaultTimeout_returnsNull() throws Exception {
	// Arrange
	AmazonSQSAsync amazonSqs = mock(AmazonSQSAsync.class);
	when(amazonSqs.receiveMessage(new ReceiveMessageRequest("http://testQueue")
			.withWaitTimeSeconds(0).withMaxNumberOfMessages(1)
			.withAttributeNames(QueueMessageChannel.ATTRIBUTE_NAMES)
			.withMessageAttributeNames("All"))).thenReturn(
					new ReceiveMessageResult().withMessages(Collections.emptyList()));

	PollableChannel messageChannel = new QueueMessageChannel(amazonSqs,
			"http://testQueue");

	// Act
	Message<?> receivedMessage = messageChannel.receive(0);

	// Assert
	assertThat(receivedMessage).isNull();
}
 
Example #16
Source File: SimpleMessageListenerContainerTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void doDestroy_whenContainerCallsDestroy_DestroysDefaultTaskExecutor()
		throws Exception {
	// Arrange
	SimpleMessageListenerContainer simpleMessageListenerContainer = new SimpleMessageListenerContainer();
	AmazonSQSAsync sqs = mock(AmazonSQSAsync.class, withSettings().stubOnly());
	simpleMessageListenerContainer.setAmazonSqs(sqs);
	QueueMessageHandler messageHandler = mock(QueueMessageHandler.class);
	simpleMessageListenerContainer.setMessageHandler(messageHandler);
	simpleMessageListenerContainer.afterPropertiesSet();
	simpleMessageListenerContainer.start();

	// Act
	simpleMessageListenerContainer.destroy();

	// Assert
	assertThat(((ThreadPoolTaskExecutor) simpleMessageListenerContainer
			.getTaskExecutor()).getThreadPoolExecutor().isTerminated()).isTrue();
}
 
Example #17
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 #18
Source File: QueueMessagingTemplateTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void receive_withDefaultDestination_useDefaultDestination() {
	AmazonSQSAsync amazonSqs = createAmazonSqs();
	QueueMessagingTemplate queueMessagingTemplate = new QueueMessagingTemplate(
			amazonSqs);
	queueMessagingTemplate.setDefaultDestinationName("my-queue");

	queueMessagingTemplate.receive();

	ArgumentCaptor<ReceiveMessageRequest> sendMessageRequestArgumentCaptor = ArgumentCaptor
			.forClass(ReceiveMessageRequest.class);
	verify(amazonSqs).receiveMessage(sendMessageRequestArgumentCaptor.capture());
	assertThat(sendMessageRequestArgumentCaptor.getValue().getQueueUrl())
			.isEqualTo("https://queue-url.com");
}
 
Example #19
Source File: QueueMessagingTemplateTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void receiveAndConvert_withoutDefaultDestination_throwsAnException() {
	AmazonSQSAsync amazonSqs = createAmazonSqs();
	QueueMessagingTemplate queueMessagingTemplate = new QueueMessagingTemplate(
			amazonSqs);

	assertThatThrownBy(() -> queueMessagingTemplate.receiveAndConvert(String.class))
			.isInstanceOf(IllegalStateException.class);
}
 
Example #20
Source File: MessageListenerContainerTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void testStopCallsDoStopMethodWithRunnable() throws Exception {
	CountDownLatch countDownLatch = new CountDownLatch(1);
	AbstractMessageListenerContainer container = new AbstractMessageListenerContainer() {

		@Override
		protected void doStart() {
			// do nothing in this case
		}

		@Override
		protected void doStop() {
			countDownLatch.countDown();
		}
	};

	AmazonSQSAsync mock = mock(AmazonSQSAsync.class, withSettings().stubOnly());
	container.setAmazonSqs(mock);
	container.setMessageHandler(mock(QueueMessageHandler.class));
	container.afterPropertiesSet();

	when(mock.getQueueUrl(new GetQueueUrlRequest().withQueueName("testQueue")))
			.thenReturn(new GetQueueUrlResult()
					.withQueueUrl("http://testQueue.amazonaws.com"));

	container.start();

	container.stop(() -> {
		try {
			assertThat(countDownLatch.await(10, TimeUnit.MILLISECONDS)).isTrue();
		}
		catch (InterruptedException e) {
			fail("Expected doStart() method to be called");
		}
	});
}
 
Example #21
Source File: MessageListenerContainerTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void testCustomDestinationResolverSet() throws Exception {
	AbstractMessageListenerContainer container = new StubAbstractMessageListenerContainer();

	container.setAmazonSqs(mock(AmazonSQSAsync.class, withSettings().stubOnly()));
	container.setMessageHandler(mock(QueueMessageHandler.class));

	DestinationResolver<String> destinationResolver = mock(
			DynamicQueueUrlDestinationResolver.class);
	container.setDestinationResolver(destinationResolver);

	container.afterPropertiesSet();

	assertThat(container.getDestinationResolver()).isEqualTo(destinationResolver);
}
 
Example #22
Source File: MessageListenerContainerTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void testDisposableBeanResetActiveFlag() throws Exception {
	AbstractMessageListenerContainer container = new StubAbstractMessageListenerContainer();

	container.setAmazonSqs(mock(AmazonSQSAsync.class, withSettings().stubOnly()));
	container.setMessageHandler(mock(QueueMessageHandler.class));

	container.afterPropertiesSet();
	container.destroy();

	assertThat(container.isActive()).isFalse();
}
 
Example #23
Source File: MessageListenerContainerTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void receiveMessageRequests_withOneElement_created() throws Exception {
	AbstractMessageListenerContainer container = new StubAbstractMessageListenerContainer();

	AmazonSQSAsync mock = mock(AmazonSQSAsync.class, withSettings().stubOnly());
	QueueMessageHandler messageHandler = new QueueMessageHandler();
	container.setAmazonSqs(mock);
	container.setMessageHandler(mock(QueueMessageHandler.class));
	container.setMessageHandler(messageHandler);
	StaticApplicationContext applicationContext = new StaticApplicationContext();
	applicationContext.registerSingleton("messageListener", MessageListener.class);
	container.setMaxNumberOfMessages(11);
	container.setVisibilityTimeout(22);
	container.setWaitTimeOut(33);

	messageHandler.setApplicationContext(applicationContext);

	when(mock.getQueueUrl(new GetQueueUrlRequest().withQueueName("testQueue")))
			.thenReturn(new GetQueueUrlResult()
					.withQueueUrl("http://testQueue.amazonaws.com"));
	when(mock.getQueueAttributes(any(GetQueueAttributesRequest.class)))
			.thenReturn(new GetQueueAttributesResult());

	messageHandler.afterPropertiesSet();
	container.afterPropertiesSet();
	container.start();

	Map<String, QueueAttributes> registeredQueues = container.getRegisteredQueues();
	assertThat(registeredQueues.get("testQueue").getReceiveMessageRequest()
			.getQueueUrl()).isEqualTo("http://testQueue.amazonaws.com");
	assertThat(registeredQueues.get("testQueue").getReceiveMessageRequest()
			.getMaxNumberOfMessages().longValue()).isEqualTo(11L);
	assertThat(registeredQueues.get("testQueue").getReceiveMessageRequest()
			.getVisibilityTimeout().longValue()).isEqualTo(22L);
	assertThat(registeredQueues.get("testQueue").getReceiveMessageRequest()
			.getWaitTimeSeconds().longValue()).isEqualTo(33L);
}
 
Example #24
Source File: SimpleMessageListenerContainerTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void start_withQueueNameThatDoesNotExist_throwAnException() throws Exception {
	// Arrange
	SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
	container.setAmazonSqs(mock(AmazonSQSAsync.class, withSettings().stubOnly()));
	container.setMessageHandler(new QueueMessageHandler());

	container.afterPropertiesSet();

	// Assert
	assertThatThrownBy(() -> container.start("bar"))
			.isInstanceOf(IllegalArgumentException.class).hasMessageContaining("bar");
}
 
Example #25
Source File: QueueMessagingTemplateTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void send_withoutDefaultDestination_throwAnException() {
	AmazonSQSAsync amazonSqs = createAmazonSqs();
	QueueMessagingTemplate queueMessagingTemplate = new QueueMessagingTemplate(
			amazonSqs);

	Message<String> stringMessage = MessageBuilder.withPayload("message content")
			.build();
	assertThatThrownBy(() -> queueMessagingTemplate.send(stringMessage))
			.isInstanceOf(IllegalStateException.class);
}
 
Example #26
Source File: QueueMessageChannelTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void receiveMessage_withMissingNumericMessageHeaderTargetClass_shouldThrowAnException()
		throws Exception {
	// Arrange
	AmazonSQSAsync amazonSqs = mock(AmazonSQSAsync.class);

	HashMap<String, MessageAttributeValue> messageAttributes = new HashMap<>();
	messageAttributes.put("classNotFound",
			new MessageAttributeValue()
					.withDataType(
							MessageAttributeDataTypes.NUMBER + ".class.not.Found")
					.withStringValue("12"));

	when(amazonSqs.receiveMessage(new ReceiveMessageRequest("http://testQueue")
			.withWaitTimeSeconds(0).withMaxNumberOfMessages(1)
			.withAttributeNames(QueueMessageChannel.ATTRIBUTE_NAMES)
			.withMessageAttributeNames("All")))
					.thenReturn(new ReceiveMessageResult().withMessages(
							new com.amazonaws.services.sqs.model.Message()
									.withBody("Hello")
									.withMessageAttributes(messageAttributes)));

	PollableChannel messageChannel = new QueueMessageChannel(amazonSqs,
			"http://testQueue");

	// Assert
	assertThatThrownBy(messageChannel::receive).isInstanceOf(MessagingException.class)
			.hasMessageContaining(
					"Message attribute with value '12' and data type 'Number.class.not.Found' could not be converted"
							+ " into a Number because target class was not found.");
}
 
Example #27
Source File: SimpleMessageListenerContainerTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void doStop_containerNotRunning_shouldNotThrowAnException() throws Exception {
	// Arrange
	SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
	container.setAmazonSqs(mock(AmazonSQSAsync.class, withSettings().stubOnly()));
	container.setMessageHandler(mock(QueueMessageHandler.class));
	container.setAutoStartup(false);
	container.afterPropertiesSet();

	// Act & Assert
	container.stop();
}
 
Example #28
Source File: SimpleMessageListenerContainerTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void testContainerDoesNotProcessMessageAfterBeingStopped() throws Exception {
	SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
	SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();
	container.setTaskExecutor(taskExecutor);

	AmazonSQSAsync sqs = mock(AmazonSQSAsync.class, withSettings().stubOnly());
	container.setAmazonSqs(sqs);

	QueueMessageHandler messageHandler = new QueueMessageHandler() {

		@Override
		public void handleMessage(org.springframework.messaging.Message<?> message)
				throws MessagingException {
			fail("Should not have been called");
		}
	};
	container.setMessageHandler(messageHandler);
	container.setBeanName("testContainerName");

	mockGetQueueUrl(sqs, "testQueue",
			"http://testContainerDoesNotProcessMessageAfterBeingStopped.amazonaws.com");

	container.afterPropertiesSet();

	when(sqs.receiveMessage(new ReceiveMessageRequest(
			"http://testContainerDoesNotProcessMessageAfterBeingStopped.amazonaws.com")))
					.thenAnswer((Answer<ReceiveMessageResult>) invocation -> {
						container.stop();
						return new ReceiveMessageResult()
								.withMessages(new Message().withBody("test"));
					});

	container.start();
}
 
Example #29
Source File: SimpleMessageListenerContainerTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void testCustomTaskExecutor() throws Exception {
	SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
	SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();
	container.setTaskExecutor(taskExecutor);

	container.setAmazonSqs(mock(AmazonSQSAsync.class, withSettings().stubOnly()));
	container.setMessageHandler(mock(QueueMessageHandler.class));
	container.setBeanName("testContainerName");
	container.afterPropertiesSet();

	assertThat(container.getTaskExecutor()).isEqualTo(taskExecutor);
}
 
Example #30
Source File: SimpleMessageListenerContainerTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void stop_withAQueueNameThatIsNotRunning_shouldNotStopTheQueueAgainAndIgnoreTheCall()
		throws Exception {
	// Arrange
	StaticApplicationContext applicationContext = new StaticApplicationContext();
	applicationContext.registerSingleton("testMessageListener",
			TestMessageListener.class);

	SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
	AmazonSQSAsync sqs = mock(AmazonSQSAsync.class, withSettings().stubOnly());
	container.setAmazonSqs(sqs);
	container.setBackOffTime(0);

	QueueMessageHandler messageHandler = new QueueMessageHandler();
	messageHandler.setApplicationContext(applicationContext);
	container.setMessageHandler(messageHandler);

	mockGetQueueUrl(sqs, "testQueue",
			"https://stop_withAQueueNameThatIsNotRunning_shouldNotStopTheQueueAgainAndIgnoreTheCall.amazonaws.com");
	mockGetQueueAttributesWithEmptyResult(sqs,
			"https://stop_withAQueueNameThatIsNotRunning_shouldNotStopTheQueueAgainAndIgnoreTheCall.amazonaws.com");

	messageHandler.afterPropertiesSet();
	container.afterPropertiesSet();
	container.start();

	container.stop("testQueue");
	assertThat(container.isRunning("testQueue")).isFalse();

	// Act
	container.stop("testQueue");

	// Assert
	assertThat(container.isRunning("testQueue")).isFalse();
}