15
15
16
16
package io .confluent .connect .s3 .integration ;
17
17
18
- import static io .confluent .connect .s3 .S3SinkConnectorConfig .AWS_ACCESS_KEY_ID_CONFIG ;
19
- import static io .confluent .connect .s3 .S3SinkConnectorConfig .AWS_SECRET_ACCESS_KEY_CONFIG ;
20
- import static io .confluent .kafka .schemaregistry .ClusterTestHarness .KAFKASTORE_TOPIC ;
21
-
22
18
import com .amazonaws .auth .AWSStaticCredentialsProvider ;
23
19
import com .amazonaws .auth .BasicAWSCredentials ;
24
20
import com .amazonaws .services .s3 .AmazonS3 ;
29
25
import com .amazonaws .services .s3 .model .S3ObjectSummary ;
30
26
import com .fasterxml .jackson .databind .JsonNode ;
31
27
import com .fasterxml .jackson .databind .ObjectMapper ;
28
+ import com .fasterxml .jackson .databind .node .NullNode ;
32
29
import com .google .common .collect .ImmutableMap ;
33
- import io .confluent .common .utils .IntegrationTest ;
34
- import io .confluent .kafka .schemaregistry .CompatibilityLevel ;
35
- import io .confluent .kafka .schemaregistry .RestApp ;
36
- import java .io .BufferedReader ;
37
- import java .io .File ;
38
- import java .io .FileReader ;
39
- import java .io .IOException ;
40
- import java .net .ServerSocket ;
41
- import java .util .ArrayList ;
42
- import java .util .Arrays ;
43
- import java .util .Date ;
44
- import java .util .HashMap ;
45
- import java .util .List ;
46
- import java .util .Map ;
47
- import java .util .Properties ;
48
- import java .util .concurrent .TimeUnit ;
49
-
50
- import io .confluent .connect .s3 .util .S3Utils ;
51
- import java .util .function .Function ;
52
- import java .util .stream .Collectors ;
53
30
import org .apache .avro .file .DataFileReader ;
54
31
import org .apache .avro .generic .GenericDatumReader ;
55
32
import org .apache .avro .generic .GenericRecord ;
79
56
import org .slf4j .Logger ;
80
57
import org .slf4j .LoggerFactory ;
81
58
59
+ import java .io .BufferedReader ;
60
+ import java .io .File ;
61
+ import java .io .FileReader ;
62
+ import java .io .IOException ;
63
+ import java .net .ServerSocket ;
64
+ import java .util .ArrayList ;
65
+ import java .util .Arrays ;
66
+ import java .util .HashMap ;
67
+ import java .util .List ;
68
+ import java .util .Map ;
69
+ import java .util .Objects ;
70
+ import java .util .Properties ;
71
+ import java .util .concurrent .TimeUnit ;
72
+ import java .util .function .Function ;
73
+ import java .util .stream .Collectors ;
74
+
75
+ import io .confluent .common .utils .IntegrationTest ;
76
+ import io .confluent .connect .s3 .util .S3Utils ;
77
+ import io .confluent .kafka .schemaregistry .CompatibilityLevel ;
78
+ import io .confluent .kafka .schemaregistry .RestApp ;
79
+
80
+ import static io .confluent .connect .s3 .S3SinkConnectorConfig .AWS_ACCESS_KEY_ID_CONFIG ;
81
+ import static io .confluent .connect .s3 .S3SinkConnectorConfig .AWS_SECRET_ACCESS_KEY_CONFIG ;
82
+ import static io .confluent .kafka .schemaregistry .ClusterTestHarness .KAFKASTORE_TOPIC ;
82
83
import static org .assertj .core .api .Assertions .assertThat ;
83
84
84
85
@ Category (IntegrationTest .class )
@@ -332,15 +333,15 @@ protected Schema getSampleStructSchema() {
332
333
.field ("myFloat32" , Schema .FLOAT32_SCHEMA )
333
334
.field ("myFloat64" , Schema .FLOAT64_SCHEMA )
334
335
.field ("myString" , Schema .STRING_SCHEMA )
336
+ .field ("withDefault" , SchemaBuilder .bool ().optional ().defaultValue (true ).build ())
335
337
.build ();
336
338
}
337
339
338
340
protected Struct getSampleStructVal (Schema structSchema ) {
339
- Date sampleDate = new Date (1111111 );
340
- sampleDate .setTime (0 );
341
341
return new Struct (structSchema )
342
342
.put ("ID" , (long ) 1 )
343
343
.put ("myBool" , true )
344
+ .put ("withDefault" , null )
344
345
.put ("myInt32" , 32 )
345
346
.put ("myFloat32" , 3.2f )
346
347
.put ("myFloat64" , 64.64 )
@@ -409,12 +410,15 @@ protected static void clearBucket(String bucketName) {
409
410
* @param bucketName the name of the s3 test bucket
410
411
* @param expectedRowsPerFile the number of rows a file should have
411
412
* @param expectedRow the expected row data in each file
413
+ * @param useDefaultValues
414
+ *
412
415
* @return whether every row of the files read equals the expected row
413
416
*/
414
417
protected boolean fileContentsAsExpected (
415
418
String bucketName ,
416
419
int expectedRowsPerFile ,
417
- Struct expectedRow
420
+ Struct expectedRow ,
421
+ boolean useDefaultValues
418
422
) {
419
423
log .info ("expectedRow: {}" , expectedRow );
420
424
for (String fileName :
@@ -427,7 +431,7 @@ protected boolean fileContentsAsExpected(
427
431
String fileExtension = getExtensionFromKey (fileName );
428
432
List <JsonNode > downloadedFileContents = contentGetters .get (fileExtension )
429
433
.apply (destinationPath );
430
- if (!fileContentsMatchExpected (downloadedFileContents , expectedRowsPerFile , expectedRow )) {
434
+ if (!fileContentsMatchExpected (downloadedFileContents , expectedRowsPerFile , expectedRow , useDefaultValues )) {
431
435
return false ;
432
436
}
433
437
downloadedFile .delete ();
@@ -481,20 +485,23 @@ protected boolean keyfileContentsAsExpected(
481
485
* @param fileContents the file contents as a list of JsonNodes
482
486
* @param expectedRowsPerFile the number of rows expected in the file
483
487
* @param expectedRow the expected values of each row
488
+ * @param useDefaultValues use default values from struct
489
+ *
484
490
* @return whether the file contents match the expected row
485
491
*/
486
492
protected boolean fileContentsMatchExpected (
487
493
List <JsonNode > fileContents ,
488
494
int expectedRowsPerFile ,
489
- Struct expectedRow
495
+ Struct expectedRow ,
496
+ boolean useDefaultValues
490
497
) {
491
498
if (fileContents .size () != expectedRowsPerFile ) {
492
499
log .error ("Number of rows in file do not match the expected count, actual: {}, expected: {}" ,
493
500
fileContents .size (), expectedRowsPerFile );
494
501
return false ;
495
502
}
496
503
for (JsonNode row : fileContents ) {
497
- if (!fileRowMatchesExpectedRow (row , expectedRow )) {
504
+ if (!fileRowMatchesExpectedRow (row , expectedRow , useDefaultValues )) {
498
505
return false ;
499
506
}
500
507
}
@@ -512,18 +519,34 @@ private List<String> getS3KeyFileList(List<S3ObjectSummary> summaries) {
512
519
/**
513
520
* Compare the row in the file and its values to the expected row's values.
514
521
*
515
- * @param fileRow the row read from the file as a JsonNode
516
- * @param expectedRow the expected contents of the row
522
+ * @param fileRow the row read from the file as a JsonNode
523
+ * @param expectedRow the expected contents of the row
524
+ * @param useDefaultValues
525
+ *
517
526
* @return whether the file row matches the expected row
518
527
*/
519
- private boolean fileRowMatchesExpectedRow (JsonNode fileRow , Struct expectedRow ) {
520
- log .debug ("Comparing rows: file: {}, expected: {}" , fileRow , expectedRow );
528
+ private boolean fileRowMatchesExpectedRow (JsonNode fileRow , Struct expectedRow , boolean useDefaultValues ) {
529
+ log .info ("Comparing rows: file: {}, expected: {}" , fileRow , expectedRow );
521
530
// compare the field values
522
531
for (Field key : expectedRow .schema ().fields ()) {
523
- String expectedValue = expectedRow .get (key ).toString ();
524
- String rowValue = fileRow .get (key .name ()).toString ().replaceAll ("^\" |\" $" , "" );
525
- log .debug ("Comparing values: {}, {}" , expectedValue , rowValue );
526
- if (!rowValue .equals (expectedValue )) {
532
+ String expectedValue = null ;
533
+ if (useDefaultValues ) {
534
+ expectedValue = expectedRow .get (key ).toString ();
535
+ } else {
536
+ Object withoutDefault = expectedRow .getWithoutDefault (key .name ());
537
+ if (withoutDefault != null ) {
538
+ expectedValue = withoutDefault .toString ();
539
+ }
540
+ }
541
+
542
+ JsonNode jsonValue = fileRow .get (key .name ());
543
+ String rowValue = null ;
544
+ if (!(jsonValue instanceof NullNode )) {
545
+ rowValue = jsonValue .toString ().replaceAll ("^\" |\" $" , "" );
546
+ }
547
+
548
+ log .info ("Comparing values: {}, {}, {}, {}" , key .name (), expectedValue , rowValue , Objects .equals (rowValue , expectedValue ));
549
+ if (!Objects .equals (rowValue , expectedValue )) {
527
550
return false ;
528
551
}
529
552
}
0 commit comments