Search in sources :

Example 16 with Template

use of com.redhat.cloud.notifications.models.Template 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 17 with Template

use of com.redhat.cloud.notifications.models.Template in project notifications-backend by RedHatInsights.

the class DbTemplateLocator method locate.

@Override
public Optional<TemplateLocation> locate(String name) {
    String hql = "FROM Template WHERE name = :name";
    try {
        Template template = statelessSessionFactory.getCurrentSession().createQuery(hql, Template.class).setParameter("name", name).getSingleResult();
        LOGGER.tracef("Template with [name=%s] found in the database", name);
        return Optional.of(buildTemplateLocation(template.getData()));
    } catch (NoResultException e) {
        LOGGER.tracef("Template with [name=%s] not found in the database", name);
        return Optional.empty();
    }
}
Also used : NoResultException(javax.persistence.NoResultException) Template(com.redhat.cloud.notifications.models.Template)

Example 18 with Template

use of com.redhat.cloud.notifications.models.Template in project notifications-backend by RedHatInsights.

the class DbQuteEngineTest method testToUtcFormatExtension.

@Test
void testToUtcFormatExtension() {
    Template template = createTemplate("to-utc-format-template", "{date.toUtcFormat()}");
    LocalDateTime date = LocalDateTime.of(2022, Month.JANUARY, 1, 0, 0);
    TemplateInstance templateInstance = templateService.compileTemplate(template.getData(), template.getName());
    assertEquals("01 Jan 2022 00:00 UTC", templateInstance.data("date", date).render());
    assertEquals("01 Jan 2022 00:00 UTC", templateInstance.data("date", date.toString()).render());
}
Also used : LocalDateTime(java.time.LocalDateTime) Template(com.redhat.cloud.notifications.models.Template) TemplateInstance(io.quarkus.qute.TemplateInstance) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 19 with Template

use of com.redhat.cloud.notifications.models.Template in project notifications-backend by RedHatInsights.

the class DbQuteEngineTest method testIncludeExistingTemplate.

@Test
void testIncludeExistingTemplate() {
    Template outerTemplate = createTemplate("outer-template", "Hello, {#include inner-template /}");
    Template innerTemplate = createTemplate("inner-template", "World!");
    statelessSessionFactory.withSession(statelessSession -> {
        String renderedOuterTemplate = templateService.compileTemplate(outerTemplate.getData(), outerTemplate.getName()).render();
        assertEquals("Hello, World!", renderedOuterTemplate);
    });
    /*
         * Any change to the inner template should be reflected when the outer template is rendered as long as the old
         * version of the inner template was removed from the Qute internal cache.
         */
    updateTemplateData(innerTemplate.getId(), "Red Hat!");
    statelessSessionFactory.withSession(statelessSession -> {
        String renderedOuterTemplate = templateService.compileTemplate(outerTemplate.getData(), outerTemplate.getName()).render();
        assertEquals("Hello, World!", renderedOuterTemplate);
    });
    templateService.clearTemplates();
    statelessSessionFactory.withSession(statelessSession -> {
        String renderedOuterTemplate = templateService.compileTemplate(outerTemplate.getData(), outerTemplate.getName()).render();
        assertEquals("Hello, Red Hat!", renderedOuterTemplate);
    });
    /*
         * If the inner template is deleted, the outer template rendering should fail as long as the old version of the
         * inner template was removed from the Qute internal cache.
         */
    deleteTemplate(innerTemplate.getId());
    statelessSessionFactory.withSession(statelessSession -> {
        String renderedOuterTemplate = templateService.compileTemplate(outerTemplate.getData(), outerTemplate.getName()).render();
        assertEquals("Hello, Red Hat!", renderedOuterTemplate);
    });
    templateService.clearTemplates();
    TemplateException e = assertThrows(TemplateException.class, () -> {
        statelessSessionFactory.withSession(statelessSession -> {
            templateService.compileTemplate(outerTemplate.getData(), outerTemplate.getName()).render();
        });
    });
    assertEquals("Included template [inner-template] not found in template [outer-template] on line 1", e.getMessage());
}
Also used : TemplateException(io.quarkus.qute.TemplateException) Template(com.redhat.cloud.notifications.models.Template) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 20 with Template

use of com.redhat.cloud.notifications.models.Template in project notifications-backend by RedHatInsights.

the class DbQuteEngineTest method testToStringFormatExtension.

@Test
void testToStringFormatExtension() {
    Template template = createTemplate("to-string-format-template", "{date.toStringFormat()}");
    LocalDateTime date = LocalDateTime.of(2022, Month.JANUARY, 1, 0, 0);
    TemplateInstance templateInstance = templateService.compileTemplate(template.getData(), template.getName());
    assertEquals("01 Jan 2022", templateInstance.data("date", date).render());
    assertEquals("01 Jan 2022", templateInstance.data("date", date.toString()).render());
}
Also used : LocalDateTime(java.time.LocalDateTime) Template(com.redhat.cloud.notifications.models.Template) TemplateInstance(io.quarkus.qute.TemplateInstance) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Aggregations

Template (com.redhat.cloud.notifications.models.Template)25 AggregationEmailTemplate (com.redhat.cloud.notifications.models.AggregationEmailTemplate)14 InstantEmailTemplate (com.redhat.cloud.notifications.models.InstantEmailTemplate)14 QuarkusTest (io.quarkus.test.junit.QuarkusTest)12 Test (org.junit.jupiter.api.Test)12 Transactional (javax.transaction.Transactional)8 TemplateInstance (io.quarkus.qute.TemplateInstance)6 CrudTestHelpers.createAggregationEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.createAggregationEmailTemplate)5 CrudTestHelpers.createInstantEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.createInstantEmailTemplate)5 CrudTestHelpers.createTemplate (com.redhat.cloud.notifications.CrudTestHelpers.createTemplate)5 CrudTestHelpers.deleteAggregationEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.deleteAggregationEmailTemplate)5 CrudTestHelpers.deleteInstantEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.deleteInstantEmailTemplate)5 CrudTestHelpers.deleteTemplate (com.redhat.cloud.notifications.CrudTestHelpers.deleteTemplate)5 CrudTestHelpers.updateAggregationEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.updateAggregationEmailTemplate)5 CrudTestHelpers.updateInstantEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.updateInstantEmailTemplate)5 CrudTestHelpers.updateTemplate (com.redhat.cloud.notifications.CrudTestHelpers.updateTemplate)5 DbIsolatedTest (com.redhat.cloud.notifications.db.DbIsolatedTest)4 Header (io.restassured.http.Header)4 JsonObject (io.vertx.core.json.JsonObject)4 LocalDateTime (java.time.LocalDateTime)4