Refactoring
This commit is contained in:
138
src/test/java/com/lions/dev/service/NotificationServiceTest.java
Normal file
138
src/test/java/com/lions/dev/service/NotificationServiceTest.java
Normal file
@@ -0,0 +1,138 @@
|
||||
package com.lions.dev.service;
|
||||
|
||||
import com.lions.dev.entity.notification.Notification;
|
||||
import com.lions.dev.entity.users.Users;
|
||||
import com.lions.dev.exception.UserNotFoundException;
|
||||
import com.lions.dev.repository.EventsRepository;
|
||||
import com.lions.dev.repository.NotificationRepository;
|
||||
import com.lions.dev.repository.UsersRepository;
|
||||
import io.quarkus.test.InjectMock;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@QuarkusTest
|
||||
class NotificationServiceTest {
|
||||
|
||||
@Inject
|
||||
NotificationService notificationService;
|
||||
|
||||
@InjectMock
|
||||
NotificationRepository notificationRepository;
|
||||
|
||||
@InjectMock
|
||||
UsersRepository usersRepository;
|
||||
|
||||
@InjectMock
|
||||
EventsRepository eventsRepository;
|
||||
|
||||
@Test
|
||||
@DisplayName("getNotificationsByUserId avec utilisateur existant retourne la liste")
|
||||
void getNotificationsByUserId_userExists_returnsList() {
|
||||
UUID userId = UUID.randomUUID();
|
||||
Users user = new Users();
|
||||
user.setId(userId);
|
||||
Notification notif = new Notification("Titre", "Message", "event", user);
|
||||
|
||||
when(usersRepository.findById(userId)).thenReturn(user);
|
||||
when(notificationRepository.findByUserId(userId)).thenReturn(List.of(notif));
|
||||
|
||||
List<Notification> result = notificationService.getNotificationsByUserId(userId);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(1, result.size());
|
||||
assertEquals("Titre", result.get(0).getTitle());
|
||||
verify(usersRepository).findById(userId);
|
||||
verify(notificationRepository).findByUserId(userId);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("getNotificationsByUserId avec utilisateur inconnu lance UserNotFoundException")
|
||||
void getNotificationsByUserId_userNotFound_throws() {
|
||||
UUID userId = UUID.randomUUID();
|
||||
when(usersRepository.findById(userId)).thenReturn(null);
|
||||
|
||||
assertThrows(UserNotFoundException.class, () ->
|
||||
notificationService.getNotificationsByUserId(userId));
|
||||
verify(notificationRepository, never()).findByUserId(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("createNotification avec données valides persiste et retourne la notification")
|
||||
void createNotification_validData_persistsAndReturns() {
|
||||
UUID userId = UUID.randomUUID();
|
||||
UUID eventId = UUID.randomUUID();
|
||||
Users user = new Users();
|
||||
user.setId(userId);
|
||||
|
||||
when(usersRepository.findById(userId)).thenReturn(user);
|
||||
|
||||
Notification created = notificationService.createNotification(
|
||||
"Rappel", "L'événement commence bientôt", "reminder", userId, eventId);
|
||||
|
||||
assertNotNull(created);
|
||||
assertEquals("Rappel", created.getTitle());
|
||||
assertEquals("reminder", created.getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("createNotification avec userId inconnu lance UserNotFoundException")
|
||||
void createNotification_unknownUser_throws() {
|
||||
UUID userId = UUID.randomUUID();
|
||||
when(usersRepository.findById(userId)).thenReturn(null);
|
||||
|
||||
assertThrows(UserNotFoundException.class, () ->
|
||||
notificationService.createNotification("T", "M", "event", userId, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("countUnreadNotifications avec utilisateur existant retourne le compte")
|
||||
void countUnreadNotifications_userExists_returnsCount() {
|
||||
UUID userId = UUID.randomUUID();
|
||||
Users user = new Users();
|
||||
user.setId(userId);
|
||||
when(usersRepository.findById(userId)).thenReturn(user);
|
||||
when(notificationRepository.countUnreadByUserId(userId)).thenReturn(3L);
|
||||
|
||||
long count = notificationService.countUnreadNotifications(userId);
|
||||
|
||||
assertEquals(3L, count);
|
||||
verify(notificationRepository).countUnreadByUserId(userId);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("countUnreadNotifications avec utilisateur inconnu lance UserNotFoundException")
|
||||
void countUnreadNotifications_userNotFound_throws() {
|
||||
UUID userId = UUID.randomUUID();
|
||||
when(usersRepository.findById(userId)).thenReturn(null);
|
||||
|
||||
assertThrows(UserNotFoundException.class, () ->
|
||||
notificationService.countUnreadNotifications(userId));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("getNotificationsByUserIdWithPagination avec utilisateur existant retourne la page")
|
||||
void getNotificationsByUserIdWithPagination_userExists_returnsPage() {
|
||||
UUID userId = UUID.randomUUID();
|
||||
Users user = new Users();
|
||||
user.setId(userId);
|
||||
when(usersRepository.findById(userId)).thenReturn(user);
|
||||
when(notificationRepository.findByUserIdWithPagination(userId, 0, 10))
|
||||
.thenReturn(Collections.emptyList());
|
||||
|
||||
List<Notification> result = notificationService.getNotificationsByUserIdWithPagination(userId, 0, 10);
|
||||
|
||||
assertNotNull(result);
|
||||
assertTrue(result.isEmpty());
|
||||
verify(notificationRepository).findByUserIdWithPagination(userId, 0, 10);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user