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 9a86db32e0..2f6ed4d97e 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 @@ -44,6 +44,7 @@ import static com.redhat.cloud.notifications.connector.ConnectorToEngineRouteBuilder.SUCCESS; import static com.redhat.cloud.notifications.connector.ExchangeProperty.ID; import static com.redhat.cloud.notifications.connector.ExchangeProperty.ORG_ID; +import static com.redhat.cloud.notifications.connector.email.constants.ExchangeProperty.FILTERED_USERS; import static org.apache.camel.LoggingLevel.INFO; @ApplicationScoped @@ -369,12 +370,16 @@ public void configureRoute() throws Exception { from(direct(Routes.SEND_EMAIL_BOP_CHOICE)) .routeId(Routes.SEND_EMAIL_BOP_CHOICE) .choice() + .when(simpleF("${exchangeProperty.%s.isEmpty()}", FILTERED_USERS)) + // 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() - .log(INFO, this.getClass().getName(), "Sent Email notification [orgId=${exchangeProperty." + ORG_ID + "}, historyId=${exchangeProperty." + ID + "}]") .to(direct(SUCCESS)); /* @@ -398,7 +403,7 @@ public void configureRoute() throws Exception { .routeId(Routes.SEND_EMAIL_BOP_SINGLE_PER_USER) // Clear all the headers that may come from the previous route. .removeHeaders("*") - .split(simpleF("${exchangeProperty.%s}", ExchangeProperty.FILTERED_USERS)) + .split(simpleF("${exchangeProperty.%s}", FILTERED_USERS)) .setProperty(ExchangeProperty.SINGLE_EMAIL_PER_USER, constant(true)) .to(direct(Routes.SEND_EMAIL_BOP)) .end(); 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 1d613921a8..3ef83905fb 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 @@ -46,7 +46,7 @@ public boolean isUseAdviceWith() { } /** - * Disables the rout builder to ensure that the Camel Context does not get + * Disables the route builder to ensure that the Camel Context does not get * started before the routes have been advised. More information is * available at the dkulp's Apache Camel Test documentation page. * @return {@code false} in order to stop the Camel Context from booting 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 new file mode 100644 index 0000000000..d0bb241805 --- /dev/null +++ b/connector-email/src/test/java/com/redhat/cloud/notifications/connector/email/EmptyRecipientsTest.java @@ -0,0 +1,64 @@ +package com.redhat.cloud.notifications.connector.email; + +import io.quarkus.test.junit.QuarkusTest; +import jakarta.inject.Inject; +import org.apache.camel.Exchange; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.AdviceWithRouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.quarkus.test.CamelQuarkusTestSupport; +import org.junit.jupiter.api.Test; + +import java.util.HashSet; + +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 + ProducerTemplate producerTemplate; + + @Override + public boolean isUseRouteBuilder() { + return false; + } + + @Test + void test() throws Exception { + + Exchange exchange = createExchangeWithBody(""); + exchange.setProperty(FILTERED_USERS, new HashSet<>()); + + adviceWith(SEND_EMAIL_BOP_CHOICE, context(), new AdviceWithRouteBuilder() { + @Override + public void configure() throws Exception { + mockEndpointsAndSkip( + "direct:" + SEND_EMAIL_BOP_SINGLE_PER_USER, + "direct:" + SEND_EMAIL_BOP, + "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 successEndpoint = getMockEndpoint("mock:direct:" + SUCCESS); + successEndpoint.expectedMessageCount(1); + + producerTemplate.send("direct:" + SEND_EMAIL_BOP_CHOICE, exchange); + + sendEmailBopSinglePerUserEndpoint.assertIsSatisfied(); + sendEmailBopEndpoint.assertIsSatisfied(); + successEndpoint.assertIsSatisfied(); + } +}