|
23 | 23 | import static org.junit.jupiter.api.Assertions.assertSame; |
24 | 24 | import static org.junit.jupiter.api.Assertions.assertThrows; |
25 | 25 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 26 | +import static org.mockito.Mockito.mock; |
| 27 | +import static org.mockito.Mockito.times; |
| 28 | +import static org.mockito.Mockito.verify; |
| 29 | +import static org.mockito.Mockito.when; |
26 | 30 |
|
27 | 31 | import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider; |
| 32 | +import com.google.api.gax.paging.Page; |
28 | 33 | import com.google.api.gax.rpc.HeaderProvider; |
29 | 34 | import com.google.api.gax.rpc.TransportChannelProvider; |
30 | 35 | import com.google.cloud.bigquery.BigQuery; |
| 36 | +import com.google.cloud.bigquery.BigQueryException; |
| 37 | +import com.google.cloud.bigquery.Project; |
31 | 38 | import com.google.cloud.bigquery.QueryJobConfiguration.JobCreationMode; |
32 | 39 | import com.google.cloud.bigquery.exception.BigQueryJdbcException; |
33 | 40 | import com.google.cloud.bigquery.storage.v1.BigQueryReadClient; |
34 | 41 | import com.google.cloud.bigquery.storage.v1.BigQueryWriteClient; |
35 | 42 | import java.io.IOException; |
36 | 43 | import java.io.InputStream; |
37 | 44 | import java.sql.SQLException; |
| 45 | +import java.util.Arrays; |
| 46 | +import java.util.List; |
38 | 47 | import java.util.Optional; |
39 | 48 | import java.util.Properties; |
40 | 49 | import java.util.logging.Level; |
@@ -519,4 +528,79 @@ public void testWrapperMethods() throws Exception { |
519 | 528 | assertTrue(e.getMessage().contains("Cannot unwrap to java.sql.Statement")); |
520 | 529 | } |
521 | 530 | } |
| 531 | + |
| 532 | + @Test |
| 533 | + public void testGetDiscoveredProjects_Success() throws Exception { |
| 534 | + try (BigQueryConnection connection = new BigQueryConnection(BASE_URL)) { |
| 535 | + BigQuery mockBigQuery = mock(BigQuery.class); |
| 536 | + connection.bigQuery = mockBigQuery; |
| 537 | + |
| 538 | + Page<Project> mockPage = mock(Page.class); |
| 539 | + Project project1 = mock(Project.class); |
| 540 | + when(project1.getProjectId()).thenReturn("discovered-p1"); |
| 541 | + Project project2 = mock(Project.class); |
| 542 | + when(project2.getProjectId()).thenReturn("discovered-p2"); |
| 543 | + |
| 544 | + when(mockPage.iterateAll()).thenReturn(Arrays.asList(project1, project2)); |
| 545 | + when(mockBigQuery.listProjects()).thenReturn(mockPage); |
| 546 | + |
| 547 | + List<String> discovered = connection.getDiscoveredProjects(); |
| 548 | + assertEquals(Arrays.asList("discovered-p1", "discovered-p2"), discovered); |
| 549 | + |
| 550 | + // Verify caching: second call should not invoke listProjects again |
| 551 | + List<String> discoveredCached = connection.getDiscoveredProjects(); |
| 552 | + assertSame(discovered, discoveredCached); |
| 553 | + verify(mockBigQuery, times(1)).listProjects(); |
| 554 | + } |
| 555 | + } |
| 556 | + |
| 557 | + @Test |
| 558 | + public void testGetDiscoveredProjects_BigQueryExceptionThrown() throws Exception { |
| 559 | + try (BigQueryConnection connection = new BigQueryConnection(BASE_URL)) { |
| 560 | + BigQuery mockBigQuery = mock(BigQuery.class); |
| 561 | + connection.bigQuery = mockBigQuery; |
| 562 | + |
| 563 | + BigQueryException exception = new BigQueryException(403, "Access Denied"); |
| 564 | + when(mockBigQuery.listProjects()).thenThrow(exception); |
| 565 | + |
| 566 | + // Verify that it throws BigQueryJdbcException |
| 567 | + BigQueryJdbcException ex = |
| 568 | + assertThrows( |
| 569 | + BigQueryJdbcException.class, |
| 570 | + () -> { |
| 571 | + connection.getDiscoveredProjects(); |
| 572 | + }); |
| 573 | + assertTrue(ex.getMessage().contains("Failed to list all accessible projects.")); |
| 574 | + assertEquals(exception, ex.getCause()); |
| 575 | + |
| 576 | + // Subsequent call should retry since no cache is set |
| 577 | + assertThrows( |
| 578 | + BigQueryJdbcException.class, |
| 579 | + () -> { |
| 580 | + connection.getDiscoveredProjects(); |
| 581 | + }); |
| 582 | + verify(mockBigQuery, times(2)).listProjects(); |
| 583 | + } |
| 584 | + } |
| 585 | + |
| 586 | + @Test |
| 587 | + public void testGetDiscoveredProjects_OtherExceptionThrown() throws Exception { |
| 588 | + try (BigQueryConnection connection = new BigQueryConnection(BASE_URL)) { |
| 589 | + BigQuery mockBigQuery = mock(BigQuery.class); |
| 590 | + connection.bigQuery = mockBigQuery; |
| 591 | + |
| 592 | + RuntimeException exception = new RuntimeException("Generic Network Failure"); |
| 593 | + when(mockBigQuery.listProjects()).thenThrow(exception); |
| 594 | + |
| 595 | + // Verify that it throws BigQueryJdbcException |
| 596 | + BigQueryJdbcException ex = |
| 597 | + assertThrows( |
| 598 | + BigQueryJdbcException.class, |
| 599 | + () -> { |
| 600 | + connection.getDiscoveredProjects(); |
| 601 | + }); |
| 602 | + assertTrue(ex.getMessage().contains("Failed to list all accessible projects.")); |
| 603 | + assertEquals(exception, ex.getCause()); |
| 604 | + } |
| 605 | + } |
522 | 606 | } |
0 commit comments