16
16
17
17
package com .google .cloud .bigquery ;
18
18
19
+ import static com .google .cloud .bigquery .BigQuery .JobField .STATISTICS ;
20
+ import static com .google .cloud .bigquery .BigQuery .JobField .USER_EMAIL ;
21
+ import static com .google .cloud .bigquery .BigQueryImpl .optionMap ;
19
22
import static com .google .common .truth .Truth .assertThat ;
20
- import static org .junit .Assert .assertArrayEquals ;
21
- import static org .junit .Assert .assertEquals ;
22
- import static org .junit .Assert .assertFalse ;
23
- import static org .junit .Assert .assertNotNull ;
24
- import static org .junit .Assert .assertNull ;
25
- import static org .junit .Assert .assertSame ;
26
- import static org .junit .Assert .assertTrue ;
27
- import static org .junit .Assert .fail ;
28
- import static org .mockito .Mockito .any ;
29
- import static org .mockito .Mockito .doReturn ;
30
- import static org .mockito .Mockito .eq ;
31
- import static org .mockito .Mockito .mock ;
32
- import static org .mockito .Mockito .times ;
33
- import static org .mockito .Mockito .verify ;
34
- import static org .mockito .Mockito .when ;
23
+ import static org .junit .Assert .*;
24
+ import static org .mockito .Mockito .*;
35
25
36
26
import com .google .api .gax .paging .Page ;
37
- import com .google .api .services .bigquery .model .ErrorProto ;
38
- import com .google .api .services .bigquery .model .GetQueryResultsResponse ;
39
- import com .google .api .services .bigquery .model .JobConfigurationQuery ;
40
- import com .google .api .services .bigquery .model .QueryRequest ;
41
- import com .google .api .services .bigquery .model .TableCell ;
42
- import com .google .api .services .bigquery .model .TableDataInsertAllRequest ;
43
- import com .google .api .services .bigquery .model .TableDataInsertAllResponse ;
44
- import com .google .api .services .bigquery .model .TableDataList ;
45
- import com .google .api .services .bigquery .model .TableRow ;
27
+ import com .google .api .services .bigquery .model .*;
28
+ import com .google .api .services .bigquery .model .JobStatistics ;
46
29
import com .google .cloud .Policy ;
47
30
import com .google .cloud .ServiceOptions ;
48
31
import com .google .cloud .Tuple ;
32
+ import com .google .cloud .bigquery .BigQuery .JobOption ;
49
33
import com .google .cloud .bigquery .BigQuery .QueryResultsOption ;
50
34
import com .google .cloud .bigquery .InsertAllRequest .RowToInsert ;
51
35
import com .google .cloud .bigquery .spi .BigQueryRpcFactory ;
52
36
import com .google .cloud .bigquery .spi .v2 .BigQueryRpc ;
53
37
import com .google .common .base .Function ;
54
38
import com .google .common .base .Supplier ;
55
- import com .google .common .collect .ImmutableList ;
56
- import com .google .common .collect .ImmutableMap ;
57
- import com .google .common .collect .Iterables ;
58
- import com .google .common .collect .Lists ;
59
- import com .google .common .collect .Maps ;
39
+ import com .google .common .collect .*;
60
40
import java .io .IOException ;
61
41
import java .math .BigInteger ;
62
42
import java .util .Collections ;
@@ -422,12 +402,11 @@ public class BigQueryImplTest {
422
402
BigQueryRpc .Option .START_INDEX , 0L );
423
403
424
404
// Job options
425
- private static final BigQuery .JobOption JOB_OPTION_FIELDS =
426
- BigQuery .JobOption .fields (BigQuery .JobField .USER_EMAIL );
405
+ private static final JobOption JOB_OPTION_FIELDS = JobOption .fields (USER_EMAIL );
427
406
428
407
// Job list options
429
408
private static final BigQuery .JobListOption JOB_LIST_OPTION_FIELD =
430
- BigQuery .JobListOption .fields (BigQuery . JobField . STATISTICS );
409
+ BigQuery .JobListOption .fields (STATISTICS );
431
410
private static final BigQuery .JobListOption JOB_LIST_ALL_USERS =
432
411
BigQuery .JobListOption .allUsers ();
433
412
private static final BigQuery .JobListOption JOB_LIST_STATE_FILTER =
@@ -1615,7 +1594,7 @@ public void testCreateJobWithSelectedFields() {
1615
1594
any (com .google .api .services .bigquery .model .Job .class ), capturedOptions .capture ()))
1616
1595
.thenReturn (newJobPb ());
1617
1596
1618
- BigQuery . JobOption jobOptions = BigQuery . JobOption .fields (BigQuery . JobField . USER_EMAIL );
1597
+ JobOption jobOptions = JobOption .fields (USER_EMAIL );
1619
1598
1620
1599
bigquery = options .getService ();
1621
1600
bigquery .create (JobInfo .of (QueryJobConfiguration .of ("SOME QUERY" )), jobOptions );
@@ -1674,6 +1653,35 @@ public JobId get() {
1674
1653
.getJob (any (String .class ), eq (id ), eq ((String ) null ), eq (EMPTY_RPC_OPTIONS ));
1675
1654
}
1676
1655
1656
+ @ Test
1657
+ public void testCreateJobTryGetNotRandom () {
1658
+ Map <BigQueryRpc .Option , ?> withStatisticOption = optionMap (JobOption .fields (STATISTICS ));
1659
+ final String id = "testCreateJobTryGet-id" ;
1660
+ String query = "SELECT * in FOO" ;
1661
+
1662
+ when (bigqueryRpcMock .create (jobCapture .capture (), eq (EMPTY_RPC_OPTIONS )))
1663
+ .thenThrow (
1664
+ new BigQueryException (
1665
+ 409 ,
1666
+ "already exists, for some reason" ,
1667
+ new RuntimeException ("Already Exists: Job" )));
1668
+ when (bigqueryRpcMock .getJob (
1669
+ any (String .class ), eq (id ), eq ((String ) null ), eq (withStatisticOption )))
1670
+ .thenReturn (
1671
+ newJobPb ()
1672
+ .setId (id )
1673
+ .setStatistics (new JobStatistics ().setCreationTime (System .currentTimeMillis ())));
1674
+
1675
+ bigquery = options .getService ();
1676
+ Job job =
1677
+ ((BigQueryImpl ) bigquery ).create (JobInfo .of (JobId .of (id ), QueryJobConfiguration .of (query )));
1678
+ assertThat (job ).isNotNull ();
1679
+ assertThat (jobCapture .getValue ().getJobReference ().getJobId ()).isEqualTo (id );
1680
+ verify (bigqueryRpcMock ).create (jobCapture .capture (), eq (EMPTY_RPC_OPTIONS ));
1681
+ verify (bigqueryRpcMock )
1682
+ .getJob (any (String .class ), eq (id ), eq ((String ) null ), eq (withStatisticOption ));
1683
+ }
1684
+
1677
1685
@ Test
1678
1686
public void testCreateJobWithProjectId () {
1679
1687
JobInfo jobInfo =
@@ -1925,7 +1933,7 @@ public void testQueryRequestCompleted() throws InterruptedException {
1925
1933
JOB_INFO .toPb (), Collections .<BigQueryRpc .Option , Object >emptyMap ()))
1926
1934
.thenReturn (jobResponsePb );
1927
1935
when (bigqueryRpcMock .getQueryResults (
1928
- PROJECT , JOB , null , BigQueryImpl . optionMap (Job .DEFAULT_QUERY_WAIT_OPTIONS )))
1936
+ PROJECT , JOB , null , optionMap (Job .DEFAULT_QUERY_WAIT_OPTIONS )))
1929
1937
.thenReturn (responsePb );
1930
1938
when (bigqueryRpcMock .listTableData (
1931
1939
PROJECT , DATASET , TABLE , Collections .<BigQueryRpc .Option , Object >emptyMap ()))
@@ -1946,8 +1954,7 @@ public void testQueryRequestCompleted() throws InterruptedException {
1946
1954
verify (bigqueryRpcMock )
1947
1955
.create (JOB_INFO .toPb (), Collections .<BigQueryRpc .Option , Object >emptyMap ());
1948
1956
verify (bigqueryRpcMock )
1949
- .getQueryResults (
1950
- PROJECT , JOB , null , BigQueryImpl .optionMap (Job .DEFAULT_QUERY_WAIT_OPTIONS ));
1957
+ .getQueryResults (PROJECT , JOB , null , optionMap (Job .DEFAULT_QUERY_WAIT_OPTIONS ));
1951
1958
1952
1959
verify (bigqueryRpcMock )
1953
1960
.listTableData (PROJECT , DATASET , TABLE , Collections .<BigQueryRpc .Option , Object >emptyMap ());
@@ -2003,10 +2010,7 @@ public void testFastQueryMultiplePages() throws InterruptedException {
2003
2010
responseJob .getConfiguration ().getQuery ().setDestinationTable (TABLE_ID .toPb ());
2004
2011
when (bigqueryRpcMock .getJob (PROJECT , JOB , null , EMPTY_RPC_OPTIONS )).thenReturn (responseJob );
2005
2012
when (bigqueryRpcMock .listTableData (
2006
- PROJECT ,
2007
- DATASET ,
2008
- TABLE ,
2009
- BigQueryImpl .optionMap (BigQuery .TableDataListOption .pageToken (CURSOR ))))
2013
+ PROJECT , DATASET , TABLE , optionMap (BigQuery .TableDataListOption .pageToken (CURSOR ))))
2010
2014
.thenReturn (
2011
2015
new TableDataList ()
2012
2016
.setPageToken (CURSOR )
@@ -2044,10 +2048,7 @@ public void testFastQueryMultiplePages() throws InterruptedException {
2044
2048
verify (bigqueryRpcMock ).getJob (PROJECT , JOB , null , EMPTY_RPC_OPTIONS );
2045
2049
verify (bigqueryRpcMock )
2046
2050
.listTableData (
2047
- PROJECT ,
2048
- DATASET ,
2049
- TABLE ,
2050
- BigQueryImpl .optionMap (BigQuery .TableDataListOption .pageToken (CURSOR )));
2051
+ PROJECT , DATASET , TABLE , optionMap (BigQuery .TableDataListOption .pageToken (CURSOR )));
2051
2052
verify (bigqueryRpcMock ).queryRpc (eq (PROJECT ), requestPbCapture .capture ());
2052
2053
}
2053
2054
@@ -2084,7 +2085,7 @@ public void testFastQuerySlowDdl() throws InterruptedException {
2084
2085
responseJob .getConfiguration ().getQuery ().setDestinationTable (TABLE_ID .toPb ());
2085
2086
when (bigqueryRpcMock .getJob (PROJECT , JOB , null , EMPTY_RPC_OPTIONS )).thenReturn (responseJob );
2086
2087
when (bigqueryRpcMock .getQueryResults (
2087
- PROJECT , JOB , null , BigQueryImpl . optionMap (Job .DEFAULT_QUERY_WAIT_OPTIONS )))
2088
+ PROJECT , JOB , null , optionMap (Job .DEFAULT_QUERY_WAIT_OPTIONS )))
2088
2089
.thenReturn (queryResultsResponsePb );
2089
2090
when (bigqueryRpcMock .listTableData (PROJECT , DATASET , TABLE , EMPTY_RPC_OPTIONS ))
2090
2091
.thenReturn (new TableDataList ().setRows (ImmutableList .of (TABLE_ROW )).setTotalRows (1L ));
@@ -2108,8 +2109,7 @@ public void testFastQuerySlowDdl() throws InterruptedException {
2108
2109
verify (bigqueryRpcMock ).queryRpc (eq (PROJECT ), requestPbCapture .capture ());
2109
2110
verify (bigqueryRpcMock ).getJob (PROJECT , JOB , null , EMPTY_RPC_OPTIONS );
2110
2111
verify (bigqueryRpcMock )
2111
- .getQueryResults (
2112
- PROJECT , JOB , null , BigQueryImpl .optionMap (Job .DEFAULT_QUERY_WAIT_OPTIONS ));
2112
+ .getQueryResults (PROJECT , JOB , null , optionMap (Job .DEFAULT_QUERY_WAIT_OPTIONS ));
2113
2113
verify (bigqueryRpcMock ).listTableData (PROJECT , DATASET , TABLE , EMPTY_RPC_OPTIONS );
2114
2114
}
2115
2115
@@ -2143,7 +2143,7 @@ public void testQueryRequestCompletedOptions() throws InterruptedException {
2143
2143
optionMap .put (pageSizeOption .getRpcOption (), pageSizeOption .getValue ());
2144
2144
2145
2145
when (bigqueryRpcMock .getQueryResults (
2146
- PROJECT , JOB , null , BigQueryImpl . optionMap (Job .DEFAULT_QUERY_WAIT_OPTIONS )))
2146
+ PROJECT , JOB , null , optionMap (Job .DEFAULT_QUERY_WAIT_OPTIONS )))
2147
2147
.thenReturn (responsePb );
2148
2148
when (bigqueryRpcMock .listTableData (PROJECT , DATASET , TABLE , optionMap ))
2149
2149
.thenReturn (
@@ -2164,8 +2164,7 @@ public void testQueryRequestCompletedOptions() throws InterruptedException {
2164
2164
verify (bigqueryRpcMock )
2165
2165
.create (JOB_INFO .toPb (), Collections .<BigQueryRpc .Option , Object >emptyMap ());
2166
2166
verify (bigqueryRpcMock )
2167
- .getQueryResults (
2168
- PROJECT , JOB , null , BigQueryImpl .optionMap (Job .DEFAULT_QUERY_WAIT_OPTIONS ));
2167
+ .getQueryResults (PROJECT , JOB , null , optionMap (Job .DEFAULT_QUERY_WAIT_OPTIONS ));
2169
2168
verify (bigqueryRpcMock ).listTableData (PROJECT , DATASET , TABLE , optionMap );
2170
2169
}
2171
2170
@@ -2199,10 +2198,10 @@ public void testQueryRequestCompletedOnSecondAttempt() throws InterruptedExcepti
2199
2198
JOB_INFO .toPb (), Collections .<BigQueryRpc .Option , Object >emptyMap ()))
2200
2199
.thenReturn (jobResponsePb1 );
2201
2200
when (bigqueryRpcMock .getQueryResults (
2202
- PROJECT , JOB , null , BigQueryImpl . optionMap (Job .DEFAULT_QUERY_WAIT_OPTIONS )))
2201
+ PROJECT , JOB , null , optionMap (Job .DEFAULT_QUERY_WAIT_OPTIONS )))
2203
2202
.thenReturn (responsePb1 );
2204
2203
when (bigqueryRpcMock .getQueryResults (
2205
- PROJECT , JOB , null , BigQueryImpl . optionMap (Job .DEFAULT_QUERY_WAIT_OPTIONS )))
2204
+ PROJECT , JOB , null , optionMap (Job .DEFAULT_QUERY_WAIT_OPTIONS )))
2206
2205
.thenReturn (responsePb2 );
2207
2206
when (bigqueryRpcMock .listTableData (
2208
2207
PROJECT , DATASET , TABLE , Collections .<BigQueryRpc .Option , Object >emptyMap ()))
@@ -2223,11 +2222,9 @@ public void testQueryRequestCompletedOnSecondAttempt() throws InterruptedExcepti
2223
2222
verify (bigqueryRpcMock )
2224
2223
.create (JOB_INFO .toPb (), Collections .<BigQueryRpc .Option , Object >emptyMap ());
2225
2224
verify (bigqueryRpcMock )
2226
- .getQueryResults (
2227
- PROJECT , JOB , null , BigQueryImpl .optionMap (Job .DEFAULT_QUERY_WAIT_OPTIONS ));
2225
+ .getQueryResults (PROJECT , JOB , null , optionMap (Job .DEFAULT_QUERY_WAIT_OPTIONS ));
2228
2226
verify (bigqueryRpcMock )
2229
- .getQueryResults (
2230
- PROJECT , JOB , null , BigQueryImpl .optionMap (Job .DEFAULT_QUERY_WAIT_OPTIONS ));
2227
+ .getQueryResults (PROJECT , JOB , null , optionMap (Job .DEFAULT_QUERY_WAIT_OPTIONS ));
2231
2228
verify (bigqueryRpcMock )
2232
2229
.listTableData (PROJECT , DATASET , TABLE , Collections .<BigQueryRpc .Option , Object >emptyMap ());
2233
2230
}
0 commit comments