diff --git a/service-api/src/main/java/greencity/constant/ErrorMessage.java b/service-api/src/main/java/greencity/constant/ErrorMessage.java index 50acc2cb3..076cb692c 100644 --- a/service-api/src/main/java/greencity/constant/ErrorMessage.java +++ b/service-api/src/main/java/greencity/constant/ErrorMessage.java @@ -214,4 +214,5 @@ public class ErrorMessage { public static final String YOU_HAS_ALREADY_ACCEPT_THIS_INVITATION = "Current user already has accepted invitation"; public static final String INVITATION_ALREADY_EXIST = "Invitation already exist"; public static final String INVALID_DURATION_BETWEEN_START_AND_FINISH = "Invalid duration between start and finish"; + public static final String PAGE_NOT_FOUND_MESSAGE = "Requested page %d exceeds total pages %d."; } diff --git a/service/src/main/java/greencity/service/EventServiceImpl.java b/service/src/main/java/greencity/service/EventServiceImpl.java index 5abdaa225..66243ae4d 100644 --- a/service/src/main/java/greencity/service/EventServiceImpl.java +++ b/service/src/main/java/greencity/service/EventServiceImpl.java @@ -245,6 +245,12 @@ public PageableAdvancedDto getEvents(Pageable page, FilterEventDto fil } Page eventIds = eventRepo.findEventsIds(page, filterEventDto, userId); + + if (page.getPageNumber() >= eventIds.getTotalPages() && eventIds.getTotalPages() > 0) { + throw new BadRequestException( + String.format(ErrorMessage.PAGE_NOT_FOUND_MESSAGE, page.getPageNumber(), eventIds.getTotalPages())); + } + List tuples; if (userId != null) { tuples = eventRepo.loadEventDataByIds(eventIds.getContent(), userId); diff --git a/service/src/test/java/greencity/service/EventServiceImplTest.java b/service/src/test/java/greencity/service/EventServiceImplTest.java index b727401ac..cee9cd13d 100644 --- a/service/src/test/java/greencity/service/EventServiceImplTest.java +++ b/service/src/test/java/greencity/service/EventServiceImplTest.java @@ -951,6 +951,36 @@ void getEventsForAuthorizedUserTest() { verify(eventRepo).loadEventDataByIds(idsPage.getContent(), userId); } + @Test + void testGetEventsForAuthorizedUserExceedingPageTest() { + int requestedPage = 10; + int totalPages = 2; + int pageSize = 6; + Pageable pageable = PageRequest.of(requestedPage, pageSize); + Long userId = 1L; + FilterEventDto filterEventDto = getFilterEventDto(); + + Page idsPage = new PageImpl<>( + List.of(3L, 1L), + PageRequest.of(0, pageSize), + totalPages * pageSize); + + when(restClient.findById(userId)).thenReturn(getUserVO()); + when(eventRepo.findEventsIds(pageable, filterEventDto, userId)).thenReturn(idsPage); + + BadRequestException exception = assertThrows( + BadRequestException.class, + () -> eventService.getEvents(pageable, filterEventDto, userId), + "Expected BadRequestException to be thrown"); + + String expectedMessage = String.format("Requested page %d exceeds total pages %d.", requestedPage, totalPages); + assertEquals(expectedMessage, exception.getMessage()); + + verify(restClient).findById(userId); + verify(eventRepo).findEventsIds(pageable, filterEventDto, userId); + verify(eventRepo, never()).loadEventDataByIds(anyList(), anyLong()); + } + @Test void getEventsForUnauthorizedUserTest() { Pageable pageable = PageRequest.of(0, 6);