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);
}
}
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");
}
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()));
}
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();
}
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);
}
Aggregations