17
17
18
18
package io .cdap .delta .bigquery ;
19
19
20
+ import com .google .api .gax .paging .Page ;
20
21
import com .google .auth .Credentials ;
21
22
import com .google .auth .oauth2 .GoogleCredentials ;
23
+ import com .google .cloud .PageImpl ;
22
24
import com .google .cloud .bigquery .BigQuery ;
23
25
import com .google .cloud .bigquery .BigQueryOptions ;
26
+ import com .google .cloud .bigquery .Dataset ;
24
27
import com .google .cloud .bigquery .DatasetId ;
25
28
import com .google .cloud .bigquery .DatasetInfo ;
26
29
import com .google .cloud .bigquery .Field ;
32
35
import com .google .cloud .bigquery .TableDefinition ;
33
36
import com .google .cloud .bigquery .TableId ;
34
37
import com .google .cloud .bigquery .TableInfo ;
38
+ import com .google .cloud .bigquery .TableResult ;
35
39
import com .google .common .base .Strings ;
36
40
import io .cdap .delta .api .SourceTable ;
37
41
import org .junit .AfterClass ;
52
56
import java .io .FileInputStream ;
53
57
import java .io .InputStream ;
54
58
import java .nio .charset .StandardCharsets ;
59
+ import java .util .ArrayList ;
60
+ import java .util .Arrays ;
55
61
import java .util .HashSet ;
62
+ import java .util .List ;
56
63
import java .util .Set ;
57
64
58
65
import static org .junit .Assert .assertEquals ;
@@ -69,19 +76,21 @@ public class BigQueryUtilsTest {
69
76
70
77
private static final String DATASET = "demodataset" ;
71
78
private static final String TABLE_PREFIX = "demotable_" ;
79
+ private static final String PROJECT = "testproject" ;
72
80
73
81
@ PrepareForTest (BigQueryUtils .class )
74
82
@ RunWith (PowerMockRunner .class )
75
83
public static class LocalIndependentTests {
76
84
private BigQuery bigQueryMock ;
77
- private Table tableMock ;
78
85
79
86
@ Before
80
87
public void init () throws Exception {
81
88
//Mocks
82
89
bigQueryMock = Mockito .mock (BigQuery .class );
83
- tableMock = Mockito .mock (Table .class );
90
+ Table tableMock = Mockito .mock (Table .class );
91
+ Dataset datasetMock = Mockito .mock (Dataset .class );
84
92
Mockito .when (bigQueryMock .getTable (ArgumentMatchers .any ())).thenReturn (tableMock );
93
+ Mockito .when (bigQueryMock .getDataset ("demodataset" )).thenReturn (datasetMock );
85
94
PowerMockito .spy (BigQueryUtils .class );
86
95
87
96
//Stubs
@@ -155,7 +164,8 @@ public void testNormalizeFieldName() {
155
164
public void testGetMaximumExistingSequenceNumberZeroInvocations () throws Exception {
156
165
// Zero Tables
157
166
Set <SourceTable > allTables = generateSourceTableSet (0 );
158
- long tableResult0 = BigQueryUtils .getMaximumExistingSequenceNumber (allTables , "testproject" ,
167
+ Mockito .when (bigQueryMock .listTables (ArgumentMatchers .anyString ())).thenReturn (generateBQTablesPage (0 ));
168
+ long tableResult0 = BigQueryUtils .getMaximumExistingSequenceNumber (allTables , PROJECT ,
159
169
null , bigQueryMock , null , 1000 );
160
170
assertEquals (0L , tableResult0 );
161
171
PowerMockito .verifyPrivate (BigQueryUtils .class , times (0 ))
@@ -169,7 +179,8 @@ public void testGetMaximumExistingSequenceNumberSingleInvocations() throws Excep
169
179
170
180
// Subtest : One Table
171
181
Set <SourceTable > allTables = generateSourceTableSet (1 );
172
- long tableResult = BigQueryUtils .getMaximumExistingSequenceNumber (allTables , "testproject" ,
182
+ Mockito .when (bigQueryMock .listTables (ArgumentMatchers .anyString ())).thenReturn (generateBQTablesPage (1 ));
183
+ long tableResult = BigQueryUtils .getMaximumExistingSequenceNumber (allTables , PROJECT ,
173
184
null , bigQueryMock , null , 1000 );
174
185
assertEquals (1L , tableResult );
175
186
PowerMockito .verifyPrivate (BigQueryUtils .class , times (1 ))
@@ -178,7 +189,8 @@ public void testGetMaximumExistingSequenceNumberSingleInvocations() throws Excep
178
189
179
190
// Subtest2 : Ten Tables
180
191
allTables = generateSourceTableSet (10 );
181
- tableResult = BigQueryUtils .getMaximumExistingSequenceNumber (allTables , "testproject" ,
192
+ Mockito .when (bigQueryMock .listTables (ArgumentMatchers .anyString ())).thenReturn (generateBQTablesPage (10 ));
193
+ tableResult = BigQueryUtils .getMaximumExistingSequenceNumber (allTables , PROJECT ,
182
194
null , bigQueryMock , null , 1000 );
183
195
assertEquals (2L , tableResult );
184
196
PowerMockito .verifyPrivate (BigQueryUtils .class , times (2 ))
@@ -187,7 +199,8 @@ public void testGetMaximumExistingSequenceNumberSingleInvocations() throws Excep
187
199
188
200
// Subtest3 : 1000 Tables
189
201
allTables = generateSourceTableSet (1000 );
190
- tableResult = BigQueryUtils .getMaximumExistingSequenceNumber (allTables , "testproject" ,
202
+ Mockito .when (bigQueryMock .listTables (ArgumentMatchers .anyString ())).thenReturn (generateBQTablesPage (1000 ));
203
+ tableResult = BigQueryUtils .getMaximumExistingSequenceNumber (allTables , PROJECT ,
191
204
null , bigQueryMock , null , 1000 );
192
205
assertEquals (3L , tableResult );
193
206
PowerMockito .verifyPrivate (BigQueryUtils .class , times (3 ))
@@ -201,7 +214,8 @@ public void testGetMaximumExistingSequenceNumberDoubleInvocations() throws Excep
201
214
202
215
//Subtest1 : 1001 Tables : Should call bigquery 2 times. 1000+1
203
216
Set <SourceTable > allTables = generateSourceTableSet (1001 );
204
- long tableResult = BigQueryUtils .getMaximumExistingSequenceNumber (allTables , "testproject" ,
217
+ Mockito .when (bigQueryMock .listTables (ArgumentMatchers .anyString ())).thenReturn (generateBQTablesPage (1001 ));
218
+ long tableResult = BigQueryUtils .getMaximumExistingSequenceNumber (allTables , PROJECT ,
205
219
null , bigQueryMock , null , 1000 );
206
220
assertEquals (2L , tableResult );
207
221
PowerMockito .verifyPrivate (BigQueryUtils .class , times (2 ))
@@ -210,7 +224,8 @@ public void testGetMaximumExistingSequenceNumberDoubleInvocations() throws Excep
210
224
211
225
//Subtest2 : 2000 Tables : Should call bigquery 2 times. 1000+1000
212
226
allTables = generateSourceTableSet (2000 );
213
- tableResult = BigQueryUtils .getMaximumExistingSequenceNumber (allTables , "testproject" ,
227
+ Mockito .when (bigQueryMock .listTables (ArgumentMatchers .anyString ())).thenReturn (generateBQTablesPage (2000 ));
228
+ tableResult = BigQueryUtils .getMaximumExistingSequenceNumber (allTables , PROJECT ,
214
229
null , bigQueryMock , null , 1000 );
215
230
assertEquals (4L , tableResult );
216
231
PowerMockito .verifyPrivate (BigQueryUtils .class , times (4 ))
@@ -224,7 +239,8 @@ public void testGetMaximumExistingSequenceNumberTripleInvocations() throws Excep
224
239
225
240
//Subtest1 : 2500 Tables : Should call bigquery 3 times. 1000+1000+500
226
241
Set <SourceTable > allTables = generateSourceTableSet (2500 );
227
- long tableResult = BigQueryUtils .getMaximumExistingSequenceNumber (allTables , "testproject" ,
242
+ Mockito .when (bigQueryMock .listTables (ArgumentMatchers .anyString ())).thenReturn (generateBQTablesPage (2500 ));
243
+ long tableResult = BigQueryUtils .getMaximumExistingSequenceNumber (allTables , PROJECT ,
228
244
null , bigQueryMock , null , 1000 );
229
245
assertEquals (3L , tableResult );
230
246
PowerMockito .verifyPrivate (BigQueryUtils .class , times (3 ))
@@ -350,4 +366,45 @@ private static Set<SourceTable> generateSourceTableSet(int noOfTables) {
350
366
}
351
367
return allTables ;
352
368
}
369
+
370
+ private static Page <Table > generateBQTablesPage (int num ) {
371
+ Page <Table > pg = new Page <Table >() {
372
+ @ Override
373
+ public boolean hasNextPage () {
374
+ return false ;
375
+ }
376
+
377
+ @ Override
378
+ public String getNextPageToken () {
379
+ return null ;
380
+ }
381
+
382
+ @ Override
383
+ public Page <Table > getNextPage () {
384
+ return null ;
385
+ }
386
+
387
+ @ Override
388
+ public Iterable <Table > iterateAll () {
389
+ List <Table > tableList = new ArrayList <>();
390
+
391
+ for (int i = 1 ; i <= num ; i ++) {
392
+ // Create Table
393
+ Table tableMock2 = Mockito .mock (Table .class );
394
+
395
+ String tableName = TABLE_PREFIX + i ;
396
+ TableId tableId = TableId .of (PROJECT , DATASET , tableName );
397
+ Mockito .when (tableMock2 .getTableId ()).thenReturn (tableId );
398
+ tableList .add (tableMock2 );
399
+ }
400
+ return tableList ;
401
+ }
402
+
403
+ @ Override
404
+ public Iterable <Table > getValues () {
405
+ return null ;
406
+ }
407
+ };
408
+ return pg ;
409
+ }
353
410
}
0 commit comments