diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 05e8dc8..a6d6ab0 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -2,9 +2,9 @@ name: Build on: push: - branches: [ main ] + branches: [ main, [0-9]+.[0-9]+.* ] pull_request: - branches: [ main ] + branches: [ main, [0-9]+.[0-9]+.* ] jobs: build: diff --git a/.github/workflows/renovate.yml b/.github/workflows/renovate.yml index 749949e..10d0a93 100644 --- a/.github/workflows/renovate.yml +++ b/.github/workflows/renovate.yml @@ -1,8 +1,8 @@ name: Renovate -on: - schedule: - - cron: '*/5 8-19 * * 1-5' +#on: +# schedule: +# - cron: '*/5 8-19 * * 1-5' jobs: renovate: diff --git a/pom.xml b/pom.xml index ef2c934..db31d0b 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ fr.ouestfrance.querydsl querydsl-postgrest-bom - 1.2.3-SNAPSHOT + 1.2.4-SNAPSHOT pom querydsl-postgrest-bom Implementation of unified queryDSL for postgRest API diff --git a/querydsl-postgrest-resttemplate-adapter/pom.xml b/querydsl-postgrest-resttemplate-adapter/pom.xml index d0b2f37..3f841cb 100644 --- a/querydsl-postgrest-resttemplate-adapter/pom.xml +++ b/querydsl-postgrest-resttemplate-adapter/pom.xml @@ -5,7 +5,7 @@ fr.ouestfrance.querydsl querydsl-postgrest-bom ../pom.xml - 1.2.3-SNAPSHOT + 1.2.4-SNAPSHOT querydsl-postgrest-resttemplate-adapter @@ -20,7 +20,7 @@ org.springframework spring-web - 6.1.2 + 6.1.3 org.apache.commons diff --git a/querydsl-postgrest-resttemplate-adapter/src/main/java/fr/ouestfrance/querydsl/postgrest/PostgrestRestTemplate.java b/querydsl-postgrest-resttemplate-adapter/src/main/java/fr/ouestfrance/querydsl/postgrest/PostgrestRestTemplate.java index f8a405b..58e58c0 100644 --- a/querydsl-postgrest-resttemplate-adapter/src/main/java/fr/ouestfrance/querydsl/postgrest/PostgrestRestTemplate.java +++ b/querydsl-postgrest-resttemplate-adapter/src/main/java/fr/ouestfrance/querydsl/postgrest/PostgrestRestTemplate.java @@ -4,6 +4,7 @@ import fr.ouestfrance.querydsl.postgrest.model.Page; import fr.ouestfrance.querydsl.postgrest.model.PageImpl; import fr.ouestfrance.querydsl.postgrest.model.Range; +import fr.ouestfrance.querydsl.postgrest.model.RangeResponse; import lombok.AccessLevel; import lombok.RequiredArgsConstructor; import org.apache.commons.lang3.reflect.TypeUtils; @@ -21,6 +22,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.stream.Stream; /** * Rest interface for querying postgrest @@ -37,15 +39,15 @@ public class PostgrestRestTemplate implements PostgrestClient { * Postgrest restTemplate adapter * * @param restTemplate restTemplate - * @return PostgrestResttemplate implementation + * @return PostgrestRestTemplate implementation */ public static PostgrestRestTemplate of(RestTemplate restTemplate) { return new PostgrestRestTemplate(restTemplate); } @Override - public Page search(String resource, Map> params, - Map> headers, Class clazz) { + public RangeResponse search(String resource, Map> params, + Map> headers, Class clazz) { ResponseEntity> response = restTemplate.exchange(restTemplate.getUriTemplateHandler() .expand(UriComponentsBuilder.fromPath(resource) .queryParams(toMultiMap(params)).build().toString(), new HashMap<>()), HttpMethod.GET, @@ -54,14 +56,14 @@ public Page search(String resource, Map> params, return Optional.of(response) .map(HttpEntity::getBody) .map(x -> { - PageImpl page = new PageImpl<>(x, null, x.size(), 1); - List contentRangeHeaders = response.getHeaders().get("Content-Range"); - if (contentRangeHeaders != null) { - Range range = Range.of(contentRangeHeaders.stream().findFirst().toString()); - page.withRange(range); - } - return (Page) page; - }).orElse(Page.empty()); + Range range = Optional.ofNullable(response.getHeaders().get("Content-Range")) + .map(List::stream) + .map(Stream::findFirst) + .filter(Optional::isPresent) + .map(Optional::get) + .map(Range::of).orElse(null); + return new RangeResponse<>(x, range); + }).orElse(new RangeResponse<>(List.of(), null)); } @Override @@ -74,8 +76,8 @@ public List post(String resource, List value, Map List patch(String resource, Map> params, Object value, Map> headers, Class clazz) { MultiValueMap queryParams = toMultiMap(params); return restTemplate.exchange(restTemplate.getUriTemplateHandler() - .expand(UriComponentsBuilder.fromPath(resource).queryParams(queryParams).build().toString(), new HashMap<>()), - HttpMethod.PATCH, new HttpEntity<>(value, toHeaders(headers)), listRef(clazz)) + .expand(UriComponentsBuilder.fromPath(resource).queryParams(queryParams).build().toString(), new HashMap<>()), + HttpMethod.PATCH, new HttpEntity<>(value, toHeaders(headers)), listRef(clazz)) .getBody(); } diff --git a/querydsl-postgrest-webclient-adapter/pom.xml b/querydsl-postgrest-webclient-adapter/pom.xml index 5ef98a0..2f4c38c 100644 --- a/querydsl-postgrest-webclient-adapter/pom.xml +++ b/querydsl-postgrest-webclient-adapter/pom.xml @@ -5,7 +5,7 @@ fr.ouestfrance.querydsl querydsl-postgrest-bom ../pom.xml - 1.2.3-SNAPSHOT + 1.2.4-SNAPSHOT querydsl-postgrest-webclient-adapter @@ -20,7 +20,7 @@ org.springframework spring-webflux - 6.1.2 + 6.1.3 org.apache.commons diff --git a/querydsl-postgrest-webclient-adapter/src/main/java/fr/ouestfrance/querydsl/postgrest/PostgrestWebClient.java b/querydsl-postgrest-webclient-adapter/src/main/java/fr/ouestfrance/querydsl/postgrest/PostgrestWebClient.java index 0d30ca1..01623fc 100644 --- a/querydsl-postgrest-webclient-adapter/src/main/java/fr/ouestfrance/querydsl/postgrest/PostgrestWebClient.java +++ b/querydsl-postgrest-webclient-adapter/src/main/java/fr/ouestfrance/querydsl/postgrest/PostgrestWebClient.java @@ -4,6 +4,7 @@ import fr.ouestfrance.querydsl.postgrest.model.Page; import fr.ouestfrance.querydsl.postgrest.model.PageImpl; import fr.ouestfrance.querydsl.postgrest.model.Range; +import fr.ouestfrance.querydsl.postgrest.model.RangeResponse; import lombok.AccessLevel; import lombok.RequiredArgsConstructor; import org.apache.commons.lang3.reflect.TypeUtils; @@ -19,6 +20,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.stream.Stream; /** * Rest interface for querying postgrest @@ -48,8 +50,8 @@ public static PostgrestWebClient of(WebClient webClient) { } @Override - public Page search(String resource, Map> params, - Map> headers, Class clazz) { + public RangeResponse search(String resource, Map> params, + Map> headers, Class clazz) { ResponseEntity> response = webClient.get().uri(uriBuilder -> { uriBuilder.path(resource); uriBuilder.queryParams(toMultiMap(params)); @@ -64,14 +66,14 @@ public Page search(String resource, Map> params, return Optional.ofNullable(response) .map(HttpEntity::getBody) .map(x -> { - PageImpl page = new PageImpl<>(x, null, x.size(), 1); - List contentRangeHeaders = response.getHeaders().get("Content-Range"); - if (contentRangeHeaders != null && !contentRangeHeaders.isEmpty()) { - Range range = Range.of(contentRangeHeaders.stream().findFirst().toString()); - page.withRange(range); - } - return (Page) page; - }).orElse(Page.empty()); + Range range = Optional.ofNullable(response.getHeaders().get("Content-Range")) + .map(List::stream) + .map(Stream::findFirst) + .filter(Optional::isPresent) + .map(Optional::get) + .map(Range::of).orElse(null); + return new RangeResponse<>(x, range); + }).orElse(new RangeResponse<>(List.of(), null)); } @Override diff --git a/querydsl-postgrest/pom.xml b/querydsl-postgrest/pom.xml index 4b44850..63b8c70 100644 --- a/querydsl-postgrest/pom.xml +++ b/querydsl-postgrest/pom.xml @@ -5,7 +5,7 @@ fr.ouestfrance.querydsl querydsl-postgrest-bom ../pom.xml - 1.2.3-SNAPSHOT + 1.2.4-SNAPSHOT querydsl-postgrest diff --git a/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/PostgrestClient.java b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/PostgrestClient.java index 82370e7..59ed5e7 100644 --- a/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/PostgrestClient.java +++ b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/PostgrestClient.java @@ -1,7 +1,7 @@ package fr.ouestfrance.querydsl.postgrest; import fr.ouestfrance.querydsl.postgrest.model.CountItem; -import fr.ouestfrance.querydsl.postgrest.model.Page; +import fr.ouestfrance.querydsl.postgrest.model.RangeResponse; import java.util.List; import java.util.Map; @@ -21,8 +21,8 @@ public interface PostgrestClient { * @return ResponseEntity containing the results */ - Page search(String resource, Map> params, - Map> headers, Class clazz); + RangeResponse search(String resource, Map> params, + Map> headers, Class clazz); /** * Save body diff --git a/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/PostgrestRepository.java b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/PostgrestRepository.java index c04c6c0..abcd685 100644 --- a/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/PostgrestRepository.java +++ b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/PostgrestRepository.java @@ -74,12 +74,14 @@ public Page search(Object criteria, Pageable pageable) { } // Add select criteria getSelects(criteria).ifPresent(queryParams::add); - Page response = client.search(annotation.resource(), toMap(queryParams), headers, clazz); - if (response instanceof PageImpl page) { - page.setPageable(pageable); - } - // Retrieve result headers - return response; + RangeResponse response = client.search(annotation.resource(), toMap(queryParams), headers, clazz); + + int pageSize = Optional.of(pageable) + .filter(Pageable::hasSize) + .map(Pageable::getPageSize) + .orElse(response.getPageSize()); + // Compute PageResponse + return new PageImpl<>(response.data(), pageable, response.getTotalElements(), (int) Math.ceil((double) response.getTotalElements() / pageSize)); } diff --git a/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/CountItem.java b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/CountItem.java index abd157e..b69b199 100644 --- a/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/CountItem.java +++ b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/CountItem.java @@ -1,6 +1,5 @@ package fr.ouestfrance.querydsl.postgrest.model; -import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; diff --git a/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/Page.java b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/Page.java index 5fff579..affff3a 100644 --- a/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/Page.java +++ b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/Page.java @@ -3,7 +3,6 @@ import java.util.Arrays; import java.util.Iterator; import java.util.List; -import java.util.Optional; import java.util.function.Function; import java.util.stream.Stream; @@ -101,11 +100,10 @@ default Page map(Function converter) { /** * Check that page has next page + * * @return true if totalPages is greater than pageNumber */ default boolean hasNext() { - return Optional.ofNullable(getPageable()) - .map(pageable -> pageable.getPageNumber() +1 < getTotalPages()) - .orElse(false); + return getPageable().getPageNumber() + 1 < getTotalPages(); } } diff --git a/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/PageImpl.java b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/PageImpl.java index 8be083a..7e8b8cf 100644 --- a/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/PageImpl.java +++ b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/PageImpl.java @@ -21,7 +21,6 @@ public class PageImpl implements Page { /** * Request information */ - @Setter private Pageable pageable; /** * Total elements @@ -31,15 +30,4 @@ public class PageImpl implements Page { * Total pages */ private int totalPages; - - /** - * Apply range to a specific Page - * @param range range to apply - */ - public void withRange(Range range) { - totalElements = range.getTotalElements(); - if (totalElements > 0) { - totalPages = (int) (totalElements / (range.getLimit() - range.getOffset())); - } - } } diff --git a/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/Pageable.java b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/Pageable.java index 340b2ea..5ba7e53 100644 --- a/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/Pageable.java +++ b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/Pageable.java @@ -99,6 +99,7 @@ default int pageLimit() { /** * Return the next page + * * @return next page */ default Pageable next() { @@ -107,9 +108,19 @@ default Pageable next() { /** * Return the previous page or the first one + * * @return previous page */ default Pageable previous() { return getPageNumber() == 0 ? this : new PageRequest(getPageNumber() - 1, getPageSize(), getSort()); } + + /** + * Indicates that the pageable object is unPaged + * + * @return true if unPaged + */ + default boolean hasSize() { + return getPageSize() > 0; + } } diff --git a/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/Range.java b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/Range.java index 11cffde..b58946a 100644 --- a/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/Range.java +++ b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/Range.java @@ -1,6 +1,8 @@ package fr.ouestfrance.querydsl.postgrest.model; +import lombok.AllArgsConstructor; import lombok.Getter; +import lombok.NoArgsConstructor; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -9,6 +11,8 @@ * Range object */ @Getter +@NoArgsConstructor +@AllArgsConstructor public class Range { /** diff --git a/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/RangeResponse.java b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/RangeResponse.java new file mode 100644 index 0000000..bf7db5b --- /dev/null +++ b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/RangeResponse.java @@ -0,0 +1,46 @@ +package fr.ouestfrance.querydsl.postgrest.model; + +import java.util.List; + +/** + * Postgrest range response + * + * @param type of the page + * @param data Elements returned + * @param range Range returned (first returned, last returned, total elements) + */ +public record RangeResponse(List data, Range range) { + + /** + * Create a range response from a list of elements + * + * @param items elements + * @param type of the elements + * @return range response + */ + @SafeVarargs + public static RangeResponse of(T... items) { + return new RangeResponse<>(List.of(items), null); + } + + /** + * Retrieve total elements from range response + * + * @return total elements + */ + public long getTotalElements() { + return (range != null) ? range.getTotalElements() : data.size(); + } + + /** + * Retrieve page size from range response + * + * @return page size + */ + public int getPageSize() { + if (range != null) { + return range.getLimit() - range.getOffset() + 1; + } + return data.isEmpty() ? 1 : data.size(); + } +} diff --git a/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/impl/CountFilter.java b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/impl/CountFilter.java index e4eb66c..bec0bb9 100644 --- a/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/impl/CountFilter.java +++ b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/impl/CountFilter.java @@ -4,7 +4,6 @@ import fr.ouestfrance.querydsl.postgrest.builders.QueryFilterVisitor; import fr.ouestfrance.querydsl.postgrest.model.Filter; import lombok.AccessLevel; -import lombok.AllArgsConstructor; import lombok.Getter; import lombok.RequiredArgsConstructor; diff --git a/querydsl-postgrest/src/test/java/fr/ouestfrance/querydsl/postgrest/PostgrestRepositoryGetMockTest.java b/querydsl-postgrest/src/test/java/fr/ouestfrance/querydsl/postgrest/PostgrestRepositoryGetMockTest.java index fa8e36b..4f8b152 100644 --- a/querydsl-postgrest/src/test/java/fr/ouestfrance/querydsl/postgrest/PostgrestRepositoryGetMockTest.java +++ b/querydsl-postgrest/src/test/java/fr/ouestfrance/querydsl/postgrest/PostgrestRepositoryGetMockTest.java @@ -2,10 +2,7 @@ import fr.ouestfrance.querydsl.postgrest.app.*; import fr.ouestfrance.querydsl.postgrest.criterias.Criteria; -import fr.ouestfrance.querydsl.postgrest.model.Page; -import fr.ouestfrance.querydsl.postgrest.model.PageImpl; -import fr.ouestfrance.querydsl.postgrest.model.Pageable; -import fr.ouestfrance.querydsl.postgrest.model.Sort; +import fr.ouestfrance.querydsl.postgrest.model.*; import fr.ouestfrance.querydsl.postgrest.model.exceptions.PostgrestRequestException; import fr.ouestfrance.querydsl.postgrest.utils.QueryStringUtils; import lombok.extern.slf4j.Slf4j; @@ -40,7 +37,7 @@ void beforeEach() { @Test void shouldSearchAllPosts() { - when(webClient.search(anyString(), any(), any(), eq(Post.class))).thenReturn(Page.of(new Post(), new Post())); + when(webClient.search(anyString(), any(), any(), eq(Post.class))).thenReturn(RangeResponse.of(new Post(), new Post())); Page search = repository.search(null); assertNotNull(search); assertNotNull(search.iterator()); @@ -60,7 +57,7 @@ void shouldSearchWithPaginate() { request.setValidDate(LocalDate.of(2023, 11, 10)); ArgumentCaptor>> queryArgs = multiMapCaptor(); ArgumentCaptor>> headerArgs = multiMapCaptor(); - when(webClient.search(anyString(), queryArgs.capture(), headerArgs.capture(), eq(Post.class))).thenReturn(Page.of(new Post(), new Post())); + when(webClient.search(anyString(), queryArgs.capture(), headerArgs.capture(), eq(Post.class))).thenReturn(RangeResponse.of(new Post(), new Post())); Page search = repository.search(request, Pageable.ofSize(10, Sort.by(Sort.Order.asc("id"), Sort.Order.desc("title").nullsFirst(), Sort.Order.asc("author").nullsLast()))); assertNotNull(search); @@ -94,7 +91,7 @@ void shouldSearchWithPaginate() { @Test void shouldFindById() { ArgumentCaptor>> queryArgs = multiMapCaptor(); - when(webClient.search(anyString(), queryArgs.capture(), any(), eq(Post.class))).thenReturn(Page.of(new Post())); + when(webClient.search(anyString(), queryArgs.capture(), any(), eq(Post.class))).thenReturn(RangeResponse.of(new Post())); Page search = repository.search(Criteria.byId("1"), Pageable.ofSize(6)); // Assert query captors Map> queries = queryArgs.getValue(); @@ -108,7 +105,7 @@ void shouldFindById() { @Test void shouldFindByIds() { ArgumentCaptor>> queryArgs = multiMapCaptor(); - when(webClient.search(anyString(), queryArgs.capture(), any(), eq(Post.class))).thenReturn(Page.of(new Post())); + when(webClient.search(anyString(), queryArgs.capture(), any(), eq(Post.class))).thenReturn(RangeResponse.of(new Post())); Page search = repository.search(Criteria.byIds("1", "2", "3"), Pageable.ofSize(6)); // Assert query captors Map> queries = queryArgs.getValue(); @@ -128,7 +125,7 @@ void shouldSearchWithNextPaginate() { request.setCodes(List.of("a", "b", "c")); request.setExcludes(List.of("z")); request.setValidDate(LocalDate.of(2023, 11, 10)); - when(webClient.search(anyString(), any(), any(), eq(Post.class))).thenReturn(new PageImpl<>(List.of(new Post()), null, 2, 2)); + when(webClient.search(anyString(), any(), any(), eq(Post.class))).thenReturn(new RangeResponse<>(List.of(new Post()),new Range(0,1,2))); Page search = repository.search(request, Pageable.ofSize(1, Sort.by(Sort.Order.asc("id")))); assertNotNull(search); @@ -159,7 +156,7 @@ void shouldSearchWithPreviousPaginate() { request.setValidDate(LocalDate.of(2023, 11, 10)); ArgumentCaptor>> queryArgs = multiMapCaptor(); ArgumentCaptor>> headerArgs = multiMapCaptor(); - when(webClient.search(anyString(), queryArgs.capture(), headerArgs.capture(), eq(Post.class))).thenReturn(new PageImpl<>(List.of(new Post()), null, 2, 2)); + when(webClient.search(anyString(), queryArgs.capture(), headerArgs.capture(), eq(Post.class))).thenReturn(new RangeResponse<>(List.of(new Post()), new Range(0,2, 2))); Page search = repository.search(request, Pageable.ofSize(1, 1, Sort.by(Sort.Order.asc("id")))); assertNotNull(search); @@ -182,7 +179,7 @@ void shouldSearchWithoutOrder() { PostRequest request = new PostRequest(); ArgumentCaptor>> queryArgs = multiMapCaptor(); ArgumentCaptor>> headerArgs = multiMapCaptor(); - when(webClient.search(anyString(), queryArgs.capture(), headerArgs.capture(), eq(Post.class))).thenReturn(Page.of(new Post(), new Post())); + when(webClient.search(anyString(), queryArgs.capture(), headerArgs.capture(), eq(Post.class))).thenReturn(RangeResponse.of(new Post(), new Post())); Page search = repository.search(request, Pageable.ofSize(10)); assertNotNull(search); @@ -199,13 +196,13 @@ void shouldSearchWithoutOrder() { @Test void shouldRaiseExceptionOnMultipleOne() { - when(webClient.search(anyString(), any(), any(), eq(Post.class))).thenReturn(Page.of(new Post(), new Post())); + when(webClient.search(anyString(), any(), any(), eq(Post.class))).thenReturn(RangeResponse.of(new Post(), new Post())); assertThrows(PostgrestRequestException.class, () -> repository.findOne(null)); } @Test void shouldFindOne() { - when(webClient.search(anyString(), any(), any(), eq(Post.class))).thenReturn(Page.of(new Post())); + when(webClient.search(anyString(), any(), any(), eq(Post.class))).thenReturn(RangeResponse.of(new Post())); Optional one = repository.findOne(null); assertNotNull(one); assertTrue(one.isPresent()); @@ -214,7 +211,7 @@ void shouldFindOne() { @Test void shouldFindEmptyOne() { - when(webClient.search(anyString(), any(), any(), eq(Post.class))).thenReturn(Page.of()); + when(webClient.search(anyString(), any(), any(), eq(Post.class))).thenReturn(RangeResponse.of()); Optional one = repository.findOne(null); assertNotNull(one); assertTrue(one.isEmpty()); @@ -223,14 +220,14 @@ void shouldFindEmptyOne() { @Test void shouldGetOne() { - when(webClient.search(anyString(), any(), any(), eq(Post.class))).thenReturn(Page.of(new Post())); + when(webClient.search(anyString(), any(), any(), eq(Post.class))).thenReturn(RangeResponse.of(new Post())); Post one = repository.getOne(null); assertNotNull(one); } @Test void shouldRaiseExceptionOnEmptyGetOne() { - when(webClient.search(anyString(), any(), any(), eq(Post.class))).thenReturn(Page.of()); + when(webClient.search(anyString(), any(), any(), eq(Post.class))).thenReturn(RangeResponse.of()); assertThrows(PostgrestRequestException.class, () -> repository.getOne(null)); } @@ -239,7 +236,7 @@ void shouldSearchWithJoin() { PostRequestWithSize request = new PostRequestWithSize(); request.setSize("25"); ArgumentCaptor>> queryArgs = multiMapCaptor(); - when(webClient.search(anyString(), queryArgs.capture(), any(), eq(Post.class))).thenReturn(Page.of(new Post(), new Post())); + when(webClient.search(anyString(), queryArgs.capture(), any(), eq(Post.class))).thenReturn(RangeResponse.of(new Post(), new Post())); Page search = repository.search(request, Pageable.unPaged()); assertNotNull(search); assertEquals(2, search.size()); @@ -259,7 +256,7 @@ void shouldSearchWithOrOnMultiple() { request.setAuthor("IA"); request.setSubject("IA"); ArgumentCaptor>> queryArgs = multiMapCaptor(); - when(webClient.search(anyString(), queryArgs.capture(), any(), eq(Post.class))).thenReturn(Page.of(new Post(), new Post())); + when(webClient.search(anyString(), queryArgs.capture(), any(), eq(Post.class))).thenReturn(RangeResponse.of(new Post(), new Post())); Page search = repository.search(request, Pageable.ofSize(10)); assertNotNull(search); @@ -282,7 +279,7 @@ void testPublicationRequest() { request.setPortee("['DEPARTEMENT']"); request.setDateValide(LocalDate.of(2023, 12, 4)); ArgumentCaptor>> queryArgs = multiMapCaptor(); - when(webClient.search(anyString(), queryArgs.capture(), any(), eq(Post.class))).thenReturn(Page.of(new Post(), new Post())); + when(webClient.search(anyString(), queryArgs.capture(), any(), eq(Post.class))).thenReturn(RangeResponse.of(new Post(), new Post())); Page search = repository.search(request, Pageable.ofSize(10)); assertNotNull(search); diff --git a/querydsl-postgrest/src/test/java/fr/ouestfrance/querydsl/postgrest/model/PageTest.java b/querydsl-postgrest/src/test/java/fr/ouestfrance/querydsl/postgrest/model/PageTest.java index 82f516c..dd20f15 100644 --- a/querydsl-postgrest/src/test/java/fr/ouestfrance/querydsl/postgrest/model/PageTest.java +++ b/querydsl-postgrest/src/test/java/fr/ouestfrance/querydsl/postgrest/model/PageTest.java @@ -18,17 +18,7 @@ void shouldMap() { assertNotNull(map); assertTrue(map.stream().anyMatch(Objects::nonNull)); } - - @Test - void withRange() { - List items = List.of(1, 2, 3); - PageImpl page = new PageImpl<>(items, null, items.size(), 1); - page.withRange(Range.of("0-3/3")); - assertNotNull(page); - assertEquals(3, page.getTotalElements()); - assertEquals(1, page.getTotalPages()); - } - + @Test void shouldEmptyPage() { Page page = Page.empty();