From b5ecdbf2d73eff291b19aaeba26b60be5d68e564 Mon Sep 17 00:00:00 2001 From: Artur Stepniewski Date: Tue, 26 May 2020 12:32:31 +0200 Subject: [PATCH] Fix hasMorePages not being set properly when fetching offers --- .../sdk/models/offers/MenigaOffersApiTest.kt | 5 +- .../resources/raw/offers_get.json | 2 +- .../meniga/sdk/models/offers/MenigaOffer.java | 60 +++++-------------- 3 files changed, 18 insertions(+), 49 deletions(-) diff --git a/sdk/src/integrationTest/java/com/meniga/sdk/models/offers/MenigaOffersApiTest.kt b/sdk/src/integrationTest/java/com/meniga/sdk/models/offers/MenigaOffersApiTest.kt index ef8aa4a2..4dce3137 100644 --- a/sdk/src/integrationTest/java/com/meniga/sdk/models/offers/MenigaOffersApiTest.kt +++ b/sdk/src/integrationTest/java/com/meniga/sdk/models/offers/MenigaOffersApiTest.kt @@ -37,7 +37,7 @@ object MenigaOffersApiTest : Spek({ on("fetching offers") { server.enqueue(mockResponse("offers_get.json")) - val task = MenigaOffer.fetch(0, 10).task + val task = MenigaOffer.fetch(10, 10).task task.waitForCompletion() it("should make a proper request") { @@ -45,7 +45,7 @@ object MenigaOffersApiTest : Spek({ assertThat(recordedRequest.method).isEqualTo("GET") assertThat(URI(recordedRequest.path)) .hasPath("/v1/offers") - .hasParameter("skip", "0") + .hasParameter("skip", "10") .hasParameter("take", "10") .hasParameter("filter.states", "all") .hasNoParameter("filter.offerIds") @@ -56,6 +56,7 @@ object MenigaOffersApiTest : Spek({ val offers = task.result assertThat(offers).isNotNull assertThat(offers.size).isEqualTo(4) + assertThat(offers.hasMorePages).isFalse() } it("should validate against the spec") { diff --git a/sdk/src/integrationTest/resources/raw/offers_get.json b/sdk/src/integrationTest/resources/raw/offers_get.json index 80d09082..c064adf4 100644 --- a/sdk/src/integrationTest/resources/raw/offers_get.json +++ b/sdk/src/integrationTest/resources/raw/offers_get.json @@ -198,6 +198,6 @@ "offersEnabledTimestamp": null, "termsAndConditionsAccepted": true, "isReimbursementAccountValid": false, - "totalCount": 4 + "totalCount": 14 } } diff --git a/sdk/src/main/java/com/meniga/sdk/models/offers/MenigaOffer.java b/sdk/src/main/java/com/meniga/sdk/models/offers/MenigaOffer.java index 425296c5..40574067 100644 --- a/sdk/src/main/java/com/meniga/sdk/models/offers/MenigaOffer.java +++ b/sdk/src/main/java/com/meniga/sdk/models/offers/MenigaOffer.java @@ -20,6 +20,8 @@ import java.util.Collections; import java.util.List; +import static java.util.Collections.singletonList; + /** * Copyright 2017 Meniga Iceland Inc. */ @@ -348,12 +350,10 @@ public void onFinished(MenigaOfferPage result, boolean failed) { if (!failed && result != null) { result.skip = skip; result.take = take; - result.states = states; - result.offerIds = offerIds; + result.states = states != null ? states : Collections.emptyList(); + result.offerIds = offerIds != null ? offerIds : Collections.emptyList(); result.expiredWithRedemptionOnly = expiredWithRedemptionOnly; - if (result.size() >= result.getTotalCount()) { - result.hasMorePages = false; - } + result.hasMorePages = skip + take < result.getTotalCount(); } } }); @@ -382,51 +382,31 @@ public static Result fetch(String validationToken) { /** * Returns offers and relevant meta data. * + * @deprecated Use {@link #fetch(int, int, List, List, boolean)}. Will be removed in 1.2. + * * @param states state of offers that should be fetched. * @return a page of offers */ + @Deprecated public static Result fetch(final List states) { final int take = 100; final int skip = 0; - Result task = MenigaOffer.apiOperator.getOffers(skip, take, states, Collections.singletonList(0L), false); - return MenigaSDK.getMenigaSettings().getTaskAdapter().intercept(task, new Interceptor() { - @Override - public void onFinished(MenigaOfferPage result, boolean failed) { - if (!failed && result != null) { - result.skip = skip; - result.take = take; - result.states = states; - if (result.size() >= result.getTotalCount()) { - result.hasMorePages = false; - } - } - } - }); + return fetch(skip, take, states, null, false); } /** * Returns offers and relevant meta data by specific ids * + * @deprecated Use {@link #fetch(int, int, List, List, boolean)}. Will be removed in 1.2. + * * @param offerIds specific offer ids to fetch. * @return a page of offers */ + @Deprecated public static Result fetchId(final List offerIds) { final int take = 100; final int skip = 0; - Result task = MenigaOffer.apiOperator.getOffers(skip, take, null, offerIds, false); - return MenigaSDK.getMenigaSettings().getTaskAdapter().intercept(task, new Interceptor() { - @Override - public void onFinished(MenigaOfferPage result, boolean failed) { - if (!failed && result != null) { - result.skip = skip; - result.take = take; - result.offerIds = offerIds; - if (result.size() >= result.getTotalCount()) { - result.hasMorePages = false; - } - } - } - }); + return fetch(skip, take, null, offerIds, false); } /** @@ -437,19 +417,7 @@ public void onFinished(MenigaOfferPage result, boolean failed) { * @return a page of offers */ public static Result fetch(final int skip, final int take) { - Result task = MenigaOffer.apiOperator.getOffers(skip, take, Collections.singletonList(OfferFilterState.ALL), null, false); - return MenigaSDK.getMenigaSettings().getTaskAdapter().intercept(task, new Interceptor() { - @Override - public void onFinished(MenigaOfferPage result, boolean failed) { - if (!failed && result != null) { - result.skip = skip; - result.take = take; - if (result.size() >= result.getTotalCount()) { - result.hasMorePages = false; - } - } - } - }); + return fetch(skip, take, singletonList(OfferFilterState.ALL), null, false); } /**