private static boolean isSQSQueueSubscribingToSNSTopic( AmazonSNS amazonSNS, String queueARN, String topicARN ) { // This statement will throw if the topic does not exist. ListSubscriptionsByTopicResult subscriptions = amazonSNS.listSubscriptionsByTopic( new ListSubscriptionsByTopicRequest() .withTopicArn(topicARN) ); for (Subscription subscription : subscriptions.getSubscriptions()) { if (subscription.getEndpoint().equals(queueARN)) { return true; } } return false; }
@Override public SubscriptionCollection getSubscriptions( ListSubscriptionsByTopicRequest request) { ResourceCollectionImpl result = resource.getCollection("Subscriptions", request); if (result == null) return null; return new SubscriptionCollectionImpl(result); }
@Override public ListSubscriptionsByTopicResult listSubscriptionsByTopic(ListSubscriptionsByTopicRequest listSubscriptionsByTopicRequest) throws AmazonClientException { return new ListSubscriptionsByTopicResult(). withSubscriptions(FluentIterable. from(_subscriptionsForTopic.get(listSubscriptionsByTopicRequest.getTopicArn())). filter(Predicates.in(_subscriptionsByArn.keySet())). transform(Functions.forMap(_subscriptionsByArn)). toList()); }
public void publishAndReceive() { final String queueName = someQueueName(); final String queueUrl = someNewQueue(queueName); final String topicName = "publishAndReceive"; final String message = "hi from " + topicName; AmazonSNS amazonSNS = new InMemorySNS(_amazonSQS1, new Subscription(). withTopicArn(makeTopicArn(topicName)). withProtocol("sqs"). withSubscriptionArn(makeSomeSubArn(topicName)). withEndpoint(getQueueArn(queueName))); Assert.assertEquals(amazonSNS.listTopics().getTopics().size(), 1); Assert.assertEquals(amazonSNS.listSubscriptions().getSubscriptions().size(), 1); Assert.assertEquals(amazonSNS. listSubscriptionsByTopic(new ListSubscriptionsByTopicRequest(makeTopicArn(topicName))) .getSubscriptions() .size(), 1); amazonSNS.publish(new PublishRequest(makeTopicArn(topicName), message)); ReceiveMessageResult result = _amazonSQS1.receiveMessage(new ReceiveMessageRequest(queueUrl)); Assert.assertEquals(result.getMessages().size(), 1); Assert.assertEquals(result.getMessages().get(0).getBody(), message); }
public void manualSubscriptionSetup() { final String queueName = someQueueName(); final String queueUrl = someNewQueue(queueName); final String topicName = "manualSubscriptionSetup"; final String message = "hi from " + topicName; AmazonSNS amazonSNS = new InMemorySNS(_amazonSQS1); amazonSNS.createTopic(new CreateTopicRequest(topicName)); Assert.assertEquals(amazonSNS.listTopics().getTopics().size(), 1); Assert.assertEquals(amazonSNS.listTopics().getTopics().get(0).getTopicArn(), makeTopicArn(topicName)); //make sure create is idempotent amazonSNS.createTopic(new CreateTopicRequest(topicName)); Assert.assertEquals(amazonSNS.listTopics().getTopics().size(), 1, "existing topic duplicated?"); Assert.assertEquals(amazonSNS.listTopics().getTopics().get(0).getTopicArn(), makeTopicArn(topicName)); //subscribe. amazonSNS.subscribe(new SubscribeRequest(). withEndpoint(getQueueArn(queueName)). withProtocol("sqs"). withTopicArn(makeTopicArn(topicName))); Assert.assertEquals(amazonSNS.listSubscriptions().getSubscriptions().size(), 1); Assert.assertEquals(amazonSNS.listSubscriptions().getSubscriptions().get(0).getTopicArn(), makeTopicArn(topicName)); Assert.assertEquals(amazonSNS. listSubscriptionsByTopic(new ListSubscriptionsByTopicRequest(makeTopicArn(topicName))) .getSubscriptions() .size(), 1); amazonSNS.publish(new PublishRequest(makeTopicArn(topicName), message)); ReceiveMessageResult result = _amazonSQS1.receiveMessage(new ReceiveMessageRequest(queueUrl)); Assert.assertEquals(result.getMessages().size(), 1); Assert.assertEquals(result.getMessages().get(0).getBody(), message); }
@Test public void testSubscribeConfirmListUnsubscribe_shouldCreateVerifyListAndRemoveSubscription() { mockSns(new MockParameters()); // create topic CreateTopicResult topicResult = sns.createTopic(new CreateTopicRequest().withName("important-topic")); // subscribe to first topic SubscribeResult subscribeResult = sns.subscribe(new SubscribeRequest().withTopicArn(topicResult.getTopicArn()) .withProtocol("sqs").withEndpoint("arn:aws:sqs:us-east-1:123456789012:queue1")); assertNotNull("verify subscription ARN is created", subscribeResult.getSubscriptionArn()); // create second topic and subscribe to that one CreateTopicResult secondTopicResult = sns.createTopic(new CreateTopicRequest().withName("someOther-topic")); sns.subscribe(new SubscribeRequest().withTopicArn(secondTopicResult.getTopicArn()) .withProtocol("sqs").withEndpoint("arn:aws:sqs:us-east-1:564654654:queue7")); // confirm first subscription ConfirmSubscriptionResult confirmResultAuth = sns.confirmSubscription(new ConfirmSubscriptionRequest() .withAuthenticateOnUnsubscribe("true").withToken("gold-token").withTopicArn(topicResult.getTopicArn())); assertNotNull("verify auth confirmation with responded topic arn", confirmResultAuth.getSubscriptionArn()); ConfirmSubscriptionResult confirmResultNoAuth = sns.confirmSubscription(new ConfirmSubscriptionRequest() .withAuthenticateOnUnsubscribe("false").withToken("gold-token").withTopicArn(topicResult.getTopicArn())); assertNotNull("verify no auth confirmation with responded topic arn", confirmResultNoAuth.getSubscriptionArn()); // list all subscriptions ListSubscriptionsResult allSubs = sns.listSubscriptions(); assertEquals("verify correct total subscription count", 2, allSubs.getSubscriptions().size()); com.amazonaws.services.sns.model.Subscription firstSub = allSubs.getSubscriptions().stream().filter(sub -> sub.getTopicArn().equals(topicResult.getTopicArn())).findFirst().get(); assertEquals("verify the correct subscription arn", subscribeResult.getSubscriptionArn(), firstSub.getSubscriptionArn()); assertEquals("verify the correct subscription topic", topicResult.getTopicArn(), firstSub.getTopicArn()); assertEquals("verify the correct subscription protocol", "sqs", firstSub.getProtocol()); assertEquals("verify the correct subscription endpoint", "arn:aws:sqs:us-east-1:123456789012:queue1", firstSub.getEndpoint()); assertNotNull("verify the correct subscription owner", firstSub.getOwner()); // list subscriptions of first topic ListSubscriptionsByTopicResult topicsSubscriptions = sns.listSubscriptionsByTopic(new ListSubscriptionsByTopicRequest(topicResult.getTopicArn())); assertEquals("verify that the one subscription is contained in list", 1, topicsSubscriptions.getSubscriptions().size()); assertEquals("verify the correct subscription arn", subscribeResult.getSubscriptionArn(), topicsSubscriptions.getSubscriptions().get(0).getSubscriptionArn()); assertEquals("verify the correct subscription topic", topicResult.getTopicArn(), topicsSubscriptions.getSubscriptions().get(0).getTopicArn()); assertEquals("verify the correct subscription protocol", "sqs", topicsSubscriptions.getSubscriptions().get(0).getProtocol()); assertEquals("verify the correct subscription endpoint", "arn:aws:sqs:us-east-1:123456789012:queue1", topicsSubscriptions.getSubscriptions().get(0).getEndpoint()); assertNotNull("verify the correct subscription owner", topicsSubscriptions.getSubscriptions().get(0).getOwner()); // unsubscribe first topic assertNotNull(sns.unsubscribe(subscribeResult.getSubscriptionArn())); // check if really unsubscribed ListSubscriptionsByTopicResult subsToFirstTopicAfterUnsubscribe = sns.listSubscriptionsByTopic(new ListSubscriptionsByTopicRequest(topicResult.getTopicArn())); assertEquals("topic should be gone", 0, subsToFirstTopicAfterUnsubscribe.getSubscriptions().size()); // cleanup sns.deleteTopic(topicResult.getTopicArn()); sns.deleteTopic(secondTopicResult.getTopicArn()); }
@Test public void testListSubscriptions_withNoRequestParams_shouldWork() { mockSns(new MockParameters()); assertNotNull(sns.listSubscriptionsByTopic(new ListSubscriptionsByTopicRequest())); }
@Override public ListSubscriptionsByTopicResult listSubscriptionsByTopic(ListSubscriptionsByTopicRequest listSubscriptionsByTopicRequest) throws AmazonServiceException, AmazonClientException { throw new UnsupportedOperationException(); }
/** * Retrieves the Subscriptions collection referenced by this resource. */ SubscriptionCollection getSubscriptions(ListSubscriptionsByTopicRequest request);