@Test public void testCreateListDeleteTopic_shouldCreateReturnAndDelete() { mockSns(new MockParameters()); ListTopicsResult listTopicResultBefore = sns.listTopics(); assertEquals("topic list should contain zero items before insert",0,listTopicResultBefore.getTopics().size()); CreateTopicRequest create = new CreateTopicRequest() .withName("major-topic"); CreateTopicResult createResult = sns.createTopic(create); String topicArn = createResult.getTopicArn(); assertNotNull("verify returned topic ARN", topicArn); ListTopicsResult listTopicResult = sns.listTopics(); assertEquals("after insert topic list should contain 1 item",1,listTopicResult.getTopics().size()); assertEquals("after insert topic list should contain before inserted topic arn", topicArn, listTopicResult.getTopics().get(0).getTopicArn()); DeleteTopicResult deleteResult = sns.deleteTopic(topicArn); assertNotNull(deleteResult); ListTopicsResult listTopicsAfterDeletion = sns.listTopics(); assertEquals("topic list should contain zero items after deletion",0,listTopicsAfterDeletion.getTopics().size()); }
@Test public void testSetGetSubscriptionAttributes_shouldSetAndRespondSubscriptionAttributes() { mockSns(new MockParameters()); // create topic and subscription CreateTopicResult topicResult = sns.createTopic(new CreateTopicRequest().withName("important-topic")); SubscribeResult subscribeResult = sns.subscribe(new SubscribeRequest().withTopicArn(topicResult.getTopicArn()) .withProtocol("sqs").withEndpoint("arn:aws:sqs:us-east-1:123456789012:queue1")); // set subscription attribute SetSubscriptionAttributesResult setAttrResult = sns.setSubscriptionAttributes(new SetSubscriptionAttributesRequest() .withAttributeName("unicorns-exist").withAttributeValue("only in scotland").withSubscriptionArn(subscribeResult.getSubscriptionArn())); assertNotNull("verify setting attributes result", setAttrResult); // get subscription attribute GetSubscriptionAttributesResult subAttributes = sns.getSubscriptionAttributes(new GetSubscriptionAttributesRequest() .withSubscriptionArn(subscribeResult.getSubscriptionArn())); assertEquals("verify subscription attribute","only in scotland",subAttributes.getAttributes().get("unicorns-exist")); }
@Test public void testPublish_shouldPublishTheMessage() { CreateTopicResult topicResult = sns.createTopic(new CreateTopicRequest().withName("important-topic")); PublishRequest publishReq = new PublishRequest() .withMessage("{\"state\":\"liquid\",\"color\":\"black\",\"waking-up\":true}") .withMessageStructure("json") .withPhoneNumber("00121212") .withSubject("eeffoc") .withTopicArn(topicResult.getTopicArn()); PublishResult publishResult = sns.publish(publishReq); assertNotNull("verify message id",publishResult.getMessageId()); Topic topic = getSnsTopics().get(topicResult.getTopicArn()); assertEquals("verify correct message count", 1, topic.getMessages().size()); assertEquals("verify message subject","eeffoc",topic.getMessages().get(0).getSubject()); assertEquals("verify message body","{\"state\":\"liquid\",\"color\":\"black\",\"waking-up\":true}",topic.getMessages().get(0).getBody()); assertEquals("verify message structure","json",topic.getMessages().get(0).getStructure()); }
@Override public String resolveDestination(String name) throws DestinationResolutionException { if (this.autoCreate) { return this.amazonSns.createTopic(new CreateTopicRequest(name)).getTopicArn(); } else { String physicalTopicName = name; if (this.resourceIdResolver != null) { physicalTopicName = this.resourceIdResolver.resolveToPhysicalResourceId(name); } if (physicalTopicName != null && AmazonResourceName.isValidAmazonResourceName(physicalTopicName)) { return physicalTopicName; } String topicArn = getTopicResourceName(null, physicalTopicName); if (topicArn == null) { throw new IllegalArgumentException("No Topic with name: '" + name + "' found. Please use " + "the right topic name or enable auto creation of topics for this DestinationResolver"); } return topicArn; } }
@Test public void resolveDestination_withAutoCreateEnabled_shouldCreateTopicDirectly() throws Exception { // Arrange String topicArn = "arn:aws:sns:eu-west:123456789012:test"; AmazonSNS sns = mock(AmazonSNS.class); when(sns.createTopic(new CreateTopicRequest("test"))).thenReturn(new CreateTopicResult().withTopicArn(topicArn)); DynamicTopicDestinationResolver resolver = new DynamicTopicDestinationResolver(sns); resolver.setAutoCreate(true); // Act String resolvedDestinationName = resolver.resolveDestination("test"); // Assert assertEquals(topicArn, resolvedDestinationName); }
private void createTopicIfNotExists() { for (Topic topic : client.listTopics().getTopics()) { if (topic.getTopicArn().contains(topicName)) { topicArn = topic.getTopicArn(); break; } } if (topicArn == null) { CreateTopicRequest request = new CreateTopicRequest(topicName); CreateTopicResult result = client.createTopic(request); topicArn = result.getTopicArn(); log.debug("Topic created, arn: " + topicArn); } else { log.debug("Topic already created: " + topicArn); } }
/** * Sends using the sns publisher */ @Override public boolean postMessage(NotificationMessage nMsg, BasicSubscriber subscriber) { SNSSubscriber sub; if (subscriber instanceof SNSSubscriber) { sub = (SNSSubscriber) subscriber; } else { throw new ClassCastException("invalid subscriber " + subscriber.getClass().getName()); } SNSMessage msg = buildSNSMessage(nMsg); AmazonSNS sns = new AmazonSNSClient(new BasicAWSCredentials(sub.getAwsAccessKey(), sub.getAwsSecretKey())); if (sub.getSnsEndpoint() != null) { sns.setEndpoint(sub.getSnsEndpoint()); } CreateTopicRequest tRequest = new CreateTopicRequest(); tRequest.setName(msg.getTopicName()); CreateTopicResult result = sns.createTopic(tRequest); PublishRequest pr = new PublishRequest(result.getTopicArn(), msg.getTxtMessage()).withSubject(msg.getSubject()); try { PublishResult pubresult = sns.publish(pr); logger.info("Published msg with id - " + pubresult.getMessageId()); } catch (AmazonClientException ace) { logger.error(ace.getMessage()); return false; } return true; }
@Test public void testGetSetTopicAttributes_shouldAddAndRespondAttributes() { mockSns(new MockParameters()); CreateTopicResult topicResult = sns.createTopic(new CreateTopicRequest().withName("attributefull-topic")); SetTopicAttributesResult setAttrResult = sns.setTopicAttributes(topicResult.getTopicArn(), "planet", "Omega 3"); assertNotNull(setAttrResult); GetTopicAttributesResult topicAttributes = sns.getTopicAttributes(new GetTopicAttributesRequest().withTopicArn(topicResult.getTopicArn())); assertEquals("verify added attribute is correct", "Omega 3", topicAttributes.getAttributes().get("planet")); sns.deleteTopic(topicResult.getTopicArn()); }
@Override public void doStart() throws Exception { super.doStart(); snsClient = configuration.getAmazonSNSClient() != null ? configuration.getAmazonSNSClient() : createSNSClient(); // Override the setting Endpoint from url if (ObjectHelper.isNotEmpty(configuration.getAmazonSNSEndpoint())) { LOG.trace("Updating the SNS region with : {} " + configuration.getAmazonSNSEndpoint()); snsClient.setEndpoint(configuration.getAmazonSNSEndpoint()); } if (configuration.getTopicArn() == null) { // creates a new topic, or returns the URL of an existing one CreateTopicRequest request = new CreateTopicRequest(configuration.getTopicName()); LOG.trace("Creating topic [{}] with request [{}]...", configuration.getTopicName(), request); CreateTopicResult result = snsClient.createTopic(request); configuration.setTopicArn(result.getTopicArn()); LOG.trace("Topic created with Amazon resource name: {}", configuration.getTopicArn()); } if (ObjectHelper.isNotEmpty(configuration.getPolicy())) { LOG.trace("Updating topic [{}] with policy [{}]", configuration.getTopicArn(), configuration.getPolicy()); snsClient.setTopicAttributes(new SetTopicAttributesRequest(configuration.getTopicArn(), "Policy", configuration.getPolicy())); LOG.trace("Topic policy updated"); } }
@Override public Topic createTopic(CreateTopicRequest request, ResultCapture<CreateTopicResult> extractor) { ActionResult result = service.performAction("CreateTopic", request, extractor); if (result == null) return null; return new TopicImpl(result.getResource()); }
@Override public Topic createTopic(String name, ResultCapture<CreateTopicResult> extractor) { CreateTopicRequest request = new CreateTopicRequest() .withName(name); return createTopic(request, extractor); }
@Override public CreateTopicResult createTopic(CreateTopicRequest createTopicRequest) throws AmazonClientException { String topicArn = BASE_ARN + createTopicRequest.getName(); CreateTopicResult result = new CreateTopicResult().withTopicArn(topicArn); if (!_subscriptionsForTopic.containsKey(topicArn)) { _subscriptionsForTopic.put(topicArn, new ArrayList<String>()); } return result; }
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); }
/** * Creates a SNS topic. * * @param topic string * @param email address to be notified when there is any event for this topic. * @return false if there is any error. * @throws Exception */ public boolean createTopic(final String topic, final String email) throws Exception{ CreateTopicRequest request = new CreateTopicRequest() .withName(topic); CreateTopicResult result = null; try { result = snsClient.createTopic(request); } catch (Exception e) { LGR.setUseParentHandlers(true); LGR.log(Level.SEVERE, null, e); LGR.setUseParentHandlers(false); return false; } topicARN = result.getTopicArn(); LGR.log(Level.INFO, "topic arn is {0}", topicARN); SubscribeRequest request2 = new SubscribeRequest() .withTopicArn(topicARN).withEndpoint(email).withProtocol("email"); SubscribeResult result2 = snsClient.subscribe(request2); LGR.log(Level.INFO, "subscription ARN {0}", result2.getSubscriptionArn()); return true; }
@Test public void testCreateTopic_withNoRequestParams_shouldWork() { mockSns(new MockParameters()); assertNotNull(sns.createTopic(new CreateTopicRequest())); }
@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()); }
private String createTopic(String arn) { CreateTopicResult result = snsClient.createTopic(new CreateTopicRequest(arn)); return result.getTopicArn(); }
@Override public CreateTopicResult createTopic(CreateTopicRequest createTopicRequest) throws AmazonServiceException, AmazonClientException { CreateTopicResult createTopicResult = new CreateTopicResult(); createTopicResult.setTopicArn(DEFAULT_TOPIC_ARN); return createTopicResult; }
public String createNotificationTopicIfNotExist(String topicName){ CreateTopicRequest createTopicRequest = new CreateTopicRequest(topicName); CreateTopicResult createTopicResult = snsClient.createTopic(createTopicRequest); return createTopicResult.getTopicArn(); }
@Override public Topic createTopic(CreateTopicRequest request) { return createTopic(request, null); }
public void process(Exchange exchange) throws Exception { // TODO cache arns and don't create if not necessary final String topic = determineTopic(exchange); // creates a new topic, or returns the URL of an existing one CreateTopicRequest request = new CreateTopicRequest(topic); LOG.trace("Creating topic [{}] with request [{}]...", topic, request); final AmazonSNS snsClient = getEndpoint().getSNSClient(); CreateTopicResult result = snsClient.createTopic(request); final String topicArn = result.getTopicArn(); LOG.trace("Topic created with Amazon resource name: {}", topicArn); final SnsConfiguration configuration = getEndpoint().getConfiguration(); if (ObjectHelper.isNotEmpty(configuration.getPolicy())) { LOG.trace("Updating topic [{}] with policy [{}]", topicArn, configuration.getPolicy()); snsClient.setTopicAttributes( new SetTopicAttributesRequest(topicArn, "Policy", configuration.getPolicy())); LOG.trace("Topic policy updated"); } PublishRequest publishRequest = new PublishRequest(); publishRequest.setTopicArn(topicArn); publishRequest.setSubject(determineSubject(exchange)); publishRequest.setMessageStructure(determineMessageStructure(exchange)); publishRequest.setMessage(exchange.getIn().getBody(String.class)); LOG.trace("Sending request [{}] from exchange [{}]...", publishRequest, exchange); PublishResult publishResult = snsClient.publish(publishRequest); LOG.trace("Received result [{}]", publishResult); Message message = getMessageForResponse(exchange); message.setHeader(SnsConstants.MESSAGE_ID, publishResult.getMessageId()); }
/** * Performs the <code>CreateTopic</code> action. * * <p> * * @return The <code>Topic</code> resource object associated with the result * of this action. * @see CreateTopicRequest */ com.amazonaws.resources.sns.Topic createTopic(CreateTopicRequest request);
/** * Performs the <code>CreateTopic</code> action and use a ResultCapture to * retrieve the low-level client response. * * <p> * * @return The <code>Topic</code> resource object associated with the result * of this action. * @see CreateTopicRequest */ com.amazonaws.resources.sns.Topic createTopic(CreateTopicRequest request, ResultCapture<CreateTopicResult> extractor);