Skip to content

Commit

Permalink
RHCLOUD-29035 Stop calling BOP with an empty recipients list (RedHatI…
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenneg authored Nov 2, 2023
1 parent 6ed41d1 commit c9b6aea
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));

/*
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="https://people.apache.org/~dkulp/camel/camel-test.html">dkulp's Apache Camel Test documentation page</a>.
* @return {@code false} in order to stop the Camel Context from booting
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit c9b6aea

Please sign in to comment.