33import io .mailtrap .CustomValidator ;
44import io .mailtrap .config .MailtrapConfig ;
55import io .mailtrap .exception .InvalidRequestBodyException ;
6+ import io .mailtrap .model .mailvalidation .ContentView ;
7+ import io .mailtrap .model .mailvalidation .MailContentView ;
8+ import io .mailtrap .model .mailvalidation .ResolvedMailContentView ;
9+ import io .mailtrap .model .mailvalidation .ResolvedMailView ;
10+ import io .mailtrap .model .request .emails .BatchEmailBase ;
11+ import io .mailtrap .model .request .emails .MailtrapBatchMail ;
612import io .mailtrap .model .request .emails .MailtrapMail ;
713import org .apache .commons .collections4 .MapUtils ;
814import org .apache .commons .lang3 .StringUtils ;
915
16+ import java .util .Objects ;
17+
1018/**
1119 * Abstract class representing a resource for sending emails via Mailtrap API.
1220 */
@@ -22,51 +30,87 @@ protected SendApiResource(MailtrapConfig config, CustomValidator customValidator
2230 * @param mail The email message to be validated.
2331 * @throws InvalidRequestBodyException If the request body is invalid.
2432 */
25- protected void validateRequestBodyOrThrowException (MailtrapMail mail ) throws InvalidRequestBodyException {
26- // Check if the mail object itself is null
33+ protected void validateMailPayload (MailtrapMail mail ) {
2734 if (mail == null ) {
2835 throw new InvalidRequestBodyException ("Mail must not be null" );
2936 }
3037
31- // Check if all three subject, text, and html are empty
32- boolean isSubjectTextHtmlEmpty = StringUtils .isEmpty (mail .getSubject ())
33- && StringUtils .isEmpty (mail .getText ())
34- && StringUtils .isEmpty (mail .getHtml ());
35-
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 );
40- } else {
41- // Validation for the scenario where templateUuid is provided
42- validateWithTemplate (mail );
43- }
44-
45- // Additional validation logic (assumed to be provided by the user)
38+ // Perform bean validation (NotNull, etc.)
4639 validateRequestBodyAndThrowException (mail );
40+
41+ // Validate subject/text/html/templateUuid
42+ validateContentRules (MailContentView .of (mail ));
4743 }
4844
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- }
45+ /**
46+ * Validates the request body of batch email and throws an exception if it is invalid.
47+ *
48+ * @param batch batch request to be validated.
49+ * @throws InvalidRequestBodyException If the request body is invalid.
50+ */
51+ protected void validateBatchPayload (MailtrapBatchMail batch ) {
52+ assertBatchMailNotNull (batch );
53+
54+ BatchEmailBase base = batch .getBase ();
5455
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" );
56+ for (int i = 0 ; i < batch .getRequests ().size (); i ++) {
57+ MailtrapMail mail = batch .getRequests ().get (i );
58+ ResolvedMailView mailView = new ResolvedMailView (base , mail );
59+
60+ try {
61+ // Perform bean validation (NotNull, etc.)
62+ validateRequestBodyAndThrowException (mailView );
63+ } catch (InvalidRequestBodyException e ) {
64+ throw new InvalidRequestBodyException ("requests[" + i + "]: " + e .getMessage (), e );
65+ }
66+
67+ validateContentRules (ResolvedMailContentView .of (mailView ));
68+
69+ if (mailView .getFrom () == null ) {
70+ throw new InvalidRequestBodyException ("requests[" + i + "]: from is required (either in mail or base)" );
71+ }
5872 }
73+ }
5974
60- // Ensure the subject is not empty
61- if (StringUtils .isEmpty (mail .getSubject ())) {
62- throw new InvalidRequestBodyException ("Subject must not be null or empty" );
75+ private void assertBatchMailNotNull (MailtrapBatchMail batchMail ) {
76+ if (batchMail == null ) {
77+ throw new InvalidRequestBodyException ("BatchMail must not be null" );
78+ }
79+ if (batchMail .getRequests () == null || batchMail .getRequests ().isEmpty ()) {
80+ throw new InvalidRequestBodyException ("BatchMail.requests must not be null or empty" );
81+ }
82+ if (batchMail .getRequests ().stream ().anyMatch (Objects ::isNull )) {
83+ throw new InvalidRequestBodyException ("BatchMail.requests must not contain null items" );
6384 }
85+
6486 }
6587
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" );
88+ private void validateContentRules (ContentView v ) {
89+ boolean templateUuidBlank = StringUtils .isBlank (v .getTemplateUuid ());
90+
91+ boolean subjectTextHtmlEmpty = StringUtils .isBlank (v .getSubject ())
92+ && StringUtils .isBlank (v .getText ())
93+ && StringUtils .isBlank (v .getHtml ());
94+
95+ if (templateUuidBlank ) {
96+ if (subjectTextHtmlEmpty ) {
97+ throw new InvalidRequestBodyException ("Mail must have subject and either text or html when templateUuid is not provided" );
98+ }
99+ if (MapUtils .isNotEmpty (v .getTemplateVariables ())) {
100+ throw new InvalidRequestBodyException ("Mail templateVariables must only be used with templateUuid" );
101+ }
102+ if (StringUtils .isBlank (v .getSubject ())) {
103+ throw new InvalidRequestBodyException ("Subject must not be null or empty" );
104+ }
105+ if (StringUtils .isBlank (v .getText ()) && StringUtils .isBlank (v .getHtml ())) {
106+ throw new InvalidRequestBodyException ("Mail must have subject and either text or html when templateUuid is not provided" );
107+ }
108+ } else {
109+ if (StringUtils .isNotEmpty (v .getSubject ())
110+ || StringUtils .isNotEmpty (v .getText ())
111+ || StringUtils .isNotEmpty (v .getHtml ()))
112+ throw new InvalidRequestBodyException ("When templateUuid is used, subject, text, and html must not be used" );
70113 }
71114 }
115+
72116}
0 commit comments