From c70df921ae6881b82c3f2ad9bbdc2d5edab09321 Mon Sep 17 00:00:00 2001 From: Gwenneg Lepage Date: Mon, 13 Nov 2023 10:10:03 +0100 Subject: [PATCH] Remove individual emails support in connector-email (#2310) --- .rhcicd/clowdapp-connector-email.yaml | 5 - .../connector/email/EmailRouteBuilder.java | 46 ++---- .../email/config/EmailConnectorConfig.java | 9 -- .../email/constants/ExchangeProperty.java | 4 - .../connector/email/constants/Routes.java | 4 - .../processors/bop/BOPRequestPreparer.java | 38 +---- ...derRHCLOUD28631SingleEmailPerUserTest.java | 153 ------------------ .../EmailRouteBuilderRHCLOUD28631Test.java | 18 +-- .../email/EmailRouteBuilderTest.java | 38 ----- .../connector/email/EmptyRecipientsTest.java | 25 ++- .../bop/BOPRequestPreparerTest.java | 60 ------- .../SingleEmailMultipleUsersTestProfile.java | 17 -- 12 files changed, 37 insertions(+), 380 deletions(-) delete mode 100644 connector-email/src/test/java/com/redhat/cloud/notifications/connector/email/EmailRouteBuilderRHCLOUD28631SingleEmailPerUserTest.java delete mode 100644 connector-email/src/test/java/com/redhat/cloud/notifications/connector/email/testprofile/SingleEmailMultipleUsersTestProfile.java diff --git a/.rhcicd/clowdapp-connector-email.yaml b/.rhcicd/clowdapp-connector-email.yaml index 1a9b27556c..d01f3169ed 100644 --- a/.rhcicd/clowdapp-connector-email.yaml +++ b/.rhcicd/clowdapp-connector-email.yaml @@ -95,8 +95,6 @@ objects: value: ${NOTIFICATIONS_CONNECTOR_SEDA_CONCURRENT_CONSUMERS} - name: NOTIFICATIONS_CONNECTOR_SEDA_QUEUE_SIZE value: ${NOTIFICATIONS_CONNECTOR_SEDA_QUEUE_SIZE} - - name: NOTIFICATIONS_CONNECTOR_SINGLE_EMAIL_PER_USER_ENABLED - value: ${NOTIFICATIONS_CONNECTOR_SINGLE_EMAIL_PER_USER_ENABLED} - name: NOTIFICATIONS_CONNECTOR_USER_PROVIDER_BOP_API_TOKEN valueFrom: secretKeyRef: @@ -237,9 +235,6 @@ parameters: - name: NOTIFICATIONS_CONNECTOR_SEDA_QUEUE_SIZE description: Maximum capacity of the SEDA queue value: "1" -- name: NOTIFICATIONS_CONNECTOR_SINGLE_EMAIL_PER_USER_ENABLED - description: Is sending a single email per user enabled? - value: "false" - name: NOTIFICATIONS_CONNECTOR_USER_PROVIDER_CACHE_EXPIRE_AFTER_WRITE description: Expiration delay in seconds of cached users value: "600" diff --git a/connector-email/src/main/java/com/redhat/cloud/notifications/connector/email/EmailRouteBuilder.java b/connector-email/src/main/java/com/redhat/cloud/notifications/connector/email/EmailRouteBuilder.java index 57231c007a..c9fe7b353f 100644 --- a/connector-email/src/main/java/com/redhat/cloud/notifications/connector/email/EmailRouteBuilder.java +++ b/connector-email/src/main/java/com/redhat/cloud/notifications/connector/email/EmailRouteBuilder.java @@ -179,7 +179,7 @@ public void configureRoute() throws Exception { // Once the split has finished, we can send the exchange to the BOP // route. - .to(direct(Routes.SEND_EMAIL_BOP_CHOICE)); + .to(direct(Routes.SEND_EMAIL_BOP)); /* * Fetches the users from recipients resolver. @@ -375,21 +375,6 @@ public void configureRoute() throws Exception { .end() .process(this.recipientsFilter); - from(direct(Routes.SEND_EMAIL_BOP_CHOICE)) - .routeId(Routes.SEND_EMAIL_BOP_CHOICE) - .choice() - .when(shouldSkipEmail()) - // TODO Lower this log level to DEBUG later. - .log(INFO, getClass().getName(), "Skipped Email notification because the recipients list was empty [orgId=${exchangeProperty." + ORG_ID + "}, historyId=${exchangeProperty." + ID + "}]") - .when(constant(this.emailConnectorConfig.isSingleEmailPerUserEnabled())) - .to(direct(Routes.SEND_EMAIL_BOP_SINGLE_PER_USER)) - .log(INFO, getClass().getName(), "Sent single recipient Email notification [orgId=${exchangeProperty." + ORG_ID + "}, historyId=${exchangeProperty." + ID + "}]") - .otherwise() - .to(direct(Routes.SEND_EMAIL_BOP)) - .log(INFO, getClass().getName(), "Sent Email notification [orgId=${exchangeProperty." + ORG_ID + "}, historyId=${exchangeProperty." + ID + "}]") - .end() - .to(direct(SUCCESS)); - /* * Prepares the payload accepted by BOP and sends the request to * the service. @@ -398,23 +383,18 @@ public void configureRoute() throws Exception { from(direct(Routes.SEND_EMAIL_BOP)) .routeId(Routes.SEND_EMAIL_BOP) - // Clear all the headers that may come from the previous route. - .removeHeaders("*") - .process(this.BOPRequestPreparer) - .to(bopEndpoint); - - /* - * Temporary route in order to be able to send an email per user, - * instead of one per multiple users. - */ - from(direct(Routes.SEND_EMAIL_BOP_SINGLE_PER_USER)) - .routeId(Routes.SEND_EMAIL_BOP_SINGLE_PER_USER) - // Clear all the headers that may come from the previous route. - .removeHeaders("*") - .split(simpleF("${exchangeProperty.%s}", FILTERED_USERS)) - .setProperty(ExchangeProperty.SINGLE_EMAIL_PER_USER, constant(true)) - .to(direct(Routes.SEND_EMAIL_BOP)) - .end(); + .choice() + .when(shouldSkipEmail()) + // TODO Lower this log level to DEBUG later. + .log(INFO, getClass().getName(), "Skipped Email notification because the recipients list was empty [orgId=${exchangeProperty." + ORG_ID + "}, historyId=${exchangeProperty." + ID + "}]") + .otherwise() + // Clear all the headers that may come from the previous route. + .removeHeaders("*") + .process(this.BOPRequestPreparer) + .to(bopEndpoint) + .log(INFO, getClass().getName(), "Sent Email notification [orgId=${exchangeProperty." + ORG_ID + "}, historyId=${exchangeProperty." + ID + "}]") + .end() + .to(direct(SUCCESS)); } private Predicate shouldSkipEmail() { diff --git a/connector-email/src/main/java/com/redhat/cloud/notifications/connector/email/config/EmailConnectorConfig.java b/connector-email/src/main/java/com/redhat/cloud/notifications/connector/email/config/EmailConnectorConfig.java index 369df6dee8..e4dd489752 100644 --- a/connector-email/src/main/java/com/redhat/cloud/notifications/connector/email/config/EmailConnectorConfig.java +++ b/connector-email/src/main/java/com/redhat/cloud/notifications/connector/email/config/EmailConnectorConfig.java @@ -28,8 +28,6 @@ public class EmailConnectorConfig extends ConnectorConfig { private static final String RBAC_ELEMENTS_PAGE = "notifications.connector.user-provider.rbac.elements-per-page"; private static final String RBAC_URL = "notifications.connector.user-provider.rbac.url"; private static final String SKIP_BOP_USERS_RESOLUTION = "notifications.connector.bop.skip-users-resolution"; - @Deprecated(forRemoval = true) - public static final String SINGLE_EMAIL_PER_USER = "notifications.connector.single-email-per-user.enabled"; // The following two keys are public in order to make it easier for // overriding them in the tests. @@ -54,9 +52,6 @@ public class EmailConnectorConfig extends ConnectorConfig { @ConfigProperty(name = BOP_URL) String bopURL; - @ConfigProperty(name = SINGLE_EMAIL_PER_USER, defaultValue = "true") - boolean singleEmailPerUserEnabled; - @ConfigProperty(name = FETCH_USERS_RBAC_ENABLED, defaultValue = "true") boolean fetchUsersWithRBAC; @@ -126,7 +121,6 @@ public void log() { additionalEntries.put(RBAC_APPLICATION_KEY, this.rbacApplicationKey); additionalEntries.put(RBAC_ELEMENTS_PAGE, this.rbacElementsPerPage); additionalEntries.put(RBAC_URL, this.rbacURL); - additionalEntries.put(SINGLE_EMAIL_PER_USER, this.singleEmailPerUserEnabled); additionalEntries.put(USER_PROVIDER_CACHE_EXPIRE_AFTER_WRITE, this.userProviderCacheExpireAfterWrite); additionalEntries.put(SKIP_BOP_USERS_RESOLUTION, skipBopUsersResolution); additionalEntries.put(NOTIFICATIONS_RECIPIENTS_RESOLVER_MODULE_ENABLED, recipientsResolverModuleEnabled); @@ -233,9 +227,6 @@ public String getRbacURL() { return this.rbacURL; } - public boolean isSingleEmailPerUserEnabled() { - return this.singleEmailPerUserEnabled; - } public int getUserProviderCacheExpireAfterWrite() { return userProviderCacheExpireAfterWrite; diff --git a/connector-email/src/main/java/com/redhat/cloud/notifications/connector/email/constants/ExchangeProperty.java b/connector-email/src/main/java/com/redhat/cloud/notifications/connector/email/constants/ExchangeProperty.java index a9145f9945..23f7bbfd0b 100644 --- a/connector-email/src/main/java/com/redhat/cloud/notifications/connector/email/constants/ExchangeProperty.java +++ b/connector-email/src/main/java/com/redhat/cloud/notifications/connector/email/constants/ExchangeProperty.java @@ -33,10 +33,6 @@ public class ExchangeProperty { * endpoints and the received event. */ public static final String RECIPIENT_SETTINGS = "recipient_settings"; - /** - * A flag to determine if we need to send the email individually per user. - */ - public static final String SINGLE_EMAIL_PER_USER = "single_email_per_user"; /** * Holds the list of usernames who subscribed to the event type * that triggered the notification. diff --git a/connector-email/src/main/java/com/redhat/cloud/notifications/connector/email/constants/Routes.java b/connector-email/src/main/java/com/redhat/cloud/notifications/connector/email/constants/Routes.java index 8e5762a2b2..a9ea87ccd2 100644 --- a/connector-email/src/main/java/com/redhat/cloud/notifications/connector/email/constants/Routes.java +++ b/connector-email/src/main/java/com/redhat/cloud/notifications/connector/email/constants/Routes.java @@ -8,10 +8,6 @@ public class Routes { public static final String FETCH_USERS_RBAC = "fetch-users-rbac"; public static final String SEND_EMAIL_BOP = "send-email-bop"; @Deprecated(forRemoval = true) - public static final String SEND_EMAIL_BOP_CHOICE = "send-email-bop-choice"; - @Deprecated(forRemoval = true) - public static final String SEND_EMAIL_BOP_SINGLE_PER_USER = "send-email-single-per-user"; - @Deprecated(forRemoval = true) public static final String RESOLVE_USERS_WITHOUT_RECIPIENTS_RESOLVER_CLOWDAPP = "fetch-users-without-recipients-resolver-clowdapp"; public static final String RESOLVE_USERS_WITH_RECIPIENTS_RESOLVER_CLOWDAPP = "fetch-users-with-recipients-resolver-clowdapp"; diff --git a/connector-email/src/main/java/com/redhat/cloud/notifications/connector/email/processors/bop/BOPRequestPreparer.java b/connector-email/src/main/java/com/redhat/cloud/notifications/connector/email/processors/bop/BOPRequestPreparer.java index e359f2ae90..37b72e9798 100644 --- a/connector-email/src/main/java/com/redhat/cloud/notifications/connector/email/processors/bop/BOPRequestPreparer.java +++ b/connector-email/src/main/java/com/redhat/cloud/notifications/connector/email/processors/bop/BOPRequestPreparer.java @@ -6,17 +6,13 @@ import com.redhat.cloud.notifications.connector.email.model.bop.Emails; import com.redhat.cloud.notifications.connector.email.model.bop.SendEmailsRequest; import com.redhat.cloud.notifications.connector.email.model.settings.User; -import io.quarkus.logging.Log; import io.vertx.core.json.JsonObject; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import org.apache.camel.Exchange; import org.apache.camel.Processor; import org.apache.camel.component.http.HttpMethods; -import org.eclipse.microprofile.config.inject.ConfigProperty; -import java.util.Arrays; -import java.util.Collections; import java.util.Set; import static java.util.stream.Collectors.toSet; @@ -24,9 +20,6 @@ @ApplicationScoped public class BOPRequestPreparer implements Processor { - @ConfigProperty(name = "env.name", defaultValue = "unknown") - String environment; - @Inject EmailConnectorConfig emailConnectorConfig; @@ -38,32 +31,15 @@ public class BOPRequestPreparer implements Processor { public void process(final Exchange exchange) { final String subject = exchange.getProperty(ExchangeProperty.RENDERED_SUBJECT, String.class); final String body = exchange.getProperty(ExchangeProperty.RENDERED_BODY, String.class); - final Set recipients; - // We still need to support sending individual emails per user for a - // while. However, that will go away soon, so we can consider the - // following code block very much deprecated. - final Boolean singleEmailPerUser = exchange.getProperty(ExchangeProperty.SINGLE_EMAIL_PER_USER, Boolean.class); - if (singleEmailPerUser != null && singleEmailPerUser) { - User recipient = exchange.getMessage().getBody(User.class); - if (emailConnectorConfig.isSkipBopUsersResolution()) { - recipients = Collections.singleton(recipient.getEmail()); - } else { - recipients = Collections.singleton(recipient.getUsername()); - } + final Set recipients; + final Set users = exchange.getProperty(ExchangeProperty.FILTERED_USERS, Set.class); + if (emailConnectorConfig.isSkipBopUsersResolution()) { + recipients = users.stream().map(User::getEmail).collect(toSet()); + Set emails = exchange.getProperty(ExchangeProperty.EMAIL_RECIPIENTS, Set.class); + recipients.addAll(emails); } else { - final Set users = exchange.getProperty(ExchangeProperty.FILTERED_USERS, Set.class); - if (emailConnectorConfig.isSkipBopUsersResolution()) { - recipients = users.stream().map(User::getEmail).collect(toSet()); - Set emails = exchange.getProperty(ExchangeProperty.EMAIL_RECIPIENTS, Set.class); - recipients.addAll(emails); - } else { - recipients = users.stream().map(User::getUsername).collect(toSet()); - } - } - - if ("stage".equals(environment)) { - Log.infof("Recipients: %s", Arrays.toString(recipients.toArray())); + recipients = users.stream().map(User::getUsername).collect(toSet()); } final Email email = new Email( diff --git a/connector-email/src/test/java/com/redhat/cloud/notifications/connector/email/EmailRouteBuilderRHCLOUD28631SingleEmailPerUserTest.java b/connector-email/src/test/java/com/redhat/cloud/notifications/connector/email/EmailRouteBuilderRHCLOUD28631SingleEmailPerUserTest.java deleted file mode 100644 index 0c0859db2e..0000000000 --- a/connector-email/src/test/java/com/redhat/cloud/notifications/connector/email/EmailRouteBuilderRHCLOUD28631SingleEmailPerUserTest.java +++ /dev/null @@ -1,153 +0,0 @@ -package com.redhat.cloud.notifications.connector.email; - -import com.google.common.io.Resources; -import com.redhat.cloud.notifications.connector.ConnectorToEngineRouteBuilder; -import com.redhat.cloud.notifications.connector.EngineToConnectorRouteBuilder; -import com.redhat.cloud.notifications.connector.email.config.EmailConnectorConfig; -import com.redhat.cloud.notifications.connector.email.constants.ExchangeProperty; -import com.redhat.cloud.notifications.connector.email.constants.Routes; -import com.redhat.cloud.notifications.connector.email.model.settings.RecipientSettings; -import io.quarkus.test.junit.QuarkusTest; -import io.vertx.core.json.JsonArray; -import io.vertx.core.json.JsonObject; -import jakarta.inject.Inject; -import org.apache.camel.Exchange; -import org.apache.camel.ProducerTemplate; -import org.apache.camel.builder.AdviceWith; -import org.apache.camel.component.mock.MockEndpoint; -import org.apache.camel.model.language.ConstantExpression; -import org.apache.camel.quarkus.test.CamelQuarkusTestSupport; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.io.IOException; -import java.net.URL; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - -/** - * Individual class for testing the bug specified below but with the "single - * email per user" option enabled. - */ -@Deprecated(forRemoval = true) -@QuarkusTest -public class EmailRouteBuilderRHCLOUD28631SingleEmailPerUserTest extends CamelQuarkusTestSupport { - @Inject - EmailConnectorConfig emailConnectorConfig; - - @Inject - ProducerTemplate producerTemplate; - - /** - * Regression test for RHCLOUD-28631 - * which, made the email connector send multiple emails for a single tenant - * with more than one behavior group configured. This was caused because - * the recipient settings were being processed sequentially, which in turn - * built different sets of users, which caused an email to be sent for each - * set. The fix consisted on aggregating the users set to reduce the number - * of emails being sent. - * @throws IOException if the different stubbed responses could not be - * loaded. - */ - @Test - void testMultipleRecipientSettingsSameUserSendSingleEmail() throws Exception { - // Prepare the payload that we will simulate that we receive on the - // connector. - final Set users = Set.of("foouser", "baruser"); - final RecipientSettings recipientSettings = new RecipientSettings( - true, - true, - null, - users, - null - ); - final Set users2 = Set.of("johndoe", "janedoe"); - - final RecipientSettings recipientSettings2 = new RecipientSettings( - true, - true, - null, - users2, - null - ); - - // Create the exchange that we will use in this test. - final Exchange exchange = this.createExchangeWithBody(""); - - final List recipientSettingsList = new ArrayList<>(); - recipientSettingsList.add(recipientSettings); - recipientSettingsList.add(recipientSettings2); - - final Set subscribers = Set.of("user1", "user2"); - - final String emailBody = "emailBody"; - final String emailSubject = "emailSubject"; - exchange.setProperty(ExchangeProperty.RENDERED_BODY, emailBody); - exchange.setProperty(ExchangeProperty.RENDERED_SUBJECT, emailSubject); - exchange.setProperty(ExchangeProperty.RECIPIENT_SETTINGS, recipientSettingsList); - exchange.setProperty(ExchangeProperty.SUBSCRIBERS, subscribers); - - // Prepare the stubbed response for the RBAC service. - final URL url = Resources.getResource("processors/rbac/rbacUsersResponse.json"); - final String body = Resources.toString(url, StandardCharsets.UTF_8); - - // Mock the endpoints so that the whole flow is followed. - AdviceWith.adviceWith(this.context, Routes.FETCH_USERS_RBAC, a -> { - a.weaveByToUri(this.emailConnectorConfig.getRbacURL()).replace().setBody(new ConstantExpression(body)); - }); - - // Make sure we replace the BOP endpoint too in order to check the - // usernames we are sending. - AdviceWith.adviceWith(this.context, Routes.SEND_EMAIL_BOP, a -> { - a.weaveByToUri(String.format("%s*", this.emailConnectorConfig.getBopURL())).replace().to("mock:bopendpoint"); - a.mockEndpointsAndSkip(String.format("direct:%s", ConnectorToEngineRouteBuilder.SUCCESS)); - }); - - // Set up the expected exchanges count that the success endpoint should - // receive at the end of the test. - AdviceWith.adviceWith(this.context, Routes.SEND_EMAIL_BOP_CHOICE, a -> { - a.mockEndpoints(String.format("direct:%s", ConnectorToEngineRouteBuilder.SUCCESS)); - }); - - final MockEndpoint successEndpoint = this.getMockEndpoint(String.format("mock:direct:%s", ConnectorToEngineRouteBuilder.SUCCESS)); - successEndpoint.expectedMessageCount(1); - - // Send the exchange to the entry point of the email connector. - this.producerTemplate.send(String.format("seda:%s", EngineToConnectorRouteBuilder.ENGINE_TO_CONNECTOR), exchange); - - // We need a timeout here because SEDA processes the exchange from a different thread and a race condition may happen. - successEndpoint.assertIsSatisfied(2000L); - - // Get the exchanges that we sent to BOP. In theory, since we are - // sending an email per user, we should receive many of them. - final MockEndpoint bopEndpoint = this.getMockEndpoint("mock:bopendpoint"); - final List exchanges = bopEndpoint.getExchanges(); - - Assertions.assertEquals(4, exchanges.size(), "unexpected number exchanges present in BOP's endpoint"); - - for (final Exchange bopExchange : exchanges) { - final String bopBody = bopExchange.getMessage().getBody(String.class); - final JsonObject bopBodyJson = new JsonObject(bopBody); - - Assertions.assertNotNull(bopBodyJson, "the message body sent to BOP was empty"); - - final JsonArray emails = bopBodyJson.getJsonArray("emails"); - Assertions.assertNotNull(emails, "the emails object of the BOP message was empty"); - - final JsonObject email = emails.getJsonObject(0); - Assertions.assertNotNull(email, "the email object to be sent to BOP is empty"); - - final JsonArray bccList = email.getJsonArray("bccList"); - Assertions.assertNotNull(bccList, "the email's BCC List is empty"); - Assertions.assertEquals(1, bccList.size(), "the email contained more than one recipient when the single email per user option is enabled"); - - final String username = bccList.getString(0); - - if (!users.contains(username) && !users2.contains(username)) { - Assertions.fail(String.format("The username '%s' was not found in the original usernames lists. First users list: %s, second users list: %s", username, users, users2)); - } - } - } -} diff --git a/connector-email/src/test/java/com/redhat/cloud/notifications/connector/email/EmailRouteBuilderRHCLOUD28631Test.java b/connector-email/src/test/java/com/redhat/cloud/notifications/connector/email/EmailRouteBuilderRHCLOUD28631Test.java index 1d03b90e19..988b6da366 100644 --- a/connector-email/src/test/java/com/redhat/cloud/notifications/connector/email/EmailRouteBuilderRHCLOUD28631Test.java +++ b/connector-email/src/test/java/com/redhat/cloud/notifications/connector/email/EmailRouteBuilderRHCLOUD28631Test.java @@ -8,9 +8,7 @@ import com.redhat.cloud.notifications.connector.email.constants.Routes; import com.redhat.cloud.notifications.connector.email.model.settings.RecipientSettings; import com.redhat.cloud.notifications.connector.email.processors.recipients.RecipientsFilterTest; -import com.redhat.cloud.notifications.connector.email.testprofile.SingleEmailMultipleUsersTestProfile; import io.quarkus.test.junit.QuarkusTest; -import io.quarkus.test.junit.TestProfile; import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonObject; import jakarta.inject.Inject; @@ -36,7 +34,6 @@ * email for multiple users" option enabled. */ @QuarkusTest -@TestProfile(SingleEmailMultipleUsersTestProfile.class) public class EmailRouteBuilderRHCLOUD28631Test extends CamelQuarkusTestSupport { @Inject EmailConnectorConfig emailConnectorConfig; @@ -108,19 +105,16 @@ void testMultipleRecipientSettingsSameUserSendSingleEmail() throws Exception { a.weaveByToUri(this.emailConnectorConfig.getRbacURL()).replace().setBody(new ConstantExpression(body)); }); - // Make sure we replace the BOP endpoint too in order to check the - // usernames we are sending. AdviceWith.adviceWith(this.context, Routes.SEND_EMAIL_BOP, a -> { + // Make sure we replace the BOP endpoint too in order to check the + // usernames we are sending. a.weaveByToUri(String.format("%s*", this.emailConnectorConfig.getBopURL())).replace().to("mock:bopendpoint"); - }); - - // Set up the expected exchanges count that the success endpoint should - // receive at the end of the test. - AdviceWith.adviceWith(this.context, Routes.SEND_EMAIL_BOP_CHOICE, a -> { + // Set up the expected exchanges count that the success endpoint should + // receive at the end of the test. a.mockEndpoints(String.format("direct:%s", ConnectorToEngineRouteBuilder.SUCCESS)); }); - final MockEndpoint successEndpoint = this.getMockEndpoint(String.format("mock:direct:%s", ConnectorToEngineRouteBuilder.SUCCESS)); + final MockEndpoint successEndpoint = this.getMockEndpoint(String.format("mock:direct:%s", ConnectorToEngineRouteBuilder.SUCCESS), false); successEndpoint.expectedMessageCount(1); // Send the exchange to the entry point of the email connector. @@ -132,7 +126,7 @@ void testMultipleRecipientSettingsSameUserSendSingleEmail() throws Exception { // Get the exchanges that we sent to BOP. In theory, since we are // sending a single email for multiple users, we should only receive // one exchabnge here. - final MockEndpoint bopEndpoint = this.getMockEndpoint("mock:bopendpoint"); + final MockEndpoint bopEndpoint = this.getMockEndpoint("mock:bopendpoint", false); final List exchanges = bopEndpoint.getExchanges(); Assertions.assertEquals(1, exchanges.size(), "unexpected number exchanges present in BOP's endpoint"); diff --git a/connector-email/src/test/java/com/redhat/cloud/notifications/connector/email/EmailRouteBuilderTest.java b/connector-email/src/test/java/com/redhat/cloud/notifications/connector/email/EmailRouteBuilderTest.java index 3ef83905fb..7dcdbd831a 100644 --- a/connector-email/src/test/java/com/redhat/cloud/notifications/connector/email/EmailRouteBuilderTest.java +++ b/connector-email/src/test/java/com/redhat/cloud/notifications/connector/email/EmailRouteBuilderTest.java @@ -1,32 +1,21 @@ package com.redhat.cloud.notifications.connector.email; import com.redhat.cloud.notifications.connector.email.config.EmailConnectorConfig; -import com.redhat.cloud.notifications.connector.email.constants.ExchangeProperty; -import com.redhat.cloud.notifications.connector.email.constants.Routes; -import com.redhat.cloud.notifications.connector.email.model.settings.User; import com.redhat.cloud.notifications.connector.email.processors.bop.ssl.BOPTrustManager; import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.TestProfile; import jakarta.inject.Inject; import org.apache.camel.Endpoint; -import org.apache.camel.Exchange; import org.apache.camel.Expression; -import org.apache.camel.ProducerTemplate; -import org.apache.camel.builder.AdviceWith; import org.apache.camel.component.caffeine.CaffeineConfiguration; import org.apache.camel.component.caffeine.EvictionType; import org.apache.camel.component.caffeine.cache.CaffeineCacheComponent; -import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.quarkus.test.CamelQuarkusTestSupport; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import java.io.IOException; -import java.util.List; -import java.util.Set; - -import static com.redhat.cloud.notifications.connector.email.TestUtils.createUsers; @QuarkusTest @TestProfile(EmailRouteBuilderTest.class) @@ -37,9 +26,6 @@ public class EmailRouteBuilderTest extends CamelQuarkusTestSupport { @Inject EmailRouteBuilder emailRouteBuilder; - @Inject - ProducerTemplate producerTemplate; - @Override public boolean isUseAdviceWith() { return true; @@ -57,30 +43,6 @@ public boolean isUseRouteBuilder() { return false; } - @Test - void testIndividualEmailPerUser() throws Exception { - AdviceWith.adviceWith(this.context, Routes.SEND_EMAIL_BOP_SINGLE_PER_USER, a -> { - a.mockEndpointsAndSkip(String.format("direct:%s", Routes.SEND_EMAIL_BOP)); - }); - - final Set users = createUsers("a", "b", "c", "d", "e"); - - final Exchange exchange = this.createExchangeWithBody(""); - exchange.setProperty(ExchangeProperty.FILTERED_USERS, users); - - final MockEndpoint sendEmailBopEndpoint = this.getMockEndpoint(String.format("mock:direct:%s", Routes.SEND_EMAIL_BOP), false); - sendEmailBopEndpoint.expectedMessageCount(5); - - this.producerTemplate.send(String.format("direct:%s", Routes.SEND_EMAIL_BOP_SINGLE_PER_USER), exchange); - - sendEmailBopEndpoint.assertIsSatisfied(); - - final List splittedExchanges = sendEmailBopEndpoint.getExchanges(); - for (final Exchange splittedExchange : splittedExchanges) { - Assertions.assertTrue(splittedExchange.getProperty(ExchangeProperty.SINGLE_EMAIL_PER_USER, Boolean.class), "after splitting the usernames' list, the resulting exchanges did not contain the 'single email per user' flag defined as a property"); - } - } - /** * Tests that the function under test creates the BOP endpoint with the * {@link BOPTrustManager} class as the SSL context parameters, and that diff --git a/connector-email/src/test/java/com/redhat/cloud/notifications/connector/email/EmptyRecipientsTest.java b/connector-email/src/test/java/com/redhat/cloud/notifications/connector/email/EmptyRecipientsTest.java index d0bb241805..94e1040a26 100644 --- a/connector-email/src/test/java/com/redhat/cloud/notifications/connector/email/EmptyRecipientsTest.java +++ b/connector-email/src/test/java/com/redhat/cloud/notifications/connector/email/EmptyRecipientsTest.java @@ -1,5 +1,6 @@ package com.redhat.cloud.notifications.connector.email; +import com.redhat.cloud.notifications.connector.email.config.EmailConnectorConfig; import io.quarkus.test.junit.QuarkusTest; import jakarta.inject.Inject; import org.apache.camel.Exchange; @@ -14,13 +15,14 @@ import static com.redhat.cloud.notifications.connector.ConnectorToEngineRouteBuilder.SUCCESS; import static com.redhat.cloud.notifications.connector.email.constants.ExchangeProperty.FILTERED_USERS; import static com.redhat.cloud.notifications.connector.email.constants.Routes.SEND_EMAIL_BOP; -import static com.redhat.cloud.notifications.connector.email.constants.Routes.SEND_EMAIL_BOP_CHOICE; -import static com.redhat.cloud.notifications.connector.email.constants.Routes.SEND_EMAIL_BOP_SINGLE_PER_USER; import static org.apache.camel.builder.AdviceWith.adviceWith; @QuarkusTest public class EmptyRecipientsTest extends CamelQuarkusTestSupport { + @Inject + EmailConnectorConfig emailConnectorConfig; + @Inject ProducerTemplate producerTemplate; @@ -35,30 +37,25 @@ void test() throws Exception { Exchange exchange = createExchangeWithBody(""); exchange.setProperty(FILTERED_USERS, new HashSet<>()); - adviceWith(SEND_EMAIL_BOP_CHOICE, context(), new AdviceWithRouteBuilder() { + adviceWith(SEND_EMAIL_BOP, context(), new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { mockEndpointsAndSkip( - "direct:" + SEND_EMAIL_BOP_SINGLE_PER_USER, - "direct:" + SEND_EMAIL_BOP, + emailConnectorConfig.getBopURL() + "*", "direct:" + SUCCESS ); } }); - MockEndpoint sendEmailBopSinglePerUserEndpoint = getMockEndpoint("mock:direct:" + SEND_EMAIL_BOP_SINGLE_PER_USER); - sendEmailBopSinglePerUserEndpoint.expectedMessageCount(0); - - MockEndpoint sendEmailBopEndpoint = getMockEndpoint("mock:direct:" + SEND_EMAIL_BOP); - sendEmailBopEndpoint.expectedMessageCount(0); + MockEndpoint bopEndpoint = getMockEndpoint("mock:" + emailConnectorConfig.getBopURL().replace("https://", "https:"), false); + bopEndpoint.expectedMessageCount(0); - MockEndpoint successEndpoint = getMockEndpoint("mock:direct:" + SUCCESS); + MockEndpoint successEndpoint = getMockEndpoint("mock:direct:" + SUCCESS, false); successEndpoint.expectedMessageCount(1); - producerTemplate.send("direct:" + SEND_EMAIL_BOP_CHOICE, exchange); + producerTemplate.send("direct:" + SEND_EMAIL_BOP, exchange); - sendEmailBopSinglePerUserEndpoint.assertIsSatisfied(); - sendEmailBopEndpoint.assertIsSatisfied(); + bopEndpoint.assertIsSatisfied(); successEndpoint.assertIsSatisfied(); } } diff --git a/connector-email/src/test/java/com/redhat/cloud/notifications/connector/email/processors/bop/BOPRequestPreparerTest.java b/connector-email/src/test/java/com/redhat/cloud/notifications/connector/email/processors/bop/BOPRequestPreparerTest.java index fad666fff9..097cc032e5 100644 --- a/connector-email/src/test/java/com/redhat/cloud/notifications/connector/email/processors/bop/BOPRequestPreparerTest.java +++ b/connector-email/src/test/java/com/redhat/cloud/notifications/connector/email/processors/bop/BOPRequestPreparerTest.java @@ -15,7 +15,6 @@ import java.util.Map; import java.util.Set; -import static com.redhat.cloud.notifications.connector.email.TestUtils.createUser; import static com.redhat.cloud.notifications.connector.email.TestUtils.createUsers; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; @@ -96,63 +95,4 @@ void testProcess(boolean skipBopUsersResolution) { emailConnectorConfig.setSkipBopUsersResolution(false); } } - - /** - * Tests that the processor handles individual email sending properly. - */ - @Deprecated(forRemoval = true) - @ValueSource(booleans = { true, false }) - @ParameterizedTest - void testProcessSingleEmail(boolean skipBopUsersResolution) { - emailConnectorConfig.setSkipBopUsersResolution(skipBopUsersResolution); - - try { - // Prepare the properties that the processor expects. - final String emailSubject = "this is a fake subject"; - final String emailBody = "this is a fake body"; - - // The "split" operation is going to leave the user in the - // exchange's body, so we only need to pass a simple string in this - // case. - final Exchange exchange = this.createExchangeWithBody(createUser("a")); - exchange.setProperty(ExchangeProperty.RENDERED_SUBJECT, emailSubject); - exchange.setProperty(ExchangeProperty.RENDERED_BODY, emailBody); - exchange.setProperty(ExchangeProperty.SINGLE_EMAIL_PER_USER, true); - - // Call the processor under test. - this.bopRequestPreparer.process(exchange); - - // Assert that the headers are correct. - final Map headers = exchange.getMessage().getHeaders(); - assertEquals(HttpMethods.POST, headers.get(Exchange.HTTP_METHOD)); - assertEquals("/v1/sendEmails", headers.get(Exchange.HTTP_PATH)); - assertEquals("application/json", headers.get(Exchange.CONTENT_TYPE)); - assertEquals(this.emailConnectorConfig.getBopApiToken(), headers.get(Constants.BOP_API_TOKEN_HEADER)); - assertEquals(this.emailConnectorConfig.getBopClientId(), headers.get(Constants.BOP_CLIENT_ID_HEADER)); - assertEquals(this.emailConnectorConfig.getBopEnv(), headers.get(Constants.BOP_ENV_HEADER)); - - // Assert that the message's body is correct. - final JsonObject actualBody = new JsonObject(exchange.getMessage().getBody(String.class)); - final JsonObject actualEmail = actualBody.getJsonArray("emails").getJsonObject(0); - - assertEquals("this is a fake subject", actualEmail.getString("subject")); - assertEquals("this is a fake body", actualEmail.getString("body")); - assertTrue(actualEmail.getJsonArray("recipients").isEmpty()); - assertTrue(actualEmail.getJsonArray("ccList").isEmpty()); - assertEquals(1, actualEmail.getJsonArray("bccList").size()); - if (skipBopUsersResolution) { - assertTrue(actualEmail.getJsonArray("bccList").contains("a-email")); - } else { - assertTrue(actualEmail.getJsonArray("bccList").contains("a")); - } - assertEquals("html", actualEmail.getString("bodyType")); - if (skipBopUsersResolution) { - assertTrue(actualBody.getBoolean("skipUsersResolution")); - } else { - assertNull(actualBody.getBoolean("skipUsersResolution")); - } - } finally { - emailConnectorConfig.setSkipBopUsersResolution(false); - } - } } diff --git a/connector-email/src/test/java/com/redhat/cloud/notifications/connector/email/testprofile/SingleEmailMultipleUsersTestProfile.java b/connector-email/src/test/java/com/redhat/cloud/notifications/connector/email/testprofile/SingleEmailMultipleUsersTestProfile.java deleted file mode 100644 index 891ba19374..0000000000 --- a/connector-email/src/test/java/com/redhat/cloud/notifications/connector/email/testprofile/SingleEmailMultipleUsersTestProfile.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.redhat.cloud.notifications.connector.email.testprofile; - -import com.redhat.cloud.notifications.connector.email.config.EmailConnectorConfig; -import io.quarkus.test.junit.QuarkusTestProfile; - -import java.util.Map; - -/** - * Test profile for enabling sending a single email for multiple users. - */ -@Deprecated(forRemoval = true) -public class SingleEmailMultipleUsersTestProfile implements QuarkusTestProfile { - @Override - public Map getConfigOverrides() { - return Map.of(EmailConnectorConfig.SINGLE_EMAIL_PER_USER, "false"); - } -}