11package com .merge .api .integration ;
22
3+ import static org .junit .jupiter .api .Assertions .*;
4+
35import com .merge .api .MergeApiClient ;
46import com .merge .api .core .SyncPagingIterable ;
57import com .merge .api .filestorage .types .Folder ;
8+ import com .merge .api .filestorage .types .FoldersListRequest ;
69import com .merge .api .filestorage .types .PaginatedFolderList ;
710import org .junit .jupiter .api .BeforeEach ;
811import org .junit .jupiter .api .Test ;
912
10- import static org .junit .jupiter .api .Assertions .*;
11-
1213public class CursorPaginationIntegrationTest {
1314
1415 private MergeApiClient client ;
@@ -35,20 +36,17 @@ void testCursorPaginationWithFileStorageFolders() {
3536
3637 // Test that getResponse() is available and returns pagination metadata
3738 folders .getResponse ().ifPresent (response -> {
38- assertTrue (response instanceof PaginatedFolderList ,
39- "Response should be instance of PaginatedFolderList" );
39+ assertTrue (response instanceof PaginatedFolderList , "Response should be instance of PaginatedFolderList" );
4040
4141 PaginatedFolderList paginatedResponse = (PaginatedFolderList ) response ;
4242
4343 // Test cursor pagination metadata access
4444 paginatedResponse .getNext ().ifPresent (nextCursor -> {
45- assertNotNull (nextCursor ,
46- "Cursor token should be available for pagination" );
45+ assertNotNull (nextCursor , "Cursor token should be available for pagination" );
4746 });
4847
4948 // Test that we can access results
50- assertNotNull (paginatedResponse .getResults (),
51- "Results list should not be null" );
49+ assertNotNull (paginatedResponse .getResults (), "Results list should not be null" );
5250 });
5351
5452 // Test iteration over paginated results
@@ -70,25 +68,41 @@ void testCursorPaginationWithFileStorageFolders() {
7068
7169 @ Test
7270 void testStatelessPaginationWithCursor () {
71+ // Simulate frontend flow: get first page
7372 SyncPagingIterable <Folder > firstPage = client .fileStorage ().folders ().list ();
7473
7574 // Get the first page response to extract cursor
7675 firstPage .getResponse ().ifPresent (response -> {
77- assertTrue (response instanceof PaginatedFolderList ,
78- "Response should be PaginatedFolderList" );
76+ assertTrue (response instanceof PaginatedFolderList , "Response should be PaginatedFolderList" );
7977
8078 PaginatedFolderList paginatedResponse = (PaginatedFolderList ) response ;
8179
82- // If there's a next cursor, test stateless pagination
80+ // If there's a next cursor, test stateless pagination by actually using it
8381 paginatedResponse .getNext ().ifPresent (nextCursor -> {
84- // This demonstrates how a user would implement stateless pagination
82+ // This demonstrates how a frontend would implement stateless pagination
8583 // by using the cursor token from the response
8684 assertNotNull (nextCursor , "Next cursor should be available" );
8785 assertFalse (nextCursor .trim ().isEmpty (), "Next cursor should not be empty" );
8886
89- // Note: In a real implementation, you would use this cursor
90- // to make subsequent requests with the cursor parameter
91- System .out .println ("Successfully extracted cursor token: " + nextCursor .substring (0 , Math .min (10 , nextCursor .length ())) + "..." );
87+ // Frontend sends cursor back to backend for next page
88+ FoldersListRequest secondPageRequest =
89+ FoldersListRequest .builder ().cursor (nextCursor ).build ();
90+
91+ // Backend fetches second page using the cursor
92+ SyncPagingIterable <Folder > secondPage =
93+ client .fileStorage ().folders ().list (secondPageRequest );
94+
95+ assertNotNull (secondPage , "Second page should not be null" );
96+
97+ // Verify we got a valid response with results
98+ secondPage .getResponse ().ifPresent (secondResponse -> {
99+ assertTrue (
100+ secondResponse instanceof PaginatedFolderList ,
101+ "Second page response should be PaginatedFolderList" );
102+
103+ PaginatedFolderList secondPageResponse = (PaginatedFolderList ) secondResponse ;
104+ assertNotNull (secondPageResponse .getResults (), "Second page should have results" );
105+ });
92106 });
93107 });
94108 }
@@ -98,29 +112,28 @@ void testPaginationMetadataAccess() {
98112 SyncPagingIterable <Folder > folders = client .fileStorage ().folders ().list ();
99113
100114 // Test that getResponse() provides access to full API response
101- assertTrue (folders .getResponse ().isPresent (),
102- "getResponse() should return the API response" );
115+ assertTrue (folders .getResponse ().isPresent (), "getResponse() should return the API response" );
103116
104117 folders .getResponse ().ifPresent (response -> {
105118 // Verify we can cast to the expected response type
106- assertDoesNotThrow (() -> {
107- PaginatedFolderList paginatedResponse = ( PaginatedFolderList ) response ;
108-
109- // Test access to pagination metadata
110- // These fields may be Optional.empty() if no pagination is needed
111- paginatedResponse . getNext (). ifPresent ( next -> {
112- assertNotNull ( next , "Next cursor should not be null if present" );
113- } );
114-
115- paginatedResponse . getPrevious (). ifPresent ( previous -> {
116- assertNotNull ( previous , "Previous cursor should not be null if present" );
117- } );
118-
119- // At minimum, results should be accessible
120- assertNotNull ( paginatedResponse . getResults (),
121- "Results should always be accessible" );
122-
123- }, "Should be able to access pagination metadata" );
119+ assertDoesNotThrow (
120+ () -> {
121+ PaginatedFolderList paginatedResponse = ( PaginatedFolderList ) response ;
122+
123+ // Test access to pagination metadata
124+ // These fields may be Optional.empty() if no pagination is needed
125+ paginatedResponse . getNext (). ifPresent ( next -> {
126+ assertNotNull ( next , "Next cursor should not be null if present" );
127+ });
128+
129+ paginatedResponse . getPrevious (). ifPresent ( previous -> {
130+ assertNotNull ( previous , "Previous cursor should not be null if present" );
131+ });
132+
133+ // At minimum, results should be accessible
134+ assertNotNull ( paginatedResponse . getResults (), "Results should always be accessible" );
135+ },
136+ "Should be able to access pagination metadata" );
124137 });
125138 }
126- }
139+ }
0 commit comments