|
72 | 72 |
|
73 | 73 | import static com.amazonaws.athena.connector.lambda.metadata.ListTablesRequest.UNLIMITED_PAGE_SIZE_VALUE; |
74 | 74 | import static com.amazonaws.athena.connectors.db2.Db2Constants.PARTITION_NUMBER; |
75 | | -import static org.mockito.ArgumentMatchers.any; |
76 | 75 | import static org.mockito.ArgumentMatchers.nullable; |
77 | 76 |
|
78 | 77 | public class Db2MetadataHandlerTest extends TestBase { |
@@ -352,5 +351,54 @@ public void doListTables() throws Exception { |
352 | 351 | new TableName("TESTSCHEMA", "testTABLE")}; |
353 | 352 | Assert.assertEquals(Arrays.toString(expectedTables), listTablesResponse.getTables().toString()); |
354 | 353 | } |
| 354 | + |
| 355 | + @Test |
| 356 | + public void doListPaginatedTables() |
| 357 | + throws Exception |
| 358 | + { |
| 359 | + //Test 1: Testing Single table returned in request of page size 1 and nextToken null |
| 360 | + Object[][] values = {{"testSchema", "testTable"}}; |
| 361 | + TableName[] expected = {new TableName("testSchema", "testTable")}; |
| 362 | + executePaginatedTableTest(values, expected, null, 1, "1"); |
| 363 | + |
| 364 | + // Test 2: Testing next table returned of page size 1 and nextToken 1 |
| 365 | + executePaginatedTableTest(values, expected, "1", 1, "2"); |
| 366 | + |
| 367 | + // Test 3: Testing single table returned when requesting pageSize 2 signifying end of pagination where nextToken is null. |
| 368 | + executePaginatedTableTest(values, expected, "2", 2, null); |
| 369 | + |
| 370 | + // Test 4: Testing unlimited page size (UNLIMITED_PAGE_SIZE_VALUE) which should set pageSize to Integer.MAX_VALUE |
| 371 | + values = new Object[][]{{"testSchema", "testTable1"}, {"testSchema", "testTable2"}}; |
| 372 | + expected = new TableName[]{new TableName("testSchema", "testTable1"), new TableName("testSchema", "testTable2")}; |
| 373 | + // Use a non-null token to force pagination path, which will trigger the listPaginatedTables method |
| 374 | + // With unlimited page size, there should be no next token (null) since all results are returned |
| 375 | + executePaginatedTableTest(values, expected, "0", UNLIMITED_PAGE_SIZE_VALUE, null); |
| 376 | + } |
| 377 | + |
| 378 | + /** |
| 379 | + * Helper method to execute paginated table test and verify results |
| 380 | + * @param values the table data to return in the ResultSet |
| 381 | + * @param expected the expected TableName array |
| 382 | + * @param nextToken the nextToken to use in the request |
| 383 | + * @param pageSize the pageSize to use in the request |
| 384 | + * @param expectedNextToken the expected nextToken in the response |
| 385 | + * @throws Exception if there's an error during execution |
| 386 | + */ |
| 387 | + private void executePaginatedTableTest(Object[][] values, |
| 388 | + TableName[] expected, String nextToken, int pageSize, |
| 389 | + String expectedNextToken) throws Exception { |
| 390 | + BlockAllocator blockAllocator = new BlockAllocatorImpl(); |
| 391 | + PreparedStatement preparedStatement = Mockito.mock(PreparedStatement.class); |
| 392 | + Mockito.when(this.connection.prepareStatement(Db2Constants.LIST_PAGINATED_TABLES_QUERY)).thenReturn(preparedStatement); |
| 393 | + String[] schema = {"TABLE_SCHEM", "TABLE_NAME"}; |
| 394 | + ResultSet resultSet = mockResultSet(schema, values, new AtomicInteger(-1)); |
| 395 | + Mockito.when(preparedStatement.executeQuery()).thenReturn(resultSet); |
| 396 | + ListTablesResponse listTablesResponse = this.db2MetadataHandler.doListTables( |
| 397 | + blockAllocator, new ListTablesRequest(this.federatedIdentity, "testQueryId", |
| 398 | + "testCatalog", "testSchema", nextToken, pageSize)); |
| 399 | + |
| 400 | + Assert.assertEquals(expectedNextToken, listTablesResponse.getNextToken()); |
| 401 | + Assert.assertArrayEquals(expected, listTablesResponse.getTables().toArray()); |
| 402 | + } |
355 | 403 | } |
356 | 404 |
|
0 commit comments