diff --git a/src/main/java/uk/gov/pay/ledger/transaction/service/TransactionService.java b/src/main/java/uk/gov/pay/ledger/transaction/service/TransactionService.java index 2dcc7eb7c..75caa20e7 100644 --- a/src/main/java/uk/gov/pay/ledger/transaction/service/TransactionService.java +++ b/src/main/java/uk/gov/pay/ledger/transaction/service/TransactionService.java @@ -125,7 +125,10 @@ public List searchTransactionAfter(TransactionSearchParams se private TransactionSearchResponse buildTransactionSearchResponse(TransactionSearchParams searchParams, UriInfo uriInfo, List transactionList, Long totalCount) { Long total = Optional.ofNullable(totalCount).orElse(0L); PaginationBuilder paginationBuilder = new PaginationBuilder(searchParams, uriInfo); - paginationBuilder = paginationBuilder.withTotalCount(total).buildResponse(); + paginationBuilder = paginationBuilder + .withTotalCount(total) + .withCount((long) transactionList.size()) + .buildResponse(); List transactionViewList = mapToTransactionViewList(transactionList, searchParams.getStatusVersion()); diff --git a/src/main/java/uk/gov/pay/ledger/util/pagination/PaginationBuilder.java b/src/main/java/uk/gov/pay/ledger/util/pagination/PaginationBuilder.java index d2e1a29bb..9835f66b5 100644 --- a/src/main/java/uk/gov/pay/ledger/util/pagination/PaginationBuilder.java +++ b/src/main/java/uk/gov/pay/ledger/util/pagination/PaginationBuilder.java @@ -7,6 +7,7 @@ import javax.ws.rs.core.UriInfo; import java.net.URI; +import java.util.Objects; import static com.fasterxml.jackson.annotation.JsonInclude.Include; @@ -24,6 +25,8 @@ public class PaginationBuilder { @JsonIgnore private Long totalCount; @JsonIgnore + private Long count; + @JsonIgnore private Long selfPageNum; @JsonProperty(SELF_LINK) private PaginationLink selfLink; @@ -47,6 +50,11 @@ public PaginationBuilder withTotalCount(Long total) { return this; } + public PaginationBuilder withCount(Long count) { + this.count = count; + return this; + } + public PaginationBuilder buildResponse() { if (searchParams.limitTotal()) { @@ -84,7 +92,9 @@ private void buildLinksForLimitTotal() { selfLink = PaginationLink.ofValue(uriWithParams(searchParams.buildQueryParamString(searchParams.getPageNumber()))); firstLink = PaginationLink.ofValue(uriWithParams(searchParams.buildQueryParamString(1L))); - nextLink = PaginationLink.ofValue(uriWithParams(searchParams.buildQueryParamString(selfPageNum + 1))); + if (Objects.equals(count, searchParams.getDisplaySize())) { + nextLink = PaginationLink.ofValue(uriWithParams(searchParams.buildQueryParamString(selfPageNum + 1))); + } if (selfPageNum == 1L) { prevLink = null; diff --git a/src/test/java/uk/gov/pay/ledger/util/pagination/PaginationBuilderTest.java b/src/test/java/uk/gov/pay/ledger/util/pagination/PaginationBuilderTest.java index 940835ba0..c9af6a8bb 100644 --- a/src/test/java/uk/gov/pay/ledger/util/pagination/PaginationBuilderTest.java +++ b/src/test/java/uk/gov/pay/ledger/util/pagination/PaginationBuilderTest.java @@ -102,7 +102,8 @@ public void shouldBuildLinksCorrectly_whenLimitTotalParamIsSetAndFirstPageIsAcce transactionSearchParams.setDisplaySize(10L); transactionSearchParams.setLimitTotal(true); PaginationBuilder builder = new PaginationBuilder(transactionSearchParams, mockedUriInfo) - .withTotalCount(120L); + .withTotalCount(120L) + .withCount(10L); builder = builder.buildResponse(); assertThat(builder.getFirstLink().getHref().contains("page=1&display_size=10"), is(true)); assertThat(builder.getLastLink(), is(nullValue())); @@ -117,7 +118,8 @@ public void shouldShowAllLinksCorrectly_whenLimitTotalParamIsSetAndMultiplePages transactionSearchParams.setDisplaySize(10L); transactionSearchParams.setLimitTotal(true); PaginationBuilder builder = new PaginationBuilder(transactionSearchParams, mockedUriInfo) - .withTotalCount(120L); + .withTotalCount(120L) + .withCount(10L); builder = builder.buildResponse(); assertThat(builder.getFirstLink().getHref().contains("page=1&display_size=10"), is(true)); assertThat(builder.getLastLink(), is(nullValue())); @@ -125,4 +127,15 @@ public void shouldShowAllLinksCorrectly_whenLimitTotalParamIsSetAndMultiplePages assertThat(builder.getNextLink().getHref().contains("page=4&display_size=10"), is(true)); assertThat(builder.getSelfLink().getHref().contains("page=3&display_size=10"), is(true)); } + + @Test + public void shouldNotGenerateNextLink_whenLimitTotalIsSetAndCountIsLessThanPageSize() { + transactionSearchParams.setDisplaySize(10L); + transactionSearchParams.setLimitTotal(true); + PaginationBuilder builder = new PaginationBuilder(transactionSearchParams, mockedUriInfo) + .withTotalCount(120L) + .withCount(9L); + builder = builder.buildResponse(); + assertThat(builder.getNextLink(), is(nullValue())); + } } \ No newline at end of file