Skip to content

Commit f752d2e

Browse files
committed
Code review updates, improved mail validation
1 parent 25d33e7 commit f752d2e

File tree

10 files changed

+73
-9
lines changed

10 files changed

+73
-9
lines changed

examples/java/io/mailtrap/examples/bulk/Batch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.util.List;
1111
import java.util.Map;
1212

13-
public class Bulk {
13+
public class Batch {
1414

1515
private static final String TOKEN = "<YOUR MAILTRAP TOKEN>";
1616
private static final String SENDER_EMAIL = "[email protected]";

examples/java/io/mailtrap/examples/sending/Batch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.util.List;
1111
import java.util.Map;
1212

13-
public class Bulk {
13+
public class Batch {
1414

1515
private static final String TOKEN = "<YOUR MAILTRAP TOKEN>";
1616
private static final String SENDER_EMAIL = "[email protected]";

examples/java/io/mailtrap/examples/testing/Batch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static void main(String[] args) {
3838
final var batchMail = MailtrapBatchMail.builder()
3939
// Optionally you can add this `base` object - if you have some common data across emails
4040
// Each property can be overridden in `requests` for individual emails
41-
.base(BatchEmailBase.builder().subject("Base Subject for all emails").build())
41+
.base(BatchEmailBase.builder().templateUuid("base-template-uuid").build())
4242
.requests(List.of(mail))
4343
.build();
4444

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import org.apache.commons.collections4.MapUtils;
99
import org.apache.commons.lang3.StringUtils;
1010

11+
import java.util.Objects;
12+
1113
/**
1214
* Abstract class representing a resource for sending emails via Mailtrap API.
1315
*/
@@ -17,10 +19,16 @@ protected SendApiResource(MailtrapConfig config, CustomValidator customValidator
1719
super(config, customValidator);
1820
}
1921

20-
protected void assertBatchMailNotNull(MailtrapBatchMail batchMail){
22+
protected void assertBatchMailNotNull(MailtrapBatchMail batchMail) {
2123
if (batchMail == null) {
2224
throw new InvalidRequestBodyException("BatchMail must not be null");
2325
}
26+
if (batchMail.getRequests() == null || batchMail.getRequests().isEmpty()) {
27+
throw new InvalidRequestBodyException("BatchMail.requests must not be null or empty");
28+
}
29+
if (batchMail.getRequests().stream().anyMatch(Objects::isNull)) {
30+
throw new InvalidRequestBodyException("BatchMail.requests must not contain null items");
31+
}
2432
}
2533

2634
/**
@@ -36,9 +44,9 @@ protected void validateMailPayload(MailtrapMail mail) throws InvalidRequestBodyE
3644
}
3745

3846
// Check if all three subject, text, and html are empty
39-
boolean isSubjectTextHtmlEmpty = StringUtils.isEmpty(mail.getSubject())
40-
&& StringUtils.isEmpty(mail.getText())
41-
&& StringUtils.isEmpty(mail.getHtml());
47+
boolean isSubjectTextHtmlEmpty = StringUtils.isBlank(mail.getSubject())
48+
&& StringUtils.isBlank(mail.getText())
49+
&& StringUtils.isBlank(mail.getHtml());
4250

4351
// Validate depending on whether the templateUuid is set
4452
if (StringUtils.isEmpty(mail.getTemplateUuid())) {
@@ -65,9 +73,14 @@ private void validateWithoutTemplate(MailtrapMail mail, boolean isSubjectTextHtm
6573
}
6674

6775
// Ensure the subject is not empty
68-
if (StringUtils.isEmpty(mail.getSubject())) {
76+
if (StringUtils.isBlank(mail.getSubject())) {
6977
throw new InvalidRequestBodyException("Subject must not be null or empty");
7078
}
79+
80+
// Ensure at least one of text or html is present
81+
if (StringUtils.isBlank(mail.getText()) && StringUtils.isBlank(mail.getHtml())) {
82+
throw new InvalidRequestBodyException("Mail must have subject and either text or html when templateUuid is not provided");
83+
}
7184
}
7285

7386
private void validateWithTemplate(MailtrapMail mail) throws InvalidRequestBodyException {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class BatchEmailBase {
4949
/**
5050
* Values that are specific to the entire send that will be carried along with the email and its activity data
5151
*/
52-
@JsonProperty("custom_properties")
52+
@JsonProperty("custom_variables")
5353
private Map<String, String> customVariables;
5454

5555
/**

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package io.mailtrap.model.request.emails;
22

33
import io.mailtrap.model.AbstractModel;
4+
import jakarta.validation.Valid;
5+
import jakarta.validation.constraints.NotNull;
6+
import jakarta.validation.constraints.Size;
47
import lombok.Builder;
58
import lombok.Getter;
69
import lombok.Setter;
@@ -12,8 +15,12 @@
1215
@Builder
1316
public class MailtrapBatchMail extends AbstractModel {
1417

18+
@Valid
1519
private BatchEmailBase base;
1620

21+
@NotNull
22+
@Size(min = 1)
23+
@Valid
1724
private List<MailtrapMail> requests;
1825

1926
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ void send_MailWithTemplateVariablesAndHtml_ThrowsInvalidRequestBodyException() {
101101
assertEquals(TEMPLATE_VARIABLES_SHOULD_BE_USED_WITH_TEMPLATE_UUID, exception.getMessage());
102102
}
103103

104+
@Test
105+
void send_MailWithSubjectAndNoTextNoHtml_ThrowsInvalidRequestBodyException() {
106+
// Set up invalid data
107+
MailtrapMail mail = createTestMailWithSubjectAndNoTextAndNoHtml();
108+
109+
// Assert
110+
InvalidRequestBodyException exception = assertThrows(InvalidRequestBodyException.class, () -> bulkEmails.send(mail));
111+
assertEquals(MAIL_MUST_HAVE_SUBJECT_AND_EITHER_TEXT_OR_HTML, exception.getMessage());
112+
}
113+
104114
@Test
105115
void send_NullableMail_ThrowsInvalidRequestBodyException() {
106116
// Assert

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ void send_MailWithTemplateVariablesAndHtml_ThrowsInvalidRequestBodyException() {
101101
assertEquals(TEMPLATE_VARIABLES_SHOULD_BE_USED_WITH_TEMPLATE_UUID, exception.getMessage());
102102
}
103103

104+
@Test
105+
void send_MailWithSubjectAndNoTextNoHtml_ThrowsInvalidRequestBodyException() {
106+
// Set up invalid data
107+
MailtrapMail mail = createTestMailWithSubjectAndNoTextAndNoHtml();
108+
109+
// Assert
110+
InvalidRequestBodyException exception = assertThrows(InvalidRequestBodyException.class, () -> sendApi.send(mail));
111+
assertEquals(MAIL_MUST_HAVE_SUBJECT_AND_EITHER_TEXT_OR_HTML, exception.getMessage());
112+
}
113+
104114
@Test
105115
void send_NullableMail_ThrowsInvalidRequestBodyException() {
106116
// Assert

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@ void send_MailWithTemplateVariablesAndHtml_ThrowsInvalidRequestBodyException() {
103103
assertEquals(TEMPLATE_VARIABLES_SHOULD_BE_USED_WITH_TEMPLATE_UUID, exception.getMessage());
104104
}
105105

106+
@Test
107+
void send_MailWithSubjectAndNoTextNoHtml_ThrowsInvalidRequestBodyException() {
108+
// Set up invalid data
109+
MailtrapMail mail = createTestMailWithSubjectAndNoTextAndNoHtml();
110+
111+
// Assert
112+
InvalidRequestBodyException exception = assertThrows(InvalidRequestBodyException.class, () -> testingApi.send(mail, INBOX_ID));
113+
assertEquals(MAIL_MUST_HAVE_SUBJECT_AND_EITHER_TEXT_OR_HTML, exception.getMessage());
114+
}
115+
106116
@Test
107117
void send_NullableMail_ThrowsInvalidRequestBodyException() {
108118
// Assert

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class BaseSendTest {
1717
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";
1818
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";
20+
protected final String MAIL_MUST_HAVE_SUBJECT_AND_EITHER_TEXT_OR_HTML = "Mail must have subject and either text or html when templateUuid is not provided";
2021
protected final String MAIL_MUST_NOT_BE_NULL = "Mail must not be null";
2122
protected final String BATCH_MAIL_MUST_NOT_BE_NULL = "BatchMail must not be null";
2223

@@ -74,6 +75,19 @@ protected MailtrapMail createTestMailWithoutTemplateUuidAndSubjectAndTextAndHtml
7475
.build();
7576
}
7677

78+
protected MailtrapMail createTestMailWithSubjectAndNoTextAndNoHtml() {
79+
Address from = getAddress("[email protected]", "John Doe");
80+
Address to = getAddress("[email protected]", "Jane Doe");
81+
EmailAttachment attachment = getAttachment();
82+
83+
return MailtrapMail.builder()
84+
.from(from)
85+
.subject("Sample invalidvalid mail subject")
86+
.to(List.of(to))
87+
.attachments(List.of(attachment))
88+
.build();
89+
}
90+
7791
protected MailtrapMail createTestMailWithTemplateUuidAndText() {
7892
Address from = getAddress("[email protected]", null);
7993
Address to = getAddress("[email protected]", null);

0 commit comments

Comments
 (0)