Search in sources :

Example 1 with NotificationRule

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

the class StatusMessageServiceImpl method sendNotifications.

private void sendNotifications(StatusMessage message) {
    List<String> smsRecipients = new ArrayList<>();
    for (NotificationRule notificationRule : notificationRulesDataService.retrieveAll()) {
        if (notificationRule.matches(message)) {
            if (notificationRule.getActionType() == ActionType.SMS) {
                smsRecipients.add(notificationRule.getRecipient());
            } else if (notificationRule.getActionType() == ActionType.EMAIL) {
                try {
                    emailNotifier.send(message, notificationRule.getRecipient());
                } catch (EmailSendException e) {
                    LOGGER.error("Error while sending notification email to {}", notificationRule.getRecipient(), e);
                }
            }
        }
    }
    if (!smsRecipients.isEmpty()) {
        Map<String, Object> params = new HashMap<>();
        params.put("recipients", smsRecipients);
        params.put("message", String.format("Motech %s message: [%s] %s", message.getLevel(), message.getModuleName(), message.getText()));
        MotechEvent smsEvent = new MotechEvent("send_sms", params);
        eventRelay.sendEventMessage(smsEvent);
    }
}
Also used : HashMap(java.util.HashMap) NotificationRule(org.motechproject.admin.domain.NotificationRule) ArrayList(java.util.ArrayList) EmailSendException(org.motechproject.email.exception.EmailSendException) MotechEvent(org.motechproject.event.MotechEvent)

Example 2 with NotificationRule

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

the class MessageControllerTest method shouldHandleNotificationDto.

@Test
public void shouldHandleNotificationDto() {
    NotificationRule notificationRule1 = mock(NotificationRule.class);
    NotificationRule notificationRule2 = mock(NotificationRule.class);
    List<NotificationRule> ruleList = asList(notificationRule1, notificationRule2);
    NotificationRuleDto notificationRuleDto = new NotificationRuleDto();
    notificationRuleDto.setNotificationRules(ruleList);
    notificationRuleDto.setIdsToRemove(asList("id1", "id2"));
    controller.saveNotificationRules(notificationRuleDto);
    verify(statusMessageService).saveNotificationRules(ruleList);
    verify(statusMessageService).removeNotificationRule("id1");
    verify(statusMessageService).removeNotificationRule("id2");
}
Also used : NotificationRuleDto(org.motechproject.admin.web.dto.NotificationRuleDto) NotificationRule(org.motechproject.admin.domain.NotificationRule) Test(org.junit.Test)

Example 3 with NotificationRule

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

the class NotificationRulesDataServiceBundleIT method shouldPerformCrudOperations.

@Test
public void shouldPerformCrudOperations() {
    NotificationRule notificationRule = new NotificationRule("recip", ActionType.EMAIL, Level.CRITICAL, "admin");
    NotificationRule notificationRule2 = new NotificationRule("recip2", ActionType.SMS, Level.INFO, "tasks");
    dataService.create(notificationRule);
    dataService.create(notificationRule2);
    List<NotificationRule> notificationRules = dataService.retrieveAll();
    List<String> actual = extract(notificationRules, on(NotificationRule.class).getRecipient());
    assertThat(actual, hasItems("recip", "recip2"));
    List<ActionType> actualActions = extract(notificationRules, on(NotificationRule.class).getActionType());
    assertThat(actualActions, hasItems(ActionType.EMAIL, ActionType.SMS));
    actual = extract(notificationRules, on(NotificationRule.class).getModuleName());
    assertThat(actual, hasItems("admin", "tasks"));
    List<Level> actualLevels = extract(notificationRules, on(NotificationRule.class).getLevel());
    assertThat(actualLevels, hasItems(Level.CRITICAL, Level.INFO));
    notificationRule.setRecipient("recip3");
    dataService.update(notificationRule);
    notificationRules = dataService.retrieveAll();
    actual = extract(notificationRules, on(NotificationRule.class).getRecipient());
    assertThat(actual, hasItems("recip3", "recip2"));
    actualActions = extract(notificationRules, on(NotificationRule.class).getActionType());
    assertThat(actualActions, hasItems(ActionType.EMAIL, ActionType.SMS));
    dataService.delete(notificationRule);
    notificationRules = dataService.retrieveAll();
    assertEquals(asList("recip2"), extract(notificationRules, on(NotificationRule.class).getRecipient()));
    assertEquals(asList(ActionType.SMS), extract(notificationRules, on(NotificationRule.class).getActionType()));
    assertEquals(asList("tasks"), extract(notificationRules, on(NotificationRule.class).getModuleName()));
    assertEquals(asList(Level.INFO), extract(notificationRules, on(NotificationRule.class).getLevel()));
}
Also used : ActionType(org.motechproject.admin.messages.ActionType) NotificationRule(org.motechproject.admin.domain.NotificationRule) Level(org.motechproject.admin.messages.Level) Test(org.junit.Test)

Example 4 with NotificationRule

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

the class MessageControllerTest method shouldReturnNotificationRulesList.

@Test
public void shouldReturnNotificationRulesList() {
    NotificationRule notificationRule = new NotificationRule();
    notificationRule.setActionType(ActionType.SMS);
    notificationRule.setRecipient("rec1");
    NotificationRule notificationRule2 = new NotificationRule();
    notificationRule2.setActionType(ActionType.EMAIL);
    notificationRule.setRecipient("rec2");
    when(statusMessageService.getNotificationRules()).thenReturn(asList(notificationRule, notificationRule2));
    List<NotificationRule> result = controller.getNotificationRules();
    assertEquals(asList(notificationRule, notificationRule2), result);
    verify(statusMessageService).getNotificationRules();
}
Also used : NotificationRule(org.motechproject.admin.domain.NotificationRule) Test(org.junit.Test)

Example 5 with NotificationRule

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

the class MessageControllerTest method shouldAddANotificationRule.

@Test
public void shouldAddANotificationRule() {
    NotificationRule notificationRule = new NotificationRule();
    notificationRule.setActionType(ActionType.EMAIL);
    notificationRule.setRecipient("test");
    controller.addRule(notificationRule);
    verify(statusMessageService).saveRule(notificationRule);
}
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