use of com.redhat.cloud.notifications.models.Template in project notifications-backend by RedHatInsights.
the class TemplateResourceTest method testTemplateIncludeCheckBeforeDelete.
@Test
void testTemplateIncludeCheckBeforeDelete() {
Header adminIdentity = TestHelpers.createTurnpikeIdentityHeader("user", adminRole);
// First, let's make sure the DB does not contain any template.
assertTrue(getAllTemplates(adminIdentity).isEmpty());
// Then, we'll persist an outer template, which includes another template.
Template helloTemplate = buildTemplate("hello-template", "Hello, {#include world-template /}");
JsonObject jsonHelloTemplate = createTemplate(adminIdentity, helloTemplate, 200).get();
// We also need to persist the included template.
Template worldTemplate = buildTemplate("world-template", "World!");
JsonObject jsonWorldTemplate = createTemplate(adminIdentity, worldTemplate, 200).get();
// At this point, the DB should contain two templates.
assertEquals(2, getAllTemplates(adminIdentity).size());
// If we try to delete the included template, it should fail.
deleteTemplate(adminIdentity, jsonWorldTemplate.getString("id"), 400);
// The DB still contains two templates.
assertEquals(2, getAllTemplates(adminIdentity).size());
// If we delete the outer template before deleting the included template, both REST calls should be successful.
deleteTemplate(adminIdentity, jsonHelloTemplate.getString("id"), 200);
deleteTemplate(adminIdentity, jsonWorldTemplate.getString("id"), 200);
// We deleted everything, the DB should not contain any template.
assertTrue(getAllTemplates(adminIdentity).isEmpty());
}
use of com.redhat.cloud.notifications.models.Template in project notifications-backend by RedHatInsights.
the class TemplateResourceTest method buildTemplate.
private static Template buildTemplate(String name, String data) {
Template template = new Template();
template.setName(name);
template.setDescription("My template");
template.setData(data);
return template;
}
use of com.redhat.cloud.notifications.models.Template in project notifications-backend by RedHatInsights.
the class TemplateResourceTest method testAllAggregationEmailTemplateEndpoints.
@Test
void testAllAggregationEmailTemplateEndpoints() {
Header adminIdentity = TestHelpers.createTurnpikeIdentityHeader("user", adminRole);
// First, we need a bundle and an app in the DB.
String bundleId = createBundle(adminIdentity, "bundle-name", "Bundle", OK).get();
String appId = createApp(adminIdentity, bundleId, "app-name", "App", null, OK).get();
// We also need templates that will be linked with the aggregation 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 aggregation email template.
assertTrue(getAllAggregationEmailTemplates(adminIdentity).isEmpty());
// First, we'll create, retrieve and check an aggregation email template that is NOT linked to an app.
AggregationEmailTemplate emailTemplate = buildAggregationEmailTemplate(null, subjectJsonTemplate.getString("id"), bodyJsonTemplate.getString("id"));
JsonObject jsonEmailTemplate = createAggregationEmailTemplate(adminIdentity, emailTemplate, 200).get();
// Then, we'll do the same with another aggregation email template that is linked to an app.
AggregationEmailTemplate emailTemplateWithApp = buildAggregationEmailTemplate(appId, subjectJsonTemplate.getString("id"), bodyJsonTemplate.getString("id"));
JsonObject jsonEmailTemplateWithApp = createAggregationEmailTemplate(adminIdentity, emailTemplateWithApp, 200).get();
// Now, we'll delete the second aggregation email template and check that it no longer exists with the following line.
deleteAggregationEmailTemplate(adminIdentity, jsonEmailTemplateWithApp.getString("id"));
// At this point, the DB should contain one aggregation email template: the one that wasn't linked to the app.
JsonArray jsonEmailTemplates = getAllAggregationEmailTemplates(adminIdentity);
assertEquals(1, jsonEmailTemplates.size());
jsonEmailTemplates.getJsonObject(0).mapTo(AggregationEmailTemplate.class);
assertEquals(jsonEmailTemplate.getString("id"), jsonEmailTemplates.getJsonObject(0).getString("id"));
// Retrieving the aggregation email templates from the app ID should return an empty list.
jsonEmailTemplates = getAggregationEmailTemplatesByApp(adminIdentity, appId);
assertTrue(jsonEmailTemplates.isEmpty());
// Let's update the aggregation 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.setApplicationId(UUID.fromString(appId));
emailTemplate.setSubjectTemplateId(UUID.fromString(newSubjectJsonTemplate.getString("id")));
emailTemplate.setBodyTemplateId(UUID.fromString(newBodyJsonTemplate.getString("id")));
updateAggregationEmailTemplate(adminIdentity, jsonEmailTemplate.getString("id"), emailTemplate);
// The aggregation email template is now linked to an app.
// It should be returned if we retrieve all aggregation email templates from the app ID.
jsonEmailTemplates = getAggregationEmailTemplatesByApp(adminIdentity, appId);
assertEquals(1, jsonEmailTemplates.size());
jsonEmailTemplates.getJsonObject(0).mapTo(AggregationEmailTemplate.class);
assertEquals(jsonEmailTemplate.getString("id"), jsonEmailTemplates.getJsonObject(0).getString("id"));
}
use of com.redhat.cloud.notifications.models.Template 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.Template in project notifications-backend by RedHatInsights.
the class TemplateResourceTest method testAllTemplateEndpoints.
@Test
void testAllTemplateEndpoints() {
Header adminIdentity = TestHelpers.createTurnpikeIdentityHeader("user", adminRole);
Template template = buildTemplate("template-name", "template-data");
// Before we start, the DB shouldn't contain any template.
assertTrue(getAllTemplates(adminIdentity).isEmpty());
// This creates the template, retrieves it with a separate REST call and then checks the fields values.
JsonObject jsonTemplate = createTemplate(adminIdentity, template, 200).get();
// The template should now be available in the "all templates" list.
JsonArray jsonTemplates = getAllTemplates(adminIdentity);
assertEquals(1, jsonTemplates.size());
jsonTemplates.getJsonObject(0).mapTo(Template.class);
assertEquals(jsonTemplate.getString("id"), jsonTemplates.getJsonObject(0).getString("id"));
// Let's update the template and check that the new fields values are correctly persisted.
template.setName("my-new-template");
template.setDescription("My new template");
template.setData("new-template-data");
updateTemplate(adminIdentity, jsonTemplate.getString("id"), template);
// Now we'll delete the template and check that it no longer exists with the following line.
deleteTemplate(adminIdentity, jsonTemplate.getString("id"), 200);
// We already know the template is gone, but let's check one more time.
assertTrue(getAllTemplates(adminIdentity).isEmpty());
}
Aggregations