Search in sources :

Example 11 with AlertTriggerEntity

use of io.gravitee.rest.api.model.alert.AlertTriggerEntity 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 12 with AlertTriggerEntity

use of io.gravitee.rest.api.model.alert.AlertTriggerEntity in project gravitee-management-rest-api by gravitee-io.

the class ApplicationAlertServiceTest method shouldAddMemberWhenNoOne.

@Test
public void shouldAddMemberWhenNoOne() throws Exception {
    AlertTriggerEntity trigger1 = mock(AlertTriggerEntity.class);
    when(trigger1.getNotifications()).thenReturn(Collections.emptyList());
    List<AlertTriggerEntity> triggers = new ArrayList<>();
    triggers.add(trigger1);
    when(alertService.findByReference(AlertReferenceType.APPLICATION, APPLICATION_ID)).thenReturn(triggers);
    when(applicationService.findById(APPLICATION_ID)).thenReturn(getApplication());
    cut.addMemberToApplication(APPLICATION_ID, "add@mail.gio");
    ArgumentCaptor<List<Notification>> notificationsCaptor = ArgumentCaptor.forClass(List.class);
    verify(trigger1, times(1)).setNotifications(notificationsCaptor.capture());
    final List<Notification> notifications = notificationsCaptor.getValue();
    assertThat(notifications.get(0).getConfiguration()).contains("\"add@mail.gio\"");
    verify(alertService, times(1)).update(any());
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) 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) Test(org.junit.Test)

Example 13 with AlertTriggerEntity

use of io.gravitee.rest.api.model.alert.AlertTriggerEntity 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)

Example 14 with AlertTriggerEntity

use of io.gravitee.rest.api.model.alert.AlertTriggerEntity in project gravitee-management-rest-api by gravitee-io.

the class ApplicationAlertServiceTest method shouldDeleteMember.

@Test
public void shouldDeleteMember() throws Exception {
    AlertTriggerEntity trigger1 = mock(AlertTriggerEntity.class);
    Notification notification = new Notification();
    notification.setType("default-email");
    notification.setConfiguration("");
    Notification notification2 = new Notification();
    notification2.setType("default-email");
    notification2.setConfiguration("");
    when(trigger1.getNotifications()).thenReturn(Collections.singletonList(notification));
    AlertTriggerEntity trigger2 = mock(AlertTriggerEntity.class);
    when(trigger2.getNotifications()).thenReturn(Collections.singletonList(notification2));
    List<AlertTriggerEntity> triggers = new ArrayList<>();
    triggers.add(trigger1);
    triggers.add(trigger2);
    when(alertService.findByReference(AlertReferenceType.APPLICATION, APPLICATION_ID)).thenReturn(triggers);
    JsonNode emailNode = JsonNodeFactory.instance.objectNode().put("to", "to,delete@mail.gio").put("from", "from").put("subject", "subject").put("body", "body");
    when(mapper.readTree(notification.getConfiguration())).thenReturn(emailNode);
    when(mapper.readTree(notification2.getConfiguration())).thenReturn(emailNode);
    cut.deleteMemberFromApplication(APPLICATION_ID, "delete@mail.gio");
    ArgumentCaptor<UpdateAlertTriggerEntity> updatingCaptor = ArgumentCaptor.forClass(UpdateAlertTriggerEntity.class);
    verify(alertService, times(2)).update(updatingCaptor.capture());
    final UpdateAlertTriggerEntity updating = updatingCaptor.getValue();
    assertThat(updating.getNotifications().get(0).getConfiguration()).contains("to");
    assertThat(updating.getNotifications().get(0).getConfiguration()).doesNotContain("delete@mail.gio");
}
Also used : UpdateAlertTriggerEntity(io.gravitee.rest.api.model.alert.UpdateAlertTriggerEntity) ArrayList(java.util.ArrayList) 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) Test(org.junit.Test)

Example 15 with AlertTriggerEntity

use of io.gravitee.rest.api.model.alert.AlertTriggerEntity in project gravitee-management-rest-api by gravitee-io.

the class ApplicationAlertServiceTest method shouldNotAddMemberMappingError.

@Test(expected = TechnicalManagementException.class)
public void shouldNotAddMemberMappingError() 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.addMemberToApplication(APPLICATION_ID, "add@mail.gio");
}
Also used : ArrayList(java.util.ArrayList) 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) Test(org.junit.Test)

Aggregations

AlertTriggerEntity (io.gravitee.rest.api.model.alert.AlertTriggerEntity)15 NewAlertTriggerEntity (io.gravitee.rest.api.model.alert.NewAlertTriggerEntity)14 UpdateAlertTriggerEntity (io.gravitee.rest.api.model.alert.UpdateAlertTriggerEntity)14 Test (org.junit.Test)12 Notification (io.gravitee.notifier.api.Notification)7 ArrayList (java.util.ArrayList)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 Alert (io.gravitee.rest.api.portal.rest.model.Alert)4 SimpleEvent (io.gravitee.common.event.impl.SimpleEvent)3 ApplicationAlertEventType (io.gravitee.rest.api.model.alert.ApplicationAlertEventType)3 ApplicationListItem (io.gravitee.rest.api.model.application.ApplicationListItem)3 NotificationTemplateEntity (io.gravitee.rest.api.model.notification.NotificationTemplateEntity)3 NotificationTemplateEvent (io.gravitee.rest.api.model.notification.NotificationTemplateEvent)3 RateCondition (io.gravitee.alert.api.condition.RateCondition)2 ThresholdRangeCondition (io.gravitee.alert.api.condition.ThresholdRangeCondition)2 Permissions (io.gravitee.rest.api.portal.rest.security.Permissions)2 Consumes (javax.ws.rs.Consumes)2 Produces (javax.ws.rs.Produces)2 AggregationCondition (io.gravitee.alert.api.condition.AggregationCondition)1 ApplicationAlertMaximumException (io.gravitee.rest.api.service.exceptions.ApplicationAlertMaximumException)1