Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mail-connector): check htmlBody and/or body is null and throw an error #3796

Merged
merged 8 commits into from
Dec 18, 2024
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);
}
}
Loading