diff --git a/connectors/email/src/main/java/io/camunda/connector/email/outbound/protocols/actions/SmtpSendEmail.java b/connectors/email/src/main/java/io/camunda/connector/email/outbound/protocols/actions/SmtpSendEmail.java index 9b4e99cb61..fe0e8f5e3e 100644 --- a/connectors/email/src/main/java/io/camunda/connector/email/outbound/protocols/actions/SmtpSendEmail.java +++ b/connectors/email/src/main/java/io/camunda/connector/email/outbound/protocols/actions/SmtpSendEmail.java @@ -11,6 +11,7 @@ import io.camunda.connector.generator.java.annotation.TemplateSubType; import io.camunda.document.Document; import jakarta.validation.Valid; +import jakarta.validation.constraints.AssertTrue; import jakarta.validation.constraints.NotNull; import java.util.List; import java.util.Map; @@ -138,4 +139,13 @@ public record SmtpSendEmail( description = "Email's attachment. e.g., =[ document1, document2]", binding = @TemplateProperty.PropertyBinding(name = "data.smtpAction.attachments")) List attachments) - implements SmtpAction {} + implements SmtpAction { + @AssertTrue(message = "Please provide a proper message body") + public boolean isEmailMessageValid() { + return switch (contentType) { + case PLAIN -> body != null; + case HTML -> htmlBody != null; + case MULTIPART -> body != null && htmlBody != null; + }; + } +} diff --git a/connectors/email/src/test/java/io/camunda/connector/email/outbound/protocols/actions/SmtpSendEmailTest.java b/connectors/email/src/test/java/io/camunda/connector/email/outbound/protocols/actions/SmtpSendEmailTest.java new file mode 100644 index 0000000000..489cce8a8f --- /dev/null +++ b/connectors/email/src/test/java/io/camunda/connector/email/outbound/protocols/actions/SmtpSendEmailTest.java @@ -0,0 +1,67 @@ +/* + * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH + * under one or more contributor license agreements. Licensed under a proprietary license. + * See the License.txt file for more information. You may not use this file + * except in compliance with the proprietary license. + */ +package io.camunda.connector.email.outbound.protocols.actions; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +class SmtpSendEmailTest { + + @Test + void isEmailMessageValidPlainTextWithBody() { + SmtpSendEmail smtpSendEmail = mockSmtpSendEmail(ContentType.PLAIN, "text", null); + assertTrue(smtpSendEmail.isEmailMessageValid()); + } + + @Test + void isEmailMessageNotValidPlainTextWithoutBody() { + SmtpSendEmail smtpSendEmail = mockSmtpSendEmail(ContentType.PLAIN, null, null); + assertFalse(smtpSendEmail.isEmailMessageValid()); + } + + @Test + void isEmailMessageValidHtmlTextWithBody() { + SmtpSendEmail smtpSendEmail = mockSmtpSendEmail(ContentType.HTML, null, "text"); + assertTrue(smtpSendEmail.isEmailMessageValid()); + } + + @Test + void isEmailMessageNotValidHtmlWithoutBody() { + SmtpSendEmail smtpSendEmail = mockSmtpSendEmail(ContentType.HTML, null, null); + assertFalse(smtpSendEmail.isEmailMessageValid()); + } + + @Test + void isEmailMessageValidMultiWithBody() { + SmtpSendEmail smtpSendEmail = mockSmtpSendEmail(ContentType.MULTIPART, "text", "text2"); + assertTrue(smtpSendEmail.isEmailMessageValid()); + } + + @Test + void isEmailMessageNotValidHtmlTextWithoutBothBodies() { + SmtpSendEmail smtpSendEmail = mockSmtpSendEmail(ContentType.MULTIPART, null, null); + assertFalse(smtpSendEmail.isEmailMessageValid()); + } + + @Test + void isEmailMessageNotValidMultiWithoutHtmlBody() { + SmtpSendEmail smtpSendEmail = mockSmtpSendEmail(ContentType.MULTIPART, "text", null); + assertFalse(smtpSendEmail.isEmailMessageValid()); + } + + @Test + void isEmailMessageNotValidMultiWithoutBody() { + SmtpSendEmail smtpSendEmail = mockSmtpSendEmail(ContentType.MULTIPART, null, "text"); + assertFalse(smtpSendEmail.isEmailMessageValid()); + } + + private SmtpSendEmail mockSmtpSendEmail(ContentType contentType, String body, String htmlBody) { + return new SmtpSendEmail("", "", "", "", null, "", contentType, body, htmlBody, null); + } +}