Search in sources :

Example 6 with InstantEmailTemplate

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;
}
Also used : CrudTestHelpers.deleteInstantEmailTemplate(com.redhat.cloud.notifications.CrudTestHelpers.deleteInstantEmailTemplate) CrudTestHelpers.updateInstantEmailTemplate(com.redhat.cloud.notifications.CrudTestHelpers.updateInstantEmailTemplate) InstantEmailTemplate(com.redhat.cloud.notifications.models.InstantEmailTemplate) CrudTestHelpers.createInstantEmailTemplate(com.redhat.cloud.notifications.CrudTestHelpers.createInstantEmailTemplate)

Example 7 with InstantEmailTemplate

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"));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Header(io.restassured.http.Header) JsonObject(io.vertx.core.json.JsonObject) CrudTestHelpers.deleteInstantEmailTemplate(com.redhat.cloud.notifications.CrudTestHelpers.deleteInstantEmailTemplate) CrudTestHelpers.updateInstantEmailTemplate(com.redhat.cloud.notifications.CrudTestHelpers.updateInstantEmailTemplate) InstantEmailTemplate(com.redhat.cloud.notifications.models.InstantEmailTemplate) CrudTestHelpers.createInstantEmailTemplate(com.redhat.cloud.notifications.CrudTestHelpers.createInstantEmailTemplate) CrudTestHelpers.deleteInstantEmailTemplate(com.redhat.cloud.notifications.CrudTestHelpers.deleteInstantEmailTemplate) AggregationEmailTemplate(com.redhat.cloud.notifications.models.AggregationEmailTemplate) CrudTestHelpers.deleteAggregationEmailTemplate(com.redhat.cloud.notifications.CrudTestHelpers.deleteAggregationEmailTemplate) CrudTestHelpers.updateTemplate(com.redhat.cloud.notifications.CrudTestHelpers.updateTemplate) CrudTestHelpers.updateInstantEmailTemplate(com.redhat.cloud.notifications.CrudTestHelpers.updateInstantEmailTemplate) Template(com.redhat.cloud.notifications.models.Template) InstantEmailTemplate(com.redhat.cloud.notifications.models.InstantEmailTemplate) CrudTestHelpers.deleteTemplate(com.redhat.cloud.notifications.CrudTestHelpers.deleteTemplate) CrudTestHelpers.createAggregationEmailTemplate(com.redhat.cloud.notifications.CrudTestHelpers.createAggregationEmailTemplate) CrudTestHelpers.updateAggregationEmailTemplate(com.redhat.cloud.notifications.CrudTestHelpers.updateAggregationEmailTemplate) CrudTestHelpers.createInstantEmailTemplate(com.redhat.cloud.notifications.CrudTestHelpers.createInstantEmailTemplate) CrudTestHelpers.createTemplate(com.redhat.cloud.notifications.CrudTestHelpers.createTemplate) QuarkusTest(io.quarkus.test.junit.QuarkusTest) DbIsolatedTest(com.redhat.cloud.notifications.db.DbIsolatedTest) Test(org.junit.jupiter.api.Test)

Example 8 with InstantEmailTemplate

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;
}
Also used : EventType(com.redhat.cloud.notifications.models.EventType) InstantEmailTemplate(com.redhat.cloud.notifications.models.InstantEmailTemplate) AggregationEmailTemplate(com.redhat.cloud.notifications.models.AggregationEmailTemplate) Template(com.redhat.cloud.notifications.models.Template) InstantEmailTemplate(com.redhat.cloud.notifications.models.InstantEmailTemplate) Transactional(javax.transaction.Transactional)

Example 9 with 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);
            }
        }
    }
}
Also used : EventType(com.redhat.cloud.notifications.models.EventType) InstantEmailTemplate(com.redhat.cloud.notifications.models.InstantEmailTemplate) AggregationEmailTemplate(com.redhat.cloud.notifications.models.AggregationEmailTemplate) Template(com.redhat.cloud.notifications.models.Template) InstantEmailTemplate(com.redhat.cloud.notifications.models.InstantEmailTemplate)

Example 10 with InstantEmailTemplate

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");
    }
}
Also used : NotFoundException(javax.ws.rs.NotFoundException) NoResultException(javax.persistence.NoResultException) InstantEmailTemplate(com.redhat.cloud.notifications.models.InstantEmailTemplate)

Aggregations

InstantEmailTemplate (com.redhat.cloud.notifications.models.InstantEmailTemplate)11 AggregationEmailTemplate (com.redhat.cloud.notifications.models.AggregationEmailTemplate)6 Template (com.redhat.cloud.notifications.models.Template)5 EventType (com.redhat.cloud.notifications.models.EventType)4 Transactional (javax.transaction.Transactional)3 CrudTestHelpers.createInstantEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.createInstantEmailTemplate)2 CrudTestHelpers.deleteInstantEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.deleteInstantEmailTemplate)2 CrudTestHelpers.updateInstantEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.updateInstantEmailTemplate)2 JsonObject (io.vertx.core.json.JsonObject)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 CrudTestHelpers.createAggregationEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.createAggregationEmailTemplate)1 CrudTestHelpers.createTemplate (com.redhat.cloud.notifications.CrudTestHelpers.createTemplate)1 CrudTestHelpers.deleteAggregationEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.deleteAggregationEmailTemplate)1 CrudTestHelpers.deleteTemplate (com.redhat.cloud.notifications.CrudTestHelpers.deleteTemplate)1 CrudTestHelpers.updateAggregationEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.updateAggregationEmailTemplate)1 CrudTestHelpers.updateTemplate (com.redhat.cloud.notifications.CrudTestHelpers.updateTemplate)1 DbIsolatedTest (com.redhat.cloud.notifications.db.DbIsolatedTest)1 StatelessSessionFactory (com.redhat.cloud.notifications.db.StatelessSessionFactory)1 EmailAggregationRepository (com.redhat.cloud.notifications.db.repositories.EmailAggregationRepository)1