Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
re facebook#309 Fixed APINodeList requesting extra empty page at resu…
Browse files Browse the repository at this point in the history
…lts end.
  • Loading branch information
Paweł Dąbrowski authored and Adam-Jamka committed Mar 31, 2023
1 parent 2ed4e7a commit a8b3d17
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
10 changes: 1 addition & 9 deletions src/main/java/com/facebook/ads/sdk/APINodeList.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.net.URL;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Objects;
import java.util.Spliterator;
Expand Down Expand Up @@ -86,12 +83,7 @@ public APINodeList<T> nextPage(int limit) throws APIException {
}
return (APINodeList<T>) request.execute();
}
if (after == null) return null;
this.request.setOverrideUrl(null);
Map<String, Object> extraParams = new HashMap<String, Object>();
if (limit > 0) extraParams.put("limit", limit);
extraParams.put("after", after);
return (APINodeList<T>) request.execute(extraParams);
return null;
}

public void setCursors(String before, String after) {
Expand Down
44 changes: 44 additions & 0 deletions src/test/kotlin/com/facebook/ads/sdk/ApiNodeListTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.facebook.ads.sdk

import io.mockk.Runs
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.verify
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull

class ApiNodeListTest {

@Test
fun testNextPageWhenNextPageAvailable() {
val request = mockk<APIRequest<Campaign>>()
val nextPage = mockk<APINodeList<Campaign>>()
every { request.setOverrideUrl("https://graph.facebook.api/NEXT") } just Runs
every { request.execute() } returns(nextPage)
val list = APINodeList<Campaign>(request, "raw value", "header")
list.setPaging("https://graph.facebook.api/PREVIOUS", "https://graph.facebook.api/NEXT")
list.setCursors("BEFORE_CURSOR", "AFTER_CURSOR")

val result = list.nextPage()

assertEquals(nextPage, result)
verify {
request.setOverrideUrl("https://graph.facebook.api/NEXT")
}
}

@Test
fun testNextPageWhenAlreadyOnLastPage() {
val request = mockk<APIRequest<Campaign>>()
val list = APINodeList<Campaign>(request, "raw value", "header")
list.setPaging("https://graph.facebook.api/PREVIOUS", null)
list.setCursors("BEFORE_CURSOR", "AFTER_CURSOR")

val result = list.nextPage()

assertNull(result)
}

}

0 comments on commit a8b3d17

Please sign in to comment.