Search in sources :

Example 16 with EmailAggregation

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();
    });
}
Also used : LocalDateTime(java.time.LocalDateTime) EmailAggregationKey(com.redhat.cloud.notifications.models.EmailAggregationKey) EmailAggregation(com.redhat.cloud.notifications.models.EmailAggregation) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 17 with EmailAggregation

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;
}
Also used : Action(com.redhat.cloud.notifications.ingress.Action) Event(com.redhat.cloud.notifications.ingress.Event) JsonObject(io.vertx.core.json.JsonObject) Payload(com.redhat.cloud.notifications.ingress.Payload) EmailAggregation(com.redhat.cloud.notifications.models.EmailAggregation)

Example 18 with EmailAggregation

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;
}
Also used : Action(com.redhat.cloud.notifications.ingress.Action) Event(com.redhat.cloud.notifications.ingress.Event) JsonObject(io.vertx.core.json.JsonObject) Payload(com.redhat.cloud.notifications.ingress.Payload) EmailAggregation(com.redhat.cloud.notifications.models.EmailAggregation)

Example 19 with EmailAggregation

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);
    }
}
Also used : Arrays(java.util.Arrays) Endpoint(com.redhat.cloud.notifications.models.Endpoint) BaseTransformer(com.redhat.cloud.notifications.transformers.BaseTransformer) NotificationHistory(com.redhat.cloud.notifications.models.NotificationHistory) RecipientSettings(com.redhat.cloud.notifications.recipients.RecipientSettings) EmailTemplateFactory(com.redhat.cloud.notifications.templates.EmailTemplateFactory) EmailTemplate(com.redhat.cloud.notifications.templates.EmailTemplate) Map(java.util.Map) InstantEmailTemplate(com.redhat.cloud.notifications.models.InstantEmailTemplate) Event(com.redhat.cloud.notifications.models.Event) JsonObject(io.vertx.core.json.JsonObject) User(com.redhat.cloud.notifications.recipients.User) ZoneOffset(java.time.ZoneOffset) Context(com.redhat.cloud.notifications.ingress.Context) Counter(io.micrometer.core.instrument.Counter) TemplateInstance(io.quarkus.qute.TemplateInstance) ActionRecipientSettings(com.redhat.cloud.notifications.recipients.request.ActionRecipientSettings) Set(java.util.Set) UUID(java.util.UUID) EmailAggregation(com.redhat.cloud.notifications.models.EmailAggregation) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) StatelessSessionFactory(com.redhat.cloud.notifications.db.StatelessSessionFactory) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) AggregationCommand(com.redhat.cloud.notifications.models.AggregationCommand) Incoming(org.eclipse.microprofile.reactive.messaging.Incoming) AggregationEmailTemplate(com.redhat.cloud.notifications.models.AggregationEmailTemplate) Logger(org.jboss.logging.Logger) LocalDateTime(java.time.LocalDateTime) USE_TEMPLATES_FROM_DB_KEY(com.redhat.cloud.notifications.templates.TemplateService.USE_TEMPLATES_FROM_DB_KEY) TemplateService(com.redhat.cloud.notifications.templates.TemplateService) Inject(javax.inject.Inject) EmailAggregationRepository(com.redhat.cloud.notifications.db.repositories.EmailAggregationRepository) EmailSubscriptionRepository(com.redhat.cloud.notifications.db.repositories.EmailSubscriptionRepository) EmailSubscriptionType(com.redhat.cloud.notifications.models.EmailSubscriptionType) Blocking(io.smallrye.reactive.messaging.annotations.Blocking) Acknowledgment(org.eclipse.microprofile.reactive.messaging.Acknowledgment) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) EmailAggregationKey(com.redhat.cloud.notifications.models.EmailAggregationKey) EndpointTypeProcessor(com.redhat.cloud.notifications.processors.EndpointTypeProcessor) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) TemplateRepository(com.redhat.cloud.notifications.db.repositories.TemplateRepository) RecipientResolver(com.redhat.cloud.notifications.recipients.RecipientResolver) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty) Action(com.redhat.cloud.notifications.ingress.Action) Collections(java.util.Collections) EndpointRecipientSettings(com.redhat.cloud.notifications.recipients.request.EndpointRecipientSettings) Action(com.redhat.cloud.notifications.ingress.Action) EmailTemplate(com.redhat.cloud.notifications.templates.EmailTemplate) InstantEmailTemplate(com.redhat.cloud.notifications.models.InstantEmailTemplate) AggregationEmailTemplate(com.redhat.cloud.notifications.models.AggregationEmailTemplate) JsonObject(io.vertx.core.json.JsonObject) EmailAggregation(com.redhat.cloud.notifications.models.EmailAggregation)

Aggregations

EmailAggregation (com.redhat.cloud.notifications.models.EmailAggregation)19 JsonObject (io.vertx.core.json.JsonObject)13 Action (com.redhat.cloud.notifications.ingress.Action)11 Event (com.redhat.cloud.notifications.ingress.Event)9 Payload (com.redhat.cloud.notifications.ingress.Payload)8 Context (com.redhat.cloud.notifications.ingress.Context)5 QuarkusTest (io.quarkus.test.junit.QuarkusTest)5 Test (org.junit.jupiter.api.Test)5 EmailAggregationKey (com.redhat.cloud.notifications.models.EmailAggregationKey)4 LocalDateTime (java.time.LocalDateTime)4 EmailAggregationRepository (com.redhat.cloud.notifications.db.repositories.EmailAggregationRepository)2 EmailSubscriptionRepository (com.redhat.cloud.notifications.db.repositories.EmailSubscriptionRepository)2 EmailSubscriptionType (com.redhat.cloud.notifications.models.EmailSubscriptionType)2 Endpoint (com.redhat.cloud.notifications.models.Endpoint)2 RecipientResolver (com.redhat.cloud.notifications.recipients.RecipientResolver)2 User (com.redhat.cloud.notifications.recipients.User)2 ActionRecipientSettings (com.redhat.cloud.notifications.recipients.request.ActionRecipientSettings)2 EndpointRecipientSettings (com.redhat.cloud.notifications.recipients.request.EndpointRecipientSettings)2 TemplateInstance (io.quarkus.qute.TemplateInstance)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1