Search in sources :

Example 6 with NotificationRule

use of org.motechproject.admin.domain.NotificationRule in project motech by motech.

the class StatusMessageServiceTest method shouldSaveNotificationRules.

@Test
public void shouldSaveNotificationRules() {
    NotificationRule notificationRule1 = new NotificationRule("rec1", ActionType.EMAIL, Level.CRITICAL, "admin");
    NotificationRule notificationRule2 = new NotificationRule("rec2", ActionType.SMS, Level.CRITICAL, "admin");
    NotificationRule notificationRule3 = new NotificationRule("rec3", ActionType.SMS, Level.CRITICAL, "admin");
    notificationRule2.setId(1L);
    notificationRule3.setId(1L);
    when(notificationRulesDataService.findById(1L)).thenReturn(notificationRule3);
    statusMessageService.saveNotificationRules(asList(notificationRule1, notificationRule2));
    ArgumentCaptor<TransactionCallback> transactionCaptor = ArgumentCaptor.forClass(TransactionCallback.class);
    verify(notificationRulesDataService, times(2)).doInTransaction(transactionCaptor.capture());
    transactionCaptor.getAllValues().get(0).doInTransaction(null);
    transactionCaptor.getAllValues().get(1).doInTransaction(null);
    ArgumentCaptor<NotificationRule> captor = ArgumentCaptor.forClass(NotificationRule.class);
    verify(notificationRulesDataService).create(captor.capture());
    assertNull(captor.getValue().getId());
    assertEquals("rec1", captor.getValue().getRecipient());
    assertEquals(ActionType.EMAIL, captor.getValue().getActionType());
    verify(notificationRulesDataService).findById(1L);
    verify(notificationRulesDataService).update(captor.capture());
    assertEquals(1L, captor.getValue().getId().longValue());
    assertEquals("rec2", captor.getValue().getRecipient());
    assertEquals(ActionType.SMS, captor.getValue().getActionType());
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) NotificationRule(org.motechproject.admin.domain.NotificationRule) Test(org.junit.Test)

Example 7 with NotificationRule

use of org.motechproject.admin.domain.NotificationRule in project motech by motech.

the class StatusMessageServiceTest method shouldSaveANewRule.

@Test
public void shouldSaveANewRule() {
    NotificationRule notificationRule = new NotificationRule("rec", ActionType.SMS, Level.CRITICAL, "admin");
    statusMessageService.saveRule(notificationRule);
    ArgumentCaptor<NotificationRule> captor = ArgumentCaptor.forClass(NotificationRule.class);
    verify(notificationRulesDataService).create(captor.capture());
    verify(notificationRulesDataService, never()).update(any(NotificationRule.class));
    assertNull(captor.getValue().getId());
    assertEquals("rec", captor.getValue().getRecipient());
    assertEquals(ActionType.SMS, captor.getValue().getActionType());
}
Also used : NotificationRule(org.motechproject.admin.domain.NotificationRule) Test(org.junit.Test)

Example 8 with NotificationRule

use of org.motechproject.admin.domain.NotificationRule in project motech by motech.

the class StatusMessageServiceTest method shouldSendNotifications.

@Test
public void shouldSendNotifications() throws EmailSendException {
    NotificationRule notificationRuleEmail1 = new NotificationRule("e@ma.il", ActionType.EMAIL, Level.CRITICAL, null);
    NotificationRule notificationRuleEmail2 = new NotificationRule("e2@ma.il", ActionType.EMAIL, Level.DEBUG, "module");
    NotificationRule notificationRuleSms1 = new NotificationRule("1111", ActionType.SMS, Level.CRITICAL, "module");
    NotificationRule notificationRuleSms2 = new NotificationRule("2222", ActionType.SMS, Level.INFO, "");
    NotificationRule notificationRuleNotCatched = new NotificationRule("3333", ActionType.SMS, Level.CRITICAL, "other-module");
    when(notificationRulesDataService.retrieveAll()).thenReturn(asList(notificationRuleEmail1, notificationRuleSms1, notificationRuleSms2, notificationRuleEmail2, notificationRuleNotCatched));
    StatusMessage statusMessage = new StatusMessage("text", "module", Level.CRITICAL);
    statusMessageService.postMessage(statusMessage);
    verify(statusMessagesDataService).create(statusMessage);
    verify(notificationRulesDataService).retrieveAll();
    verify(emailNotifier).send(statusMessage, "e@ma.il");
    verify(emailNotifier).send(statusMessage, "e2@ma.il");
    ArgumentCaptor<MotechEvent> captor = ArgumentCaptor.forClass(MotechEvent.class);
    verify(eventRelay).sendEventMessage(captor.capture());
    assertEquals("send_sms", captor.getValue().getSubject());
    assertEquals(asList("1111", "2222"), captor.getValue().getParameters().get("recipients"));
    assertEquals("Motech CRITICAL message: [module] text", captor.getValue().getParameters().get("message"));
    verify(uiFrameworkService).moduleNeedsAttention("admin", "messages", "");
    verify(uiFrameworkService).moduleNeedsAttention("module", "text");
}
Also used : NotificationRule(org.motechproject.admin.domain.NotificationRule) MotechEvent(org.motechproject.event.MotechEvent) StatusMessage(org.motechproject.admin.domain.StatusMessage) Test(org.junit.Test)

Example 9 with NotificationRule

use of org.motechproject.admin.domain.NotificationRule in project motech by motech.

the class StatusMessageServiceTest method shouldRemoveANotificationRule.

@Test
public void shouldRemoveANotificationRule() {
    NotificationRule notificationRule = mock(NotificationRule.class);
    when(notificationRulesDataService.findById(1L)).thenReturn(notificationRule);
    statusMessageService.removeNotificationRule(1L);
    verify(notificationRulesDataService).delete(notificationRule);
}
Also used : NotificationRule(org.motechproject.admin.domain.NotificationRule) Test(org.junit.Test)

Example 10 with NotificationRule

use of org.motechproject.admin.domain.NotificationRule in project motech by motech.

the class StatusMessageServiceTest method shouldUpdateAnExistingRule.

@Test
public void shouldUpdateAnExistingRule() {
    NotificationRule notificationRule = new NotificationRule("rec", ActionType.SMS, Level.CRITICAL, "admin");
    NotificationRule existing = new NotificationRule("rec2", ActionType.EMAIL, Level.CRITICAL, "admin");
    notificationRule.setId(1L);
    existing.setId(1L);
    when(notificationRulesDataService.findById(1L)).thenReturn(existing);
    statusMessageService.saveRule(notificationRule);
    ArgumentCaptor<NotificationRule> captor = ArgumentCaptor.forClass(NotificationRule.class);
    verify(notificationRulesDataService).update(captor.capture());
    assertEquals(1L, captor.getValue().getId().longValue());
    assertEquals("rec", captor.getValue().getRecipient());
    assertEquals(ActionType.SMS, captor.getValue().getActionType());
}
Also used : NotificationRule(org.motechproject.admin.domain.NotificationRule) Test(org.junit.Test)

Aggregations

NotificationRule (org.motechproject.admin.domain.NotificationRule)10 Test (org.junit.Test)9 MotechEvent (org.motechproject.event.MotechEvent)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 StatusMessage (org.motechproject.admin.domain.StatusMessage)1 ActionType (org.motechproject.admin.messages.ActionType)1 Level (org.motechproject.admin.messages.Level)1 NotificationRuleDto (org.motechproject.admin.web.dto.NotificationRuleDto)1 EmailSendException (org.motechproject.email.exception.EmailSendException)1 TransactionCallback (org.springframework.transaction.support.TransactionCallback)1