Search in sources :

Example 11 with NotificationTemplateEntity

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

the class NotificationTemplateServiceTest method shouldFindNotificationTemplate.

@Test
public void shouldFindNotificationTemplate() throws TechnicalException {
    when(notificationTemplateRepository.findById(NOTIFICATION_TEMPLATE_ID)).thenReturn(Optional.of(notificationTemplate));
    final NotificationTemplateEntity foundTemplate = notificationTemplateService.findById(NOTIFICATION_TEMPLATE_ID);
    assertNotNull(foundTemplate);
    assertEquals(notificationTemplate.getId(), foundTemplate.getId());
    assertEquals(notificationTemplate.getHook(), foundTemplate.getHook());
    assertEquals(notificationTemplate.getScope(), foundTemplate.getScope());
    assertEquals(notificationTemplate.getName(), foundTemplate.getName());
    assertEquals(notificationTemplate.getDescription(), foundTemplate.getDescription());
    assertEquals(notificationTemplate.getTitle(), foundTemplate.getTitle());
    assertEquals(notificationTemplate.getContent(), foundTemplate.getContent());
    assertEquals(notificationTemplate.getType().name(), foundTemplate.getType().name());
    assertEquals(notificationTemplate.getCreatedAt(), foundTemplate.getCreatedAt());
    assertEquals(notificationTemplate.getUpdatedAt(), foundTemplate.getUpdatedAt());
}
Also used : NotificationTemplateEntity(io.gravitee.rest.api.model.notification.NotificationTemplateEntity) Test(org.junit.Test)

Example 12 with NotificationTemplateEntity

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

the class NotificationTemplateServiceTest method shouldUpdateNotificationTemplate.

@Test
public void shouldUpdateNotificationTemplate() throws TechnicalException {
    NotificationTemplateEntity updatingNotificationTemplateEntity = new NotificationTemplateEntity();
    updatingNotificationTemplateEntity.setId(NOTIFICATION_TEMPLATE_ID);
    updatingNotificationTemplateEntity.setName("New Name");
    updatingNotificationTemplateEntity.setType(io.gravitee.rest.api.model.notification.NotificationTemplateType.valueOf(NOTIFICATION_TEMPLATE_TYPE.name()));
    updatingNotificationTemplateEntity.setEnabled(NOTIFICATION_TEMPLATE_ENABLED);
    final NotificationTemplate toUpdate = mock(NotificationTemplate.class);
    when(notificationTemplateRepository.findById(NOTIFICATION_TEMPLATE_ID)).thenReturn(Optional.of(toUpdate));
    when(notificationTemplateRepository.update(any())).thenReturn(notificationTemplate);
    notificationTemplateService.update(updatingNotificationTemplateEntity);
    verify(notificationTemplateRepository, times(1)).update(any());
    verify(auditService, times(1)).createOrganizationAuditLog(any(), eq(NotificationTemplate.AuditEvent.NOTIFICATION_TEMPLATE_UPDATED), any(), eq(toUpdate), eq(notificationTemplate));
}
Also used : NotificationTemplateEntity(io.gravitee.rest.api.model.notification.NotificationTemplateEntity) NotificationTemplate(io.gravitee.repository.management.model.NotificationTemplate) Test(org.junit.Test)

Example 13 with NotificationTemplateEntity

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

the class NotificationTemplateServiceImpl method loadEmailNotificationTemplateFromFile.

private NotificationTemplateEntity loadEmailNotificationTemplateFromFile(Path fileTemplateName) {
    String templateName = fileTemplateName.getFileName().toString();
    try {
        File templateFile = fileTemplateName.toFile();
        String content = new String(Files.readAllBytes(templateFile.toPath()));
        return new NotificationTemplateEntity("", TEMPLATES_TO_INCLUDE_SCOPE, templateName, templateName, null, null, content, io.gravitee.rest.api.model.notification.NotificationTemplateType.EMAIL);
    } catch (IOException e) {
        LOGGER.warn("Problem while getting freemarker template {} from file : {}", templateName, e.getMessage());
    }
    return null;
}
Also used : NotificationTemplateEntity(io.gravitee.rest.api.model.notification.NotificationTemplateEntity) UuidString(io.gravitee.rest.api.service.common.UuidString)

Example 14 with NotificationTemplateEntity

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

the class NotificationTemplateServiceImpl method update.

@Override
public NotificationTemplateEntity update(NotificationTemplateEntity updatingNotificationTemplate) {
    try {
        LOGGER.debug("Update notificationTemplate {}", updatingNotificationTemplate);
        if (updatingNotificationTemplate.getUpdatedAt() == null) {
            updatingNotificationTemplate.setUpdatedAt(new Date());
        }
        Optional<NotificationTemplate> optNotificationTemplate = notificationTemplateRepository.findById(updatingNotificationTemplate.getId());
        NotificationTemplate notificationTemplateToUpdate = optNotificationTemplate.orElseThrow(() -> new NotificationTemplateNotFoundException(updatingNotificationTemplate.getId()));
        notificationTemplateToUpdate.setTitle(updatingNotificationTemplate.getTitle());
        notificationTemplateToUpdate.setContent(updatingNotificationTemplate.getContent());
        notificationTemplateToUpdate.setEnabled(updatingNotificationTemplate.isEnabled());
        NotificationTemplate updatedNotificationTemplate = notificationTemplateRepository.update(notificationTemplateToUpdate);
        createAuditLog(NotificationTemplate.AuditEvent.NOTIFICATION_TEMPLATE_UPDATED, updatingNotificationTemplate.getUpdatedAt(), optNotificationTemplate.get(), updatedNotificationTemplate);
        final NotificationTemplateEntity updatedNotificationTemplateEntity = convert(updatedNotificationTemplate);
        // Update template in loader cache
        updateFreemarkerCache(updatedNotificationTemplateEntity);
        return updatedNotificationTemplateEntity;
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to create or update notificationTemplate {}", updatingNotificationTemplate, ex);
        throw new TechnicalManagementException("An error occurs while trying to create or update " + updatingNotificationTemplate, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) NotificationTemplateNotFoundException(io.gravitee.rest.api.service.exceptions.NotificationTemplateNotFoundException) NotificationTemplateEntity(io.gravitee.rest.api.model.notification.NotificationTemplateEntity) NotificationTemplate(io.gravitee.repository.management.model.NotificationTemplate) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 15 with NotificationTemplateEntity

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

the class NotificationTemplateServiceImpl method loadPortalNotificationTemplateFromHook.

private NotificationTemplateEntity loadPortalNotificationTemplateFromHook(Hook hook) {
    final String RELATIVE_TPL_PATH = "notifications/portal/";
    String templateName = hook.getTemplate() + "." + io.gravitee.rest.api.model.notification.NotificationTemplateType.PORTAL.name();
    File portalTemplateFile = new File(templatesPath + "/" + RELATIVE_TPL_PATH + hook.getTemplate() + ".yml");
    try {
        Yaml yaml = new Yaml();
        Map<String, String> load = yaml.load(new FileInputStream(portalTemplateFile));
        return new NotificationTemplateEntity(hook.name(), hook.getScope().name(), templateName, hook.getLabel(), hook.getDescription(), load.get("title"), load.get("message"), io.gravitee.rest.api.model.notification.NotificationTemplateType.PORTAL);
    } catch (IOException e) {
        LOGGER.warn("Problem while getting freemarker template {} from file : {}", hook.getTemplate(), e.getMessage());
        return null;
    }
}
Also used : NotificationTemplateEntity(io.gravitee.rest.api.model.notification.NotificationTemplateEntity) UuidString(io.gravitee.rest.api.service.common.UuidString) Yaml(org.yaml.snakeyaml.Yaml)

Aggregations

NotificationTemplateEntity (io.gravitee.rest.api.model.notification.NotificationTemplateEntity)16 Test (org.junit.Test)8 NotificationTemplate (io.gravitee.repository.management.model.NotificationTemplate)5 NotificationTemplateEvent (io.gravitee.rest.api.model.notification.NotificationTemplateEvent)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 UuidString (io.gravitee.rest.api.service.common.UuidString)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 Notification (io.gravitee.notifier.api.Notification)2 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)2 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)2 EmailTemplate (io.gravitee.rest.api.service.builder.EmailNotificationBuilder.EmailTemplate)1 NotificationTemplateNotFoundException (io.gravitee.rest.api.service.exceptions.NotificationTemplateNotFoundException)1 Async (org.springframework.scheduling.annotation.Async)1 Yaml (org.yaml.snakeyaml.Yaml)1