use of com.redhat.cloud.notifications.models.EmailAggregation in project notifications-backend by RedHatInsights.
the class EmailAggregationRepositoryTest method testAllMethodsWithOrgIdDisabled.
// TODO NOTIF-603 Remove when switching to orgId
@Test
void testAllMethodsWithOrgIdDisabled() {
LocalDateTime start = LocalDateTime.now(UTC).minusHours(1L);
LocalDateTime end = LocalDateTime.now(UTC).plusHours(1L);
EmailAggregationKey key = new EmailAggregationKey(ACCOUNT_ID, ORG_ID, BUNDLE_NAME, APP_NAME);
statelessSessionFactory.withSession(statelessSession -> {
clearEmailAggregations();
resourceHelpers.addEmailAggregation(ACCOUNT_ID, BUNDLE_NAME, APP_NAME, PAYLOAD1);
resourceHelpers.addEmailAggregation(ACCOUNT_ID, BUNDLE_NAME, APP_NAME, PAYLOAD2);
resourceHelpers.addEmailAggregation("other-account", ORG_ID, BUNDLE_NAME, APP_NAME, PAYLOAD2);
resourceHelpers.addEmailAggregation(ACCOUNT_ID, "other-bundle", APP_NAME, PAYLOAD2);
resourceHelpers.addEmailAggregation(ACCOUNT_ID, BUNDLE_NAME, "other-app", PAYLOAD2);
List<EmailAggregation> aggregations = emailAggregationRepository.getEmailAggregation(key, start, end);
assertEquals(2, aggregations.size());
assertNull(aggregations.get(0).getOrgId());
assertNull(aggregations.get(1).getOrgId());
assertTrue(aggregations.stream().map(EmailAggregation::getAccountId).allMatch(ACCOUNT_ID::equals));
assertTrue(aggregations.stream().map(EmailAggregation::getBundleName).allMatch(BUNDLE_NAME::equals));
assertTrue(aggregations.stream().map(EmailAggregation::getApplicationName).allMatch(APP_NAME::equals));
assertEquals(1, aggregations.stream().map(EmailAggregation::getPayload).filter(PAYLOAD1::equals).count());
assertEquals(1, aggregations.stream().map(EmailAggregation::getPayload).filter(PAYLOAD2::equals).count());
List<EmailAggregationKey> keys = getApplicationsWithPendingAggregation(start, end);
assertEquals(4, keys.size());
assertEquals(ACCOUNT_ID, keys.get(0).getAccountId());
assertEquals(BUNDLE_NAME, keys.get(0).getBundle());
assertEquals(APP_NAME, keys.get(0).getApplication());
assertEquals(2, emailAggregationRepository.purgeOldAggregation(key, end));
assertEquals(0, emailAggregationRepository.getEmailAggregation(key, start, end).size());
assertEquals(3, getApplicationsWithPendingAggregation(start, end).size());
clearEmailAggregations();
});
}
use of com.redhat.cloud.notifications.models.EmailAggregation in project notifications-backend by RedHatInsights.
the class ComplianceTestHelpers method createEmailAggregation.
public static EmailAggregation createEmailAggregation(String tenant, String bundle, String application, String eventType, String policyId, String inventoryId) {
EmailAggregation aggregation = new EmailAggregation();
aggregation.setBundleName(bundle);
aggregation.setApplicationName(application);
aggregation.setAccountId(tenant);
aggregation.setCreated(LocalDateTime.now(UTC).minusHours(5L));
Action emailActionMessage = new Action();
emailActionMessage.setBundle(bundle);
emailActionMessage.setApplication(application);
emailActionMessage.setTimestamp(LocalDateTime.now());
emailActionMessage.setEventType(eventType);
emailActionMessage.setContext(new Context.ContextBuilder().withAdditionalProperty("inventory_id", inventoryId).withAdditionalProperty("system_check_in", "2020-08-03T15:22:42.199046").withAdditionalProperty("display_name", "My test machine").withAdditionalProperty("tags", List.of()).build());
emailActionMessage.setEvents(List.of(new Event.EventBuilder().withMetadata(new Metadata.MetadataBuilder().build()).withPayload(new Payload.PayloadBuilder().withAdditionalProperty("policy_id", policyId).withAdditionalProperty("policy_name", "not-tested-name").withAdditionalProperty("policy_description", "not-used-desc").withAdditionalProperty("policy_condition", "not-used-condition").build()).build()));
emailActionMessage.setAccountId(tenant);
JsonObject payload = baseTransformer.transform(emailActionMessage);
aggregation.setPayload(payload);
return aggregation;
}
use of com.redhat.cloud.notifications.models.EmailAggregation in project notifications-backend by RedHatInsights.
the class DriftTestHelpers method createEmailAggregation.
public static EmailAggregation createEmailAggregation(String tenant, String bundle, String application, String baselineId, String baselineName, String inventory_id, String inventory_name) {
EmailAggregation aggregation = new EmailAggregation();
aggregation.setBundleName(bundle);
aggregation.setApplicationName(application);
aggregation.setAccountId(tenant);
Action emailActionMessage = new Action();
emailActionMessage.setBundle(bundle);
emailActionMessage.setApplication(application);
emailActionMessage.setTimestamp(LocalDateTime.now());
emailActionMessage.setEventType("testEmailSubscriptionInstant");
emailActionMessage.setContext(new Context.ContextBuilder().withAdditionalProperty("inventory_id", inventory_id).withAdditionalProperty("system_check_in", "2021-07-13T15:22:42.199046").withAdditionalProperty("display_name", inventory_name).withAdditionalProperty("tags", List.of()).build());
emailActionMessage.setEvents(List.of(new Event.EventBuilder().withMetadata(new Metadata.MetadataBuilder().build()).withPayload(new Payload.PayloadBuilder().withAdditionalProperty("baseline_id", baselineId).withAdditionalProperty("baseline_name", baselineName).build()).build()));
emailActionMessage.setAccountId(tenant);
JsonObject payload = baseTransformer.transform(emailActionMessage);
aggregation.setPayload(payload);
return aggregation;
}
use of com.redhat.cloud.notifications.models.EmailAggregation in project notifications-backend by RedHatInsights.
the class EmailSubscriptionTypeProcessor method process.
@Override
public List<NotificationHistory> process(Event event, List<Endpoint> endpoints) {
if (endpoints == null || endpoints.isEmpty()) {
return Collections.emptyList();
} else {
Action action = event.getAction();
final EmailTemplate template = emailTemplateFactory.get(action.getBundle(), action.getApplication());
boolean shouldSaveAggregation;
if (useTemplatesFromDb) {
shouldSaveAggregation = templateRepository.isEmailAggregationSupported(action.getBundle(), action.getApplication(), NON_INSTANT_SUBSCRIPTION_TYPES);
} else {
shouldSaveAggregation = NON_INSTANT_SUBSCRIPTION_TYPES.stream().anyMatch(emailSubscriptionType -> template.isSupported(action.getEventType(), emailSubscriptionType));
}
if (shouldSaveAggregation) {
EmailAggregation aggregation = new EmailAggregation();
aggregation.setAccountId(action.getAccountId());
aggregation.setApplicationName(action.getApplication());
aggregation.setBundleName(action.getBundle());
JsonObject transformedAction = baseTransformer.transform(action);
aggregation.setPayload(transformedAction);
emailAggregationRepository.addEmailAggregation(aggregation);
}
return sendEmail(event, Set.copyOf(endpoints), template);
}
}
Aggregations