Skip to content

Commit ae7e6c9

Browse files
authored
Merge pull request #76 from milanoid/EmailSendTestFixes
fixes ScheduleEmailTest, adds and improves assertions
2 parents 2466e38 + 6b8a893 commit ae7e6c9

File tree

2 files changed

+181
-142
lines changed

2 files changed

+181
-142
lines changed

src/test/java/com/mailersend/sdk/tests/EmailSendTest.java

Lines changed: 59 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,30 @@
77
**************************************************/
88
package com.mailersend.sdk.tests;
99

10-
import static org.junit.jupiter.api.Assertions.assertEquals;
11-
import static org.junit.jupiter.api.Assertions.assertTrue;
12-
import static org.junit.jupiter.api.Assertions.fail;
13-
1410
import java.io.IOException;
1511
import java.time.Instant;
1612
import java.time.format.DateTimeFormatter;
1713
import java.time.temporal.TemporalAccessor;
1814
import java.util.Calendar;
1915
import java.util.Date;
2016

21-
import org.junit.jupiter.api.AfterAll;
2217
import org.junit.jupiter.api.AfterEach;
23-
import org.junit.jupiter.api.BeforeAll;
2418
import org.junit.jupiter.api.BeforeEach;
2519
import org.junit.jupiter.api.Test;
2620
import org.junit.jupiter.api.TestInfo;
2721

2822
import com.mailersend.sdk.MailerSend;
29-
import com.mailersend.sdk.MailerSendResponse;
3023
import com.mailersend.sdk.Recipient;
31-
import com.mailersend.sdk.emails.BulkSendStatus;
3224
import com.mailersend.sdk.emails.Email;
3325
import com.mailersend.sdk.exceptions.MailerSendException;
3426
import com.mailersend.sdk.vcr.VcrRecorder;
3527

28+
import static com.mailersend.sdk.util.EventTypes.QUEUED;
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
import static org.junit.jupiter.api.Assertions.assertThrows;
31+
import static org.junit.jupiter.api.Assertions.assertTrue;
32+
import static org.junit.jupiter.api.Assertions.assertNotNull;
33+
3634

3735
public class EmailSendTest {
3836

@@ -41,7 +39,7 @@ public void setupEach(TestInfo info) throws IOException
4139
{
4240
VcrRecorder.useRecording("EmailSendTest_" + info.getDisplayName());
4341
}
44-
42+
4543
@AfterEach
4644
public void afterEach() throws IOException
4745
{
@@ -59,18 +57,11 @@ public void TestInvalidTokenFailsWith401() {
5957

6058
MailerSend ms = new MailerSend();
6159
ms.setToken(TestHelper.invalidToken);
62-
63-
64-
try {
65-
ms.emails().send(email);
66-
} catch (MailerSendException e) {
6760

68-
assertEquals(e.code, 401);
69-
return;
70-
}
71-
72-
// fail if it reaches here
73-
fail();
61+
MailerSendException e = assertThrows(MailerSendException.class, () -> {
62+
ms.emails().send(email);
63+
});
64+
assertEquals(401, e.code);
7465
}
7566

7667

@@ -87,29 +78,20 @@ public void TestInvalidPersonalization() {
8778

8879
MailerSend ms = new MailerSend();
8980
ms.setToken(TestHelper.validToken);
90-
91-
try {
92-
81+
82+
MailerSendException e = assertThrows(MailerSendException.class, () -> {
9383
ms.emails().send(email);
94-
95-
} catch (MailerSendException e) {
96-
97-
assertEquals(e.code, 422);
98-
assertTrue(e.errors.containsKey("personalization.0.data"));
99-
100-
return;
101-
}
102-
103-
// fail if it reaches here
104-
fail();
84+
});
85+
assertEquals(422, e.code);
86+
assertTrue(e.errors.containsKey("personalization.0.data"));
10587
}
10688

10789

10890
/**
10991
* Simple email send
11092
*/
11193
@Test
112-
public void TestSimpleSend() {
94+
public void TestSimpleSend() throws MailerSendException {
11395

11496
Email email = new Email();
11597

@@ -124,23 +106,17 @@ public void TestSimpleSend() {
124106

125107
MailerSend ms = new MailerSend();
126108
ms.setToken(TestHelper.validToken);
127-
128-
try {
129-
130-
MailerSendResponse response = ms.emails().send(email);
131-
} catch (MailerSendException e) {
132-
133-
// fail if any error is thrown
134-
fail();
135-
}
109+
110+
assertEquals(202, ms.emails().send(email).responseStatusCode);
111+
136112
}
137113

138114

139115
/**
140116
* Test personalization from a POJO
141117
*/
142118
@Test
143-
public void TestPojoPersonalization() {
119+
public void TestPojoPersonalization() throws MailerSendException {
144120

145121
Email email = TestHelper.createBasicEmail(false);
146122

@@ -150,154 +126,103 @@ public void TestPojoPersonalization() {
150126

151127
MailerSend ms = new MailerSend();
152128
ms.setToken(TestHelper.validToken);
153-
154-
155-
try {
156-
157-
MailerSendResponse response = ms.emails().send(email);
158-
} catch (MailerSendException e) {
159-
160-
// fail if any error is thrown
161-
fail();
162-
}
163-
129+
130+
assertEquals(202, ms.emails().send(email).responseStatusCode);
164131
}
165132

166133

167134
/**
168135
* Test email with CC
169136
*/
170137
@Test
171-
public void TestCcSend() {
138+
public void TestCcSend() throws MailerSendException {
172139

173140
Email email = TestHelper.createBasicEmail(false);
174141

175142
email.AddCc(TestHelper.ccName, TestHelper.ccEmail);
176143

177144
MailerSend ms = new MailerSend();
178145
ms.setToken(TestHelper.validToken);
179-
180-
181-
try {
182-
183-
ms.emails().send(email);
184-
} catch (MailerSendException e) {
185-
186-
// fail if any error is thrown
187-
fail();
188-
}
146+
147+
assertEquals(202, ms.emails().send(email).responseStatusCode);
189148
}
190149

191150

192151
/**
193152
* Test email with BCC
194153
*/
195154
@Test
196-
public void TestBccSend() {
155+
public void TestBccSend() throws MailerSendException {
197156

198157
Email email = TestHelper.createBasicEmail(false);
199158

200159
email.AddBcc(TestHelper.bccName, TestHelper.bccEmail);
201160

202161
MailerSend ms = new MailerSend();
203162
ms.setToken(TestHelper.validToken);
204-
205-
try {
206-
207-
ms.emails().send(email);
208-
} catch (MailerSendException e) {
209-
210-
// fail if any error is thrown
211-
fail();
212-
}
163+
164+
assertEquals(202, ms.emails().send(email).responseStatusCode);
213165
}
214166

215167

216168
/**
217169
* Test email with attachment
218170
*/
219171
@Test
220-
public void TestEmailWithAttachment() {
221-
172+
public void TestEmailWithAttachment() throws IOException, MailerSendException {
173+
222174
Email email = TestHelper.createBasicEmail(true);
223-
224-
try {
225-
email.attachFile("LICENSE");
226-
227-
MailerSend ms = new MailerSend();
228-
ms.setToken(TestHelper.validToken);
229-
230-
ms.emails().send(email);
231-
232-
} catch (IOException | MailerSendException e) {
233-
234-
// fail if any error is thrown
235-
fail();
236-
}
175+
176+
email.attachFile("LICENSE");
177+
178+
MailerSend ms = new MailerSend();
179+
ms.setToken(TestHelper.validToken);
180+
181+
assertEquals(202, ms.emails().send(email).responseStatusCode);
237182
}
238183

239184

240185
/**
241186
* Test bulk email send
242187
*/
243188
@Test
244-
public void TestSendBulkEmail() {
189+
public void TestSendBulkEmail() throws MailerSendException {
245190

246191
Email email = TestHelper.createBasicEmail(true);
247192
Email email2 = TestHelper.createBasicEmail(true);
248193

249194
email2.setHtml("<b>Test bulk</b>");
250195
email2.setPlain("Test bulk");
251-
252-
try {
253-
254-
MailerSend ms = new MailerSend();
255-
ms.setToken(TestHelper.validToken);
256-
257-
String bulkSendId = ms.emails().bulkSend(new Email[] { email, email2 });
258-
259-
System.out.println(bulkSendId);
260-
261-
} catch (MailerSendException e) {
262-
263-
// fail if any error is thrown
264-
fail();
265-
}
196+
197+
MailerSend ms = new MailerSend();
198+
ms.setToken(TestHelper.validToken);
199+
String bulkSendId = ms.emails().bulkSend(new Email[] { email, email2 });
200+
201+
assertNotNull(bulkSendId, "Bulk send ID should not be null");
266202
}
267203

268204

269205
/**
270206
* Test retrieving the status for a bulk send
271207
*/
272208
@Test
273-
public void TestBulkSendStatus() {
209+
public void TestBulkSendStatus() throws MailerSendException {
274210

275211
Email email = TestHelper.createBasicEmail(true);
276212
Email email2 = TestHelper.createBasicEmail(true);
277213

278214
email2.setHtml("<b>Test bulk</b>");
279215
email2.setPlain("Test bulk");
280-
281-
try {
282-
283-
MailerSend ms = new MailerSend();
284-
ms.setToken(TestHelper.validToken);
285-
286-
String bulkSendId = ms.emails().bulkSend(new Email[] { email, email2 });
287-
288-
BulkSendStatus status = ms.emails().bulkSendStatus(bulkSendId);
289-
290-
System.out.println(status.state);
291-
292-
} catch (MailerSendException e) {
293-
294-
// fail if any error is thrown
295-
fail();
296-
}
216+
217+
MailerSend ms = new MailerSend();
218+
ms.setToken(TestHelper.validToken);
219+
220+
String bulkSendId = ms.emails().bulkSend(new Email[] { email, email2 });
221+
assertEquals(QUEUED, ms.emails().bulkSendStatus(bulkSendId).state);
297222
}
298223

299224
@Test
300-
public void ScheduleEmailTest() {
225+
public void ScheduleEmailTest() throws MailerSendException {
301226
Email email = new Email();
302227

303228
email.subject = TestHelper.subject;
@@ -308,26 +233,19 @@ public void ScheduleEmailTest() {
308233
email.AddReplyTo(new Recipient(TestHelper.fromName, TestHelper.emailFrom));
309234

310235
email.setFrom(TestHelper.fromName, TestHelper.emailFrom);
311-
236+
312237
TemporalAccessor ta = DateTimeFormatter.ISO_INSTANT.parse("2024-08-03T00:00:00.875000Z");
313238
Date scheduleDate = Date.from(Instant.from(ta));
314-
239+
315240
Calendar calendar = Calendar.getInstance();
316241
calendar.setTime(scheduleDate);
317242
calendar.add(Calendar.DATE, 1);
318-
243+
319244
email.setSendAt(calendar.getTime());
320-
245+
321246
MailerSend ms = new MailerSend();
322247
ms.setToken(TestHelper.validToken);
323-
324-
try {
325-
326-
MailerSendResponse response = ms.emails().send(email);
327-
} catch (MailerSendException e) {
328-
329-
// fail if any error is thrown
330-
fail();
331-
}
248+
249+
assertEquals(202, ms.emails().send(email).responseStatusCode);
332250
}
333251
}

0 commit comments

Comments
 (0)