33import static org .junit .Assert .assertEquals ;
44import static org .junit .Assert .assertFalse ;
55import static org .junit .Assert .assertNotNull ;
6+ import static org .junit .Assert .assertNull ;
67import static org .junit .Assert .assertTrue ;
78import static org .junit .Assert .fail ;
89import static org .mockito .Mockito .mock ;
1718import io .apimatic .core .HttpRequest .Builder ;
1819import io .apimatic .core .types .pagination .CursorPagination ;
1920import io .apimatic .core .types .pagination .PaginatedData ;
21+ import io .apimatic .coreinterfaces .http .response .Response ;
2022
2123/**
2224 * Unit tests for the {@link CursorPagination} class.
@@ -38,11 +40,13 @@ public class CursorPaginationTest {
3840 @ Test
3941 public void testWithValidCursorReturnsTrue () {
4042 PaginatedData <?, ?, ?, ?> paginatedData = mock (PaginatedData .class );
43+ Response response = mock (Response .class );
4144
4245 when (paginatedData .getRequestBuilder ())
4346 .thenReturn (new HttpRequest .Builder ().queryParam (
4447 q -> q .key (CURSOR_KEY ).value ("abc" )));
45- when (paginatedData .getResponse ().getBody ()).thenReturn ("{\" next_cursor\" : \" xyz123\" }" );
48+ when (paginatedData .getResponse ()).thenReturn (response );
49+ when (response .getBody ()).thenReturn ("{\" next_cursor\" : \" xyz123\" }" );
4650
4751 CursorPagination cursor = new CursorPagination ("$response.body#/next_cursor" ,
4852 "$request.query#/cursor" );
@@ -61,181 +65,178 @@ public void testWithValidCursorReturnsTrue() {
6165 */
6266 @ Test
6367 public void testWithValidCursorAndDifferentTypeReturnsTrueA () {
64- PaginatedData <?, ?> paginatedData = mock (PaginatedData .class );
68+ PaginatedData <?, ?, ?, ?> paginatedData = mock (PaginatedData .class );
69+ Response response = mock (Response .class );
6570
66- when (paginatedData .getLastRequestBuilder ())
71+ when (paginatedData .getRequestBuilder ())
6772 .thenReturn (new HttpRequest .Builder ().queryParam (
6873 q -> q .key (CURSOR_KEY ).value ("abc" )));
69- when (paginatedData .getLastResponseBody ()).thenReturn ("{\" next_cursor\" : 123}" );
74+ when (paginatedData .getResponse ()).thenReturn (response );
75+ when (response .getBody ()).thenReturn ("{\" next_cursor\" : 123}" );
7076
7177 CursorPagination cursor = new CursorPagination ("$response.body#/next_cursor" ,
7278 "$request.query#/cursor" );
7379
74- assertTrue ( cursor .isValid (paginatedData ) );
75- assertNotNull (cursor . getNextRequestBuilder () );
80+ Builder requestBuilder = cursor .apply (paginatedData );
81+ assertNotNull (requestBuilder );
7682
77- cursor . getNextRequestBuilder () .updateByReference ("$request.query#/cursor" , v -> {
83+ requestBuilder .updateByReference ("$request.query#/cursor" , v -> {
7884 assertEquals ("123" , v );
7985 return v ;
8086 });
8187 }
8288
89+
8390 @ Test
8491 public void testWithValidCursorAndDifferentTypeReturnsTrueB () {
85- PaginatedData <?, ?> paginatedData = mock (PaginatedData .class );
92+ PaginatedData <?, ?, ?, ? > paginatedData = mock (PaginatedData .class );
8693 final int current = 456 ;
94+ Response response = mock (Response .class );
8795
88- when (paginatedData .getLastRequestBuilder ())
96+ when (paginatedData .getRequestBuilder ())
8997 .thenReturn (new HttpRequest .Builder ().queryParam (
9098 q -> q .key ("cursor" ).value (current )));
91- when (paginatedData .getLastResponseBody ()).thenReturn ("{\" next_cursor\" : 123}" );
99+ when (paginatedData .getResponse ()).thenReturn (response );
100+ when (response .getBody ()).thenReturn ("{\" next_cursor\" : 123}" );
92101
93102 CursorPagination cursor = new CursorPagination ("$response.body#/next_cursor" ,
94103 "$request.query#/cursor" );
95104
96- assertTrue ( cursor .isValid (paginatedData ) );
97- assertNotNull (cursor . getNextRequestBuilder () );
105+ Builder requestBuilder = cursor .apply (paginatedData );
106+ assertNotNull (requestBuilder );
98107
99- cursor . getNextRequestBuilder () .updateByReference ("$request.query#/cursor" , v -> {
108+ requestBuilder .updateByReference ("$request.query#/cursor" , v -> {
100109 assertEquals ("123" , v );
101110 return v ;
102111 });
103112 }
104113
114+
105115 @ Test
106116 public void testWithValidCursorButMissingInFirstRequestReturnsFalse () {
107- PaginatedData <?, ?> paginatedData = mock (PaginatedData .class );
117+ PaginatedData <?, ?, ?, ?> paginatedData = mock (PaginatedData .class );
118+ Response response = mock (Response .class );
108119
109- when (paginatedData .getLastRequestBuilder ()).thenReturn (new HttpRequest .Builder ());
110- when (paginatedData .getLastResponseBody ()).thenReturn ("{\" next_cursor\" : \" xyz123\" }" );
120+ when (paginatedData .getRequestBuilder ()).thenReturn (new HttpRequest .Builder ());
121+ when (paginatedData .getResponse ()).thenReturn (response );
122+ when (response .getBody ()).thenReturn ("{\" next_cursor\" : \" xyz123\" }" );
111123
112124 CursorPagination cursor = new CursorPagination ("$response.body#/next_cursor" ,
113125 "$request.query#/cursor" );
114126
115- assertFalse (cursor .isValid (paginatedData ));
116- assertNotNull (cursor .getNextRequestBuilder ());
117-
118- cursor .getNextRequestBuilder ().updateByReference ("$request.query#/cursor" , v -> {
119- fail ();
120- return v ;
121- });
127+ Builder requestBuilder = cursor .apply (paginatedData );
128+ assertNull (requestBuilder );
122129 }
123130
131+
124132 /**
125133 * Test with valid cursor from a response body and different type.
126134 */
127135 @ Test
128136 public void testWithValidCursorFromResponseBodyReturnsTrue () {
129- PaginatedData <?, ?> paginatedData = mock (PaginatedData .class );
137+ PaginatedData <?, ?, ?, ?> paginatedData = mock (PaginatedData .class );
138+ Response response = mock (Response .class );
130139
131- when (paginatedData .getLastRequestBuilder ())
140+ when (paginatedData .getRequestBuilder ())
132141 .thenReturn (new HttpRequest .Builder ().queryParam (
133142 q -> q .key (CURSOR_KEY ).value ("abc" )));
134- when (paginatedData .getLastResponseBody ()).thenReturn ("{\" next_cursor\" : 123}" );
143+ when (paginatedData .getResponse ()).thenReturn (response );
144+ when (response .getBody ()).thenReturn ("{\" next_cursor\" : 123}" );
135145
136146 CursorPagination cursor = new CursorPagination ("$response.body#/next_cursor" ,
137147 "$request.query#/cursor" );
138148
139- assertTrue ( cursor .isValid (paginatedData ) );
140- assertNotNull (cursor . getNextRequestBuilder () );
149+ Builder requestBuilder = cursor .apply (paginatedData );
150+ assertNotNull (requestBuilder );
141151
142- cursor . getNextRequestBuilder () .updateByReference ("$request.query#/cursor" , v -> {
152+ requestBuilder .updateByReference ("$request.query#/cursor" , v -> {
143153 assertEquals ("123" , v );
144154 return v ;
145155 });
146156 }
147157
158+
148159 /**
149160 * Test case where the response pointer is invalid.
150161 */
151162 @ Test
152163 public void testWithInvalidResponsePointerReturnsFalse () {
153- PaginatedData <?, ?> paginatedData = mock (PaginatedData .class );
164+ PaginatedData <?, ?, ?, ?> paginatedData = mock (PaginatedData .class );
165+ Response response = mock (Response .class );
154166
155- when (paginatedData .getLastRequestBuilder ())
167+ when (paginatedData .getRequestBuilder ())
156168 .thenReturn (new HttpRequest .Builder ().headerParam (
157169 h -> h .key (CURSOR_KEY ).value ("abc" )));
158- when (paginatedData .getLastResponseBody ()).thenReturn ("{\" next_cursor\" : \" xyz123\" }" );
170+ when (paginatedData .getResponse ()).thenReturn (response );
171+ when (response .getBody ()).thenReturn ("{\" next_cursor\" : \" xyz123\" }" );
159172
160- CursorPagination cursor = new CursorPagination ("$response.body#/next" ,
173+ CursorPagination cursor = new CursorPagination ("$response.body#/next" , // invalid pointer
161174 "$request.headers#/cursor" );
162175
163- assertFalse (cursor .isValid (paginatedData ));
164- assertNotNull (cursor .getNextRequestBuilder ());
165-
166- cursor .getNextRequestBuilder ().updateByReference ("$request.headers#/cursor" , v -> {
167- assertEquals ("abc" , v );
168- return v ;
169- });
176+ Builder requestBuilder = cursor .apply (paginatedData );
177+ assertNull (requestBuilder );
170178 }
171179
180+
172181 /**
173182 * Test with missing response pointer.
174183 */
175184 @ Test
176185 public void testWithMissingResponsePointerReturnsFalse () {
177- PaginatedData <?, ?> paginatedData = mock (PaginatedData .class );
186+ PaginatedData <?, ?, ?, ?> paginatedData = mock (PaginatedData .class );
187+ Response response = mock (Response .class );
178188
179- when (paginatedData .getLastRequestBuilder ())
189+ when (paginatedData .getRequestBuilder ())
180190 .thenReturn (new HttpRequest .Builder ().headerParam (
181191 h -> h .key (CURSOR_KEY ).value ("abc" )));
182- when (paginatedData .getLastResponseBody ()).thenReturn ("{\" next_cursor\" : \" xyz123\" }" );
192+ when (paginatedData .getResponse ()).thenReturn (response );
193+ when (response .getBody ()).thenReturn ("{\" next_cursor\" : \" xyz123\" }" );
183194
184195 CursorPagination cursor = new CursorPagination (null , "$request.headers#/cursor" );
185196
186- assertFalse (cursor .isValid (paginatedData ));
187- assertNotNull (cursor .getNextRequestBuilder ());
188-
189- cursor .getNextRequestBuilder ().updateByReference ("$request.headers#/cursor" , v -> {
190- assertEquals ("abc" , v );
191- return v ;
192- });
197+ Builder requestBuilder = cursor .apply (paginatedData );
198+ assertNull (requestBuilder );
193199 }
194200
201+
195202 /**
196203 * Test case with invalid request pointer.
197204 */
198205 @ Test
199206 public void testWithInvalidRequestPointerReturnsFalse () {
200- PaginatedData <?, ?> paginatedData = mock (PaginatedData .class );
207+ PaginatedData <?, ?, ?, ?> paginatedData = mock (PaginatedData .class );
208+ Response response = mock (Response .class );
201209
202- when (paginatedData .getLastRequestBuilder ())
210+ when (paginatedData .getRequestBuilder ())
203211 .thenReturn (new HttpRequest .Builder ().headerParam (
204212 h -> h .key (CURSOR_KEY ).value ("abc" )));
205- when (paginatedData .getLastResponseBody ()).thenReturn ("{\" next_cursor\" : \" xyz123\" }" );
213+ when (paginatedData .getResponse ()).thenReturn (response );
214+ when (response .getBody ()).thenReturn ("{\" next_cursor\" : \" xyz123\" }" );
206215
207216 CursorPagination cursor = new CursorPagination ("$response.body#/next_cursor" ,
208217 "$request.headers#/next_cursor" );
209218
210- assertFalse (cursor .isValid (paginatedData ));
211- assertNotNull (cursor .getNextRequestBuilder ());
212-
213- cursor .getNextRequestBuilder ().updateByReference ("$request.headers#/cursor" , v -> {
214- assertEquals ("abc" , v );
215- return v ;
216- });
219+ assertNull (cursor .apply (paginatedData ));
217220 }
218221
222+
219223 /**
220224 * Test with missing request pointer.
221225 */
222226 @ Test
223227 public void testWithMissingRequestPointerReturnsFalse () {
224- PaginatedData <?, ?> paginatedData = mock (PaginatedData .class );
228+ PaginatedData <?, ?, ?, ?> paginatedData = mock (PaginatedData .class );
229+ Response response = mock (Response .class );
225230
226- when (paginatedData .getLastRequestBuilder ())
231+ when (paginatedData .getRequestBuilder ())
227232 .thenReturn (new HttpRequest .Builder ().headerParam (
228233 h -> h .key (CURSOR_KEY ).value ("abc" )));
229- when (paginatedData .getLastResponseBody ()).thenReturn ("{\" next_cursor\" : \" xyz123\" }" );
234+ when (paginatedData .getResponse ()).thenReturn (response );
235+ when (response .getBody ()).thenReturn ("{\" next_cursor\" : \" xyz123\" }" );
230236
231237 CursorPagination cursor = new CursorPagination ("$response.body#/next_cursor" , null );
238+ assertNull (cursor .apply (paginatedData ));
239+ }
232240
233- assertFalse (cursor .isValid (paginatedData ));
234- assertNotNull (cursor .getNextRequestBuilder ());
235241
236- cursor .getNextRequestBuilder ().updateByReference ("$request.headers#/cursor" , v -> {
237- assertEquals ("abc" , v );
238- return v ;
239- });
240- }
241242}
0 commit comments