Skip to content

Commit

Permalink
Allows the MailTemplate to produce / return the sent Mail.
Browse files Browse the repository at this point in the history
Relates to quarkusio#45135
  • Loading branch information
cescoffier committed Dec 16, 2024
1 parent 5e6fb77 commit a7177aa
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import jakarta.inject.Inject;
import jakarta.inject.Singleton;

import org.assertj.core.api.Assertions;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
Expand All @@ -29,6 +30,8 @@ public class MailTemplateLocationTest {
@Test
public void testLocation() {
mailTemplates.send().await().atMost(Duration.ofSeconds(5));
Assertions.assertThat(mailTemplates.sendAndGetMail().await().atMost(Duration.ofSeconds(5))).isNotNull();
Assertions.assertThat(mailTemplates.sendAndAwait()).isNotNull();
}

@Singleton
Expand All @@ -41,5 +44,15 @@ static class MailTemplates {
Uni<Void> send() {
return confirmationMailTemplate.to("quarkus@quarkus.io").subject("Test").data("name", "Foo").send();
}

Uni<Mail> sendAndGetMail() {
return confirmationMailTemplate.to("quarkus@quarkus.io").subject("Test").data("name", "Foo")
.sendAndGetMail();
}

Mail sendAndAwait() {
return confirmationMailTemplate.to("quarkus@quarkus.io").subject("Test").data("name", "Foo")
.sendAndAwait();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,24 @@ default MailTemplateInstance setAttribute(String key, Object value) {
* Sends all e-mail definitions based on available template variants, i.e. {@code text/html} and {@code text/plain}
* template variants.
*
* @return a {@link Uni} indicating when the mails have been sent
* @return a {@link Uni} indicating when the mail has been sent
* @see ReactiveMailer#send(Mail...)
*/
@CheckReturnValue
default Uni<Void> send() {
return sendAndGetMail().replaceWithVoid();
}

/**
* Sends all e-mail definitions based on available template variants, i.e. {@code text/html} and {@code text/plain}
* template variants.
*
* @return a {@link Uni} indicating when the mail has been sent. Unlike {{@link #send()}}, this method returns the sent
* {@link Mail} object.
* @see ReactiveMailer#send(Mail...)
*/
@CheckReturnValue
default Uni<Mail> sendAndGetMail() {
throw new UnsupportedOperationException();
}

Expand All @@ -128,8 +141,8 @@ default Uni<Void> send() {
*
* @see #send()
*/
default void sendAndAwait() {
send().await().indefinitely();
default Mail sendAndAwait() {
return sendAndGetMail().await().indefinitely();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public TemplateInstance templateInstance() {
}

@Override
public Uni<Void> send() {
public Uni<Mail> sendAndGetMail() {
Object variantsAttr = templateInstance.getAttribute(TemplateInstance.VARIANTS);
if (variantsAttr != null) {
List<Result> results = new ArrayList<>();
Expand All @@ -148,11 +148,11 @@ public CompletionStage<? extends String> get() {
}
List<Uni<String>> unis = results.stream().map(Result::resolve).collect(Collectors.toList());
return Uni.combine().all().unis(unis)
.combinedWith(combine(results))
.chain(new Function<Mail, Uni<? extends Void>>() {
.with(combine(results))
.chain(new Function<Mail, Uni<? extends Mail>>() {
@Override
public Uni<? extends Void> apply(Mail m) {
return mailer.send(m);
public Uni<? extends Mail> apply(Mail m) {
return mailer.send(m).replaceWith(m);
}
});
} else {
Expand Down

0 comments on commit a7177aa

Please sign in to comment.