Search in sources :

Example 1 with NotificationTemplateEvent

use of io.gravitee.rest.api.model.notification.NotificationTemplateEvent 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");
}
Also used : UpdateAlertTriggerEntity(io.gravitee.rest.api.model.alert.UpdateAlertTriggerEntity) SimpleEvent(io.gravitee.common.event.impl.SimpleEvent) NotificationTemplateEntity(io.gravitee.rest.api.model.notification.NotificationTemplateEntity) JsonNode(com.fasterxml.jackson.databind.JsonNode) UpdateAlertTriggerEntity(io.gravitee.rest.api.model.alert.UpdateAlertTriggerEntity) NewAlertTriggerEntity(io.gravitee.rest.api.model.alert.NewAlertTriggerEntity) AlertTriggerEntity(io.gravitee.rest.api.model.alert.AlertTriggerEntity) Notification(io.gravitee.notifier.api.Notification) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) NotificationTemplateEvent(io.gravitee.rest.api.model.notification.NotificationTemplateEvent) ApplicationAlertEventType(io.gravitee.rest.api.model.alert.ApplicationAlertEventType) Test(org.junit.Test)

Example 2 with NotificationTemplateEvent

use of io.gravitee.rest.api.model.notification.NotificationTemplateEvent in project gravitee-management-rest-api by gravitee-io.

the class ApplicationAlertServiceImpl method handleEvent.

@Override
@Async
public void handleEvent(Event<ApplicationAlertEventType, Object> event) {
    switch(event.type()) {
        case NOTIFICATION_TEMPLATE_UPDATE:
            final NotificationTemplateEvent notificationEvent = (NotificationTemplateEvent) event.content();
            final NotificationTemplateEntity notificationTemplate = notificationEvent.getNotificationTemplateEntity();
            final String organizationId = notificationEvent.getOrganizationId();
            if (notificationTemplate.getHook().equals(AlertHook.CONSUMER_HTTP_STATUS.name())) {
                updateAllAlertsBody(organizationId, STATUS_ALERT, notificationTemplate.getContent(), notificationTemplate.getTitle());
            } else if (notificationTemplate.getHook().equals(AlertHook.CONSUMER_RESPONSE_TIME.name())) {
                updateAllAlertsBody(organizationId, RESPONSE_TIME_ALERT, notificationTemplate.getContent(), notificationTemplate.getTitle());
            }
            break;
        case APPLICATION_MEMBERSHIP_UPDATE:
            updateAlertsRecipients((ApplicationAlertMembershipEvent) event.content());
            break;
    }
}
Also used : NotificationTemplateEvent(io.gravitee.rest.api.model.notification.NotificationTemplateEvent) NotificationTemplateEntity(io.gravitee.rest.api.model.notification.NotificationTemplateEntity) Async(org.springframework.scheduling.annotation.Async)

Example 3 with NotificationTemplateEvent

use of io.gravitee.rest.api.model.notification.NotificationTemplateEvent in project gravitee-management-rest-api by gravitee-io.

the class NotificationTemplateServiceImpl method updateFreemarkerCache.

private void updateFreemarkerCache(NotificationTemplateEntity notificationTemplate) {
    StringTemplateLoader orgCustomizedTemplatesLoader = stringTemplateLoaderMapByOrg.get(GraviteeContext.getCurrentOrganization());
    if (orgCustomizedTemplatesLoader == null) {
        Configuration orgFreemarkerConfiguration = this.initCurrentOrgFreemarkerConfiguration(GraviteeContext.getCurrentOrganization());
        freemarkerConfigurationByOrg.put(GraviteeContext.getCurrentOrganization(), orgFreemarkerConfiguration);
        orgCustomizedTemplatesLoader = stringTemplateLoaderMapByOrg.get(GraviteeContext.getCurrentOrganization());
    }
    if (notificationTemplate.isEnabled()) {
        // override the template in the loader
        if (notificationTemplate.getTitle() != null && !notificationTemplate.getTitle().isEmpty()) {
            orgCustomizedTemplatesLoader.putTemplate(notificationTemplate.getTitleTemplateName(), notificationTemplate.getTitle());
        }
        orgCustomizedTemplatesLoader.putTemplate(notificationTemplate.getContentTemplateName(), notificationTemplate.getContent());
    } else {
        // remove template so the template from file will be selected
        orgCustomizedTemplatesLoader.removeTemplate(notificationTemplate.getTitleTemplateName());
        orgCustomizedTemplatesLoader.removeTemplate(notificationTemplate.getContentTemplateName());
    }
    try {
        // force cache to be reloaded
        freemarkerConfigurationByOrg.get(GraviteeContext.getCurrentOrganization()).removeTemplateFromCache(notificationTemplate.getTitleTemplateName());
        freemarkerConfigurationByOrg.get(GraviteeContext.getCurrentOrganization()).removeTemplateFromCache(notificationTemplate.getName());
    } catch (IOException ex) {
        LOGGER.error("An error occurs while trying to update freemarker cache with this template {}", notificationTemplate, ex);
    }
    // Send an event to notify listener to reload template
    if (HookScope.TEMPLATES_FOR_ALERT.name().equals(notificationTemplate.getScope())) {
        eventManager.publishEvent(ApplicationAlertEventType.NOTIFICATION_TEMPLATE_UPDATE, new NotificationTemplateEvent(GraviteeContext.getCurrentOrganization(), notificationTemplate));
    }
}
Also used : StringTemplateLoader(freemarker.cache.StringTemplateLoader) NotificationTemplateEvent(io.gravitee.rest.api.model.notification.NotificationTemplateEvent) Configuration(freemarker.template.Configuration)

Example 4 with NotificationTemplateEvent

use of io.gravitee.rest.api.model.notification.NotificationTemplateEvent in project gravitee-management-rest-api by gravitee-io.

the class ApplicationAlertServiceTest method shouldHandleNotificationTemplateUpdatedEventEmptyNotification.

@Test
public void shouldHandleNotificationTemplateUpdatedEventEmptyNotification() 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));
    when(alertTrigger.getNotifications()).thenReturn(Collections.emptyList());
    cut.handleEvent(event);
    verify(alertService, times(0)).update(any());
}
Also used : ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) NotificationTemplateEvent(io.gravitee.rest.api.model.notification.NotificationTemplateEvent) SimpleEvent(io.gravitee.common.event.impl.SimpleEvent) ApplicationAlertEventType(io.gravitee.rest.api.model.alert.ApplicationAlertEventType) NotificationTemplateEntity(io.gravitee.rest.api.model.notification.NotificationTemplateEntity) UpdateAlertTriggerEntity(io.gravitee.rest.api.model.alert.UpdateAlertTriggerEntity) NewAlertTriggerEntity(io.gravitee.rest.api.model.alert.NewAlertTriggerEntity) AlertTriggerEntity(io.gravitee.rest.api.model.alert.AlertTriggerEntity) Test(org.junit.Test)

Example 5 with NotificationTemplateEvent

use of io.gravitee.rest.api.model.notification.NotificationTemplateEvent in project gravitee-management-rest-api by gravitee-io.

the class ApplicationAlertServiceTest method shouldHandleNotificationTemplateUpdatedEventStatusAlert.

@Test
public void shouldHandleNotificationTemplateUpdatedEventStatusAlert() throws Exception {
    NotificationTemplateEntity notificationTemplate = new NotificationTemplateEntity();
    notificationTemplate.setHook(AlertHook.CONSUMER_HTTP_STATUS.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_RATE");
    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");
}
Also used : UpdateAlertTriggerEntity(io.gravitee.rest.api.model.alert.UpdateAlertTriggerEntity) SimpleEvent(io.gravitee.common.event.impl.SimpleEvent) NotificationTemplateEntity(io.gravitee.rest.api.model.notification.NotificationTemplateEntity) JsonNode(com.fasterxml.jackson.databind.JsonNode) UpdateAlertTriggerEntity(io.gravitee.rest.api.model.alert.UpdateAlertTriggerEntity) NewAlertTriggerEntity(io.gravitee.rest.api.model.alert.NewAlertTriggerEntity) AlertTriggerEntity(io.gravitee.rest.api.model.alert.AlertTriggerEntity) Notification(io.gravitee.notifier.api.Notification) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) NotificationTemplateEvent(io.gravitee.rest.api.model.notification.NotificationTemplateEvent) ApplicationAlertEventType(io.gravitee.rest.api.model.alert.ApplicationAlertEventType) Test(org.junit.Test)

Aggregations

NotificationTemplateEvent (io.gravitee.rest.api.model.notification.NotificationTemplateEvent)5 NotificationTemplateEntity (io.gravitee.rest.api.model.notification.NotificationTemplateEntity)4 SimpleEvent (io.gravitee.common.event.impl.SimpleEvent)3 AlertTriggerEntity (io.gravitee.rest.api.model.alert.AlertTriggerEntity)3 ApplicationAlertEventType (io.gravitee.rest.api.model.alert.ApplicationAlertEventType)3 NewAlertTriggerEntity (io.gravitee.rest.api.model.alert.NewAlertTriggerEntity)3 UpdateAlertTriggerEntity (io.gravitee.rest.api.model.alert.UpdateAlertTriggerEntity)3 ApplicationListItem (io.gravitee.rest.api.model.application.ApplicationListItem)3 Test (org.junit.Test)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 Notification (io.gravitee.notifier.api.Notification)2 StringTemplateLoader (freemarker.cache.StringTemplateLoader)1 Configuration (freemarker.template.Configuration)1 Async (org.springframework.scheduling.annotation.Async)1