|
|
@@ -0,0 +1,110 @@
|
|
|
+package pl.dmcs;
|
|
|
+
|
|
|
+import org.apache.commons.lang3.tuple.Pair;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
+
|
|
|
+public class NotificationServiceTest {
|
|
|
+
|
|
|
+ private MailService mailServiceMock;
|
|
|
+ private NotificationService notificationService;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ public void setUp() {
|
|
|
+ mailServiceMock = mock(MailService.class, withSettings().withoutAnnotations().defaultAnswer(CALLS_REAL_METHODS));
|
|
|
+ notificationService = new NotificationServiceImpl(mailServiceMock);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void sendAndVerifyNotifications(User user, List<String> recipients, String subject, String content) throws Exception {
|
|
|
+ notificationService.sendNotifications(user, recipients, subject, content);
|
|
|
+
|
|
|
+ String expectedSubject = "Powiadomienie: " + subject;
|
|
|
+ String expectedContent = "Powiadomienie od " + user.getFirstName() + " " + user.getLastName() + "\n" + content;
|
|
|
+
|
|
|
+ for (String recipient : recipients) {
|
|
|
+ verify(mailServiceMock).sendEmail(user.getEmail(), recipient, expectedSubject, expectedContent);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testSendNotifications() throws Exception {
|
|
|
+ User user = new User("Jan", "Kowalski", "jan.kowalski@example.com");
|
|
|
+ List<String> recipients = Arrays.asList("recipient1@example.com", "recipient2@example.com");
|
|
|
+ String subject = "Testowy temat";
|
|
|
+ String content = "Treść wiadomości";
|
|
|
+
|
|
|
+ sendAndVerifyNotifications(user, recipients, subject, content);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testSendNotificationsFromCSV() throws Exception {
|
|
|
+ String filePath = "src/test/resources/notifications.csv";
|
|
|
+
|
|
|
+ Pair<User, List<String>> data = DataLoader.loadData(filePath);
|
|
|
+ User user = data.getLeft();
|
|
|
+ List<String> recipients = data.getRight();
|
|
|
+ String subject = "Testowy temat";
|
|
|
+ String content = "Treść wiadomości";
|
|
|
+
|
|
|
+ sendAndVerifyNotifications(user, recipients, subject, content);
|
|
|
+
|
|
|
+ for (String recipient : recipients) {
|
|
|
+ System.out.println("Sent email to: " + recipient);
|
|
|
+ System.out.println("Subject: " + "Powiadomienie: " + subject);
|
|
|
+ System.out.println("Content: " + "Powiadomienie od " + user.getFirstName() + " " + user.getLastName() + "\n" + content);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testSendNotificationsWithException() throws Exception {
|
|
|
+ MailService mailServiceMock = mock(MailService.class);
|
|
|
+ NotificationService notificationService = new NotificationServiceImpl(mailServiceMock);
|
|
|
+
|
|
|
+ User user = new User("Jan", "Kowalski", "jan.kowalski@example.com");
|
|
|
+ List<String> recipients = Arrays.asList("recipient1@example.com", "recipient2@example.com");
|
|
|
+ String subject = "Testowy temat";
|
|
|
+ String content = "Treść wiadomości";
|
|
|
+
|
|
|
+ doThrow(new RuntimeException("Email sending failed"))
|
|
|
+ .when(mailServiceMock).sendEmail(anyString(), eq("recipient1@example.com"), anyString(), anyString());
|
|
|
+
|
|
|
+ notificationService.sendNotifications(user, recipients, subject, content);
|
|
|
+
|
|
|
+ String expectedSubject = "Powiadomienie: Testowy temat";
|
|
|
+ String expectedContent = "Powiadomienie od Jan Kowalski\nTreść wiadomości";
|
|
|
+
|
|
|
+ verify(mailServiceMock).sendEmail(user.getEmail(), "recipient1@example.com", expectedSubject, expectedContent);
|
|
|
+ verify(mailServiceMock).sendEmail(user.getEmail(), "recipient2@example.com", expectedSubject, expectedContent);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testPartialFailuresStillSendToOthers() throws Exception {
|
|
|
+ User user = new User("Jan", "Kowalski", "jan.kowalski@example.com");
|
|
|
+ List<String> recipients = Arrays.asList("recipient1@example.com", "recipient2@example.com", "recipient3@example.com");
|
|
|
+ String subject = "Testowy temat";
|
|
|
+ String content = "Treść wiadomości";
|
|
|
+
|
|
|
+ doThrow(new RuntimeException("Email sending failed"))
|
|
|
+ .when(mailServiceMock).sendEmail(anyString(), eq("recipient1@example.com"), anyString(), anyString());
|
|
|
+
|
|
|
+ doNothing()
|
|
|
+ .when(mailServiceMock).sendEmail(anyString(), eq("recipient2@example.com"), anyString(), anyString());
|
|
|
+ doNothing()
|
|
|
+ .when(mailServiceMock).sendEmail(anyString(), eq("recipient3@example.com"), anyString(), anyString());
|
|
|
+
|
|
|
+ notificationService.sendNotifications(user, recipients, subject, content);
|
|
|
+
|
|
|
+ String expectedSubject = "Powiadomienie: " + subject;
|
|
|
+ String expectedContent = "Powiadomienie od " + user.getFirstName() + " " + user.getLastName() + "\n" + content;
|
|
|
+
|
|
|
+ verify(mailServiceMock).sendEmail(user.getEmail(), "recipient1@example.com", expectedSubject, expectedContent);
|
|
|
+ verify(mailServiceMock).sendEmail(user.getEmail(), "recipient2@example.com", expectedSubject, expectedContent);
|
|
|
+ verify(mailServiceMock).sendEmail(user.getEmail(), "recipient3@example.com", expectedSubject, expectedContent);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|