Search in sources :

Example 1 with TopicSubscription

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);
        }
    }
}
Also used : NotificationTopic(org.sagebionetworks.bridge.models.notifications.NotificationTopic) TopicSubscription(org.sagebionetworks.bridge.models.notifications.TopicSubscription) NotificationRegistration(org.sagebionetworks.bridge.models.notifications.NotificationRegistration)

Example 2 with TopicSubscription

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;
}
Also used : TopicSubscription(org.sagebionetworks.bridge.models.notifications.TopicSubscription) AmazonServiceException(com.amazonaws.AmazonServiceException) NotFoundException(com.amazonaws.services.sns.model.NotFoundException)

Example 3 with TopicSubscription

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);
}
Also used : TopicSubscription(org.sagebionetworks.bridge.models.notifications.TopicSubscription) AmazonServiceException(com.amazonaws.AmazonServiceException) Mockito.anyString(org.mockito.Mockito.anyString) Test(org.testng.annotations.Test)

Example 4 with TopicSubscription

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");
}
Also used : TestUtils.getNotificationTopic(org.sagebionetworks.bridge.TestUtils.getNotificationTopic) NotificationTopic(org.sagebionetworks.bridge.models.notifications.NotificationTopic) TopicSubscription(org.sagebionetworks.bridge.models.notifications.TopicSubscription) NotificationRegistration(org.sagebionetworks.bridge.models.notifications.NotificationRegistration) TestUtils.getNotificationRegistration(org.sagebionetworks.bridge.TestUtils.getNotificationRegistration) AmazonServiceException(com.amazonaws.AmazonServiceException) Mockito.anyString(org.mockito.Mockito.anyString) Test(org.testng.annotations.Test)

Example 5 with TopicSubscription

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");
}
Also used : TestUtils.getNotificationTopic(org.sagebionetworks.bridge.TestUtils.getNotificationTopic) NotificationTopic(org.sagebionetworks.bridge.models.notifications.NotificationTopic) TopicSubscription(org.sagebionetworks.bridge.models.notifications.TopicSubscription) SubscribeRequest(com.amazonaws.services.sns.model.SubscribeRequest) NotificationRegistration(org.sagebionetworks.bridge.models.notifications.NotificationRegistration) TestUtils.getNotificationRegistration(org.sagebionetworks.bridge.TestUtils.getNotificationRegistration) Test(org.testng.annotations.Test)

Aggregations

TopicSubscription (org.sagebionetworks.bridge.models.notifications.TopicSubscription)7 Test (org.testng.annotations.Test)4 AmazonServiceException (com.amazonaws.AmazonServiceException)3 Mockito.anyString (org.mockito.Mockito.anyString)3 NotificationRegistration (org.sagebionetworks.bridge.models.notifications.NotificationRegistration)3 NotificationTopic (org.sagebionetworks.bridge.models.notifications.NotificationTopic)3 TestUtils.getNotificationRegistration (org.sagebionetworks.bridge.TestUtils.getNotificationRegistration)2 TestUtils.getNotificationTopic (org.sagebionetworks.bridge.TestUtils.getNotificationTopic)2 NotFoundException (com.amazonaws.services.sns.model.NotFoundException)1 SubscribeRequest (com.amazonaws.services.sns.model.SubscribeRequest)1