BigQueryHelper { public boolean tableExists(TableReference tableRef) throws IOException { try { Table fetchedTable = service .tables() .get(tableRef.getProjectId(), tableRef.getDatasetId(), tableRef.getTableId()) .execute(); logger.atFine().log( "Successfully fetched table '%s' for tableRef '%s'", fetchedTable, tableRef); return true; } catch (IOException ioe) { if (errorExtractor.itemNotFound(ioe)) { return false; } throw new IOException( String.format("Unhandled exception trying to get table '%s'", tableRef), ioe); } } BigQueryHelper(Bigquery service); Bigquery getRawBigquery(); void importFederatedFromGcs(
      String projectId,
      TableReference tableRef,
      @Nullable TableSchema schema,
      BigQueryFileFormat sourceFormat,
      List<String> gcsPaths); void importFromGcs(
      String projectId,
      TableReference tableRef,
      @Nullable TableSchema schema,
      @Nullable TimePartitioning timePartitioning,
      @Nullable String kmsKeyName,
      BigQueryFileFormat sourceFormat,
      String createDisposition,
      String writeDisposition,
      List<String> gcsPaths,
      boolean awaitCompletion); void exportBigQueryToGcs(
      String projectId, TableReference tableRef, List<String> gcsPaths, boolean awaitCompletion); boolean tableExists(TableReference tableRef); Table getTable(TableReference tableRef); JobReference createJobReference(
      String projectId, String jobIdPrefix, @Nullable String location); void checkJobIdEquality(Job expected, Job actual); Job insertJobOrFetchDuplicate(String projectId, Job job); static final String BIGQUERY_JOB_ID_PATTERN; static final int BIGQUERY_JOB_ID_MAX_LENGTH; }
@Test public void testTableExistsTrue() throws IOException { when(mockBigqueryTablesGet.execute()).thenReturn(fakeTable); boolean exists = helper.tableExists(tableRef); verify(mockBigquery, times(1)).tables(); verify(mockBigqueryTables, times(1)).get(eq(projectId), eq(datasetId), eq(tableId)); verify(mockBigqueryTablesGet, times(1)).execute(); assertThat(exists).isTrue(); }
@Test public void testTableExistsFalse() throws IOException { IOException fakeNotFoundException = new IOException("Fake not found exception"); when(mockBigqueryTablesGet.execute()).thenThrow(fakeNotFoundException); when(mockErrorExtractor.itemNotFound(any(IOException.class))).thenReturn(true); boolean exists = helper.tableExists(tableRef); verify(mockBigquery, times(1)).tables(); verify(mockBigqueryTables, times(1)).get(eq(projectId), eq(datasetId), eq(tableId)); verify(mockBigqueryTablesGet, times(1)).execute(); verify(mockErrorExtractor, times(1)).itemNotFound(eq(fakeNotFoundException)); assertThat(exists).isFalse(); }
@Test public void testTableExistsUnhandledException() throws IOException { IOException fakeUnhandledException = new IOException("Fake unhandled exception"); when(mockBigqueryTablesGet.execute()).thenThrow(fakeUnhandledException); when(mockErrorExtractor.itemNotFound(any(IOException.class))).thenReturn(false); IOException thrown = assertThrows(IOException.class, () -> helper.tableExists(tableRef)); assertThat(thrown).hasCauseThat().isEqualTo(fakeUnhandledException); verify(mockBigquery, times(1)).tables(); verify(mockBigqueryTables, times(1)).get(eq(projectId), eq(datasetId), eq(tableId)); verify(mockBigqueryTablesGet, times(1)).execute(); verify(mockErrorExtractor, times(1)).itemNotFound(eq(fakeUnhandledException)); }