Skip to content

Commit

Permalink
hotfix: implement HTML email sending without BCC and add unit tests f…
Browse files Browse the repository at this point in the history
…or email service
  • Loading branch information
LeonardoMeireles55 committed Jan 29, 2025
1 parent 252aab8 commit 930885b
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -61,7 +60,26 @@ public void sendPlainTextEmail(EmailDTO email) {
javaMailSender.send(message);
}

@Async
public void sendHtmlEmailWithoutBcc(EmailDTO emailDTO) {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom(emailFrom);

helper.addTo(emailDTO.to());
helper.setSubject(EMAIL_SUBJECT_PREFIX + emailDTO.subject());
helper.setText(buildEmailBody(emailDTO.body()), true);

javaMailSender.send(mimeMessage);
log.info("HTML identifier sent successfully to {} client", emailDTO.to());

} catch (MessagingException e) {
log.error("Failed to send HTML identifier: {}", e.getMessage(), e);
throw new RuntimeException("Email sending failed", e);
}
}


public void sendHtmlEmail(EmailDTO emailDTO) {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
try {
Expand Down Expand Up @@ -170,7 +188,7 @@ public void sendUserActionEmail(String actionType, String username, String email
LocalDateTime date) {
String subject = String.format("User %s - %s", username, actionType);
String content = createUserActionEmailContent(actionType, username, email, date);
this.sendPlainTextEmail(new EmailDTO(email, subject, content));
sendHtmlEmailWithoutBcc(new EmailDTO(email, subject, content));
}

private String createUserActionEmailContent(String actionType, String username, String email,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package leonardo.labutilities.qualitylabpro.services;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.mail.MailSendException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.test.util.ReflectionTestUtils;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import leonardo.labutilities.qualitylabpro.dtos.email.EmailDTO;
import leonardo.labutilities.qualitylabpro.services.email.EmailService;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;

@ExtendWith(MockitoExtension.class)
class EmailServiceTest {

@Mock
private JavaMailSender javaMailSender;

@InjectMocks
private EmailService emailService;

private static final String TEST_EMAIL_FROM = "[email protected]";

@BeforeEach
void setUp() {
ReflectionTestUtils.setField(emailService, "emailFrom", TEST_EMAIL_FROM);
}

@Test
void shouldSendHtmlEmailSuccessfully() throws MessagingException {
// Given
EmailDTO emailDTO = new EmailDTO("[email protected]", "Test Subject", "Test Body");
MimeMessage mimeMessage = mock(MimeMessage.class);
when(javaMailSender.createMimeMessage()).thenReturn(mimeMessage);

// When
emailService.sendHtmlEmailWithoutBcc(emailDTO);

// Then
verify(javaMailSender).createMimeMessage();
verify(javaMailSender).send(mimeMessage);
}

@Test
void shouldThrowRuntimeExceptionWhenEmailFails() throws MessagingException {
// Given
EmailDTO emailDTO = new EmailDTO("[email protected]", "Test Subject", "Test Body");
MimeMessage mimeMessage = mock(MimeMessage.class);
when(javaMailSender.createMimeMessage()).thenReturn(mimeMessage);
doThrow(new MailSendException("Email sending failed")).when(javaMailSender)
.send(any(MimeMessage.class));

// Then
RuntimeException exception = assertThrows(RuntimeException.class, () -> {
emailService.sendHtmlEmailWithoutBcc(emailDTO);
});
assertEquals("Email sending failed", exception.getMessage());
}
}

0 comments on commit 930885b

Please sign in to comment.