Skip to content

Commit

Permalink
Fix hasMorePages not being set properly when fetching offers
Browse files Browse the repository at this point in the history
  • Loading branch information
arturdm committed May 26, 2020
1 parent 7c0c792 commit b5ecdbf
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ 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") {
val recordedRequest = server.takeRequest()
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")
Expand All @@ -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") {
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/integrationTest/resources/raw/offers_get.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,6 @@
"offersEnabledTimestamp": null,
"termsAndConditionsAccepted": true,
"isReimbursementAccountValid": false,
"totalCount": 4
"totalCount": 14
}
}
60 changes: 14 additions & 46 deletions sdk/src/main/java/com/meniga/sdk/models/offers/MenigaOffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.util.Collections;
import java.util.List;

import static java.util.Collections.singletonList;

/**
* Copyright 2017 Meniga Iceland Inc.
*/
Expand Down Expand Up @@ -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.<OfferFilterState>emptyList();
result.offerIds = offerIds != null ? offerIds : Collections.<Long>emptyList();
result.expiredWithRedemptionOnly = expiredWithRedemptionOnly;
if (result.size() >= result.getTotalCount()) {
result.hasMorePages = false;
}
result.hasMorePages = skip + take < result.getTotalCount();
}
}
});
Expand Down Expand Up @@ -382,51 +382,31 @@ public static Result<MenigaOffer> 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<MenigaOfferPage> fetch(final List<OfferFilterState> states) {
final int take = 100;
final int skip = 0;
Result<MenigaOfferPage> task = MenigaOffer.apiOperator.getOffers(skip, take, states, Collections.singletonList(0L), false);
return MenigaSDK.getMenigaSettings().getTaskAdapter().intercept(task, new Interceptor<MenigaOfferPage>() {
@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<MenigaOfferPage> fetchId(final List<Long> offerIds) {
final int take = 100;
final int skip = 0;
Result<MenigaOfferPage> task = MenigaOffer.apiOperator.getOffers(skip, take, null, offerIds, false);
return MenigaSDK.getMenigaSettings().getTaskAdapter().intercept(task, new Interceptor<MenigaOfferPage>() {
@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);
}

/**
Expand All @@ -437,19 +417,7 @@ public void onFinished(MenigaOfferPage result, boolean failed) {
* @return a page of offers
*/
public static Result<MenigaOfferPage> fetch(final int skip, final int take) {
Result<MenigaOfferPage> task = MenigaOffer.apiOperator.getOffers(skip, take, Collections.singletonList(OfferFilterState.ALL), null, false);
return MenigaSDK.getMenigaSettings().getTaskAdapter().intercept(task, new Interceptor<MenigaOfferPage>() {
@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);
}

/**
Expand Down

0 comments on commit b5ecdbf

Please sign in to comment.