From ce665d143f723637e34193cfd4d67705cdc7241e Mon Sep 17 00:00:00 2001 From: David Neuhauss Date: Tue, 13 Jan 2026 18:44:27 +0100 Subject: [PATCH] add tests for NotificationSchedulerServiceTest --- .../trend/services/TrendMessageGenerator.java | 8 - src/main/resources/application.properties | 2 +- .../NotificationSchedulerServiceTest.java | 248 ++++++++++++++++++ 3 files changed, 249 insertions(+), 9 deletions(-) delete mode 100644 src/main/java/SuMoC/backend/trend/services/TrendMessageGenerator.java create mode 100644 src/test/java/SuMoC/backend/notification/services/NotificationSchedulerServiceTest.java diff --git a/src/main/java/SuMoC/backend/trend/services/TrendMessageGenerator.java b/src/main/java/SuMoC/backend/trend/services/TrendMessageGenerator.java deleted file mode 100644 index 59754b7..0000000 --- a/src/main/java/SuMoC/backend/trend/services/TrendMessageGenerator.java +++ /dev/null @@ -1,8 +0,0 @@ -package SuMoC.backend.trend.services; - - -import org.springframework.stereotype.Component; - -@Component -public class TrendMessageGenerator { -} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index eca34b8..fcc7672 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -21,7 +21,7 @@ trenddetection.positive-threshold=25.0 # Regular trip detection settings #regulartripdetection.detection-algorithm=iterative # FireBase -firebase.config.enabled=true +firebase.config.enabled=false firebase.config.service-account=classpath:firebase-private-key.json # Base URL to external services carta.base-url=https://www.innamoruhr.de/c2 diff --git a/src/test/java/SuMoC/backend/notification/services/NotificationSchedulerServiceTest.java b/src/test/java/SuMoC/backend/notification/services/NotificationSchedulerServiceTest.java new file mode 100644 index 0000000..5df0e47 --- /dev/null +++ b/src/test/java/SuMoC/backend/notification/services/NotificationSchedulerServiceTest.java @@ -0,0 +1,248 @@ +package SuMoC.backend.notification.services; + +import SuMoC.backend.notification.enums.NotificationStatus; +import SuMoC.backend.notification.enums.NotificationType; +import SuMoC.backend.notification.models.Notification; +import SuMoC.backend.notification.repositories.NotificationRepository; +import SuMoC.backend.trend.dto.TrendDetectionDTO; +import SuMoC.backend.trend.enums.DeltaType; +import SuMoC.backend.trend.enums.StatAttribute; +import SuMoC.backend.trend.enums.TrendTimespan; +import SuMoC.backend.trend.enums.TrendType; +import SuMoC.backend.user.models.User; +import com.fasterxml.jackson.databind.ObjectMapper; +import jakarta.persistence.EntityManager; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.time.LocalTime; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +public class NotificationSchedulerServiceTest { + + @Mock + private NotificationRepository notificationRepository; + + @Mock + private EntityManager entityManager; + + private ObjectMapper objectMapper; + + private NotificationSchedulerService notificationSchedulerService; + + @Captor + private ArgumentCaptor> notificationListCaptor; + + private User mockUser; + private LocalTime testTime; + + @BeforeEach + void setUp() { + objectMapper = new ObjectMapper(); + notificationSchedulerService = new NotificationSchedulerService(objectMapper, notificationRepository, entityManager); + mockUser = new User("privateName", "publicName", "test@example.com", "password"); + mockUser.setId(1L); + testTime = LocalTime.of(9, 0); + } + + @Test + @SuppressWarnings("unchecked") + void scheduleAll_WithTrendDetectionDTO_SerializesAndPersistsSuccessfully() { + // Arrange + TrendDetectionDTO trendDetectionDTO = new TrendDetectionDTO( + 1L, + TrendTimespan.WEEKLY, + TrendType.INCREASING, + StatAttribute.CAR_DISTANCE, + DeltaType.ABSOLUTE, + 25.5 + ); + + List dataBodies = List.of(trendDetectionDTO); + + // Mock EntityManager to return a proxy + when(entityManager.getReference(eq(User.class), eq(1L))).thenReturn(mockUser); + + // Act + notificationSchedulerService.scheduleAll( + (List) dataBodies, + NotificationType.TREND, + testTime + ); + + // Assert + verify(notificationRepository, times(1)).saveAll(notificationListCaptor.capture()); + List savedNotifications = notificationListCaptor.getValue(); + + assertEquals(1, savedNotifications.size()); + Notification notification = savedNotifications.getFirst(); + + // Verify notification properties + assertEquals(mockUser, notification.getUser()); + assertEquals(NotificationType.TREND, notification.getType()); + assertEquals(NotificationStatus.PENDING, notification.getStatus()); + assertEquals(testTime, notification.getEarliestTimeOfDayToSend()); + assertEquals(0, notification.getFailedSendAttempts()); + assertNotNull(notification.getCreatedAt()); + + // Verify that the data was serialized correctly + assertNotNull(notification.getData()); + assertTrue(notification.getData().contains("userId")); + assertTrue(notification.getData().contains("WEEKLY")); + assertTrue(notification.getData().contains("INCREASING")); + assertTrue(notification.getData().contains("CAR_DISTANCE")); + } + + @Test + @SuppressWarnings("unchecked") + void scheduleAll_WithTrendDetectionDTO_DataCanBeDeserialized() throws Exception { + // Arrange + TrendDetectionDTO originalDTO = new TrendDetectionDTO( + 1L, + TrendTimespan.WEEKLY, + TrendType.DECREASING, + StatAttribute.CAR_DISTANCE, + DeltaType.PERCENTAGE, + 15.75 + ); + + List dataBodies = List.of(originalDTO); + when(entityManager.getReference(eq(User.class), eq(1L))).thenReturn(mockUser); + + // Act + notificationSchedulerService.scheduleAll( + (List) dataBodies, + NotificationType.TREND, + testTime + ); + + // Capture the saved notification + verify(notificationRepository).saveAll(notificationListCaptor.capture()); + Notification savedNotification = notificationListCaptor.getValue().getFirst(); + + // Assert - deserialize and verify the data + TrendDetectionDTO deserializedDTO = objectMapper.readValue( + savedNotification.getData(), + TrendDetectionDTO.class + ); + + assertEquals(originalDTO.getUserId(), deserializedDTO.getUserId()); + assertEquals(originalDTO.getTimespan(), deserializedDTO.getTimespan()); + assertEquals(originalDTO.getType(), deserializedDTO.getType()); + assertEquals(originalDTO.getAttribute(), deserializedDTO.getAttribute()); + assertEquals(originalDTO.getDeltaType(), deserializedDTO.getDeltaType()); + assertEquals(originalDTO.getChangeValue(), deserializedDTO.getChangeValue()); + } + + @Test + @SuppressWarnings("unchecked") + void scheduleAll_WithMultipleTrendDetectionDTOs_SerializesAll() { + // Arrange + TrendDetectionDTO dto1 = new TrendDetectionDTO( + 1L, + TrendTimespan.WEEKLY, + TrendType.INCREASING, + StatAttribute.CAR_DISTANCE, + DeltaType.ABSOLUTE, + 10.0 + ); + + TrendDetectionDTO dto2 = new TrendDetectionDTO( + 2L, + TrendTimespan.WEEKLY, + TrendType.DECREASING, + StatAttribute.CAR_DISTANCE, + DeltaType.PERCENTAGE, + 5.0 + ); + + List dataBodies = List.of(dto1, dto2); + + // Mock EntityManager to return proxies + User mockUser2 = new User("privateName2", "publicName2", "test2@example.com", "password"); + mockUser2.setId(2L); + + when(entityManager.getReference(eq(User.class), eq(1L))).thenReturn(mockUser); + when(entityManager.getReference(eq(User.class), eq(2L))).thenReturn(mockUser2); + + // Act + notificationSchedulerService.scheduleAll( + (List) dataBodies, + NotificationType.TREND, + testTime + ); + + // Assert + verify(notificationRepository, times(1)).saveAll(notificationListCaptor.capture()); + List savedNotifications = notificationListCaptor.getValue(); + + assertEquals(2, savedNotifications.size()); + assertEquals(mockUser, savedNotifications.get(0).getUser()); + assertEquals(mockUser2, savedNotifications.get(1).getUser()); + } + + @Test + @SuppressWarnings("unchecked") + void scheduleAll_WithTrendDetectionDTO_SerializationException_ThrowsRuntimeException() { + // Arrange + TrendDetectionDTO dto = new TrendDetectionDTO( + 1L, + TrendTimespan.WEEKLY, + TrendType.INCREASING, + StatAttribute.CAR_DISTANCE, + DeltaType.ABSOLUTE, + 25.5 + ); + + List dataBodies = List.of(dto); + + when(entityManager.getReference(eq(User.class), eq(1L))).thenReturn(mockUser); + + // Act & Assert + // The service should handle serialization correctly and not throw exceptions + // for valid DTOs + assertDoesNotThrow(() -> + notificationSchedulerService.scheduleAll( + (List) dataBodies, + NotificationType.TREND, + testTime + ) + ); + } + + @Test + @SuppressWarnings("unchecked") + void scheduleAll_VerifyNotificationStatusIsInitiallyPending() { + // Arrange + TrendDetectionDTO trendDetectionDTO = TrendDetectionDTO.mockTrendDetectionDTO(); + trendDetectionDTO.setUserId(1L); + + List dataBodies = List.of(trendDetectionDTO); + when(entityManager.getReference(eq(User.class), eq(1L))).thenReturn(mockUser); + + // Act + notificationSchedulerService.scheduleAll( + (List) dataBodies, + NotificationType.TREND, + testTime + ); + + // Assert + verify(notificationRepository).saveAll(notificationListCaptor.capture()); + Notification notification = notificationListCaptor.getValue().getFirst(); + + assertEquals(NotificationStatus.PENDING, notification.getStatus()); + assertEquals(0, notification.getFailedSendAttempts()); + } +} +