-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManageEmailTest.java
More file actions
219 lines (182 loc) · 10.2 KB
/
Copy pathManageEmailTest.java
File metadata and controls
219 lines (182 loc) · 10.2 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package it.aboutbits.springboot.emailservice.lib.application;
import it.aboutbits.springboot.emailservice.lib.AttachmentDataSource;
import it.aboutbits.springboot.emailservice.lib.EmailState;
import it.aboutbits.springboot.emailservice.lib.exception.AttachmentException;
import it.aboutbits.springboot.emailservice.lib.exception.EmailException;
import it.aboutbits.springboot.emailservice.support.database.WithPostgres;
import jakarta.mail.internet.MimeMessage;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.mail.MailSendException;
import org.springframework.mail.javamail.JavaMailSender;
import java.io.ByteArrayInputStream;
import java.time.OffsetDateTime;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@SpringBootTest
@WithPostgres
class ManageEmailTest {
@SpyBean
JavaMailSender javaMailSender;
@MockBean
AttachmentDataSource attachmentDataSource;
@Autowired
private ManageEmail manageEmail;
@BeforeEach
void setup() {
doNothing().when(javaMailSender).send(any(MimeMessage.class));
}
@Test
void givenRequiredParameters_schedule_shouldCreateNewNotification() throws EmailException {
var parameter = getValidParameterWithoutAttachment();
var result = manageEmail.schedule(parameter);
assertThat(result.id()).isPositive();
assertThat(result.state()).isEqualTo(EmailState.PENDING);
assertThat(result.subject()).isEqualTo(parameter.email().subject());
assertThat(result.fromAddress()).isEqualTo(parameter.email().fromAddress());
assertThat(result.fromName()).isEqualTo(parameter.email().fromName());
assertThat(result.replyToAddress()).isEqualTo(parameter.email().replyToAddress());
assertThat(result.replyToName()).isEqualTo(parameter.email().replyToName());
assertThat(result.recipients()).containsAll(parameter.email().recipients());
assertThat(result.textBody()).isEqualTo(parameter.email().textBody());
assertThat(result.htmlBody()).isEqualTo(parameter.email().htmlBody());
assertThat(result.attachments()).isEmpty();
assertThat(result.scheduledAt()).isEqualTo(parameter.scheduledAt());
}
@Test
void givenRequiredParameterWithAttachedFiles_schedule_shouldCreateNewNotification() throws EmailException, AttachmentException {
when(attachmentDataSource.storeAttachmentPayload(any())).thenReturn(33L);
var parameter = getValidParameterWithAttachment();
var result = manageEmail.schedule(parameter);
assertThat(result.id()).isPositive();
assertThat(result.state()).isEqualTo(EmailState.PENDING);
assertThat(result.subject()).isEqualTo(parameter.email().subject());
assertThat(result.fromAddress()).isEqualTo(parameter.email().fromAddress());
assertThat(result.fromName()).isEqualTo(parameter.email().fromName());
assertThat(result.replyToAddress()).isEqualTo(parameter.email().replyToAddress());
assertThat(result.replyToName()).isEqualTo(parameter.email().replyToName());
assertThat(result.recipients()).containsAll(parameter.email().recipients());
assertThat(result.textBody()).isEqualTo(parameter.email().textBody());
assertThat(result.htmlBody()).isEqualTo(parameter.email().htmlBody());
assertThat(result.attachments()).hasSize(1);
assertThat(result.scheduledAt()).isEqualTo(parameter.scheduledAt());
}
@Test
void givenRequiredParameters_sendOrFail_shouldCreateNewNotification() throws EmailException {
var parameter = getValidParameterWithoutAttachment();
var result = manageEmail.sendOrFail(parameter);
assertThat(result.id()).isPositive();
assertThat(result.state()).isEqualTo(EmailState.SENT);
assertThat(result.subject()).isEqualTo(parameter.email().subject());
assertThat(result.fromAddress()).isEqualTo(parameter.email().fromAddress());
assertThat(result.fromName()).isEqualTo(parameter.email().fromName());
assertThat(result.replyToAddress()).isEqualTo(parameter.email().replyToAddress());
assertThat(result.replyToName()).isEqualTo(parameter.email().replyToName());
assertThat(result.recipients()).containsAll(parameter.email().recipients());
assertThat(result.textBody()).isEqualTo(parameter.email().textBody());
assertThat(result.htmlBody()).isEqualTo(parameter.email().htmlBody());
assertThat(result.attachments()).isEmpty();
assertThat(result.scheduledAt()).isEqualTo(parameter.scheduledAt());
}
@Test
void givenRequiredParameterWithAttachedFiles_sendOrFail_shouldCreateNewNotification() throws EmailException, AttachmentException {
when(attachmentDataSource.storeAttachmentPayload(any())).thenReturn(33L);
when(attachmentDataSource.getAttachmentPayload(anyLong())).thenReturn(new ByteArrayInputStream(new byte[0]));
var parameter = getValidParameterWithAttachment();
var result = manageEmail.sendOrFail(parameter);
assertThat(result.id()).isPositive();
assertThat(result.state()).isEqualTo(EmailState.SENT);
assertThat(result.subject()).isEqualTo(parameter.email().subject());
assertThat(result.fromAddress()).isEqualTo(parameter.email().fromAddress());
assertThat(result.fromName()).isEqualTo(parameter.email().fromName());
assertThat(result.replyToAddress()).isEqualTo(parameter.email().replyToAddress());
assertThat(result.replyToName()).isEqualTo(parameter.email().replyToName());
assertThat(result.recipients()).containsAll(parameter.email().recipients());
assertThat(result.textBody()).isEqualTo(parameter.email().textBody());
assertThat(result.htmlBody()).isEqualTo(parameter.email().htmlBody());
assertThat(result.attachments()).hasSize(1);
assertThat(result.scheduledAt()).isEqualTo(parameter.scheduledAt());
}
@Test
void givenRequiredParameters_sendOrFail_shouldSendImmediately() throws EmailException {
EmailParameter parameter = getValidParameterWithoutAttachment();
manageEmail.sendOrFail(parameter);
verify(javaMailSender, times(1)).send(any(MimeMessage.class));
}
@Test
void givenRequiredParameterWithAttachedFiles_sendOrFail_shouldSendImmediately() throws EmailException, AttachmentException {
when(attachmentDataSource.storeAttachmentPayload(any())).thenReturn(33L);
when(attachmentDataSource.getAttachmentPayload(anyLong())).thenReturn(new ByteArrayInputStream(new byte[0]));
var parameter = getValidParameterWithAttachment();
manageEmail.sendOrFail(parameter);
verify(javaMailSender, times(1)).send(any(MimeMessage.class));
}
@Test
void givenAttachmentError_sendOrFail_shouldFail() throws EmailException, AttachmentException {
when(attachmentDataSource.storeAttachmentPayload(any())).thenThrow(new AttachmentException());
var parameter = getValidParameterWithAttachment();
assertThatExceptionOfType(EmailException.class).isThrownBy(
() -> manageEmail.sendOrFail(parameter)
);
}
@Test
void givenMailSenderError_sendOrFail_shouldFail() throws EmailException, AttachmentException {
when(attachmentDataSource.storeAttachmentPayload(any())).thenReturn(33L);
when(attachmentDataSource.getAttachmentPayload(anyLong())).thenReturn(new ByteArrayInputStream(new byte[0]));
doThrow(new MailSendException("any")).when(javaMailSender).send(any(MimeMessage.class));
var parameter = getValidParameterWithAttachment();
assertThatExceptionOfType(EmailException.class).isThrownBy(
() -> manageEmail.sendOrFail(parameter)
);
}
private static EmailParameter getValidParameterWithoutAttachment() {
return EmailParameter.builder()
.scheduledAt(OffsetDateTime.now())
.email(EmailParameter.Email.builder()
.subject("Example email subject")
.textBody("Email body")
.htmlBody("<h1>Html email body</h1>")
.recipient("person1@example.com")
.recipient("person2@example.com")
.fromAddress("somebody@aboutbits.it")
.fromName("somebody")
.replyToAddress("somebodyElse@aboutbits.it")
.replyToName("somebodyElse")
.build()
).build();
}
private static EmailParameter getValidParameterWithAttachment() {
return EmailParameter.builder()
.scheduledAt(OffsetDateTime.now())
.email(EmailParameter.Email.builder()
.subject("Example email subject")
.textBody("Email body")
.htmlBody("<h1>Html email body</h1>")
.recipient("person1@example.com")
.recipient("person2@example.com")
.attachment(
EmailParameter.Email.Attachment.builder()
.contentType("image/png")
.fileName("x.png")
.payload(new ByteArrayInputStream(new byte[0]))
.build()
)
.fromAddress("somebody@aboutbits.it")
.fromName("somebody")
.replyToAddress("somebodyElse@aboutbits.it")
.replyToName("somebodyElse")
.build()
).build();
}
}