1
+ package com .merge .api .integration ;
2
+
3
+ import com .merge .api .MergeApi ;
4
+ import com .merge .api .core .SyncPagingIterable ;
5
+ import com .merge .api .filestorage .types .Folder ;
6
+ import com .merge .api .filestorage .types .PaginatedFolderList ;
7
+ import org .junit .jupiter .api .BeforeEach ;
8
+ import org .junit .jupiter .api .Test ;
9
+ import org .junit .jupiter .api .condition .EnabledIfEnvironmentVariable ;
10
+
11
+ import static org .junit .jupiter .api .Assertions .*;
12
+
13
+ @ EnabledIfEnvironmentVariable (named = "MERGE_API_KEY_FILESTORAGE" , matches = ".+" )
14
+ @ EnabledIfEnvironmentVariable (named = "MERGE_ACCOUNT_TOKEN_FILESTORAGE" , matches = ".+" )
15
+ public class CursorPaginationIntegrationTest {
16
+
17
+ private MergeApi client ;
18
+
19
+ @ BeforeEach
20
+ void setUp () {
21
+ String apiKey = System .getenv ("MERGE_API_KEY_FILESTORAGE" );
22
+ String accountToken = System .getenv ("MERGE_ACCOUNT_TOKEN_FILESTORAGE" );
23
+
24
+ assertNotNull (apiKey , "MERGE_API_KEY environment variable must be set" );
25
+ assertNotNull (accountToken , "MERGE_ACCOUNT_TOKEN environment variable must be set" );
26
+
27
+ client = MergeApi .builder ()
28
+ .apiKey (apiKey )
29
+ .accountToken (accountToken )
30
+ .build ();
31
+ }
32
+
33
+ @ Test
34
+ void testCursorPaginationWithFileStorageFolders () {
35
+ SyncPagingIterable <Folder > folders = client .filestorage ().folders ().list ();
36
+
37
+ assertNotNull (folders , "Folders list should not be null" );
38
+
39
+ // Test that getResponse() is available and returns pagination metadata
40
+ folders .getResponse ().ifPresent (response -> {
41
+ assertTrue (response instanceof PaginatedFolderList ,
42
+ "Response should be instance of PaginatedFolderList" );
43
+
44
+ PaginatedFolderList paginatedResponse = (PaginatedFolderList ) response ;
45
+
46
+ // Test cursor pagination metadata access
47
+ if (paginatedResponse .getNext () != null ) {
48
+ assertNotNull (paginatedResponse .getNext (),
49
+ "Cursor token should be available for pagination" );
50
+ }
51
+
52
+ // Test that we can access results
53
+ assertNotNull (paginatedResponse .getResults (),
54
+ "Results list should not be null" );
55
+ });
56
+
57
+ // Test iteration over paginated results
58
+ int itemCount = 0 ;
59
+ int pageCount = 0 ;
60
+
61
+ for (Folder folder : folders ) {
62
+ assertNotNull (folder , "Each folder should not be null" );
63
+ itemCount ++;
64
+
65
+ // Limit test to prevent excessive API calls
66
+ if (itemCount >= 10 ) {
67
+ break ;
68
+ }
69
+ }
70
+
71
+ assertTrue (itemCount >= 0 , "Should have processed at least 0 items" );
72
+ }
73
+
74
+ @ Test
75
+ void testStatelessPaginationWithCursor () {
76
+ SyncPagingIterable <Folder > firstPage = client .filestorage ().folders ().list ();
77
+
78
+ // Get the first page response to extract cursor
79
+ firstPage .getResponse ().ifPresent (response -> {
80
+ assertTrue (response instanceof PaginatedFolderList ,
81
+ "Response should be PaginatedFolderList" );
82
+
83
+ PaginatedFolderList paginatedResponse = (PaginatedFolderList ) response ;
84
+
85
+ // If there's a next cursor, test stateless pagination
86
+ if (paginatedResponse .getNext () != null && !paginatedResponse .getNext ().isEmpty ()) {
87
+ String nextCursor = paginatedResponse .getNext ();
88
+
89
+ // This demonstrates how a user would implement stateless pagination
90
+ // by using the cursor token from the response
91
+ assertNotNull (nextCursor , "Next cursor should be available" );
92
+ assertFalse (nextCursor .trim ().isEmpty (), "Next cursor should not be empty" );
93
+
94
+ // Note: In a real implementation, you would use this cursor
95
+ // to make subsequent requests with the cursor parameter
96
+ System .out .println ("Successfully extracted cursor token: " + nextCursor .substring (0 , Math .min (10 , nextCursor .length ())) + "..." );
97
+ }
98
+ });
99
+ }
100
+
101
+ @ Test
102
+ void testPaginationMetadataAccess () {
103
+ SyncPagingIterable <Folder > folders = client .filestorage ().folders ().list ();
104
+
105
+ // Test that getResponse() provides access to full API response
106
+ assertTrue (folders .getResponse ().isPresent (),
107
+ "getResponse() should return the API response" );
108
+
109
+ folders .getResponse ().ifPresent (response -> {
110
+ // Verify we can cast to the expected response type
111
+ assertDoesNotThrow (() -> {
112
+ PaginatedFolderList paginatedResponse = (PaginatedFolderList ) response ;
113
+
114
+ // Test access to pagination metadata
115
+ // These fields may be null if no pagination is needed
116
+ String next = paginatedResponse .getNext ();
117
+ String previous = paginatedResponse .getPrevious ();
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" );
124
+ });
125
+ }
126
+ }
0 commit comments