Skip to content

Commit

Permalink
[MSEARCH-812] Increase the limit to fetch more than 1k locations (#633)
Browse files Browse the repository at this point in the history
* Increase the limit to fetch more than 1k locations

* Increase limit param to 10000 for GET consortium locations endpoint with validation

* Fixed code style

* Added test cases

* minor improvements

* Error message improved

* fixed unit test
  • Loading branch information
azizbekxm authored Jul 24, 2024
1 parent 4f6532a commit fab7a99
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.folio.search.service.consortium;

import static org.folio.search.service.SearchService.DEFAULT_MAX_SEARCH_RESULT_WINDOW;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.folio.search.domain.dto.ConsortiumLocation;
import org.folio.search.domain.dto.SortOrder;
import org.folio.search.exception.RequestValidationException;
import org.folio.search.model.SearchResult;
import org.folio.search.repository.ConsortiumLocationRepository;
import org.springframework.stereotype.Service;
Expand All @@ -29,6 +32,7 @@ public SearchResult<ConsortiumLocation> fetchLocations(String tenantHeader,
tenantHeader,
tenantId,
sortBy);
validatePaginationParameters(limit, offset);
validateSortByValue(sortBy);
return executor.execute(
tenantHeader,
Expand All @@ -37,9 +41,20 @@ public SearchResult<ConsortiumLocation> fetchLocations(String tenantHeader,

private void validateSortByValue(String sortBy) {
if (!(NAME.equals(sortBy) || ID.equals(sortBy) || TENANT_ID.equals(sortBy))) {
throw new IllegalArgumentException("Invalid sortBy value: " + sortBy);
var validationException = new RequestValidationException("Invalid sortBy value is being used",
"sortBy", sortBy);
log.warn(validationException.getMessage());
throw validationException;
}
}

private void validatePaginationParameters(Integer limit, Integer offset) {
if (offset + limit > DEFAULT_MAX_SEARCH_RESULT_WINDOW) {
var validationException = new RequestValidationException("The sum of limit and offset should not exceed 10000.",
"offset + limit", String.valueOf(offset + limit));
log.warn(validationException.getMessage());
throw validationException;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ description: Limit the number of elements returned in the response.
schema:
type: integer
minimum: 0
maximum: 1000
default: 1000
maximum: 10000
default: 10000
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
import java.util.function.Supplier;
import org.folio.search.domain.dto.ConsortiumLocation;
import org.folio.search.domain.dto.SortOrder;
import org.folio.search.exception.RequestValidationException;
import org.folio.search.model.SearchResult;
import org.folio.search.repository.ConsortiumLocationRepository;
import org.folio.spring.testing.type.UnitTest;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
Expand Down Expand Up @@ -59,6 +61,28 @@ void fetchLocations_ValidSortBy() {
verify(executor).execute(eq(tenantId), any(Supplier.class));
}

@Test
void fetchLocations_InvalidSortBy() {
var sortOrder = SortOrder.ASC;
var limit = 10;
var offset = 0;

Assertions.assertThrows(RequestValidationException.class, () ->
service.fetchLocations(CONSORTIUM_TENANT, CONSORTIUM_TENANT, limit, offset, "invalid", sortOrder)
);
}

@Test
void fetchLocations_InvalidPaginationParameters() {
var sortOrder = SortOrder.ASC;
var limit = 1000;
var offset = 9900;

Assertions.assertThrows(RequestValidationException.class, () ->
service.fetchLocations(CONSORTIUM_TENANT, CONSORTIUM_TENANT, limit, offset, NAME, sortOrder)
);
}

@NotNull
private static SearchResult<ConsortiumLocation> prepareSearchResult() {
var location = new ConsortiumLocation()
Expand Down

0 comments on commit fab7a99

Please sign in to comment.