Skip to content

Commit 5203767

Browse files
committed
Added comment for MailtrapMail#subject field, updated mail validation
1 parent ddda74d commit 5203767

File tree

6 files changed

+47
-41
lines changed

6 files changed

+47
-41
lines changed

src/main/java/io/mailtrap/api/api_resource/SendApiResource.java

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,47 +23,50 @@ protected SendApiResource(MailtrapConfig config, CustomValidator customValidator
2323
* @throws InvalidRequestBodyException If the request body is invalid.
2424
*/
2525
protected void validateRequestBodyOrThrowException(MailtrapMail mail) throws InvalidRequestBodyException {
26+
// Check if the mail object itself is null
2627
if (mail == null) {
2728
throw new InvalidRequestBodyException("Mail must not be null");
2829
}
2930

30-
// All three fields can not be null - we should submit either subject and text and/or html or templateUuid
31-
if (StringUtils.isEmpty(mail.getSubject())
31+
// Check if all three subject, text, and html are empty
32+
boolean isSubjectTextHtmlEmpty = StringUtils.isEmpty(mail.getSubject())
3233
&& StringUtils.isEmpty(mail.getText())
33-
&& StringUtils.isEmpty(mail.getHtml())
34-
&& StringUtils.isEmpty(mail.getTemplateUuid())) {
35-
throw new InvalidRequestBodyException(String.format("Mail %s or %s and %s or %s or both must not be null or empty",
36-
MailtrapMail.Fields.templateUuid,
37-
MailtrapMail.Fields.subject,
38-
MailtrapMail.Fields.text,
39-
MailtrapMail.Fields.html));
40-
}
34+
&& StringUtils.isEmpty(mail.getHtml());
4135

42-
if (StringUtils.isNotEmpty(mail.getTemplateUuid())) {
43-
// When templateUuid is set
44-
if (StringUtils.isNotEmpty(mail.getText()) ||
45-
StringUtils.isNotEmpty(mail.getHtml()) ||
46-
StringUtils.isNotEmpty(mail.getSubject())) {
47-
throw new InvalidRequestBodyException(String.format("When %s is used - %s and %s and %s must not be used",
48-
MailtrapMail.Fields.templateUuid,
49-
MailtrapMail.Fields.text,
50-
MailtrapMail.Fields.subject,
51-
MailtrapMail.Fields.html));
52-
}
36+
// Validate depending on whether the templateUuid is set
37+
if (StringUtils.isEmpty(mail.getTemplateUuid())) {
38+
// Validation for the scenario where templateUuid is not provided
39+
validateWithoutTemplate(mail, isSubjectTextHtmlEmpty);
5340
} else {
54-
// When templateUuid is not set - templateVariables should not be used
55-
if (MapUtils.isNotEmpty(mail.getTemplateVariables())) {
56-
throw new InvalidRequestBodyException(String.format("Mail %s must only be used with %s",
57-
MailtrapMail.Fields.templateVariables,
58-
MailtrapMail.Fields.templateUuid));
59-
}
60-
if(StringUtils.isEmpty(mail.getSubject())) {
61-
throw new InvalidRequestBodyException(String.format("When %s is not set - the %s must be set",
62-
MailtrapMail.Fields.templateUuid,
63-
MailtrapMail.Fields.subject));
64-
}
41+
// Validation for the scenario where templateUuid is provided
42+
validateWithTemplate(mail);
6543
}
6644

45+
// Additional validation logic (assumed to be provided by the user)
6746
validateRequestBodyAndThrowException(mail);
6847
}
48+
49+
private void validateWithoutTemplate(MailtrapMail mail, boolean isSubjectTextHtmlEmpty) throws InvalidRequestBodyException {
50+
// Ensure that at least subject, text, or html is provided if templateUuid is not set
51+
if (isSubjectTextHtmlEmpty) {
52+
throw new InvalidRequestBodyException("Mail must have subject and either text or html when templateUuid is not provided");
53+
}
54+
55+
// Ensure templateVariables are not used if templateUuid is not set
56+
if (MapUtils.isNotEmpty(mail.getTemplateVariables())) {
57+
throw new InvalidRequestBodyException("Mail templateVariables must only be used with templateUuid");
58+
}
59+
60+
// Ensure the subject is not empty
61+
if (StringUtils.isEmpty(mail.getSubject())) {
62+
throw new InvalidRequestBodyException("Subject must not be null or empty");
63+
}
64+
}
65+
66+
private void validateWithTemplate(MailtrapMail mail) throws InvalidRequestBodyException {
67+
// Ensure that subject, text, and html are not used when templateUuid is set
68+
if (StringUtils.isNotEmpty(mail.getText()) || StringUtils.isNotEmpty(mail.getHtml()) || StringUtils.isNotEmpty(mail.getSubject())) {
69+
throw new InvalidRequestBodyException("When templateUuid is used, subject, text, and html must not be used");
70+
}
71+
}
6972
}

src/main/java/io/mailtrap/model/request/emails/MailtrapMail.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public class MailtrapMail extends AbstractModel {
4545
@JsonProperty("custom_variables")
4646
private Map<String, String> customVariables;
4747

48+
/**
49+
* Should be used in conjunction with {@link #text} and/or {@link #html}
50+
*/
4851
private String subject;
4952

5053
private String text;
@@ -54,7 +57,7 @@ public class MailtrapMail extends AbstractModel {
5457
private String category;
5558

5659
/**
57-
* Should be used in conjunction with {@link #subject}, {@link #text} or {@link #html}
60+
* Should NOT be used if {@link #subject}, {@link #text} or {@link #html} is used
5861
*/
5962
@JsonProperty("template_uuid")
6063
private String templateUuid;

src/test/java/io/mailtrap/api/bulk_emails/BulkEmailsImplTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void send_MailWithTemplateUuidAndText_ThrowsInvalidRequestBodyException() {
6868

6969
// Assert
7070
InvalidRequestBodyException exception = assertThrows(InvalidRequestBodyException.class, () -> bulkEmails.send(mail));
71-
assertEquals(TEMPLATE_UUID_IS_USED_TEXT_AND_HTML_SHOULD_BE_EMPTY, exception.getMessage());
71+
assertEquals(TEMPLATE_UUID_IS_USED_SUBJECT_AND_TEXT_AND_HTML_SHOULD_BE_EMPTY, exception.getMessage());
7272
}
7373

7474
@Test
@@ -78,7 +78,7 @@ void send_MailWithTemplateUuidAndHtml_ThrowsInvalidRequestBodyException() {
7878

7979
// Assert
8080
InvalidRequestBodyException exception = assertThrows(InvalidRequestBodyException.class, () -> bulkEmails.send(mail));
81-
assertEquals(TEMPLATE_UUID_IS_USED_TEXT_AND_HTML_SHOULD_BE_EMPTY, exception.getMessage());
81+
assertEquals(TEMPLATE_UUID_IS_USED_SUBJECT_AND_TEXT_AND_HTML_SHOULD_BE_EMPTY, exception.getMessage());
8282
}
8383

8484
@Test

src/test/java/io/mailtrap/api/sending_emails/SendingEmailsImplTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void send_MailWithTemplateUuidAndText_ThrowsInvalidRequestBodyException() {
6868

6969
// Assert
7070
InvalidRequestBodyException exception = assertThrows(InvalidRequestBodyException.class, () -> sendApi.send(mail));
71-
assertEquals(TEMPLATE_UUID_IS_USED_TEXT_AND_HTML_SHOULD_BE_EMPTY, exception.getMessage());
71+
assertEquals(TEMPLATE_UUID_IS_USED_SUBJECT_AND_TEXT_AND_HTML_SHOULD_BE_EMPTY, exception.getMessage());
7272
}
7373

7474
@Test
@@ -78,7 +78,7 @@ void send_MailWithTemplateUuidAndHtml_ThrowsInvalidRequestBodyException() {
7878

7979
// Assert
8080
InvalidRequestBodyException exception = assertThrows(InvalidRequestBodyException.class, () -> sendApi.send(mail));
81-
assertEquals(TEMPLATE_UUID_IS_USED_TEXT_AND_HTML_SHOULD_BE_EMPTY, exception.getMessage());
81+
assertEquals(TEMPLATE_UUID_IS_USED_SUBJECT_AND_TEXT_AND_HTML_SHOULD_BE_EMPTY, exception.getMessage());
8282
}
8383

8484
@Test

src/test/java/io/mailtrap/api/testing_emails/TestingEmailsImplTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void send_MailWithTemplateUuidAndText_ThrowsInvalidRequestBodyException() {
7070

7171
// Assert
7272
InvalidRequestBodyException exception = assertThrows(InvalidRequestBodyException.class, () -> testingApi.send(mail, INBOX_ID));
73-
assertEquals(TEMPLATE_UUID_IS_USED_TEXT_AND_HTML_SHOULD_BE_EMPTY, exception.getMessage());
73+
assertEquals(TEMPLATE_UUID_IS_USED_SUBJECT_AND_TEXT_AND_HTML_SHOULD_BE_EMPTY, exception.getMessage());
7474
}
7575

7676
@Test
@@ -80,7 +80,7 @@ void send_MailWithTemplateUuidAndHtml_ThrowsInvalidRequestBodyException() {
8080

8181
// Assert
8282
InvalidRequestBodyException exception = assertThrows(InvalidRequestBodyException.class, () -> testingApi.send(mail, INBOX_ID));
83-
assertEquals(TEMPLATE_UUID_IS_USED_TEXT_AND_HTML_SHOULD_BE_EMPTY, exception.getMessage());
83+
assertEquals(TEMPLATE_UUID_IS_USED_SUBJECT_AND_TEXT_AND_HTML_SHOULD_BE_EMPTY, exception.getMessage());
8484
}
8585

8686
@Test

src/test/java/io/mailtrap/testutils/BaseSendTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public class BaseSendTest {
1414
protected final String BULK_AND_SANDBOX_TRUE_IN_SENDING_CONFIG = "Bulk mode is not applicable for Testing API";
1515
protected final String INBOX_ID_REQUIRED = "Testing API requires inbox ID";
1616
protected final String INVALID_REQUEST_EMPTY_BODY_FROM_EMAIL = "Invalid request body. Violations: from.email=must not be empty";
17-
protected final String TEMPLATE_UUID_OR_SUBJECT_AND_TEXT_OR_HTML_MUST_NOT_BE_EMPTY = "Mail templateUuid or subject and text or html or both must not be null or empty";
18-
protected final String TEMPLATE_UUID_IS_USED_TEXT_AND_HTML_SHOULD_BE_EMPTY = "When templateUuid is used - text and subject and html must not be used";
17+
protected final String TEMPLATE_UUID_OR_SUBJECT_AND_TEXT_OR_HTML_MUST_NOT_BE_EMPTY = "Mail must have subject and either text or html when templateUuid is not provided";
18+
protected final String TEMPLATE_UUID_IS_USED_SUBJECT_AND_TEXT_AND_HTML_SHOULD_BE_EMPTY = "When templateUuid is used, subject, text, and html must not be used";
1919
protected final String TEMPLATE_VARIABLES_SHOULD_BE_USED_WITH_TEMPLATE_UUID = "Mail templateVariables must only be used with templateUuid";
2020
protected final String MAIL_MUST_NOT_BE_NULL = "Mail must not be null";
2121

0 commit comments

Comments
 (0)