Skip to content

Commit 2710816

Browse files
committed
update unit tests
1 parent ffd3db2 commit 2710816

4 files changed

Lines changed: 66 additions & 69 deletions

File tree

src/main/java/io/apimatic/core/types/pagination/PageWrapper.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.apimatic.core.types.pagination;
22

3-
import java.io.IOException;
43
import java.util.List;
54

65
import io.apimatic.coreinterfaces.http.HttpHeaders;
@@ -98,8 +97,6 @@ public HttpHeaders getHeaders() {
9897
/**
9998
* Content of the page.
10099
* @return Content
101-
* @throws E
102-
* @throws IOException
103100
*/
104101
public P getResult() {
105102
return page;
@@ -108,8 +105,6 @@ public P getResult() {
108105
/**
109106
* Content of the page.
110107
* @return List of items on this page.
111-
* @throws E
112-
* @throws IOException
113108
*/
114109
public List<I> getItems() {
115110
return items;

src/main/java/io/apimatic/core/types/pagination/PaginatedData.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.ArrayList;
55
import java.util.Iterator;
66
import java.util.List;
7+
import java.util.NoSuchElementException;
78
import java.util.concurrent.CompletableFuture;
89
import java.util.function.Function;
910

@@ -93,6 +94,10 @@ public boolean hasNext() {
9394

9495
@Override
9596
public T next() {
97+
if (paginatedData.itemIndex == paginatedData.items.size()) {
98+
throw new NoSuchElementException("No more items available.");
99+
}
100+
96101
return itemSupplier.apply(paginatedData.items.get(paginatedData.itemIndex++));
97102
}
98103
};
@@ -113,7 +118,13 @@ public boolean hasNext() {
113118

114119
@Override
115120
public T next() {
116-
return pageSupplier.apply(paginatedData.page);
121+
if (paginatedData.page == null) {
122+
throw new NoSuchElementException("No more pages available.");
123+
}
124+
125+
T page = pageSupplier.apply(paginatedData.page);
126+
paginatedData.page = null;
127+
return page;
117128
}
118129
};
119130
}

src/test/java/apimatic/core/EndToEndTest.java

Lines changed: 47 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package apimatic.core;
22

33
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
45
import static org.junit.Assert.assertThrows;
56
import static org.mockito.ArgumentMatchers.any;
67
import static org.mockito.ArgumentMatchers.anyList;
@@ -24,8 +25,6 @@
2425
import org.mockito.junit.MockitoJUnit;
2526
import org.mockito.junit.MockitoRule;
2627

27-
import com.fasterxml.jackson.core.type.TypeReference;
28-
2928
import apimatic.core.exceptions.GlobalTestException;
3029
import apimatic.core.mocks.MockCoreConfig;
3130
import apimatic.core.type.pagination.RecordPage;
@@ -37,6 +36,7 @@
3736
import io.apimatic.core.types.pagination.LinkPagination;
3837
import io.apimatic.core.types.pagination.OffsetPagination;
3938
import io.apimatic.core.types.pagination.PagePagination;
39+
import io.apimatic.core.types.pagination.PageWrapper;
4040
import io.apimatic.core.types.pagination.PaginatedData;
4141
import io.apimatic.core.types.pagination.PaginationStrategy;
4242
import io.apimatic.core.utilities.CoreHelper;
@@ -171,72 +171,48 @@ public void testEndToEndSyncCall() throws IOException, CoreApiException {
171171

172172
@Test
173173
public void testLinkPaginationData() throws IOException, CoreApiException {
174-
verifyData(getPaginatedApiCall(new LinkPagination("$response.body#/next_link")).execute());
174+
verifyData(getPaginatedApiCall(new LinkPagination("$response.body#/next_link")));
175175
}
176176

177177
@Test
178178
public void testCursorPaginationData() throws IOException, CoreApiException {
179179
verifyData(getPaginatedApiCall(new CursorPagination("$response.body#/page_info",
180-
"$request.path#/cursor")).execute());
180+
"$request.path#/cursor")));
181181
}
182182

183183
@Test
184184
public void testOffsetPaginationData() throws IOException, CoreApiException {
185-
verifyData(getPaginatedApiCall(new OffsetPagination("$request.headers#/offset")).execute());
185+
verifyData(getPaginatedApiCall(new OffsetPagination("$request.headers#/offset")));
186186
}
187187

188188
@Test
189189
public void testPagePaginationData() throws IOException, CoreApiException {
190-
verifyData(getPaginatedApiCall(new PagePagination("$request.query#/page")).execute());
191-
verifyOnlyPages(getPaginatedApiCall(new PagePagination("$request.query#/page")).execute());
190+
verifyData(getPaginatedApiCall(new PagePagination("$request.query#/page")));
192191
}
193192

194-
private void verifyOnlyPages(PaginatedData<String, RecordPage> paginatedData) {
195-
RecordPage expectedPage1 = new RecordPage();
196-
expectedPage1.setData(Arrays.asList("apple", "mango", "orange"));
197-
expectedPage1.setPageInfo("fruits");
198-
expectedPage1.setNextLink("https://localhost:3000/path?page=2");
199-
200-
RecordPage expectedPage2 = new RecordPage();
201-
expectedPage2.setData(Arrays.asList("potato", "carrot", "tomato"));
202-
expectedPage2.setPageInfo("vegitables");
203-
expectedPage2.setNextLink(null);
204-
205-
int pageNum = 0;
206-
List<RecordPage> expectedPages = Arrays.asList(expectedPage1, expectedPage2);
207-
for (RecordPage p : paginatedData.pages()) {
208-
assertEquals(expectedPages.get(pageNum).getData(), p.getData());
209-
assertEquals(expectedPages.get(pageNum).getPageInfo(), p.getPageInfo());
210-
assertEquals(expectedPages.get(pageNum).getNextLink(), p.getNextLink());
211-
pageNum++;
212-
}
213-
214-
Iterator<RecordPage> iterator = paginatedData.pages().iterator();
215-
Exception exception = assertThrows(NoSuchElementException.class, () -> {
216-
while (iterator.hasNext()) {
217-
iterator.next();
218-
}
219-
iterator.next();
220-
});
221-
assertEquals("No more data available.", exception.getMessage());
222-
}
223-
224-
private void verifyData(PaginatedData<String, RecordPage> paginatedData) {
193+
private void verifyData(PaginatedData<String, PageWrapper<String, RecordPage>, RecordPage, CoreApiException> paginatedData) {
225194
int index = 0;
226195
List<String> expectedData = Arrays.asList(
227196
"apple", "mango", "orange", "potato", "carrot", "tomato");
228197

229-
Iterator<String> iterator = paginatedData.iterator();
230-
while (iterator.hasNext()) {
231-
String d = iterator.next();
198+
Iterator<String> itemIterator = paginatedData.items(cs -> {
199+
try {
200+
return cs.get();
201+
} catch (CoreApiException | IOException e) {
202+
return null;
203+
}
204+
});
205+
while (itemIterator.hasNext()) {
206+
String d = itemIterator.next();
207+
assertNotNull(d);
232208
assertEquals(expectedData.get(index), d);
233209
index++;
234210
}
235211

236212
Exception exception = assertThrows(NoSuchElementException.class, () -> {
237-
iterator.next();
213+
itemIterator.next();
238214
});
239-
assertEquals("No more data available.", exception.getMessage());
215+
assertEquals("No more items available.", exception.getMessage());
240216

241217
RecordPage expectedPage1 = new RecordPage();
242218
expectedPage1.setData(Arrays.asList("apple", "mango", "orange"));
@@ -250,10 +226,19 @@ private void verifyData(PaginatedData<String, RecordPage> paginatedData) {
250226

251227
int pageNum = 0;
252228
List<RecordPage> expectedPages = Arrays.asList(expectedPage1, expectedPage2);
253-
for (RecordPage p : paginatedData.pages()) {
254-
assertEquals(expectedPages.get(pageNum).getData(), p.getData());
255-
assertEquals(expectedPages.get(pageNum).getPageInfo(), p.getPageInfo());
256-
assertEquals(expectedPages.get(pageNum).getNextLink(), p.getNextLink());
229+
Iterator<PageWrapper<String, RecordPage>> pagesIterator = paginatedData.pages(cs -> {
230+
try {
231+
return cs.get();
232+
} catch (CoreApiException | IOException e) {
233+
return null;
234+
}
235+
});
236+
while (pagesIterator.hasNext()) {
237+
PageWrapper<String, RecordPage> p = pagesIterator.next();
238+
assertNotNull(p);
239+
assertEquals(expectedPages.get(pageNum).getData(), p.getResult().getData());
240+
assertEquals(expectedPages.get(pageNum).getPageInfo(), p.getResult().getPageInfo());
241+
assertEquals(expectedPages.get(pageNum).getNextLink(), p.getResult().getNextLink());
257242
pageNum++;
258243
}
259244
}
@@ -519,22 +504,27 @@ private ApiCall<String, CoreApiException> getApiCall() throws IOException {
519504
.build();
520505
}
521506

522-
private ApiCall<PaginatedData<String, RecordPage>, CoreApiException> getPaginatedApiCall(
507+
private PaginatedData<String, PageWrapper<String, RecordPage>, RecordPage, CoreApiException> getPaginatedApiCall(
523508
PaginationStrategy... pagination) throws IOException {
524-
when(response.getBody()).thenReturn("{\"data\":[\"apple\",\"mango\",\"orange\"],\""
525-
+ "page_info\":\"fruits\",\"next_link\":\"https://localhost:3000/path?page=2\"}");
526509
when(response.getHeaders()).thenReturn(getHttpHeaders());
527510
Callback pageCallback = new Callback() {
528511
private int callNumber = 1;
529512
@Override
530513
public void onBeforeRequest(Request request) {
531-
if (callNumber > 1) {
514+
if (callNumber == 1) {
515+
when(response.getBody()).thenReturn(
516+
"{\"data\":[\"apple\",\"mango\",\"orange\"],\""
517+
+ "page_info\":\"fruits\","
518+
+ "\"next_link\":\"https://localhost:3000/path?page=2\"}");
519+
}
520+
else if (callNumber == 2) {
532521
when(response.getBody()).thenReturn(
533522
"{\"data\":[\"potato\",\"carrot\",\"tomato\"],"
534523
+ "\"page_info\":\"vegitables\"}");
535524
}
536-
if (callNumber > 2) {
537-
throw new NoSuchElementException("No more data available.");
525+
else if (callNumber == 3) {
526+
when(response.getBody()).thenReturn(
527+
"{\"data\":[]}");
538528
}
539529
callNumber++;
540530
}
@@ -543,7 +533,7 @@ public void onAfterResponse(Context context) {
543533
// TODO Auto-generated method stub
544534
}
545535
};
546-
return new ApiCall.Builder<PaginatedData<String, RecordPage>, CoreApiException>()
536+
return new ApiCall.Builder<RecordPage, CoreApiException>()
547537
.globalConfig(getGlobalConfig(pageCallback))
548538
.requestBuilder(requestBuilder -> requestBuilder.server("https://localhost:3000")
549539
.path("/path/{cursor}")
@@ -555,14 +545,14 @@ public void onAfterResponse(Context context) {
555545
.headerParam(param -> param.key("accept").value("application/json"))
556546
.httpMethod(Method.GET))
557547
.responseHandler(responseHandler -> responseHandler
558-
.paginatedDeserializer(new TypeReference<RecordPage>() {},
559-
t -> t.getData(), r -> r, pagination)
548+
.deserializer(res -> CoreHelper.deserialize(res, RecordPage.class))
560549
.nullify404(false)
561550
.globalErrorCase(Collections.emptyMap()))
562551
.endpointConfiguration(
563552
param -> param.arraySerializationFormat(ArraySerializationFormat.INDEXED)
564553
.hasBinaryResponse(false).retryOption(RetryOption.DEFAULT))
565-
.build();
554+
.build()
555+
.paginate(p -> p, pw -> pw, r -> r.getData(), pagination);
566556
}
567557

568558
private ApiCall<String, CoreApiException> getApiCallLocalErrorTemplate(String responseString,

src/test/java/apimatic/core/type/pagination/CursorPaginationTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.mockito.junit.MockitoRule;
1515

1616
import io.apimatic.core.HttpRequest;
17+
import io.apimatic.core.HttpRequest.Builder;
1718
import io.apimatic.core.types.pagination.CursorPagination;
1819
import io.apimatic.core.types.pagination.PaginatedData;
1920

@@ -36,20 +37,20 @@ public class CursorPaginationTest {
3637
*/
3738
@Test
3839
public void testWithValidCursorReturnsTrue() {
39-
PaginatedData<?, ?> paginatedData = mock(PaginatedData.class);
40+
PaginatedData<?, ?, ?, ?> paginatedData = mock(PaginatedData.class);
4041

41-
when(paginatedData.getLastRequestBuilder())
42+
when(paginatedData.getRequestBuilder())
4243
.thenReturn(new HttpRequest.Builder().queryParam(
4344
q -> q.key(CURSOR_KEY).value("abc")));
44-
when(paginatedData.getLastResponseBody()).thenReturn("{\"next_cursor\": \"xyz123\"}");
45+
when(paginatedData.getResponse().getBody()).thenReturn("{\"next_cursor\": \"xyz123\"}");
4546

4647
CursorPagination cursor = new CursorPagination("$response.body#/next_cursor",
4748
"$request.query#/cursor");
4849

49-
assertTrue(cursor.isValid(paginatedData));
50-
assertNotNull(cursor.getNextRequestBuilder());
50+
Builder requestBuilder = cursor.apply(paginatedData);
51+
assertNotNull(requestBuilder);
5152

52-
cursor.getNextRequestBuilder().updateByReference("$request.query#/cursor", v -> {
53+
requestBuilder.updateByReference("$request.query#/cursor", v -> {
5354
assertEquals("xyz123", v);
5455
return v;
5556
});

0 commit comments

Comments
 (0)