From 3a2754b452070813f0421c7cd787a02ada85393c Mon Sep 17 00:00:00 2001 From: blackbaud-alanacable Date: Wed, 1 May 2019 21:41:16 -0500 Subject: [PATCH] Issue 363 (#365) Preserve Sort when creating DocumentDbPageRequest --- .../cosmosdb/core/DocumentDbTemplate.java | 3 +- .../cosmosdb/core/DocumentDbTemplateIT.java | 33 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/microsoft/azure/spring/data/cosmosdb/core/DocumentDbTemplate.java b/src/main/java/com/microsoft/azure/spring/data/cosmosdb/core/DocumentDbTemplate.java index 3bb63df..aa88079 100644 --- a/src/main/java/com/microsoft/azure/spring/data/cosmosdb/core/DocumentDbTemplate.java +++ b/src/main/java/com/microsoft/azure/spring/data/cosmosdb/core/DocumentDbTemplate.java @@ -509,7 +509,8 @@ public class DocumentDbTemplate implements DocumentDbOperations, ApplicationCont final DocumentDbPageRequest pageRequest = DocumentDbPageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), - response.getResponseContinuation()); + response.getResponseContinuation(), + query.getSort()); return new PageImpl<>(result, pageRequest, count(query, domainClass, collectionName)); } diff --git a/src/test/java/com/microsoft/azure/spring/data/cosmosdb/core/DocumentDbTemplateIT.java b/src/test/java/com/microsoft/azure/spring/data/cosmosdb/core/DocumentDbTemplateIT.java index a549e26..9c049ac 100644 --- a/src/test/java/com/microsoft/azure/spring/data/cosmosdb/core/DocumentDbTemplateIT.java +++ b/src/test/java/com/microsoft/azure/spring/data/cosmosdb/core/DocumentDbTemplateIT.java @@ -244,4 +244,37 @@ public class DocumentDbTemplateIT { assertThat(result.get(2).getFirstName()).isEqualTo(FIRST_NAME); } + + @Test + public void testFindAllWithTwoPagesAndVerifySortOrder() { + final Person testPerson4 = new Person("id_4", "barney", NEW_LAST_NAME, HOBBIES, ADDRESSES); + final Person testPerson5 = new Person("id_5", "fred", NEW_LAST_NAME, HOBBIES, ADDRESSES); + + dbTemplate.insert(TEST_PERSON_2, null); + dbTemplate.insert(TEST_PERSON_3, null); + dbTemplate.insert(testPerson4, null); + dbTemplate.insert(testPerson5, null); + + final Sort sort = new Sort(Sort.Direction.ASC, "firstName"); + final PageRequest pageRequest = DocumentDbPageRequest.of(0, PAGE_SIZE_3, null, sort); + + final Page firstPage = dbTemplate.findAll(pageRequest, Person.class, collectionName); + + assertThat(firstPage.getContent().size()).isEqualTo(3); + validateNonLastPage(firstPage, PAGE_SIZE_3); + + final List firstPageResults = firstPage.getContent(); + assertThat(firstPageResults.get(0).getFirstName()).isEqualTo(testPerson4.getFirstName()); + assertThat(firstPageResults.get(1).getFirstName()).isEqualTo(FIRST_NAME); + assertThat(firstPageResults.get(2).getFirstName()).isEqualTo(testPerson5.getFirstName()); + + final Page secondPage = dbTemplate.findAll(firstPage.getPageable(), Person.class, collectionName); + + assertThat(secondPage.getContent().size()).isEqualTo(2); + validateLastPage(secondPage, PAGE_SIZE_3); + + final List secondPageResults = secondPage.getContent(); + assertThat(secondPageResults.get(0).getFirstName()).isEqualTo(NEW_FIRST_NAME); + assertThat(secondPageResults.get(1).getFirstName()).isEqualTo(NEW_FIRST_NAME); + } }