Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add bad request if page exceeding total pages #7866

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,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.";
}
6 changes: 6 additions & 0 deletions service/src/main/java/greencity/service/EventServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ public PageableAdvancedDto<EventDto> getEvents(Pageable page, FilterEventDto fil
}

Page<Long> 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<Tuple> tuples;
if (userId != null) {
tuples = eventRepo.loadEventDataByIds(eventIds.getContent(), userId);
Expand Down
30 changes: 30 additions & 0 deletions service/src/test/java/greencity/service/EventServiceImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,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<Long> 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);
Cr1stal423 marked this conversation as resolved.
Show resolved Hide resolved
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);
Expand Down
Loading