From 028fcae6945479d7e22a44382a3b69b4dcadb2c4 Mon Sep 17 00:00:00 2001 From: Jean-Pierre Portier Date: Mon, 27 Nov 2023 15:06:00 +0100 Subject: [PATCH] fix: 'page_size' returned by SMS API cannot be used to compute total pages count to detect end of list/pagination. Use dedicated 'SMSCursorPageNavigator' to compute end of list by checking result size against empty list --- .../domains/sms/adapters/BatchesService.java | 10 +++--- .../sms/adapters/DeliveryReportsService.java | 11 +++--- .../domains/sms/adapters/GroupsService.java | 10 +++--- .../domains/sms/adapters/InboundsService.java | 11 +++--- .../converters/BatchDtoConverter.java | 10 ++---- .../DeliveryReportDtoConverter.java | 9 ++--- .../converters/GroupsDtoConverter.java | 9 ++--- .../converters/InboundsDtoConverter.java | 9 ++--- .../sms/models/SMSCursorPageNavigator.java | 35 +++++++++++++++++++ .../models/responses/BatchesListResponse.java | 19 +++++++--- .../DeliveryReportsListResponse.java | 17 ++++++--- .../models/responses/GroupsListResponse.java | 17 ++++++--- .../responses/InboundsListResponse.java | 16 ++++++--- .../sms/adapters/BatchesServiceTest.java | 13 ++++++- .../adapters/DeliveryReportsServiceTest.java | 14 +++++++- .../sms/adapters/GroupsServiceTest.java | 5 +++ .../sms/adapters/InboundsServiceTest.java | 13 ++++++- .../models/SMSCursorPageNavigatorTest.java | 25 +++++++++++++ .../sms/v1/GroupsListResponseDtoPage1.json | 2 +- .../sms/v1/GroupsListResponseDtoPage2.json | 6 ++++ .../sms/v1/InboundsListResponseDtoPage1.json | 2 +- .../sms/v1/InboundsListResponseDtoPage2.json | 7 ++++ .../sms/v1/ListBatchesResponseDtoPage1.json | 2 +- .../sms/v1/ListBatchesResponseDtoPage2.json | 7 ++++ .../ListDeliveryReportResponseDtoPage1.json | 2 +- .../ListDeliveryReportResponseDtoPage2.json | 6 ++++ 26 files changed, 212 insertions(+), 75 deletions(-) create mode 100644 client/src/main/com/sinch/sdk/domains/sms/models/SMSCursorPageNavigator.java create mode 100644 client/src/test/java/com/sinch/sdk/domains/sms/models/SMSCursorPageNavigatorTest.java create mode 100644 test-resources/src/test/resources/domains/sms/v1/GroupsListResponseDtoPage2.json create mode 100644 test-resources/src/test/resources/domains/sms/v1/InboundsListResponseDtoPage2.json create mode 100644 test-resources/src/test/resources/domains/sms/v1/ListBatchesResponseDtoPage2.json create mode 100644 test-resources/src/test/resources/domains/sms/v1/ListDeliveryReportResponseDtoPage2.json diff --git a/client/src/main/com/sinch/sdk/domains/sms/adapters/BatchesService.java b/client/src/main/com/sinch/sdk/domains/sms/adapters/BatchesService.java index 9e85d85b..dbb0a0f9 100644 --- a/client/src/main/com/sinch/sdk/domains/sms/adapters/BatchesService.java +++ b/client/src/main/com/sinch/sdk/domains/sms/adapters/BatchesService.java @@ -3,15 +3,14 @@ import com.sinch.sdk.core.exceptions.ApiException; import com.sinch.sdk.core.http.HttpClient; import com.sinch.sdk.core.http.HttpMapper; -import com.sinch.sdk.core.models.pagination.CursorPageNavigator; import com.sinch.sdk.core.models.pagination.Page; -import com.sinch.sdk.core.utils.Pair; import com.sinch.sdk.domains.sms.adapters.api.v1.BatchesApi; import com.sinch.sdk.domains.sms.adapters.converters.BatchDtoConverter; import com.sinch.sdk.domains.sms.adapters.converters.DryRunDtoConverter; import com.sinch.sdk.domains.sms.models.BaseBatch; import com.sinch.sdk.domains.sms.models.Batch; import com.sinch.sdk.domains.sms.models.DryRun; +import com.sinch.sdk.domains.sms.models.SMSCursorPageNavigator; import com.sinch.sdk.domains.sms.models.dto.v1.ApiBatchListDto; import com.sinch.sdk.domains.sms.models.requests.BatchesListRequestParameters; import com.sinch.sdk.domains.sms.models.requests.UpdateBaseBatchRequest; @@ -76,10 +75,11 @@ public BatchesListResponse list(BatchesListRequestParameters parameters) throws guardParameters.getEndDate().map(Instant::toString).orElse(null), guardParameters.getClientReference().orElse(null)); - Pair>, CursorPageNavigator> content = BatchDtoConverter.convert(response); + Collection> content = BatchDtoConverter.convert(response); + SMSCursorPageNavigator navigator = + new SMSCursorPageNavigator(response.getPage(), response.getPageSize()); - return new BatchesListResponse( - this, new Page<>(guardParameters, content.getLeft(), content.getRight())); + return new BatchesListResponse(this, new Page<>(guardParameters, content, navigator)); } public > T update(String batchId, UpdateBaseBatchRequest batch) diff --git a/client/src/main/com/sinch/sdk/domains/sms/adapters/DeliveryReportsService.java b/client/src/main/com/sinch/sdk/domains/sms/adapters/DeliveryReportsService.java index 6b6fde31..9bb6bddb 100644 --- a/client/src/main/com/sinch/sdk/domains/sms/adapters/DeliveryReportsService.java +++ b/client/src/main/com/sinch/sdk/domains/sms/adapters/DeliveryReportsService.java @@ -3,15 +3,14 @@ import com.sinch.sdk.core.exceptions.ApiException; import com.sinch.sdk.core.http.HttpClient; import com.sinch.sdk.core.http.HttpMapper; -import com.sinch.sdk.core.models.pagination.CursorPageNavigator; import com.sinch.sdk.core.models.pagination.Page; import com.sinch.sdk.core.utils.EnumDynamic; -import com.sinch.sdk.core.utils.Pair; import com.sinch.sdk.domains.sms.adapters.api.v1.DeliveryReportsApi; import com.sinch.sdk.domains.sms.adapters.converters.DeliveryReportDtoConverter; import com.sinch.sdk.domains.sms.models.DeliveryReportBatch; import com.sinch.sdk.domains.sms.models.DeliveryReportRecipient; import com.sinch.sdk.domains.sms.models.DeliveryReportType; +import com.sinch.sdk.domains.sms.models.SMSCursorPageNavigator; import com.sinch.sdk.domains.sms.models.dto.v1.DeliveryReportListDto; import com.sinch.sdk.domains.sms.models.requests.DeliveryReportBatchGetRequestParameters; import com.sinch.sdk.domains.sms.models.requests.DeliveryReportListRequestParameters; @@ -104,10 +103,10 @@ public DeliveryReportsListResponse list(DeliveryReportListRequestParameters para .orElse(null), guardParameters.getClientReference().orElse(null)); - Pair, CursorPageNavigator> content = - DeliveryReportDtoConverter.convert(response); + Collection content = DeliveryReportDtoConverter.convert(response); + SMSCursorPageNavigator navigator = + new SMSCursorPageNavigator(response.getPage(), response.getPageSize()); - return new DeliveryReportsListResponse( - this, new Page<>(guardParameters, content.getLeft(), content.getRight())); + return new DeliveryReportsListResponse(this, new Page<>(guardParameters, content, navigator)); } } diff --git a/client/src/main/com/sinch/sdk/domains/sms/adapters/GroupsService.java b/client/src/main/com/sinch/sdk/domains/sms/adapters/GroupsService.java index 70575b03..fe291761 100644 --- a/client/src/main/com/sinch/sdk/domains/sms/adapters/GroupsService.java +++ b/client/src/main/com/sinch/sdk/domains/sms/adapters/GroupsService.java @@ -3,12 +3,11 @@ import com.sinch.sdk.core.exceptions.ApiException; import com.sinch.sdk.core.http.HttpClient; import com.sinch.sdk.core.http.HttpMapper; -import com.sinch.sdk.core.models.pagination.CursorPageNavigator; import com.sinch.sdk.core.models.pagination.Page; -import com.sinch.sdk.core.utils.Pair; import com.sinch.sdk.domains.sms.adapters.api.v1.GroupsApi; import com.sinch.sdk.domains.sms.adapters.converters.GroupsDtoConverter; import com.sinch.sdk.domains.sms.models.Group; +import com.sinch.sdk.domains.sms.models.SMSCursorPageNavigator; import com.sinch.sdk.domains.sms.models.dto.v1.ApiGroupListDto; import com.sinch.sdk.domains.sms.models.dto.v1.CreateGroupResponseDto; import com.sinch.sdk.domains.sms.models.requests.GroupCreateRequestParameters; @@ -70,10 +69,11 @@ public GroupsListResponse list(GroupsListRequestParameters parameters) throws Ap guardParameters.getPage().orElse(null), guardParameters.getPageSize().orElse(null)); - Pair, CursorPageNavigator> content = GroupsDtoConverter.convert(response); + Collection content = GroupsDtoConverter.convert(response); + SMSCursorPageNavigator navigator = + new SMSCursorPageNavigator(response.getPage(), response.getPageSize()); - return new GroupsListResponse( - this, new Page<>(guardParameters, content.getLeft(), content.getRight())); + return new GroupsListResponse(this, new Page<>(guardParameters, content, navigator)); } public Group replace(String groupId, GroupReplaceRequestParameters parameters) diff --git a/client/src/main/com/sinch/sdk/domains/sms/adapters/InboundsService.java b/client/src/main/com/sinch/sdk/domains/sms/adapters/InboundsService.java index cb12b013..4f6efcda 100644 --- a/client/src/main/com/sinch/sdk/domains/sms/adapters/InboundsService.java +++ b/client/src/main/com/sinch/sdk/domains/sms/adapters/InboundsService.java @@ -3,12 +3,11 @@ import com.sinch.sdk.core.exceptions.ApiException; import com.sinch.sdk.core.http.HttpClient; import com.sinch.sdk.core.http.HttpMapper; -import com.sinch.sdk.core.models.pagination.CursorPageNavigator; import com.sinch.sdk.core.models.pagination.Page; -import com.sinch.sdk.core.utils.Pair; import com.sinch.sdk.domains.sms.adapters.api.v1.InboundsApi; import com.sinch.sdk.domains.sms.adapters.converters.InboundsDtoConverter; import com.sinch.sdk.domains.sms.models.Inbound; +import com.sinch.sdk.domains.sms.models.SMSCursorPageNavigator; import com.sinch.sdk.domains.sms.models.dto.v1.ApiInboundListDto; import com.sinch.sdk.domains.sms.models.dto.v1.InboundDto; import com.sinch.sdk.domains.sms.models.requests.InboundsListRequestParameters; @@ -52,11 +51,11 @@ public InboundsListResponse list(InboundsListRequestParameters parameters) throw guardParameters.getEndDate().map(Instant::toString).orElse(null), guardParameters.getClientReference().orElse(null)); - Pair>, CursorPageNavigator> content = - InboundsDtoConverter.convert(response); + Collection> content = InboundsDtoConverter.convert(response); + SMSCursorPageNavigator navigator = + new SMSCursorPageNavigator(response.getPage(), response.getPageSize()); - return new InboundsListResponse( - this, new Page<>(guardParameters, content.getLeft(), content.getRight())); + return new InboundsListResponse(this, new Page<>(guardParameters, content, navigator)); } public Inbound get(String inboundId) throws ApiException { diff --git a/client/src/main/com/sinch/sdk/domains/sms/adapters/converters/BatchDtoConverter.java b/client/src/main/com/sinch/sdk/domains/sms/adapters/converters/BatchDtoConverter.java index ca364cb6..d1b6e33b 100644 --- a/client/src/main/com/sinch/sdk/domains/sms/adapters/converters/BatchDtoConverter.java +++ b/client/src/main/com/sinch/sdk/domains/sms/adapters/converters/BatchDtoConverter.java @@ -2,8 +2,6 @@ import com.sinch.sdk.core.exceptions.ApiException; import com.sinch.sdk.core.models.AbstractOpenApiSchema; -import com.sinch.sdk.core.models.pagination.CursorPageNavigator; -import com.sinch.sdk.core.utils.Pair; import com.sinch.sdk.domains.sms.models.BaseBatch; import com.sinch.sdk.domains.sms.models.Batch; import com.sinch.sdk.domains.sms.models.BatchBinary; @@ -269,10 +267,8 @@ private static MediaBodyDto convert(MediaBody value) { return new MediaBodyDto().url(value.getUrl()).message(value.getMessage().orElse(null)); } - public static > Pair, CursorPageNavigator> convert( - ApiBatchListDto dto) { - CursorPageNavigator navigator = - new CursorPageNavigator(dto.getPage(), dto.getPageSize(), dto.getCount()); + public static > Collection convert(ApiBatchListDto dto) { + Collection collection = dto.getBatches(); Collection pageContent = new ArrayList<>(); if (null != collection) { @@ -281,7 +277,7 @@ public static > Pair, CursorPageNavigator> conv pageContent.add(convert); } } - return new Pair<>(pageContent, navigator); + return pageContent; } public static ApiDeliveryFeedbackDto convert(Collection recipients) { diff --git a/client/src/main/com/sinch/sdk/domains/sms/adapters/converters/DeliveryReportDtoConverter.java b/client/src/main/com/sinch/sdk/domains/sms/adapters/converters/DeliveryReportDtoConverter.java index 30f4a5d6..a5c35b29 100644 --- a/client/src/main/com/sinch/sdk/domains/sms/adapters/converters/DeliveryReportDtoConverter.java +++ b/client/src/main/com/sinch/sdk/domains/sms/adapters/converters/DeliveryReportDtoConverter.java @@ -1,7 +1,5 @@ package com.sinch.sdk.domains.sms.adapters.converters; -import com.sinch.sdk.core.models.pagination.CursorPageNavigator; -import com.sinch.sdk.core.utils.Pair; import com.sinch.sdk.domains.sms.models.DeliveryReportBatch; import com.sinch.sdk.domains.sms.models.DeliveryReportBatchMMS; import com.sinch.sdk.domains.sms.models.DeliveryReportBatchSMS; @@ -74,10 +72,7 @@ public static DeliveryReportRecipient convert(RecipientDeliveryReportDto dto) { .build(); } - public static Pair, CursorPageNavigator> convert( - DeliveryReportListDto dto) { - CursorPageNavigator navigator = - new CursorPageNavigator(dto.getPage(), dto.getPageSize(), dto.getCount()); + public static Collection convert(DeliveryReportListDto dto) { Collection collection = dto.getDeliveryReports(); Collection pageContent = new ArrayList<>(); if (null != collection) { @@ -86,7 +81,7 @@ public static Pair, CursorPageNavigator> con pageContent.add(convert); } } - return new Pair<>(pageContent, navigator); + return pageContent; } private static DeliveryReportStatusDetails convert(MessageDeliveryStatusDto dto) { diff --git a/client/src/main/com/sinch/sdk/domains/sms/adapters/converters/GroupsDtoConverter.java b/client/src/main/com/sinch/sdk/domains/sms/adapters/converters/GroupsDtoConverter.java index 1daba126..c0a7c610 100644 --- a/client/src/main/com/sinch/sdk/domains/sms/adapters/converters/GroupsDtoConverter.java +++ b/client/src/main/com/sinch/sdk/domains/sms/adapters/converters/GroupsDtoConverter.java @@ -1,8 +1,6 @@ package com.sinch.sdk.domains.sms.adapters.converters; -import com.sinch.sdk.core.models.pagination.CursorPageNavigator; import com.sinch.sdk.core.utils.DateUtil; -import com.sinch.sdk.core.utils.Pair; import com.sinch.sdk.domains.sms.models.Group; import com.sinch.sdk.domains.sms.models.GroupAutoUpdate; import com.sinch.sdk.domains.sms.models.GroupAutoUpdateKeyword; @@ -51,9 +49,8 @@ public static GroupObjectDto convert(GroupCreateRequestParameters client) { .autoUpdate(client.getAutoUpdate().map(GroupsDtoConverter::convert).orElse(null)); } - public static Pair, CursorPageNavigator> convert(ApiGroupListDto dto) { - CursorPageNavigator navigator = - new CursorPageNavigator(dto.getPage(), dto.getPageSize(), dto.getCount()); + public static Collection convert(ApiGroupListDto dto) { + Collection collection = dto.getGroups(); Collection pageContent = new ArrayList<>(); if (null != collection) { @@ -62,7 +59,7 @@ public static Pair, CursorPageNavigator> convert(ApiGroupListD pageContent.add(convert); } } - return new Pair<>(pageContent, navigator); + return pageContent; } public static ReplaceGroupRequestDto convert(GroupReplaceRequestParameters client) { diff --git a/client/src/main/com/sinch/sdk/domains/sms/adapters/converters/InboundsDtoConverter.java b/client/src/main/com/sinch/sdk/domains/sms/adapters/converters/InboundsDtoConverter.java index 24e81063..c8cd25d9 100644 --- a/client/src/main/com/sinch/sdk/domains/sms/adapters/converters/InboundsDtoConverter.java +++ b/client/src/main/com/sinch/sdk/domains/sms/adapters/converters/InboundsDtoConverter.java @@ -2,8 +2,6 @@ import com.sinch.sdk.core.exceptions.ApiException; import com.sinch.sdk.core.models.AbstractOpenApiSchema; -import com.sinch.sdk.core.models.pagination.CursorPageNavigator; -import com.sinch.sdk.core.utils.Pair; import com.sinch.sdk.domains.sms.models.Inbound; import com.sinch.sdk.domains.sms.models.InboundBinary; import com.sinch.sdk.domains.sms.models.InboundText; @@ -27,9 +25,8 @@ public static Inbound convert(AbstractOpenApiSchema dto) { } } - public static Pair>, CursorPageNavigator> convert(ApiInboundListDto dto) { - CursorPageNavigator navigator = - new CursorPageNavigator(dto.getPage(), dto.getPageSize(), dto.getCount()); + public static Collection> convert(ApiInboundListDto dto) { + Collection collection = dto.getInbounds(); Collection> pageContent = new ArrayList<>(); if (null != collection) { @@ -38,7 +35,7 @@ public static Pair>, CursorPageNavigator> convert(ApiInbou pageContent.add(convert); } } - return new Pair<>(pageContent, navigator); + return pageContent; } public static InboundBinary convert(MOBinaryDto dto) { diff --git a/client/src/main/com/sinch/sdk/domains/sms/models/SMSCursorPageNavigator.java b/client/src/main/com/sinch/sdk/domains/sms/models/SMSCursorPageNavigator.java new file mode 100644 index 00000000..29a988cd --- /dev/null +++ b/client/src/main/com/sinch/sdk/domains/sms/models/SMSCursorPageNavigator.java @@ -0,0 +1,35 @@ +package com.sinch.sdk.domains.sms.models; + +import com.sinch.sdk.core.models.pagination.PageNavigator; + +public class SMSCursorPageNavigator extends PageNavigator { + + private final Integer currentPage; + private final Integer pageSize; + + public SMSCursorPageNavigator(Integer currentPage, Integer pageSize) { + super(null); + this.currentPage = currentPage; + this.pageSize = pageSize; + } + + private Integer computeNextPageCursor() { + return null == pageSize || pageSize == 0 ? null : currentPage + 1; + } + + @Override + public Integer getToken() { + return computeNextPageCursor(); + } + + @Override + public String toString() { + return "SMSCursorPageNavigator{" + + "currentPage=" + + currentPage + + ", pageSize=" + + pageSize + + "} " + + super.toString(); + } +} diff --git a/client/src/main/com/sinch/sdk/domains/sms/models/responses/BatchesListResponse.java b/client/src/main/com/sinch/sdk/domains/sms/models/responses/BatchesListResponse.java index c7b46aa3..b8da8575 100644 --- a/client/src/main/com/sinch/sdk/domains/sms/models/responses/BatchesListResponse.java +++ b/client/src/main/com/sinch/sdk/domains/sms/models/responses/BatchesListResponse.java @@ -17,6 +17,7 @@ public class BatchesListResponse extends ListResponse> { private final Page, Integer> page; private final BatchesService service; + private BatchesListResponse nextPage; public BatchesListResponse( BatchesService service, Page, Integer> page) { @@ -25,17 +26,25 @@ public BatchesListResponse( } public boolean hasNextPage() { - return (null != page.getNextPageToken()); + + if (null == nextPage) { + BatchesListRequestParameters.Builder newParameters = + BatchesListRequestParameters.builder(page.getParameters()); + newParameters.setPage(page.getNextPageToken()); + nextPage = service.list(newParameters.build()); + } + return (null != nextPage.getContent() && !nextPage.getContent().isEmpty()); } public BatchesListResponse nextPage() { + if (!hasNextPage()) { throw new NoSuchElementException("Reached the last page of the API response"); } - BatchesListRequestParameters.Builder newParameters = - BatchesListRequestParameters.builder(page.getParameters()); - newParameters.setPage(page.getNextPageToken()); - return service.list(newParameters.build()); + + BatchesListResponse response = nextPage; + nextPage = null; + return response; } public Collection> getContent() { diff --git a/client/src/main/com/sinch/sdk/domains/sms/models/responses/DeliveryReportsListResponse.java b/client/src/main/com/sinch/sdk/domains/sms/models/responses/DeliveryReportsListResponse.java index c135cb13..a726156a 100644 --- a/client/src/main/com/sinch/sdk/domains/sms/models/responses/DeliveryReportsListResponse.java +++ b/client/src/main/com/sinch/sdk/domains/sms/models/responses/DeliveryReportsListResponse.java @@ -17,6 +17,7 @@ public class DeliveryReportsListResponse extends ListResponse page; private final DeliveryReportsService service; + private DeliveryReportsListResponse nextPage; public DeliveryReportsListResponse( DeliveryReportsService service, @@ -26,17 +27,23 @@ public DeliveryReportsListResponse( } public boolean hasNextPage() { - return (null != page.getNextPageToken()); + + if (null == nextPage) { + DeliveryReportListRequestParameters.Builder newParameters = + DeliveryReportListRequestParameters.builder(page.getParameters()); + newParameters.setPage(page.getNextPageToken()); + nextPage = service.list(newParameters.build()); + } + return (null != nextPage.getContent() && !nextPage.getContent().isEmpty()); } public DeliveryReportsListResponse nextPage() { if (!hasNextPage()) { throw new NoSuchElementException("Reached the last page of the API response"); } - DeliveryReportListRequestParameters.Builder newParameters = - DeliveryReportListRequestParameters.builder(page.getParameters()); - newParameters.setPage(page.getNextPageToken()); - return service.list(newParameters.build()); + DeliveryReportsListResponse response = nextPage; + nextPage = null; + return response; } public Collection getContent() { diff --git a/client/src/main/com/sinch/sdk/domains/sms/models/responses/GroupsListResponse.java b/client/src/main/com/sinch/sdk/domains/sms/models/responses/GroupsListResponse.java index c61e8dd4..c0121757 100644 --- a/client/src/main/com/sinch/sdk/domains/sms/models/responses/GroupsListResponse.java +++ b/client/src/main/com/sinch/sdk/domains/sms/models/responses/GroupsListResponse.java @@ -17,6 +17,7 @@ public class GroupsListResponse extends ListResponse { private final Page page; private final GroupsService service; + private GroupsListResponse nextPage; public GroupsListResponse( GroupsService service, Page page) { @@ -25,17 +26,23 @@ public GroupsListResponse( } public boolean hasNextPage() { - return (null != page.getNextPageToken()); + + if (null == nextPage) { + GroupsListRequestParameters.Builder newParameters = + GroupsListRequestParameters.builder(page.getParameters()); + newParameters.setPage(page.getNextPageToken()); + nextPage = service.list(newParameters.build()); + } + return (null != nextPage.getContent() && !nextPage.getContent().isEmpty()); } public GroupsListResponse nextPage() { if (!hasNextPage()) { throw new NoSuchElementException("Reached the last page of the API response"); } - GroupsListRequestParameters.Builder newParameters = - GroupsListRequestParameters.builder(page.getParameters()); - newParameters.setPage(page.getNextPageToken()); - return service.list(newParameters.build()); + GroupsListResponse response = nextPage; + nextPage = null; + return response; } public Collection getContent() { diff --git a/client/src/main/com/sinch/sdk/domains/sms/models/responses/InboundsListResponse.java b/client/src/main/com/sinch/sdk/domains/sms/models/responses/InboundsListResponse.java index 23cf60f0..e582fe54 100644 --- a/client/src/main/com/sinch/sdk/domains/sms/models/responses/InboundsListResponse.java +++ b/client/src/main/com/sinch/sdk/domains/sms/models/responses/InboundsListResponse.java @@ -17,6 +17,7 @@ public class InboundsListResponse extends ListResponse> { private final Page, Integer> page; private final InboundsService service; + private InboundsListResponse nextPage; public InboundsListResponse( InboundsService service, Page, Integer> page) { @@ -25,17 +26,22 @@ public InboundsListResponse( } public boolean hasNextPage() { - return (null != page.getNextPageToken()); + if (null == nextPage) { + InboundsListRequestParameters.Builder newParameters = + InboundsListRequestParameters.builder(page.getParameters()); + newParameters.setPage(page.getNextPageToken()); + nextPage = service.list(newParameters.build()); + } + return (null != nextPage.getContent() && !nextPage.getContent().isEmpty()); } public InboundsListResponse nextPage() { if (!hasNextPage()) { throw new NoSuchElementException("Reached the last page of the API response"); } - InboundsListRequestParameters.Builder newParameters = - InboundsListRequestParameters.builder(page.getParameters()); - newParameters.setPage(page.getNextPageToken()); - return service.list(newParameters.build()); + InboundsListResponse response = nextPage; + nextPage = null; + return response; } public Collection> getContent() { diff --git a/client/src/test/java/com/sinch/sdk/domains/sms/adapters/BatchesServiceTest.java b/client/src/test/java/com/sinch/sdk/domains/sms/adapters/BatchesServiceTest.java index 5caa2633..a6c6dbc0 100644 --- a/client/src/test/java/com/sinch/sdk/domains/sms/adapters/BatchesServiceTest.java +++ b/client/src/test/java/com/sinch/sdk/domains/sms/adapters/BatchesServiceTest.java @@ -260,6 +260,9 @@ public class BatchesServiceTest extends BaseTest { @GivenJsonResource("/domains/sms/v1/ListBatchesResponseDtoPage1.json") ApiBatchListDto listBatchesResponseDtoPage1; + @GivenJsonResource("/domains/sms/v1/ListBatchesResponseDtoPage2.json") + ApiBatchListDto listBatchesResponseDtoPage2; + @Mock Configuration configuration; @Mock BatchesApi api; @InjectMocks BatchesService service; @@ -376,7 +379,15 @@ void list() throws ApiException { eq(null), eq(null))) .thenReturn(listBatchesResponseDtoPage1); - + when(api.listBatches( + eq(configuration.getProjectId()), + eq(2), + eq(null), + eq(null), + eq(null), + eq(null), + eq(null))) + .thenReturn(listBatchesResponseDtoPage2); BatchesListResponse response = service.list(null); Iterator> iterator = response.iterator(); diff --git a/client/src/test/java/com/sinch/sdk/domains/sms/adapters/DeliveryReportsServiceTest.java b/client/src/test/java/com/sinch/sdk/domains/sms/adapters/DeliveryReportsServiceTest.java index 1542d0bf..abba01af 100644 --- a/client/src/test/java/com/sinch/sdk/domains/sms/adapters/DeliveryReportsServiceTest.java +++ b/client/src/test/java/com/sinch/sdk/domains/sms/adapters/DeliveryReportsServiceTest.java @@ -59,6 +59,9 @@ class DeliveryReportsServiceTest extends BaseTest { @GivenJsonResource("/domains/sms/v1/ListDeliveryReportResponseDtoPage1.json") DeliveryReportListDto listDeliveryReportResponseDtoPage1; + @GivenJsonResource("/domains/sms/v1/ListDeliveryReportResponseDtoPage2.json") + DeliveryReportListDto listDeliveryReportResponseDtoPage2; + @Test void getDeliveryReportBatchSMS() throws ApiException { @@ -168,7 +171,16 @@ void list() throws ApiException { eq(null), eq(null))) .thenReturn(listDeliveryReportResponseDtoPage1); - + when(api.getDeliveryReports( + eq(configuration.getProjectId()), + eq(2), + eq(null), + eq(null), + eq(null), + eq(null), + eq(null), + eq(null))) + .thenReturn(listDeliveryReportResponseDtoPage2); DeliveryReportsListResponse response = service.list(null); Iterator iterator = response.iterator(); diff --git a/client/src/test/java/com/sinch/sdk/domains/sms/adapters/GroupsServiceTest.java b/client/src/test/java/com/sinch/sdk/domains/sms/adapters/GroupsServiceTest.java index 81b75df9..8e223880 100644 --- a/client/src/test/java/com/sinch/sdk/domains/sms/adapters/GroupsServiceTest.java +++ b/client/src/test/java/com/sinch/sdk/domains/sms/adapters/GroupsServiceTest.java @@ -52,6 +52,9 @@ class GroupsServiceTest extends BaseTest { @GivenJsonResource("/domains/sms/v1/GroupsListResponseDtoPage1.json") ApiGroupListDto groupsListResponseDtoPage1; + @GivenJsonResource("/domains/sms/v1/GroupsListResponseDtoPage2.json") + ApiGroupListDto groupsListResponseDtoPage2; + @Test void get() throws ApiException { @@ -85,6 +88,8 @@ void list() throws ApiException { .thenReturn(groupsListResponseDtoPage0); when(api.listGroups(eq(configuration.getProjectId()), eq(1), eq(null))) .thenReturn(groupsListResponseDtoPage1); + when(api.listGroups(eq(configuration.getProjectId()), eq(2), eq(null))) + .thenReturn(groupsListResponseDtoPage2); GroupsListResponse response = service.list(null); diff --git a/client/src/test/java/com/sinch/sdk/domains/sms/adapters/InboundsServiceTest.java b/client/src/test/java/com/sinch/sdk/domains/sms/adapters/InboundsServiceTest.java index 465bedbe..1e07cc83 100644 --- a/client/src/test/java/com/sinch/sdk/domains/sms/adapters/InboundsServiceTest.java +++ b/client/src/test/java/com/sinch/sdk/domains/sms/adapters/InboundsServiceTest.java @@ -43,6 +43,9 @@ class InboundsServiceTest extends BaseTest { @GivenJsonResource("/domains/sms/v1/InboundsListResponseDtoPage1.json") ApiInboundListDto inboundsLisResponseDtoPage1; + @GivenJsonResource("/domains/sms/v1/InboundsListResponseDtoPage2.json") + ApiInboundListDto inboundsLisResponseDtoPage2; + @Test void getBinary() throws ApiException { @@ -92,7 +95,15 @@ void list() throws ApiException { eq(null), eq(null))) .thenReturn(inboundsLisResponseDtoPage1); - + when(api.listInboundMessages( + eq(configuration.getProjectId()), + eq(2), + eq(null), + eq(null), + eq(null), + eq(null), + eq(null))) + .thenReturn(inboundsLisResponseDtoPage2); InboundsListResponse response = service.list(null); Iterator> iterator = response.iterator(); diff --git a/client/src/test/java/com/sinch/sdk/domains/sms/models/SMSCursorPageNavigatorTest.java b/client/src/test/java/com/sinch/sdk/domains/sms/models/SMSCursorPageNavigatorTest.java new file mode 100644 index 00000000..b734ee12 --- /dev/null +++ b/client/src/test/java/com/sinch/sdk/domains/sms/models/SMSCursorPageNavigatorTest.java @@ -0,0 +1,25 @@ +package com.sinch.sdk.domains.sms.models; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +class SMSCursorPageNavigatorTest { + + @Test + void getTokenNullPageSize() { + SMSCursorPageNavigator cursorNavigator = new SMSCursorPageNavigator(45, null); + Assertions.assertThat(cursorNavigator.getToken()).isEqualTo(null); + } + + @Test + void getTokenZeroPageSize() { + SMSCursorPageNavigator cursorNavigator = new SMSCursorPageNavigator(45, 0); + Assertions.assertThat(cursorNavigator.getToken()).isEqualTo(null); + } + + @Test + void getToken() { + SMSCursorPageNavigator cursorNavigator = new SMSCursorPageNavigator(0, 15); + Assertions.assertThat(cursorNavigator.getToken()).isEqualTo(1); + } +} diff --git a/test-resources/src/test/resources/domains/sms/v1/GroupsListResponseDtoPage1.json b/test-resources/src/test/resources/domains/sms/v1/GroupsListResponseDtoPage1.json index 75247563..73a385ad 100644 --- a/test-resources/src/test/resources/domains/sms/v1/GroupsListResponseDtoPage1.json +++ b/test-resources/src/test/resources/domains/sms/v1/GroupsListResponseDtoPage1.json @@ -1,7 +1,7 @@ { "count": 3, "page": 1, - "page_size": 2, + "page_size": 1, "groups": [ { "id": "01FC66621XXXXX119Z8PMV1QPU", diff --git a/test-resources/src/test/resources/domains/sms/v1/GroupsListResponseDtoPage2.json b/test-resources/src/test/resources/domains/sms/v1/GroupsListResponseDtoPage2.json new file mode 100644 index 00000000..59576bf5 --- /dev/null +++ b/test-resources/src/test/resources/domains/sms/v1/GroupsListResponseDtoPage2.json @@ -0,0 +1,6 @@ +{ + "count": 3, + "page": 2, + "page_size": 0, + "groups": [ ] +} \ No newline at end of file diff --git a/test-resources/src/test/resources/domains/sms/v1/InboundsListResponseDtoPage1.json b/test-resources/src/test/resources/domains/sms/v1/InboundsListResponseDtoPage1.json index 6dd9da1d..12636aee 100644 --- a/test-resources/src/test/resources/domains/sms/v1/InboundsListResponseDtoPage1.json +++ b/test-resources/src/test/resources/domains/sms/v1/InboundsListResponseDtoPage1.json @@ -15,5 +15,5 @@ "udh": "foo udh" } ], - "page_size": 2 + "page_size": 1 } diff --git a/test-resources/src/test/resources/domains/sms/v1/InboundsListResponseDtoPage2.json b/test-resources/src/test/resources/domains/sms/v1/InboundsListResponseDtoPage2.json new file mode 100644 index 00000000..85d9f26b --- /dev/null +++ b/test-resources/src/test/resources/domains/sms/v1/InboundsListResponseDtoPage2.json @@ -0,0 +1,7 @@ +{ + "count": 3, + "page": 2, + "inbounds": [ + ], + "page_size": 0 +} diff --git a/test-resources/src/test/resources/domains/sms/v1/ListBatchesResponseDtoPage1.json b/test-resources/src/test/resources/domains/sms/v1/ListBatchesResponseDtoPage1.json index 17259ab3..8fd7b40c 100644 --- a/test-resources/src/test/resources/domains/sms/v1/ListBatchesResponseDtoPage1.json +++ b/test-resources/src/test/resources/domains/sms/v1/ListBatchesResponseDtoPage1.json @@ -19,6 +19,6 @@ "feedback_enabled": false } ], - "page_size": 2 + "page_size": 1 } diff --git a/test-resources/src/test/resources/domains/sms/v1/ListBatchesResponseDtoPage2.json b/test-resources/src/test/resources/domains/sms/v1/ListBatchesResponseDtoPage2.json new file mode 100644 index 00000000..ed38f42a --- /dev/null +++ b/test-resources/src/test/resources/domains/sms/v1/ListBatchesResponseDtoPage2.json @@ -0,0 +1,7 @@ +{ + "count": 3, + "page": 2, + "batches": [], + "page_size": 0 +} + diff --git a/test-resources/src/test/resources/domains/sms/v1/ListDeliveryReportResponseDtoPage1.json b/test-resources/src/test/resources/domains/sms/v1/ListDeliveryReportResponseDtoPage1.json index 162d7ff3..835988ac 100644 --- a/test-resources/src/test/resources/domains/sms/v1/ListDeliveryReportResponseDtoPage1.json +++ b/test-resources/src/test/resources/domains/sms/v1/ListDeliveryReportResponseDtoPage1.json @@ -17,5 +17,5 @@ "operator_status_at": "2022-08-30T08:16:08.150Z" } ], - "page_size": 2 + "page_size": 1 } diff --git a/test-resources/src/test/resources/domains/sms/v1/ListDeliveryReportResponseDtoPage2.json b/test-resources/src/test/resources/domains/sms/v1/ListDeliveryReportResponseDtoPage2.json new file mode 100644 index 00000000..cca5b1ee --- /dev/null +++ b/test-resources/src/test/resources/domains/sms/v1/ListDeliveryReportResponseDtoPage2.json @@ -0,0 +1,6 @@ +{ + "count": 3, + "page": 2, + "delivery_reports": [ ], + "page_size": 0 +}