Skip to content

Commit 6f49809

Browse files
committed
add tests and improve errror handling
1 parent ba9e577 commit 6f49809

2 files changed

Lines changed: 130 additions & 28 deletions

File tree

src/main/java/it/aboutbits/springboot/emailservice/lib/application/ManageEmail.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import lombok.NonNull;
1616
import lombok.extern.slf4j.Slf4j;
1717
import org.springframework.core.io.ByteArrayResource;
18+
import org.springframework.mail.MailException;
1819
import org.springframework.mail.javamail.JavaMailSender;
1920
import org.springframework.mail.javamail.MimeMessageHelper;
2021
import org.springframework.validation.annotation.Validated;
@@ -86,7 +87,7 @@ Email send(Email email) {
8687
email.setState(EmailState.SENT);
8788
email.setErrorMessage("");
8889
email.setSentAt(OffsetDateTime.now());
89-
} catch (MessagingException | AttachmentException | IOException e) {
90+
} catch (MailException | MessagingException | AttachmentException | IOException e) {
9091
log.error("Failed to send email: " + email.getId(), e);
9192
email.setErrorMessage(e.getMessage());
9293
email.setState(EmailState.ERROR);
@@ -166,6 +167,7 @@ private void sendMail(String fromAddress, String fromName, List<String> recipien
166167
payload.close();
167168
}
168169

170+
169171
mailSender.send(message);
170172
}
171173
}

src/test/java/it/aboutbits/springboot/emailservice/lib/application/ManageEmailTest.java

Lines changed: 127 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,30 @@
66
import it.aboutbits.springboot.emailservice.lib.exception.AttachmentException;
77
import it.aboutbits.springboot.emailservice.lib.exception.EmailException;
88
import it.aboutbits.springboot.emailservice.support.database.WithPostgres;
9+
import jakarta.mail.internet.MimeMessage;
910
import org.junit.jupiter.api.Test;
1011
import org.springframework.beans.factory.annotation.Autowired;
1112
import org.springframework.boot.test.context.SpringBootTest;
1213
import org.springframework.boot.test.mock.mockito.MockBean;
14+
import org.springframework.boot.test.mock.mockito.SpyBean;
15+
import org.springframework.mail.MailSendException;
1316
import org.springframework.mail.javamail.JavaMailSender;
1417

1518
import java.io.ByteArrayInputStream;
1619
import java.time.OffsetDateTime;
1720

1821
import static org.assertj.core.api.Assertions.assertThat;
22+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
1923
import static org.mockito.ArgumentMatchers.any;
24+
import static org.mockito.Mockito.doThrow;
25+
import static org.mockito.Mockito.times;
26+
import static org.mockito.Mockito.verify;
2027
import static org.mockito.Mockito.when;
2128

2229
@SpringBootTest
2330
@WithPostgres
2431
class ManageEmailTest {
25-
@MockBean
32+
@SpyBean
2633
JavaMailSender javaMailSender;
2734

2835
@MockBean
@@ -33,18 +40,7 @@ class ManageEmailTest {
3340

3441
@Test
3542
void givenRequiredParameters_schedule_shouldCreateNewNotification() throws EmailException {
36-
var parameter = EmailParameter.builder()
37-
.scheduledAt(OffsetDateTime.now())
38-
.email(EmailParameter.Email.builder()
39-
.subject("Example email subject")
40-
.textBody("Email body")
41-
.htmlBody("<h1>Html email body</h1>")
42-
.recipient("person1@example.com")
43-
.recipient("person2@example.com")
44-
.fromAddress("somebody@aboutbits.it")
45-
.fromName("somebody")
46-
.build()
47-
).build();
43+
var parameter = getValidParameterWithoutAttachment();
4844

4945
var result = manageEmail.schedule(parameter);
5046

@@ -64,7 +60,124 @@ void givenRequiredParameters_schedule_shouldCreateNewNotification() throws Email
6460
void givenRequiredParameterWithAttachedFiles_schedule_shouldCreateNewNotification() throws EmailException, AttachmentException {
6561
when(attachmentDataSource.storeAttachmentPayload(any())).thenReturn(new AttachmentReference("ref"));
6662

67-
var parameter = EmailParameter.builder()
63+
var parameter = getValidParameterWithAttachment();
64+
65+
var result = manageEmail.schedule(parameter);
66+
67+
assertThat(result.id()).isPositive();
68+
assertThat(result.state()).isEqualTo(EmailState.PENDING);
69+
assertThat(result.subject()).isEqualTo(parameter.email().subject());
70+
assertThat(result.fromAddress()).isEqualTo(parameter.email().fromAddress());
71+
assertThat(result.fromName()).isEqualTo(parameter.email().fromName());
72+
assertThat(result.recipients()).containsAll(parameter.email().recipients());
73+
assertThat(result.textBody()).isEqualTo(parameter.email().textBody());
74+
assertThat(result.htmlBody()).isEqualTo(parameter.email().htmlBody());
75+
assertThat(result.attachments()).hasSize(1);
76+
assertThat(result.scheduledAt()).isEqualTo(parameter.scheduledAt());
77+
}
78+
79+
@Test
80+
void givenRequiredParameters_sendOrFail_shouldCreateNewNotification() throws EmailException {
81+
var parameter = getValidParameterWithoutAttachment();
82+
83+
var result = manageEmail.sendOrFail(parameter);
84+
85+
assertThat(result.id()).isPositive();
86+
assertThat(result.state()).isEqualTo(EmailState.SENT);
87+
assertThat(result.subject()).isEqualTo(parameter.email().subject());
88+
assertThat(result.fromAddress()).isEqualTo(parameter.email().fromAddress());
89+
assertThat(result.fromName()).isEqualTo(parameter.email().fromName());
90+
assertThat(result.recipients()).containsAll(parameter.email().recipients());
91+
assertThat(result.textBody()).isEqualTo(parameter.email().textBody());
92+
assertThat(result.htmlBody()).isEqualTo(parameter.email().htmlBody());
93+
assertThat(result.attachments()).isEmpty();
94+
assertThat(result.scheduledAt()).isEqualTo(parameter.scheduledAt());
95+
}
96+
97+
@Test
98+
void givenRequiredParameterWithAttachedFiles_sendOrFail_shouldCreateNewNotification() throws EmailException, AttachmentException {
99+
when(attachmentDataSource.storeAttachmentPayload(any())).thenReturn(new AttachmentReference("ref"));
100+
when(attachmentDataSource.getAttachmentPayload(any())).thenReturn(new ByteArrayInputStream(new byte[0]));
101+
102+
var parameter = getValidParameterWithAttachment();
103+
104+
var result = manageEmail.sendOrFail(parameter);
105+
106+
assertThat(result.id()).isPositive();
107+
assertThat(result.state()).isEqualTo(EmailState.SENT);
108+
assertThat(result.subject()).isEqualTo(parameter.email().subject());
109+
assertThat(result.fromAddress()).isEqualTo(parameter.email().fromAddress());
110+
assertThat(result.fromName()).isEqualTo(parameter.email().fromName());
111+
assertThat(result.recipients()).containsAll(parameter.email().recipients());
112+
assertThat(result.textBody()).isEqualTo(parameter.email().textBody());
113+
assertThat(result.htmlBody()).isEqualTo(parameter.email().htmlBody());
114+
assertThat(result.attachments()).hasSize(1);
115+
assertThat(result.scheduledAt()).isEqualTo(parameter.scheduledAt());
116+
}
117+
118+
@Test
119+
void givenRequiredParameters_sendOrFail_shouldSendImmediately() throws EmailException {
120+
EmailParameter parameter = getValidParameterWithoutAttachment();
121+
122+
manageEmail.sendOrFail(parameter);
123+
124+
verify(javaMailSender, times(1)).send(any(MimeMessage.class));
125+
}
126+
127+
@Test
128+
void givenRequiredParameterWithAttachedFiles_sendOrFail_shouldSendImmediately() throws EmailException, AttachmentException {
129+
when(attachmentDataSource.storeAttachmentPayload(any())).thenReturn(new AttachmentReference("ref"));
130+
when(attachmentDataSource.getAttachmentPayload(any())).thenReturn(new ByteArrayInputStream(new byte[0]));
131+
132+
var parameter = getValidParameterWithAttachment();
133+
134+
manageEmail.sendOrFail(parameter);
135+
136+
verify(javaMailSender, times(1)).send(any(MimeMessage.class));
137+
}
138+
139+
@Test
140+
void givenAttachmentError_sendOrFail_shouldFail() throws EmailException, AttachmentException {
141+
when(attachmentDataSource.storeAttachmentPayload(any())).thenThrow(new AttachmentException());
142+
143+
var parameter = getValidParameterWithAttachment();
144+
145+
assertThatExceptionOfType(EmailException.class).isThrownBy(
146+
() -> manageEmail.sendOrFail(parameter)
147+
);
148+
}
149+
150+
@Test
151+
void givenMailSenderError_sendOrFail_shouldFail() throws EmailException, AttachmentException {
152+
when(attachmentDataSource.storeAttachmentPayload(any())).thenReturn(new AttachmentReference("ref"));
153+
when(attachmentDataSource.getAttachmentPayload(any())).thenReturn(new ByteArrayInputStream(new byte[0]));
154+
155+
doThrow(new MailSendException("any")).when(javaMailSender).send(any(MimeMessage.class));
156+
157+
var parameter = getValidParameterWithAttachment();
158+
159+
assertThatExceptionOfType(EmailException.class).isThrownBy(
160+
() -> manageEmail.sendOrFail(parameter)
161+
);
162+
}
163+
164+
private static EmailParameter getValidParameterWithoutAttachment() {
165+
return EmailParameter.builder()
166+
.scheduledAt(OffsetDateTime.now())
167+
.email(EmailParameter.Email.builder()
168+
.subject("Example email subject")
169+
.textBody("Email body")
170+
.htmlBody("<h1>Html email body</h1>")
171+
.recipient("person1@example.com")
172+
.recipient("person2@example.com")
173+
.fromAddress("somebody@aboutbits.it")
174+
.fromName("somebody")
175+
.build()
176+
).build();
177+
}
178+
179+
private static EmailParameter getValidParameterWithAttachment() {
180+
return EmailParameter.builder()
68181
.scheduledAt(OffsetDateTime.now())
69182
.email(EmailParameter.Email.builder()
70183
.subject("Example email subject")
@@ -83,18 +196,5 @@ void givenRequiredParameterWithAttachedFiles_schedule_shouldCreateNewNotificatio
83196
.fromName("somebody")
84197
.build()
85198
).build();
86-
87-
var result = manageEmail.schedule(parameter);
88-
89-
assertThat(result.id()).isPositive();
90-
assertThat(result.state()).isEqualTo(EmailState.PENDING);
91-
assertThat(result.subject()).isEqualTo(parameter.email().subject());
92-
assertThat(result.fromAddress()).isEqualTo(parameter.email().fromAddress());
93-
assertThat(result.fromName()).isEqualTo(parameter.email().fromName());
94-
assertThat(result.recipients()).containsAll(parameter.email().recipients());
95-
assertThat(result.textBody()).isEqualTo(parameter.email().textBody());
96-
assertThat(result.htmlBody()).isEqualTo(parameter.email().htmlBody());
97-
assertThat(result.attachments()).hasSize(1);
98-
assertThat(result.scheduledAt()).isEqualTo(parameter.scheduledAt());
99199
}
100200
}

0 commit comments

Comments
 (0)