use of com.redhat.cloud.notifications.models.InstantEmailTemplate in project notifications-backend by RedHatInsights.
the class TemplateResourceTest method buildInstantEmailTemplate.
private static InstantEmailTemplate buildInstantEmailTemplate(String eventTypeId, String subjectTemplateId, String bodyTemplateId) {
InstantEmailTemplate emailTemplate = new InstantEmailTemplate();
if (eventTypeId != null) {
emailTemplate.setEventTypeId(UUID.fromString(eventTypeId));
}
emailTemplate.setSubjectTemplateId(UUID.fromString(subjectTemplateId));
emailTemplate.setBodyTemplateId(UUID.fromString(bodyTemplateId));
return emailTemplate;
}
use of com.redhat.cloud.notifications.models.InstantEmailTemplate in project notifications-backend by RedHatInsights.
the class TemplateResourceTest method testAllInstantEmailTemplateEndpoints.
@Test
void testAllInstantEmailTemplateEndpoints() {
Header adminIdentity = TestHelpers.createTurnpikeIdentityHeader("user", adminRole);
// First, we need a bundle, an app and an event type in the DB.
String bundleId = createBundle(adminIdentity, "bundle-name", "Bundle", OK).get();
String appId = createApp(adminIdentity, bundleId, "app-name", "App", null, OK).get();
String eventTypeId = createEventType(adminIdentity, appId, "event-type-name", "Event type", "Event type description", OK).get();
String appId2 = createApp(adminIdentity, bundleId, "app-name-2", "App2", null, OK).get();
String eventTypeId2 = createEventType(adminIdentity, appId2, "event-type-name-2", "Event type 2", "Event type description", OK).get();
// We also need templates that will be linked with the instant email template tested below.
Template subjectTemplate = buildTemplate("subject-template-name", "template-data");
JsonObject subjectJsonTemplate = createTemplate(adminIdentity, subjectTemplate, 200).get();
Template bodyTemplate = buildTemplate("body-template-name", "template-data");
JsonObject bodyJsonTemplate = createTemplate(adminIdentity, bodyTemplate, 200).get();
// Before we start, the DB shouldn't contain any instant email template.
assertTrue(getAllInstantEmailTemplates(adminIdentity).isEmpty());
// First, we'll create, retrieve and check an instant email template that is NOT linked to an event type.
InstantEmailTemplate emailTemplate = buildInstantEmailTemplate(null, subjectJsonTemplate.getString("id"), bodyJsonTemplate.getString("id"));
JsonObject jsonEmailTemplate = createInstantEmailTemplate(adminIdentity, emailTemplate, 200).get();
// Then, we'll do the same with another instant email template that is linked to an event type.
InstantEmailTemplate emailTemplateWithEventType = buildInstantEmailTemplate(eventTypeId, subjectJsonTemplate.getString("id"), bodyJsonTemplate.getString("id"));
JsonObject jsonEmailTemplateWithEventType = createInstantEmailTemplate(adminIdentity, emailTemplateWithEventType, 200).get();
// Then, we'll do the same with another instant email template that is linked to event type 2.
InstantEmailTemplate emailTemplateWithEventType2 = buildInstantEmailTemplate(eventTypeId2, subjectJsonTemplate.getString("id"), bodyJsonTemplate.getString("id"));
JsonObject jsonEmailTemplateWithEventType2 = createInstantEmailTemplate(adminIdentity, emailTemplateWithEventType2, 200).get();
// At this point, the DB should contain 3 instant email template: the one that wasn't linked to the event type and 2 linked to an event type.
JsonArray jsonEmailTemplates = getAllInstantEmailTemplates(adminIdentity);
assertEquals(3, jsonEmailTemplates.size());
// Querying for the specific application (appId2) should yield only 1
jsonEmailTemplates = getAllInstantEmailTemplates(adminIdentity, appId2);
assertEquals(1, jsonEmailTemplates.size());
jsonEmailTemplates.getJsonObject(0).mapTo(InstantEmailTemplate.class);
assertEquals(jsonEmailTemplateWithEventType2.getString("id"), jsonEmailTemplates.getJsonObject(0).getString("id"));
// Now, we'll delete the second instant email template and check that it no longer exists with the following line.
deleteInstantEmailTemplate(adminIdentity, jsonEmailTemplateWithEventType.getString("id"));
// Now, we'll delete the third instant email template and check that it no longer exists with the following line.
deleteInstantEmailTemplate(adminIdentity, jsonEmailTemplateWithEventType2.getString("id"));
// At this point, the DB should contain one instant email template: the one that wasn't linked to the event type.
jsonEmailTemplates = getAllInstantEmailTemplates(adminIdentity);
assertEquals(1, jsonEmailTemplates.size());
jsonEmailTemplates.getJsonObject(0).mapTo(InstantEmailTemplate.class);
assertEquals(jsonEmailTemplate.getString("id"), jsonEmailTemplates.getJsonObject(0).getString("id"));
// Retrieving the instant email templates from the event type ID should return an empty list.
getInstantEmailTemplatesByEventType(adminIdentity, eventTypeId, false);
// Let's update the instant email template and check that the new fields values are correctly persisted.
Template newSubjectTemplate = buildTemplate("new-subject-template-name", "template-data");
JsonObject newSubjectJsonTemplate = createTemplate(adminIdentity, newSubjectTemplate, 200).get();
Template newBodyTemplate = buildTemplate("new-body-template-name", "template-data");
JsonObject newBodyJsonTemplate = createTemplate(adminIdentity, newBodyTemplate, 200).get();
emailTemplate.setEventTypeId(UUID.fromString(eventTypeId));
emailTemplate.setSubjectTemplateId(UUID.fromString(newSubjectJsonTemplate.getString("id")));
emailTemplate.setBodyTemplateId(UUID.fromString(newBodyJsonTemplate.getString("id")));
updateInstantEmailTemplate(adminIdentity, jsonEmailTemplate.getString("id"), emailTemplate);
// The instant email template is now linked to an event type.
// It should be returned if we retrieve all instant email templates from the event type ID.
JsonObject jsonEmailTemplate2 = getInstantEmailTemplatesByEventType(adminIdentity, eventTypeId, true);
jsonEmailTemplate2.mapTo(InstantEmailTemplate.class);
assertEquals(jsonEmailTemplate.getString("id"), jsonEmailTemplate2.getString("id"));
}
use of com.redhat.cloud.notifications.models.InstantEmailTemplate in project notifications-backend by RedHatInsights.
the class ResourceHelpers method createInstantEmailTemplate.
@Transactional
public InstantEmailTemplate createInstantEmailTemplate(UUID eventTypeId, UUID subjectTemplateId, UUID bodyTemplateId, boolean enabled) {
InstantEmailTemplate instantEmailTemplate = new InstantEmailTemplate();
instantEmailTemplate.setEventType(entityManager.find(EventType.class, eventTypeId));
instantEmailTemplate.setEventTypeId(eventTypeId);
instantEmailTemplate.setSubjectTemplate(entityManager.find(Template.class, subjectTemplateId));
instantEmailTemplate.setSubjectTemplateId(subjectTemplateId);
instantEmailTemplate.setBodyTemplate(entityManager.find(Template.class, bodyTemplateId));
instantEmailTemplate.setBodyTemplateId(bodyTemplateId);
entityManager.persist(instantEmailTemplate);
return instantEmailTemplate;
}
use of com.redhat.cloud.notifications.models.InstantEmailTemplate in project notifications-backend by RedHatInsights.
the class EmailTemplateMigrationService method createInstantEmailTemplate.
/*
* Creates an instant email template and the underlying templates only if:
* - the event type exists in the DB
* - the instant email template does not already exist in the DB
* Existing instant email templates are never updated by this migration service.
*/
private void createInstantEmailTemplate(List<String> warnings, String bundleName, String appName, List<String> eventTypeNames, String subjectTemplateName, String subjectTemplateExtension, String subjectTemplateDescription, String bodyTemplateName, String bodyTemplateExtension, String bodyTemplateDescription) {
for (String eventTypeName : eventTypeNames) {
Optional<EventType> eventType = findEventType(warnings, bundleName, appName, eventTypeName);
if (eventType.isPresent()) {
if (instantEmailTemplateExists(eventType.get())) {
warnings.add(String.format("Instant email template found in DB for event type: %s/%s/%s", bundleName, appName, eventTypeName));
} else {
Template subjectTemplate = getOrCreateTemplate(warnings, subjectTemplateName, subjectTemplateExtension, subjectTemplateDescription);
Template bodyTemplate = getOrCreateTemplate(warnings, bodyTemplateName, bodyTemplateExtension, bodyTemplateDescription);
LOGGER.infof("Creating instant email template for event type: %s/%s/%s", bundleName, appName, eventTypeName);
InstantEmailTemplate emailTemplate = new InstantEmailTemplate();
emailTemplate.setEventType(eventType.get());
emailTemplate.setSubjectTemplate(subjectTemplate);
emailTemplate.setSubjectTemplateId(subjectTemplate.getId());
emailTemplate.setBodyTemplate(bodyTemplate);
emailTemplate.setBodyTemplateId(bodyTemplate.getId());
entityManager.persist(emailTemplate);
}
}
}
}
use of com.redhat.cloud.notifications.models.InstantEmailTemplate in project notifications-backend by RedHatInsights.
the class TemplateRepository method findInstantEmailTemplateByEventType.
public InstantEmailTemplate findInstantEmailTemplateByEventType(UUID eventTypeId) {
try {
String hql = "FROM InstantEmailTemplate t JOIN FETCH t.eventType et WHERE et.id = :eventTypeId";
InstantEmailTemplate instantEmailTemplate = entityManager.createQuery(hql, InstantEmailTemplate.class).setParameter("eventTypeId", eventTypeId).getSingleResult();
// The full event types aren't needed in the REST response.
instantEmailTemplate.filterOutEventType();
// The full templates aren't needed in the REST response.
instantEmailTemplate.filterOutTemplates();
return instantEmailTemplate;
} catch (NoResultException e) {
throw new NotFoundException("Instant email template not found");
}
}
Aggregations