Search in sources :

Example 11 with Template

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

the class DbQuteEngineTest method updateTemplateData.

@Transactional
void updateTemplateData(UUID id, String data) {
    Template template = entityManager.find(Template.class, id);
    template.setData(data);
}
Also used : Template(com.redhat.cloud.notifications.models.Template) Transactional(javax.transaction.Transactional)

Example 12 with Template

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

the class DbQuteEngineTest method testToTimeAgoExtension.

@Test
void testToTimeAgoExtension() {
    Template template = createTemplate("to-time-ago-template", "{date.toTimeAgo()}");
    LocalDateTime date = LocalDateTime.now().minusDays(2L);
    TemplateInstance templateInstance = templateService.compileTemplate(template.getData(), template.getName());
    assertEquals("2 days ago", templateInstance.data("date", date).render());
    assertEquals("2 days ago", 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 13 with Template

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

the class DbQuteEngineTest method testIncludeUnknownTemplate.

@Test
void testIncludeUnknownTemplate() {
    Template outerTemplate = createTemplate("other-outer-template", "Hello, {#include unknown-inner-template /}");
    TemplateException e = assertThrows(TemplateException.class, () -> {
        statelessSessionFactory.withSession(statelessSession -> {
            templateService.compileTemplate(outerTemplate.getData(), outerTemplate.getName()).render();
        });
    });
    assertEquals("Included template [unknown-inner-template] not found in template [other-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 14 with Template

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

the class DbQuteEngineTest method createTemplate.

@Transactional
Template createTemplate(String name, String data) {
    Template template = new Template();
    template.setName(name);
    template.setDescription("The best template ever created");
    template.setData(data);
    entityManager.persist(template);
    return template;
}
Also used : Template(com.redhat.cloud.notifications.models.Template) Transactional(javax.transaction.Transactional)

Example 15 with Template

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

the class EmailTemplateMigrationService method getOrCreateTemplate.

/*
     * Creates a template only if it does not already exist in the DB.
     * Existing templates are never updated by this migration service.
     */
Template getOrCreateTemplate(List<String> warnings, String name, String extension, String description) {
    try {
        Template template = entityManager.createQuery("FROM Template WHERE name = :name", Template.class).setParameter("name", name).getSingleResult();
        warnings.add(String.format("Template found in DB: %s", name));
        return template;
    } catch (NoResultException e) {
        LOGGER.infof("Creating template: %s", name);
        Template template = new Template();
        template.setName(name);
        template.setDescription(description);
        template.setData(loadResourceTemplate(name, extension));
        entityManager.persist(template);
        return template;
    }
}
Also used : NoResultException(javax.persistence.NoResultException) AggregationEmailTemplate(com.redhat.cloud.notifications.models.AggregationEmailTemplate) Template(com.redhat.cloud.notifications.models.Template) InstantEmailTemplate(com.redhat.cloud.notifications.models.InstantEmailTemplate)

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