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