forked from mailjet/mailjet-apiv3-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransactionalEmailBuilderIT.java
More file actions
174 lines (142 loc) · 6.75 KB
/
TransactionalEmailBuilderIT.java
File metadata and controls
174 lines (142 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.mailjet.client;
import com.mailjet.client.errors.MailjetClientRequestException;
import com.mailjet.client.errors.MailjetException;
import com.mailjet.client.transactional.*;
import com.mailjet.client.transactional.response.MessageResult;
import com.mailjet.client.transactional.response.SendEmailError;
import com.mailjet.client.transactional.response.SendEmailsResponse;
import com.mailjet.client.transactional.response.SentMessageStatus;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class TransactionalEmailBuilderIT {
private MailjetClient client;
private String attachmentPath;
private String senderEmail;
@Before
public void setUp() throws MailjetException {
client = TestHelper.getClient();
attachmentPath = Paths.get("src","test", "resources")
.resolve("Test-attachment.txt")
.toAbsolutePath()
.toString();
senderEmail = TestHelper.getValidSenderEmail(client);
}
@Test
public void SendEmailsRequest_SendsMessage() throws MailjetException, IOException {
// arrange
TransactionalEmail message1 = TransactionalEmail
.builder()
.to(new SendContact(senderEmail, "stanislav"))
.from(new SendContact(senderEmail, "Mailjet integration test"))
.htmlPart("<h1>This is the HTML content of the mail</h1>")
.subject("This is the subject")
.trackOpens(TrackOpens.ENABLED)
.attachment(Attachment.fromFile(attachmentPath))
.header("test-header-key", "test-value")
.variable("test-vars-array", new String[] {"a", "b", "c"})
.variable("test-vars-string", "abc")
.variable("test-vars-primitive", 123)
.customID("custom-id-value")
.build();
SendEmailsRequest request = SendEmailsRequest
.builder()
.message(message1) // you can add up to 50 messages per request
.build();
// act
SendEmailsResponse response = request.sendWith(client);
// assert
Assert.assertEquals(1, response.getMessages().length);
MessageResult messageResult = response.getMessages()[0];
Assert.assertEquals(SentMessageStatus.SUCCESS, messageResult.getStatus());
Assert.assertEquals("custom-id-value", messageResult.getCustomID());
}
@Test
public void SendEmailsRequest_SendsMessageAsync_WithStreamAttachment() throws IOException, ExecutionException, InterruptedException {
// arrange
InputStream fileStream = new FileInputStream(attachmentPath);
Attachment textAttachment = Attachment.fromInputStream(fileStream, "utf8.txt", "text/plain");
TransactionalEmail message1 = TransactionalEmail
.builder()
.to(new SendContact(senderEmail, "stanislav"))
.from(new SendContact(senderEmail, "Mailjet integration test"))
.htmlPart("<h1>This is the HTML content of the mail</h1>")
.subject("This is the subject")
.trackOpens(TrackOpens.ENABLED)
.attachment(textAttachment)
.header("test-header-key", "test-value")
.variable("test-vars-array", new String[] {"a", "b", "c"})
.variable("test-vars-string", "abc")
.variable("test-vars-primitive", 123)
.customID("custom-id-value")
.build();
SendEmailsRequest request = SendEmailsRequest
.builder()
.message(message1) // you can add up to 50 messages per request
.build();
// act
CompletableFuture<SendEmailsResponse> responseFuture = request.sendAsyncWith(client);
SendEmailsResponse response = responseFuture.get();
// assert
Assert.assertEquals(1, response.getMessages().length);
MessageResult messageResult = response.getMessages()[0];
Assert.assertEquals(SentMessageStatus.SUCCESS, messageResult.getStatus());
Assert.assertEquals("custom-id-value", messageResult.getCustomID());
}
@Test
public void SendEmailsRequest_InvalidToField_ReturnsAnError() throws MailjetException {
// arrange
TransactionalEmail message1 = TransactionalEmail
.builder()
.to(new SendContact("invalid-email"))
.from(new SendContact(senderEmail, "Mailjet integration test"))
.htmlPart("<h1>This is the HTML content of the mail</h1>")
.subject("This is the subject")
.build();
SendEmailsRequest request = SendEmailsRequest
.builder()
.message(message1)
.build();
// act
SendEmailsResponse response = request.sendWith(client);
// assert
Assert.assertEquals(1, response.getMessages().length);
MessageResult messageResult = response.getMessages()[0];
Assert.assertEquals(SentMessageStatus.ERROR, messageResult.getStatus());
SendEmailError error = messageResult.getErrors()[0];
Assert.assertEquals("mj-0013", error.getErrorCode());
Assert.assertEquals(400, error.getStatusCode());
Assert.assertEquals("\"invalid-email\" is an invalid email address.", error.getErrorMessage());
Assert.assertEquals("To[0].Email", error.getErrorRelatedTo()[0]);
}
@Test
public void SendEmailsRequest_MoreThan50MessagesPassed_ReturnsAnError() {
// arrange
LinkedList<TransactionalEmail> messages = new LinkedList<>();
for (int i = 0; i < 51; i++) {
TransactionalEmail message = TransactionalEmail
.builder()
.to(new SendContact("test@mailjet.com"))
.from(new SendContact(senderEmail, "Mailjet integration test"))
.htmlPart("<h1>This is the HTML content of the mail</h1>")
.subject("This is the subject")
.build();
messages.add(message);
}
SendEmailsRequest request = SendEmailsRequest
.builder()
.messages(messages)
.build();
// act
MailjetClientRequestException exception = Assert.assertThrows(MailjetClientRequestException.class, () -> request.sendWith(client));
// assert
Assert.assertTrue(exception.getMessage().contains("Total number of recipients exceeded. Max allowed - 50"));
}
}