Search in sources :

Example 1 with NotificationMessage

use of org.sagebionetworks.bridge.models.notifications.NotificationMessage in project BridgeServer2 by Sage-Bionetworks.

the class NotificationsServiceTest method sendNotificationNoRegistration.

@Test
public void sendNotificationNoRegistration() {
    doReturn(Lists.newArrayList()).when(mockRegistrationDao).listRegistrations(HEALTH_CODE);
    NotificationMessage message = getNotificationMessage();
    try {
        service.sendNotificationToUser(TEST_APP_ID, HEALTH_CODE, message);
        fail("Should have thrown exception.");
    } catch (BadRequestException e) {
        assertEquals(e.getMessage(), "Participant has not registered to receive push notifications.");
    }
}
Also used : NotificationMessage(org.sagebionetworks.bridge.models.notifications.NotificationMessage) TestUtils.getNotificationMessage(org.sagebionetworks.bridge.TestUtils.getNotificationMessage) BadRequestException(org.sagebionetworks.bridge.exceptions.BadRequestException) Test(org.testng.annotations.Test)

Example 2 with NotificationMessage

use of org.sagebionetworks.bridge.models.notifications.NotificationMessage in project BridgeServer2 by Sage-Bionetworks.

the class NotificationsServiceTest method sendNotificationOK.

@Test
public void sendNotificationOK() {
    NotificationRegistration registration = getNotificationRegistration();
    registration.setEndpoint("endpointARN");
    List<NotificationRegistration> list = Lists.newArrayList(registration);
    doReturn(list).when(mockRegistrationDao).listRegistrations(HEALTH_CODE);
    doReturn(mockPublishResult).when(mockSnsClient).publish(any());
    NotificationMessage message = getNotificationMessage();
    service.sendNotificationToUser(TEST_APP_ID, HEALTH_CODE, message);
    verify(mockSnsClient).publish(requestCaptor.capture());
    PublishRequest request = requestCaptor.getValue();
    assertEquals(request.getSubject(), message.getSubject());
    assertEquals(request.getMessage(), message.getMessage());
    assertEquals(request.getTargetArn(), "endpointARN");
}
Also used : NotificationMessage(org.sagebionetworks.bridge.models.notifications.NotificationMessage) TestUtils.getNotificationMessage(org.sagebionetworks.bridge.TestUtils.getNotificationMessage) NotificationRegistration(org.sagebionetworks.bridge.models.notifications.NotificationRegistration) TestUtils.getNotificationRegistration(org.sagebionetworks.bridge.TestUtils.getNotificationRegistration) PublishRequest(com.amazonaws.services.sns.model.PublishRequest) Test(org.testng.annotations.Test)

Example 3 with NotificationMessage

use of org.sagebionetworks.bridge.models.notifications.NotificationMessage in project BridgeServer2 by Sage-Bionetworks.

the class NotificationsServiceTest method sendNotificationWithPartialErrors.

// Publish to two devices, where one device fails but the other sends to the user.
// Method succeeds but returns the GUID of the failed call for reporting back to the user.
@Test
public void sendNotificationWithPartialErrors() {
    NotificationRegistration reg1 = getNotificationRegistration();
    NotificationRegistration reg2 = getNotificationRegistration();
    reg2.setGuid("registrationGuid2");
    List<NotificationRegistration> list = Lists.newArrayList(reg1, reg2);
    doReturn(list).when(mockRegistrationDao).listRegistrations(HEALTH_CODE);
    when(mockSnsClient.publish(any())).thenReturn(mockPublishResult).thenThrow(new InvalidParameterException("bad parameter"));
    NotificationMessage message = getNotificationMessage();
    Set<String> erroredNotifications = service.sendNotificationToUser(TEST_APP_ID, HEALTH_CODE, message);
    assertEquals(erroredNotifications.size(), 1);
    assertEquals(Iterables.getFirst(erroredNotifications, null), "registrationGuid2");
}
Also used : InvalidParameterException(com.amazonaws.services.sns.model.InvalidParameterException) NotificationMessage(org.sagebionetworks.bridge.models.notifications.NotificationMessage) TestUtils.getNotificationMessage(org.sagebionetworks.bridge.TestUtils.getNotificationMessage) NotificationRegistration(org.sagebionetworks.bridge.models.notifications.NotificationRegistration) TestUtils.getNotificationRegistration(org.sagebionetworks.bridge.TestUtils.getNotificationRegistration) Test(org.testng.annotations.Test)

Example 4 with NotificationMessage

use of org.sagebionetworks.bridge.models.notifications.NotificationMessage in project BridgeServer2 by Sage-Bionetworks.

the class NotificationsServiceTest method sendNotificationAmazonExceptionConverted.

// Publish to two devices, where all the devices fail. This should throw an exception as nothing
// was successfully returned to the user.
@Test(expectedExceptions = BadRequestException.class)
public void sendNotificationAmazonExceptionConverted() {
    NotificationRegistration reg1 = getNotificationRegistration();
    NotificationRegistration reg2 = getNotificationRegistration();
    // This has to be different
    reg2.setGuid("registrationGuid2");
    List<NotificationRegistration> list = Lists.newArrayList(reg1, reg2);
    doReturn(list).when(mockRegistrationDao).listRegistrations(HEALTH_CODE);
    doThrow(new InvalidParameterException("bad parameter")).when(mockSnsClient).publish(any());
    NotificationMessage message = getNotificationMessage();
    service.sendNotificationToUser(TEST_APP_ID, HEALTH_CODE, message);
}
Also used : InvalidParameterException(com.amazonaws.services.sns.model.InvalidParameterException) NotificationMessage(org.sagebionetworks.bridge.models.notifications.NotificationMessage) TestUtils.getNotificationMessage(org.sagebionetworks.bridge.TestUtils.getNotificationMessage) NotificationRegistration(org.sagebionetworks.bridge.models.notifications.NotificationRegistration) TestUtils.getNotificationRegistration(org.sagebionetworks.bridge.TestUtils.getNotificationRegistration) Test(org.testng.annotations.Test)

Example 5 with NotificationMessage

use of org.sagebionetworks.bridge.models.notifications.NotificationMessage in project BridgeServer2 by Sage-Bionetworks.

the class NotificationTopicServiceTest method sendNotification.

@Test
public void sendNotification() {
    NotificationMessage message = TestUtils.getNotificationMessage();
    NotificationTopic topic = getNotificationTopic();
    topic.setTopicARN("topicARN");
    doReturn(topic).when(mockTopicDao).getTopic(TEST_APP_ID, "ABC-DEF");
    service.sendNotification(TEST_APP_ID, "ABC-DEF", message);
    verify(mockSnsClient).publish(publishRequestCaptor.capture());
    PublishRequest request = publishRequestCaptor.getValue();
    assertEquals(request.getSubject(), "a subject");
    assertEquals(request.getMessage(), "a message");
    assertEquals(request.getTopicArn(), "topicARN");
}
Also used : TestUtils.getNotificationTopic(org.sagebionetworks.bridge.TestUtils.getNotificationTopic) NotificationTopic(org.sagebionetworks.bridge.models.notifications.NotificationTopic) NotificationMessage(org.sagebionetworks.bridge.models.notifications.NotificationMessage) PublishRequest(com.amazonaws.services.sns.model.PublishRequest) Test(org.testng.annotations.Test)

Aggregations

NotificationMessage (org.sagebionetworks.bridge.models.notifications.NotificationMessage)31 Test (org.testng.annotations.Test)25 Errors (org.springframework.validation.Errors)12 StatusMessage (org.sagebionetworks.bridge.models.StatusMessage)6 TestUtils.getNotificationMessage (org.sagebionetworks.bridge.TestUtils.getNotificationMessage)4 TestUtils.getNotificationRegistration (org.sagebionetworks.bridge.TestUtils.getNotificationRegistration)3 UserSession (org.sagebionetworks.bridge.models.accounts.UserSession)3 NotificationRegistration (org.sagebionetworks.bridge.models.notifications.NotificationRegistration)3 PostMapping (org.springframework.web.bind.annotation.PostMapping)3 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)3 InvalidParameterException (com.amazonaws.services.sns.model.InvalidParameterException)2 PublishRequest (com.amazonaws.services.sns.model.PublishRequest)2 RequestContext (org.sagebionetworks.bridge.RequestContext)2 App (org.sagebionetworks.bridge.models.apps.App)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 TestUtils.getNotificationTopic (org.sagebionetworks.bridge.TestUtils.getNotificationTopic)1 BadRequestException (org.sagebionetworks.bridge.exceptions.BadRequestException)1 Account (org.sagebionetworks.bridge.models.accounts.Account)1 NotificationTopic (org.sagebionetworks.bridge.models.notifications.NotificationTopic)1 Notification (org.sagebionetworks.bridge.models.schedules2.Notification)1