Search in sources :

Example 6 with EmailAggregationKey

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

the class EmailSubscriptionTypeProcessorTest method shouldSuccessfullySendEmail.

@Test
void shouldSuccessfullySendEmail() {
    micrometerAssertionHelper.saveCounterValuesBeforeTest(AGGREGATION_COMMAND_REJECTED_COUNTER_NAME, AGGREGATION_COMMAND_PROCESSED_COUNTER_NAME, AGGREGATION_COMMAND_ERROR_COUNTER_NAME);
    AggregationCommand aggregationCommand1 = new AggregationCommand(new EmailAggregationKey("account-1", "org-1", "bundle-1", "app-1"), LocalDateTime.now(), LocalDateTime.now().plusDays(1), DAILY);
    AggregationCommand aggregationCommand2 = new AggregationCommand(new EmailAggregationKey("account-2", "org-2", "bundle-2", "app-2"), LocalDateTime.now(ZoneOffset.UTC).plusDays(1), LocalDateTime.now(ZoneOffset.UTC).plusDays(2), DAILY);
    when(emailTemplateFactory.get(anyString(), anyString())).thenReturn(new Blank());
    inMemoryConnector.source(AGGREGATION_CHANNEL).send(Json.encode(aggregationCommand1));
    inMemoryConnector.source(AGGREGATION_CHANNEL).send(Json.encode(aggregationCommand2));
    micrometerAssertionHelper.awaitAndAssertCounterIncrement(AGGREGATION_COMMAND_PROCESSED_COUNTER_NAME, 2);
    micrometerAssertionHelper.assertCounterIncrement(AGGREGATION_COMMAND_REJECTED_COUNTER_NAME, 0);
    micrometerAssertionHelper.assertCounterIncrement(AGGREGATION_COMMAND_ERROR_COUNTER_NAME, 0);
    // Let's check that EndpointEmailSubscriptionResources#sendEmail was called for each aggregation.
    verify(emailAggregationRepository, times(1)).getEmailAggregation(eq(aggregationCommand1.getAggregationKey()), eq(aggregationCommand1.getStart()), eq(aggregationCommand1.getEnd()));
    verify(emailAggregationRepository, times(1)).purgeOldAggregation(eq(aggregationCommand1.getAggregationKey()), eq(aggregationCommand1.getEnd()));
    verify(emailAggregationRepository, times(1)).getEmailAggregation(eq(aggregationCommand2.getAggregationKey()), eq(aggregationCommand2.getStart()), eq(aggregationCommand2.getEnd()));
    verify(emailAggregationRepository, times(1)).purgeOldAggregation(eq(aggregationCommand2.getAggregationKey()), eq(aggregationCommand2.getEnd()));
    verifyNoMoreInteractions(emailAggregationRepository);
    micrometerAssertionHelper.clearSavedValues();
}
Also used : Blank(com.redhat.cloud.notifications.templates.Blank) AggregationCommand(com.redhat.cloud.notifications.models.AggregationCommand) EmailAggregationKey(com.redhat.cloud.notifications.models.EmailAggregationKey) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 7 with EmailAggregationKey

use of com.redhat.cloud.notifications.models.EmailAggregationKey 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)

Aggregations

EmailAggregationKey (com.redhat.cloud.notifications.models.EmailAggregationKey)7 QuarkusTest (io.quarkus.test.junit.QuarkusTest)6 LocalDateTime (java.time.LocalDateTime)6 Test (org.junit.jupiter.api.Test)6 EmailAggregation (com.redhat.cloud.notifications.models.EmailAggregation)3 EmailAggregationRepository (com.redhat.cloud.notifications.db.repositories.EmailAggregationRepository)1 EmailSubscriptionRepository (com.redhat.cloud.notifications.db.repositories.EmailSubscriptionRepository)1 EndpointRepository (com.redhat.cloud.notifications.db.repositories.EndpointRepository)1 Recipient (com.redhat.cloud.notifications.ingress.Recipient)1 AggregationCommand (com.redhat.cloud.notifications.models.AggregationCommand)1 EmailSubscriptionType (com.redhat.cloud.notifications.models.EmailSubscriptionType)1 Endpoint (com.redhat.cloud.notifications.models.Endpoint)1 AbstractEmailPayloadAggregator (com.redhat.cloud.notifications.processors.email.aggregators.AbstractEmailPayloadAggregator)1 EmailPayloadAggregatorFactory (com.redhat.cloud.notifications.processors.email.aggregators.EmailPayloadAggregatorFactory)1 RecipientResolver (com.redhat.cloud.notifications.recipients.RecipientResolver)1 User (com.redhat.cloud.notifications.recipients.User)1 ActionRecipientSettings (com.redhat.cloud.notifications.recipients.request.ActionRecipientSettings)1 EndpointRecipientSettings (com.redhat.cloud.notifications.recipients.request.EndpointRecipientSettings)1 Blank (com.redhat.cloud.notifications.templates.Blank)1 JsonArray (io.vertx.core.json.JsonArray)1