Skip to content

Commit

Permalink
fix(mail-connector): check htmlBody and/or body is null and throw an …
Browse files Browse the repository at this point in the history
…error (#3796)

* fix(mail-connector): check htmlBody and/or body is null and throw an error
if not throw an error, connector will retry it over and over again.

* chore(mail-connector): build the assertation inside the `SmtpSendMail` record

* test(mail-connector): adding unit test to validate `isEmailMessageValid`

* chore(mail-connector): run mvn spotless to format the `SmtpSendEmailTest` class

* chore(mail-connector): use mvn:licence to add the licence to the file
  • Loading branch information
itsnuyen authored Dec 18, 2024
1 parent 8a887b5 commit 657810f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -138,4 +139,13 @@ public record SmtpSendEmail(
description = "Email's attachment. e.g., =[ document1, document2]",
binding = @TemplateProperty.PropertyBinding(name = "data.smtpAction.attachments"))
List<Document> 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;
};
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 657810f

Please sign in to comment.