11package apimatic .core ;
22
33import static org .junit .Assert .assertEquals ;
4+ import static org .junit .Assert .assertNotNull ;
45import static org .junit .Assert .assertThrows ;
56import static org .mockito .ArgumentMatchers .any ;
67import static org .mockito .ArgumentMatchers .anyList ;
2425import org .mockito .junit .MockitoJUnit ;
2526import org .mockito .junit .MockitoRule ;
2627
27- import com .fasterxml .jackson .core .type .TypeReference ;
28-
2928import apimatic .core .exceptions .GlobalTestException ;
3029import apimatic .core .mocks .MockCoreConfig ;
3130import apimatic .core .type .pagination .RecordPage ;
3736import io .apimatic .core .types .pagination .LinkPagination ;
3837import io .apimatic .core .types .pagination .OffsetPagination ;
3938import io .apimatic .core .types .pagination .PagePagination ;
39+ import io .apimatic .core .types .pagination .PageWrapper ;
4040import io .apimatic .core .types .pagination .PaginatedData ;
4141import io .apimatic .core .types .pagination .PaginationStrategy ;
4242import 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 ,
0 commit comments