1414import io .apimatic .core .HttpRequest ;
1515import io .apimatic .core .HttpRequest .Builder ;
1616import io .apimatic .core .types .pagination .CursorPagination ;
17+ import io .apimatic .core .types .pagination .PageWrapper ;
1718import io .apimatic .core .types .pagination .PaginatedData ;
1819import io .apimatic .coreinterfaces .http .response .Response ;
1920
@@ -29,211 +30,222 @@ public class CursorPaginationTest {
2930 @ Rule
3031 public MockitoRule initRule = MockitoJUnit .rule ().silent ();
3132
33+ // Constants
3234 private static final String CURSOR_KEY = "cursor" ;
35+ private static final String RESPONSE_POINTER_VALID = "$response.body#/next_cursor" ;
36+ private static final String RESPONSE_POINTER_INVALID = "$response.body#/next" ;
37+ private static final String REQUEST_QUERY_POINTER = "$request.query#/cursor" ;
38+ private static final String REQUEST_HEADER_POINTER = "$request.headers#/cursor" ;
39+ private static final String REQUEST_POINTER_INVALID = "$request.headers#/next_cursor" ;
40+ private static final String NEXT_CURSOR_JSON_STRING = "{\" next_cursor\" : \" xyz123\" }" ;
41+ private static final String NEXT_CURSOR_JSON_INT = "{\" next_cursor\" : 123}" ;
3342
3443 /**
3544 * Test with a valid cursor value.
3645 */
3746 @ Test
38- public void testWithValidCursorReturnsTrue () {
47+ public void testWithValidCursor () {
3948 PaginatedData <?, ?, ?, ?> paginatedData = mock (PaginatedData .class );
4049 Response response = mock (Response .class );
4150
4251 when (paginatedData .getRequestBuilder ())
4352 .thenReturn (new HttpRequest .Builder ().queryParam (
4453 q -> q .key (CURSOR_KEY ).value ("abc" )));
4554 when (paginatedData .getResponse ()).thenReturn (response );
46- when (response .getBody ()).thenReturn ("{ \" next_cursor \" : \" xyz123 \" }" );
55+ when (response .getBody ()).thenReturn (NEXT_CURSOR_JSON_STRING );
4756
48- CursorPagination cursor = new CursorPagination ("$response.body#/next_cursor" ,
49- "$request.query#/cursor" );
57+ CursorPagination cursor = new CursorPagination (RESPONSE_POINTER_VALID , REQUEST_QUERY_POINTER );
5058
5159 Builder requestBuilder = cursor .apply (paginatedData );
5260 assertNotNull (requestBuilder );
5361
54- requestBuilder .updateByReference ("$request.query#/cursor" , v -> {
62+ requestBuilder .updateByReference (REQUEST_QUERY_POINTER , v -> {
5563 assertEquals ("xyz123" , v );
5664 return v ;
5765 });
66+ PageWrapper <?, ?> pageWrapper = PageWrapper .Create (response , null , null );
67+ cursor .addMetaData (pageWrapper );
68+ assertEquals ("xyz123" , pageWrapper .getCursorInput ());
5869 }
5970
6071 /**
6172 * Test with a valid cursor but different response type (integer).
6273 */
6374 @ Test
64- public void testWithValidCursorAndDifferentTypeReturnsTrueA () {
75+ public void testWithValidCursorAndDifferentTypeA () {
6576 PaginatedData <?, ?, ?, ?> paginatedData = mock (PaginatedData .class );
6677 Response response = mock (Response .class );
6778
6879 when (paginatedData .getRequestBuilder ())
6980 .thenReturn (new HttpRequest .Builder ().queryParam (
7081 q -> q .key (CURSOR_KEY ).value ("abc" )));
7182 when (paginatedData .getResponse ()).thenReturn (response );
72- when (response .getBody ()).thenReturn ("{ \" next_cursor \" : 123}" );
83+ when (response .getBody ()).thenReturn (NEXT_CURSOR_JSON_INT );
7384
74- CursorPagination cursor = new CursorPagination ("$response.body#/next_cursor" ,
75- "$request.query#/cursor" );
85+ CursorPagination cursor = new CursorPagination (RESPONSE_POINTER_VALID , REQUEST_QUERY_POINTER );
7686
7787 Builder requestBuilder = cursor .apply (paginatedData );
7888 assertNotNull (requestBuilder );
7989
80- requestBuilder .updateByReference ("$request.query#/cursor" , v -> {
90+ requestBuilder .updateByReference (REQUEST_QUERY_POINTER , v -> {
8191 assertEquals ("123" , v );
8292 return v ;
8393 });
94+ PageWrapper <?, ?> pageWrapper = PageWrapper .Create (response , null , null );
95+ cursor .addMetaData (pageWrapper );
96+ assertEquals ("123" , pageWrapper .getCursorInput ());
8497 }
8598
86-
99+ /**
100+ * Test with a valid cursor but different response type (integer) and integer cursor input.
101+ */
87102 @ Test
88- public void testWithValidCursorAndDifferentTypeReturnsTrueB () {
103+ public void testWithValidCursorAndDifferentType () {
89104 PaginatedData <?, ?, ?, ?> paginatedData = mock (PaginatedData .class );
90105 final int current = 456 ;
91106 Response response = mock (Response .class );
92107
93108 when (paginatedData .getRequestBuilder ())
94109 .thenReturn (new HttpRequest .Builder ().queryParam (
95- q -> q .key ("cursor" ).value (current )));
110+ q -> q .key (CURSOR_KEY ).value (current )));
96111 when (paginatedData .getResponse ()).thenReturn (response );
97- when (response .getBody ()).thenReturn ("{ \" next_cursor \" : 123}" );
112+ when (response .getBody ()).thenReturn (NEXT_CURSOR_JSON_INT );
98113
99- CursorPagination cursor = new CursorPagination ("$response.body#/next_cursor" ,
100- "$request.query#/cursor" );
114+ CursorPagination cursor = new CursorPagination (RESPONSE_POINTER_VALID , REQUEST_QUERY_POINTER );
101115
102116 Builder requestBuilder = cursor .apply (paginatedData );
103117 assertNotNull (requestBuilder );
104118
105- requestBuilder .updateByReference ("$request.query#/cursor" , v -> {
119+ requestBuilder .updateByReference (REQUEST_QUERY_POINTER , v -> {
106120 assertEquals ("123" , v );
107121 return v ;
108122 });
123+ PageWrapper <?, ?> pageWrapper = PageWrapper .Create (response , null , null );
124+ cursor .addMetaData (pageWrapper );
125+ assertEquals ("123" , pageWrapper .getCursorInput ());
109126 }
110127
111-
128+ /**
129+ * Test with valid cursor but missing in the first request.
130+ */
112131 @ Test
113- public void testWithValidCursorButMissingInFirstRequestReturnsFalse () {
132+ public void testWithValidCursorButMissingInFirstRequest () {
114133 PaginatedData <?, ?, ?, ?> paginatedData = mock (PaginatedData .class );
115134 Response response = mock (Response .class );
116135
117136 when (paginatedData .getRequestBuilder ()).thenReturn (new HttpRequest .Builder ());
118137 when (paginatedData .getResponse ()).thenReturn (response );
119- when (response .getBody ()).thenReturn ("{ \" next_cursor \" : \" xyz123 \" }" );
138+ when (response .getBody ()).thenReturn (NEXT_CURSOR_JSON_STRING );
120139
121- CursorPagination cursor = new CursorPagination ("$response.body#/next_cursor" ,
122- "$request.query#/cursor" );
140+ CursorPagination cursor = new CursorPagination (RESPONSE_POINTER_VALID , REQUEST_QUERY_POINTER );
123141
124142 Builder requestBuilder = cursor .apply (paginatedData );
125143 assertNull (requestBuilder );
126144 }
127145
128-
129146 /**
130147 * Test with valid cursor from a response body and different type.
131148 */
132149 @ Test
133- public void testWithValidCursorFromResponseBodyReturnsTrue () {
150+ public void testWithValidCursorFromResponseBody () {
134151 PaginatedData <?, ?, ?, ?> paginatedData = mock (PaginatedData .class );
135152 Response response = mock (Response .class );
136153
137154 when (paginatedData .getRequestBuilder ())
138155 .thenReturn (new HttpRequest .Builder ().queryParam (
139156 q -> q .key (CURSOR_KEY ).value ("abc" )));
140157 when (paginatedData .getResponse ()).thenReturn (response );
141- when (response .getBody ()).thenReturn ("{ \" next_cursor \" : 123}" );
158+ when (response .getBody ()).thenReturn (NEXT_CURSOR_JSON_INT );
142159
143- CursorPagination cursor = new CursorPagination ("$response.body#/next_cursor" ,
144- "$request.query#/cursor" );
160+ CursorPagination cursor = new CursorPagination (RESPONSE_POINTER_VALID , REQUEST_QUERY_POINTER );
145161
146162 Builder requestBuilder = cursor .apply (paginatedData );
147163 assertNotNull (requestBuilder );
148164
149- requestBuilder .updateByReference ("$request.query#/cursor" , v -> {
165+ requestBuilder .updateByReference (REQUEST_QUERY_POINTER , v -> {
150166 assertEquals ("123" , v );
151167 return v ;
152168 });
169+ PageWrapper <?, ?> pageWrapper = PageWrapper .Create (response , null , null );
170+ cursor .addMetaData (pageWrapper );
171+ assertEquals ("123" , pageWrapper .getCursorInput ());
153172 }
154173
155-
156174 /**
157175 * Test case where the response pointer is invalid.
158176 */
159177 @ Test
160- public void testWithInvalidResponsePointerReturnsFalse () {
178+ public void testWithInvalidResponsePointer () {
161179 PaginatedData <?, ?, ?, ?> paginatedData = mock (PaginatedData .class );
162180 Response response = mock (Response .class );
163181
164182 when (paginatedData .getRequestBuilder ())
165183 .thenReturn (new HttpRequest .Builder ().headerParam (
166184 h -> h .key (CURSOR_KEY ).value ("abc" )));
167185 when (paginatedData .getResponse ()).thenReturn (response );
168- when (response .getBody ()).thenReturn ("{ \" next_cursor \" : \" xyz123 \" }" );
186+ when (response .getBody ()).thenReturn (NEXT_CURSOR_JSON_STRING );
169187
170- CursorPagination cursor = new CursorPagination ("$response.body#/next" , // invalid pointer
171- "$request.headers#/cursor" );
188+ CursorPagination cursor = new CursorPagination (RESPONSE_POINTER_INVALID , REQUEST_HEADER_POINTER );
172189
173190 Builder requestBuilder = cursor .apply (paginatedData );
174191 assertNull (requestBuilder );
175192 }
176193
177-
178194 /**
179195 * Test with missing response pointer.
180196 */
181197 @ Test
182- public void testWithMissingResponsePointerReturnsFalse () {
198+ public void testWithMissingResponsePointer () {
183199 PaginatedData <?, ?, ?, ?> paginatedData = mock (PaginatedData .class );
184200 Response response = mock (Response .class );
185201
186202 when (paginatedData .getRequestBuilder ())
187203 .thenReturn (new HttpRequest .Builder ().headerParam (
188204 h -> h .key (CURSOR_KEY ).value ("abc" )));
189205 when (paginatedData .getResponse ()).thenReturn (response );
190- when (response .getBody ()).thenReturn ("{ \" next_cursor \" : \" xyz123 \" }" );
206+ when (response .getBody ()).thenReturn (NEXT_CURSOR_JSON_STRING );
191207
192- CursorPagination cursor = new CursorPagination (null , "$request.headers#/cursor" );
208+ CursorPagination cursor = new CursorPagination (null , REQUEST_HEADER_POINTER );
193209
194210 Builder requestBuilder = cursor .apply (paginatedData );
195211 assertNull (requestBuilder );
196212 }
197213
198-
199214 /**
200215 * Test case with invalid request pointer.
201216 */
202217 @ Test
203- public void testWithInvalidRequestPointerReturnsFalse () {
218+ public void testWithInvalidRequestPointer () {
204219 PaginatedData <?, ?, ?, ?> paginatedData = mock (PaginatedData .class );
205220 Response response = mock (Response .class );
206221
207222 when (paginatedData .getRequestBuilder ())
208223 .thenReturn (new HttpRequest .Builder ().headerParam (
209224 h -> h .key (CURSOR_KEY ).value ("abc" )));
210225 when (paginatedData .getResponse ()).thenReturn (response );
211- when (response .getBody ()).thenReturn ("{ \" next_cursor \" : \" xyz123 \" }" );
226+ when (response .getBody ()).thenReturn (NEXT_CURSOR_JSON_STRING );
212227
213- CursorPagination cursor = new CursorPagination ("$response.body#/next_cursor" ,
214- "$request.headers#/next_cursor" );
228+ CursorPagination cursor = new CursorPagination (RESPONSE_POINTER_VALID , REQUEST_POINTER_INVALID );
215229
216230 assertNull (cursor .apply (paginatedData ));
217231 }
218232
219-
220233 /**
221234 * Test with missing request pointer.
222235 */
223236 @ Test
224- public void testWithMissingRequestPointerReturnsFalse () {
237+ public void testWithMissingRequest () {
225238 PaginatedData <?, ?, ?, ?> paginatedData = mock (PaginatedData .class );
226239 Response response = mock (Response .class );
227240
228241 when (paginatedData .getRequestBuilder ())
229242 .thenReturn (new HttpRequest .Builder ().headerParam (
230243 h -> h .key (CURSOR_KEY ).value ("abc" )));
231244 when (paginatedData .getResponse ()).thenReturn (response );
232- when (response .getBody ()).thenReturn ("{ \" next_cursor \" : \" xyz123 \" }" );
245+ when (response .getBody ()).thenReturn (NEXT_CURSOR_JSON_STRING );
233246
234- CursorPagination cursor = new CursorPagination ("$response.body#/next_cursor" , null );
247+ CursorPagination cursor = new CursorPagination (RESPONSE_POINTER_VALID , null );
235248 assertNull (cursor .apply (paginatedData ));
236249 }
237250
238-
239251}
0 commit comments