use of io.gravitee.notifier.api.Notification in project gravitee-management-rest-api by gravitee-io.
the class ApplicationAlertServiceTest method shouldHandleNotificationTemplateUpdatedEventResponseTimeAlert.
@Test
public void shouldHandleNotificationTemplateUpdatedEventResponseTimeAlert() throws Exception {
NotificationTemplateEntity notificationTemplate = new NotificationTemplateEntity();
notificationTemplate.setHook(AlertHook.CONSUMER_RESPONSE_TIME.name());
notificationTemplate.setTitle("notification-template-title");
notificationTemplate.setContent("notification-template-content");
NotificationTemplateEvent notificationTemplateEvent = new NotificationTemplateEvent("org-id", notificationTemplate);
final SimpleEvent<ApplicationAlertEventType, Object> event = new SimpleEvent<>(ApplicationAlertEventType.NOTIFICATION_TEMPLATE_UPDATE, notificationTemplateEvent);
ApplicationListItem app1 = new ApplicationListItem();
app1.setId("app1");
ApplicationListItem app2 = new ApplicationListItem();
app2.setId("app2");
when(applicationService.findByOrganization("org-id")).thenReturn(new HashSet<>(Arrays.asList(app1, app2)));
final AlertTriggerEntity alertTrigger = mock(AlertTriggerEntity.class);
when(alertTrigger.getType()).thenReturn("METRICS_AGGREGATION");
when(alertService.findByReferenceAndReferenceIds(AlertReferenceType.APPLICATION, Arrays.asList("app1", "app2"))).thenReturn(Collections.singletonList(alertTrigger));
Notification notification = new Notification();
notification.setType("default-email");
notification.setConfiguration("");
when(alertTrigger.getNotifications()).thenReturn(Collections.singletonList(notification));
JsonNode emailNode = JsonNodeFactory.instance.objectNode().put("to", "to").put("from", "from").put("subject", "subject").put("body", "body");
when(mapper.readTree(notification.getConfiguration())).thenReturn(emailNode);
cut.handleEvent(event);
ArgumentCaptor<UpdateAlertTriggerEntity> updatingCaptor = ArgumentCaptor.forClass(UpdateAlertTriggerEntity.class);
verify(alertService, times(1)).update(updatingCaptor.capture());
final UpdateAlertTriggerEntity updating = updatingCaptor.getValue();
assertThat(updating.getNotifications().get(0).getConfiguration()).contains("notification-template-content");
assertThat(updating.getNotifications().get(0).getConfiguration()).contains("notification-template-title");
}
use of io.gravitee.notifier.api.Notification in project gravitee-management-rest-api by gravitee-io.
the class ApplicationAlertServiceImpl method createNotification.
private List<Notification> createNotification(String alertType, List<String> recipients) {
try {
Notification notification = new Notification();
notification.setType(DEFAULT_EMAIL_NOTIFIER);
ObjectNode configuration = mapper.createObjectNode();
configuration.put("from", emailFrom);
configuration.put("to", String.join(",", recipients));
generateNotificationBodyFromTemplate(configuration, alertType);
notification.setPeriods(emptyList());
notification.setConfiguration(mapper.writeValueAsString(configuration));
return singletonList(notification);
} catch (JsonProcessingException e) {
LOGGER.error("An error occurs while trying to create the Alert notification", e);
throw new TechnicalManagementException("An error occurs while trying to create the Alert notification");
}
}
use of io.gravitee.notifier.api.Notification in project gravitee-management-rest-api by gravitee-io.
the class ApplicationAlertServiceImpl method addMemberToApplication.
@Override
public void addMemberToApplication(String applicationId, String email) {
if (StringUtils.isEmpty(email)) {
return;
}
// check existence of application
applicationService.findById(applicationId);
alertService.findByReference(AlertReferenceType.APPLICATION, applicationId).forEach(trigger -> {
if (trigger.getNotifications() == null) {
trigger.setNotifications(createNotification(trigger.getType()));
}
final Optional<Notification> notificationOpt = trigger.getNotifications().stream().filter(n -> DEFAULT_EMAIL_NOTIFIER.equals(n.getType())).findFirst();
if (notificationOpt.isPresent()) {
Notification notification = notificationOpt.get();
try {
ObjectNode configuration = mapper.createObjectNode();
JsonNode emailNode = mapper.readTree(notification.getConfiguration());
configuration.put("to", emailNode.path("to").asText() + "," + email);
configuration.put("from", emailNode.path("from").asText());
configuration.put("subject", emailNode.path("subject").asText());
configuration.put("body", emailNode.path("body").asText());
notification.setConfiguration(mapper.writeValueAsString(configuration));
} catch (JsonProcessingException e) {
LOGGER.error("An error occurs while trying to add a recipient to the Alert notification", e);
throw new TechnicalManagementException("An error occurs while trying to add a recipient to the Alert notification");
}
} else {
trigger.setNotifications(createNotification(trigger.getType(), singletonList(email)));
}
alertService.update(convert(trigger));
});
}
use of io.gravitee.notifier.api.Notification in project gravitee-management-rest-api by gravitee-io.
the class ApplicationAlertServiceImpl method deleteMemberFromApplication.
@Override
public void deleteMemberFromApplication(String applicationId, String email) {
if (StringUtils.isEmpty(email)) {
return;
}
// check existence of application
applicationService.findById(applicationId);
alertService.findByReference(AlertReferenceType.APPLICATION, applicationId).forEach(trigger -> {
if (trigger.getNotifications() == null) {
trigger.setNotifications(createNotification(trigger.getType()));
}
final Optional<Notification> notificationOpt = trigger.getNotifications().stream().filter(n -> DEFAULT_EMAIL_NOTIFIER.equals(n.getType())).findFirst();
if (notificationOpt.isPresent()) {
final Notification notification = notificationOpt.get();
try {
ObjectNode configuration = mapper.createObjectNode();
JsonNode emailNode = mapper.readTree(notification.getConfiguration());
final String to = Arrays.stream(emailNode.path("to").asText().split(",|;|\\s")).filter(mailTo -> !mailTo.equals(email)).collect(Collectors.joining(","));
if (StringUtils.isEmpty(to)) {
trigger.setNotifications(emptyList());
} else {
configuration.put("to", to);
configuration.put("from", emailNode.path("from").asText());
configuration.put("subject", emailNode.path("subject").asText());
configuration.put("body", emailNode.path("body").asText());
notification.setConfiguration(mapper.writeValueAsString(configuration));
}
alertService.update(convert(trigger));
} catch (JsonProcessingException e) {
LOGGER.error("An error occurs while trying to add a recipient to the Alert notification", e);
throw new TechnicalManagementException("An error occurs while trying to add a recipient to the Alert notification");
}
}
});
}
use of io.gravitee.notifier.api.Notification in project gravitee-management-rest-api by gravitee-io.
the class ApplicationAlertServiceTest method shouldNotDeleteMemberMappingError.
@Test(expected = TechnicalManagementException.class)
public void shouldNotDeleteMemberMappingError() throws Exception {
AlertTriggerEntity trigger1 = mock(AlertTriggerEntity.class);
Notification notification = new Notification();
notification.setType("default-email");
notification.setConfiguration("");
when(trigger1.getNotifications()).thenReturn(Collections.singletonList(notification));
List<AlertTriggerEntity> triggers = new ArrayList<>();
triggers.add(trigger1);
when(alertService.findByReference(AlertReferenceType.APPLICATION, APPLICATION_ID)).thenReturn(triggers);
when(mapper.readTree(notification.getConfiguration())).thenThrow(JsonProcessingException.class);
cut.deleteMemberFromApplication(APPLICATION_ID, "delete@mail.gio");
}
Aggregations