use of org.sagebionetworks.bridge.models.notifications.TopicSubscription in project BridgeServer2 by Sage-Bionetworks.
the class NotificationTopicService method unsubscribeAll.
/**
* Unsubscribe the given registration from all topics. This is generally used before deleting a registration, to
* clean up any orphaned subscriptions.
*/
public void unsubscribeAll(String appId, String healthCode, String registrationGuid) {
checkNotNull(appId);
checkNotNull(healthCode);
checkNotNull(registrationGuid);
// Get subscriptions.
NotificationRegistration registration = registrationDao.getRegistration(healthCode, registrationGuid);
List<? extends TopicSubscription> subscriptionList = subscriptionDao.listSubscriptions(registration);
if (subscriptionList.isEmpty()) {
// Shortcut: No subscriptions to unsubscribe from.
return;
}
// Unsubscribe from each subscription.
for (TopicSubscription oneSubscription : subscriptionList) {
String topicGuid = oneSubscription.getTopicGuid();
try {
NotificationTopic topic = topicDao.getTopic(appId, topicGuid);
subscriptionDao.unsubscribe(registration, topic);
} catch (RuntimeException ex) {
LOG.error("Error unsubscribing registration " + registrationGuid + " from topic " + topicGuid + ": " + ex.getMessage(), ex);
}
}
}
use of org.sagebionetworks.bridge.models.notifications.TopicSubscription in project BridgeServer2 by Sage-Bionetworks.
the class NotificationTopicService method cleanupSubscriptions.
/**
* Get a set of the user's current topic subscriptions, but take the opportunity to check for data integrity and
* delete any DDB records that don't have a corresponding SNS record... these are unsuccessful unsubscribes
* and we're trying here again to finish them.
*/
private Set<String> cleanupSubscriptions(NotificationRegistration registration) {
Set<String> subscribedTopicGuids = Sets.newHashSet();
List<? extends TopicSubscription> subscriptions = subscriptionDao.listSubscriptions(registration);
for (TopicSubscription subscription : subscriptions) {
try {
snsClient.getSubscriptionAttributes(subscription.getSubscriptionARN());
subscribedTopicGuids.add(subscription.getTopicGuid());
} catch (NotFoundException e) {
LOG.warn("SNS topic " + subscription.getTopicGuid() + " not found, deleting DDB record", e);
subscriptionDao.removeOrphanedSubscription(subscription);
} catch (AmazonServiceException e) {
LOG.warn("Error cleaning up subscriptions", e);
// However, it is there, so include it in the list of subscriptions.
subscribedTopicGuids.add(subscription.getTopicGuid());
}
}
return subscribedTopicGuids;
}
use of org.sagebionetworks.bridge.models.notifications.TopicSubscription in project BridgeServer2 by Sage-Bionetworks.
the class DynamoTopicSubscriptionDaoTest method deleteTriesBothStoresIfSnsFails.
@Test
public void deleteTriesBothStoresIfSnsFails() {
TopicSubscription subscription = TopicSubscription.create();
subscription.setSubscriptionARN("subscriptionARN");
doThrow(EXCEPTION).when(mockSnsClient).unsubscribe(anyString());
try {
subDao.removeOrphanedSubscription(subscription);
fail("expected exception");
} catch (AmazonServiceException e) {
// expected exception
}
verify(mockSnsClient).unsubscribe(unsubscribeStringCaptor.capture());
String subscriptionARN = unsubscribeStringCaptor.getValue();
assertEquals(subscriptionARN, "subscriptionARN");
verify(mockMapper).delete(subscriptionCaptor.capture());
DynamoTopicSubscription capturedSub = subscriptionCaptor.getValue();
assertEquals(capturedSub, subscription);
}
use of org.sagebionetworks.bridge.models.notifications.TopicSubscription in project BridgeServer2 by Sage-Bionetworks.
the class DynamoTopicSubscriptionDaoTest method subscribeWhenMapperFails.
@Test
public void subscribeWhenMapperFails() {
doReturn("subscriptionARN").when(mockSubscribeResult).getSubscriptionArn();
doReturn(mockSubscribeResult).when(mockSnsClient).subscribe(any());
doThrow(EXCEPTION).when(mockMapper).save(any());
NotificationRegistration registration = getNotificationRegistration();
NotificationTopic topic = getNotificationTopic();
try {
subDao.subscribe(registration, topic);
fail("expected exception");
} catch (AmazonServiceException e) {
// expected exception
}
verify(mockSnsClient).subscribe(any());
verify(mockMapper).save(subscriptionCaptor.capture());
verify(mockSnsClient).unsubscribe(unsubscribeStringCaptor.capture());
TopicSubscription saved = subscriptionCaptor.getValue();
assertEquals(saved.getRegistrationGuid(), "registrationGuid");
assertEquals(saved.getSubscriptionARN(), "subscriptionARN");
assertEquals(saved.getTopicGuid(), "topicGuid");
String subscriptionARN = unsubscribeStringCaptor.getValue();
assertEquals(subscriptionARN, "subscriptionARN");
}
use of org.sagebionetworks.bridge.models.notifications.TopicSubscription in project BridgeServer2 by Sage-Bionetworks.
the class DynamoTopicSubscriptionDaoTest method successfulSubscribePushNotification.
@Test
public void successfulSubscribePushNotification() {
// Mock SNS.
doReturn("subscriptionARN").when(mockSubscribeResult).getSubscriptionArn();
doReturn(mockSubscribeResult).when(mockSnsClient).subscribe(any());
// Make registration and topic.
NotificationRegistration registration = getNotificationRegistration();
registration.setProtocol(NotificationProtocol.APPLICATION);
registration.setEndpoint(ENDPOINT_ARN);
NotificationTopic topic = getNotificationTopic();
topic.setTopicARN(TOPIC_ARN);
// Execute and validate.
TopicSubscription sub = subDao.subscribe(registration, topic);
verify(mockSnsClient).subscribe(subscribeRequestCaptor.capture());
SubscribeRequest request = subscribeRequestCaptor.getValue();
assertEquals(request.getTopicArn(), TOPIC_ARN);
assertEquals(request.getProtocol(), "application");
assertEquals(request.getEndpoint(), ENDPOINT_ARN);
verify(mockMapper).save(subscriptionCaptor.capture());
TopicSubscription saved = subscriptionCaptor.getValue();
assertEquals(saved.getRegistrationGuid(), "registrationGuid");
assertEquals(saved.getSubscriptionARN(), "subscriptionARN");
assertEquals(saved.getTopicGuid(), "topicGuid");
assertEquals(sub.getRegistrationGuid(), "registrationGuid");
assertEquals(sub.getSubscriptionARN(), "subscriptionARN");
assertEquals(sub.getTopicGuid(), "topicGuid");
}
Aggregations