-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RHCLOUD-36586] Various improvement of email connector:
* refactor tests * use a jackson mapper to read incoming payload data * rename external criteria to extenal criterion
- Loading branch information
Showing
16 changed files
with
435 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...rc/main/java/com/redhat/cloud/notifications/connector/email/CloudEventHistoryBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.redhat.cloud.notifications.connector.email; | ||
|
||
import com.redhat.cloud.notifications.connector.OutgoingCloudEventBuilder; | ||
import io.vertx.core.json.JsonObject; | ||
import jakarta.annotation.Priority; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.inject.Alternative; | ||
import org.apache.camel.Exchange; | ||
import org.apache.camel.Message; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
import static com.redhat.cloud.notifications.connector.ExchangeProperty.SUCCESSFUL; | ||
|
||
@ApplicationScoped | ||
@Alternative | ||
@Priority(0) // The value doesn't matter. | ||
public class CloudEventHistoryBuilder extends OutgoingCloudEventBuilder { | ||
|
||
public static final String TOTAL_RECIPIENTS_KEY = "total_recipients"; | ||
public static final String TOTAL_FAILURE_RECIPIENTS_KEY = "total_failure_recipients"; | ||
|
||
@Override | ||
public void process(Exchange exchange) throws Exception { | ||
int totalRecipients = exchange.getProperty(TOTAL_RECIPIENTS_KEY, Integer.class); | ||
Optional<Set<String>> recipientsWithError = Optional.ofNullable(exchange.getProperty("TODO", Set.class)); | ||
exchange.setProperty(SUCCESSFUL, recipientsWithError.isEmpty() || recipientsWithError.get().size() == 0); | ||
super.process(exchange); | ||
|
||
Message in = exchange.getIn(); | ||
JsonObject cloudEvent = new JsonObject(in.getBody(String.class)); | ||
JsonObject data = new JsonObject(cloudEvent.getString("data")); | ||
data.getJsonObject("details").put(TOTAL_RECIPIENTS_KEY, totalRecipients); | ||
|
||
if (recipientsWithError.isPresent()) { | ||
data.getJsonObject("details").put(TOTAL_FAILURE_RECIPIENTS_KEY, recipientsWithError.get().size()); | ||
} | ||
|
||
cloudEvent.put("data", data.encode()); | ||
in.setBody(cloudEvent.encode()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...src/main/java/com/redhat/cloud/notifications/connector/email/model/EmailNotification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.redhat.cloud.notifications.connector.email.model; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.redhat.cloud.notifications.connector.email.model.settings.RecipientSettings; | ||
import io.vertx.core.json.JsonObject; | ||
import java.util.Collection; | ||
|
||
/** | ||
* Represents the data structure that the email connector is expecting. | ||
* @param emailBody the rendered body of the email to be sent. | ||
* @param emailSubject the rendered subject of the email to be sent. | ||
* @param emailSender the sender that will appear in the email when | ||
* the user receives it. | ||
* @param orgId the organization ID associated with the | ||
* triggered event. | ||
* @param recipientSettings the collection of recipient settings extracted | ||
* from both the event and the related endpoints | ||
* to the event. | ||
* @param subscribers the list of usernames who subscribed to the | ||
* event type. | ||
* @param unsubscribers the list of usernames who unsubscribed from the | ||
* event type. | ||
* @param subscribedByDefault true if the event type is subscribed by | ||
* default. | ||
* @param externalAuthorizationCriterion forward received authorization criterion. | ||
* | ||
*/ | ||
public record EmailNotification( | ||
@JsonProperty("email_body") String emailBody, | ||
@JsonProperty("email_subject") String emailSubject, | ||
@JsonProperty("email_sender") String emailSender, | ||
@JsonProperty("orgId") String orgId, | ||
@JsonProperty("recipient_settings") Collection<RecipientSettings> recipientSettings, | ||
@JsonProperty("subscribers") Collection<String> subscribers, | ||
@JsonProperty("unsubscribers") Collection<String> unsubscribers, | ||
@JsonProperty("subscribed_by_default") boolean subscribedByDefault, | ||
@JsonProperty("external_authorization_criterion") JsonObject externalAuthorizationCriterion | ||
) { } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.stream.Collectors; | ||
|
||
import static com.redhat.cloud.notifications.connector.email.CloudEventHistoryBuilder.TOTAL_RECIPIENTS_KEY; | ||
import static java.util.stream.Collectors.toSet; | ||
|
||
@ApplicationScoped | ||
|
@@ -49,6 +50,7 @@ public void process(final Exchange exchange) throws JsonProcessingException { | |
emails.removeAll(forbiddenEmail); | ||
} | ||
recipientsList.addAll(emails); | ||
exchange.setProperty(TOTAL_RECIPIENTS_KEY, recipientsList.size()); | ||
|
||
// We have to remove one from the limit, because a default recipient (like [email protected]) will be automatically added | ||
exchange.setProperty(ExchangeProperty.FILTERED_USERS, partition(recipientsList, emailConnectorConfig.getMaxRecipientsPerEmail() - 1)); | ||
|
59 changes: 59 additions & 0 deletions
59
...il/src/test/java/com/redhat/cloud/notifications/connector/email/BuildBopEndpointTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.redhat.cloud.notifications.connector.email; | ||
|
||
import com.redhat.cloud.notifications.connector.email.config.EmailConnectorConfig; | ||
import com.redhat.cloud.notifications.connector.http.SslTrustAllManager; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.quarkus.test.junit.mockito.InjectSpy; | ||
import jakarta.inject.Inject; | ||
import org.apache.camel.Endpoint; | ||
import org.apache.camel.quarkus.test.CamelQuarkusTestSupport; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.mockito.Mockito.when; | ||
|
||
@QuarkusTest | ||
public class BuildBopEndpointTest extends CamelQuarkusTestSupport { | ||
|
||
@InjectSpy | ||
EmailConnectorConfig emailConnectorConfig; | ||
|
||
@Inject | ||
EmailRouteBuilder emailRouteBuilder; | ||
|
||
@Override | ||
public boolean isUseAdviceWith() { | ||
return true; | ||
} | ||
|
||
/** | ||
* 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 | ||
* before the routes have been advised. | ||
*/ | ||
@Override | ||
public boolean isUseRouteBuilder() { | ||
return false; | ||
} | ||
|
||
/** | ||
* Tests that the function under test creates the BOP endpoint with the | ||
* {@link SslTrustAllManager} class as the SSL context parameters, and that | ||
* that class is essentially a NOOP class. | ||
* @throws Exception if the endpoint could not be created. | ||
*/ | ||
@Test | ||
void testBuildBOPEndpoint() throws Exception { | ||
String initialBopUrl = emailConnectorConfig.getBopURL(); | ||
when(emailConnectorConfig.getBopURL()).thenReturn("https://test.com"); | ||
|
||
Endpoint bopEndpoint = this.emailRouteBuilder.setUpBOPEndpointV1().resolve(this.context); | ||
Assertions.assertEquals(this.emailConnectorConfig.getBopURL(), bopEndpoint.getEndpointBaseUri(), "the base URI of the endpoint is not the same as the one set through the properties"); | ||
|
||
final String bopEndpointURI = bopEndpoint.getEndpointUri(); | ||
Assertions.assertTrue(bopEndpointURI.contains("trustManager%3Dcom.redhat.cloud.notifications.connector.http.SslTrustAllManager"), "the endpoint does not contain a reference to the SslTrustAllManager"); | ||
Assertions.assertTrue(bopEndpointURI.contains("x509HostnameVerifier=NO_OP"), "the base URI does not contain a mention to the NO_OP hostname verifier"); | ||
} | ||
} |
Oops, something went wrong.