@Test public void resolveDestination_withExistentTopic_returnsTopicArnFoundWhileListingTopic() throws Exception { // Arrange String topicArn = "arn:aws:sns:eu-west:123456789012:test"; AmazonSNS sns = mock(AmazonSNS.class); when(sns.listTopics(new ListTopicsRequest(null))).thenReturn(new ListTopicsResult().withTopics(new Topic().withTopicArn(topicArn))); DynamicTopicDestinationResolver resolver = new DynamicTopicDestinationResolver(sns); // Act String resolvedDestinationName = resolver.resolveDestination("test"); // Assert assertEquals(topicArn, resolvedDestinationName); }
@Test public void resolveDestination_withExistentTopicAndMarker_returnsTopicArnFoundWhileListingTopic() throws Exception { // Arrange AmazonSNS sns = mock(AmazonSNS.class); when(sns.listTopics(new ListTopicsRequest(null))).thenReturn(new ListTopicsResult().withNextToken("mark")); String topicArn = "arn:aws:sns:eu-west:123456789012:test"; when(sns.listTopics(new ListTopicsRequest("mark"))).thenReturn(new ListTopicsResult().withTopics(new Topic().withTopicArn(topicArn))); DynamicTopicDestinationResolver resolver = new DynamicTopicDestinationResolver(sns); // Act String resolvedDestinationName = resolver.resolveDestination("test"); // Assert assertEquals(topicArn, resolvedDestinationName); }
@Test public void resolveDestination_withResourceIdResolver_shouldCallIt() throws Exception { // Arrange String physicalTopicName = "arn:aws:sns:eu-west:123456789012:myTopic"; String logicalTopicName = "myTopic"; ResourceIdResolver resourceIdResolver = mock(ResourceIdResolver.class); when(resourceIdResolver.resolveToPhysicalResourceId(logicalTopicName)).thenReturn(physicalTopicName); AmazonSNS sns = mock(AmazonSNS.class); when(sns.listTopics(new ListTopicsRequest(null))).thenReturn(new ListTopicsResult().withTopics(new Topic().withTopicArn(physicalTopicName))); DynamicTopicDestinationResolver resolver = new DynamicTopicDestinationResolver(sns, resourceIdResolver); // Assert String resolvedDestinationName = resolver.resolveDestination(logicalTopicName); // Assert assertEquals(physicalTopicName, resolvedDestinationName); }
@Test public void send_validTextMessage_usesTopicChannel() throws Exception { // Arrange AmazonSNS amazonSns = mock(AmazonSNS.class); NotificationMessagingTemplate notificationMessagingTemplate = new NotificationMessagingTemplate(amazonSns); String physicalTopicName = "arn:aws:sns:eu-west:123456789012:test"; when(amazonSns.listTopics(new ListTopicsRequest(null))).thenReturn(new ListTopicsResult().withTopics(new Topic().withTopicArn(physicalTopicName))); notificationMessagingTemplate.setDefaultDestinationName(physicalTopicName); // Act notificationMessagingTemplate.send(MessageBuilder.withPayload("Message content").build()); // Assert verify(amazonSns).publish(new PublishRequest(physicalTopicName, "Message content", null).withMessageAttributes(isNotNull())); }
@Override public TopicCollection getTopics(ListTopicsRequest request) { ResourceCollectionImpl result = service.getCollection("Topics", request); if (result == null) return null; return new TopicCollectionImpl(result); }
private String getTopicResourceName(String marker, String topicName) { ListTopicsResult listTopicsResult = this.amazonSns.listTopics(new ListTopicsRequest(marker)); for (Topic topic : listTopicsResult.getTopics()) { AmazonResourceName resourceName = AmazonResourceName.fromString(topic.getTopicArn()); if (resourceName.getResourceType().equals(topicName)) { return topic.getTopicArn(); } } if (StringUtils.hasText(listTopicsResult.getNextToken())) { return getTopicResourceName(listTopicsResult.getNextToken(), topicName); } else { throw new IllegalArgumentException("No topic found for name :'" + topicName + "'"); } }
@Test public void resolveDestination_withNonExistentTopicAndWithoutMarkerReturnedOnListTopics_shouldThrowIllegalArgumentException() throws Exception { // Arrange this.expectedException.expect(IllegalArgumentException.class); this.expectedException.expectMessage("No topic found for name :'test'"); AmazonSNS sns = mock(AmazonSNS.class); when(sns.listTopics(new ListTopicsRequest(null))).thenReturn(new ListTopicsResult()); DynamicTopicDestinationResolver resolver = new DynamicTopicDestinationResolver(sns); // Act resolver.resolveDestination("test"); }
@Test public void resolveDestination_withNonExistentTopicAndWithMarkerReturnedOnListTopics_shouldCallListMultipleTimeWithMarkerAndThrowIllegalArgumentException() throws Exception { // Arrange this.expectedException.expect(IllegalArgumentException.class); this.expectedException.expectMessage("No topic found for name :'test'"); AmazonSNS sns = mock(AmazonSNS.class); when(sns.listTopics(new ListTopicsRequest(null))).thenReturn(new ListTopicsResult().withNextToken("foo")); when(sns.listTopics(new ListTopicsRequest("foo"))).thenReturn(new ListTopicsResult()); DynamicTopicDestinationResolver resolver = new DynamicTopicDestinationResolver(sns); // Act resolver.resolveDestination("test"); }
@Test public void convertAndSend_withDestinationPayloadAndSubject_shouldSetSubject() throws Exception { // Arrange AmazonSNS amazonSns = mock(AmazonSNS.class); NotificationMessagingTemplate notificationMessagingTemplate = new NotificationMessagingTemplate(amazonSns); String physicalTopicName = "arn:aws:sns:eu-west:123456789012:test"; when(amazonSns.listTopics(new ListTopicsRequest(null))).thenReturn(new ListTopicsResult().withTopics(new Topic().withTopicArn(physicalTopicName))); // Act notificationMessagingTemplate.sendNotification(physicalTopicName, "My message", "My subject"); // Assert verify(amazonSns).publish(new PublishRequest(physicalTopicName, "My message", "My subject").withMessageAttributes(isNotNull())); }
@Test public void convertAndSend_withPayloadAndSubject_shouldSetSubject() throws Exception { // Arrange AmazonSNS amazonSns = mock(AmazonSNS.class); NotificationMessagingTemplate notificationMessagingTemplate = new NotificationMessagingTemplate(amazonSns); String physicalTopicName = "arn:aws:sns:eu-west:123456789012:test"; when(amazonSns.listTopics(new ListTopicsRequest(null))).thenReturn(new ListTopicsResult().withTopics(new Topic().withTopicArn(physicalTopicName))); notificationMessagingTemplate.setDefaultDestinationName(physicalTopicName); // Act notificationMessagingTemplate.sendNotification("My message", "My subject"); // Assert verify(amazonSns).publish(new PublishRequest(physicalTopicName, "My message", "My subject").withMessageAttributes(isNotNull())); }
@Override public ListTopicsResult listTopics(ListTopicsRequest listTopicsRequest) throws AmazonServiceException, AmazonClientException { throw new UnsupportedOperationException(); }
@Override public TopicCollection getTopics() { return getTopics((ListTopicsRequest)null); }
@Override public ListTopicsResult listTopics(ListTopicsRequest listTopicsRequest) throws AmazonClientException { return listTopics(); }
/** * Retrieves the Topics collection referenced by this resource. */ TopicCollection getTopics(ListTopicsRequest request);