1
+ /*
2
+ * Copyright 2023 StreamThoughts.
3
+ *
4
+ * Licensed to the Apache Software Foundation (ASF) under one or more
5
+ * contributor license agreements. See the NOTICE file distributed with
6
+ * this work for additional information regarding copyright ownership.
7
+ * The ASF licenses this file to You under the Apache License, Version 2.0
8
+ * (the "License"); you may not use this file except in compliance with
9
+ * the License. You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+ package io .streamthoughts .kafka .connect .filepulse .filter ;
20
+
21
+ import static io .streamthoughts .kafka .connect .filepulse .config .ExcludeFieldsMatchingPatternsConfig .EXCLUDE_FIELDS_BLOCK_FIELD_CONFIG ;
22
+ import static io .streamthoughts .kafka .connect .filepulse .config .ExcludeFieldsMatchingPatternsConfig .EXCLUDE_FIELDS_REGEX_CONFIG ;
23
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
24
+ import static org .junit .jupiter .api .Assertions .assertFalse ;
25
+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
26
+ import static org .junit .jupiter .api .Assertions .assertNull ;
27
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
28
+ import static org .mockito .Mockito .mock ;
29
+
30
+ import io .streamthoughts .kafka .connect .filepulse .data .TypedStruct ;
31
+ import io .streamthoughts .kafka .connect .filepulse .reader .RecordsIterable ;
32
+ import java .util .Map ;
33
+ import org .junit .jupiter .api .Test ;
34
+
35
+ class ExcludeFieldsMatchingPatternsFilterTest {
36
+
37
+ @ Test
38
+ void when_record_null_apply_should_return_empty_iterable () {
39
+ FilterContext context = mock (FilterContext .class );
40
+ ExcludeFieldsMatchingPatternsFilter filter = new ExcludeFieldsMatchingPatternsFilter ();
41
+ filter .configure (Map .of (EXCLUDE_FIELDS_REGEX_CONFIG , "[a-z]" ));
42
+
43
+ RecordsIterable <TypedStruct > iterable = filter .apply (context , null , false );
44
+ assertNotNull (iterable );
45
+ assertEquals (0 , iterable .size ());
46
+ }
47
+
48
+ @ Test
49
+ void when_record_apply_should_propagate_null_for_fields_matching_regex_and_propagate_all_other_fields () {
50
+ FilterContext context = mock (FilterContext .class );
51
+ ExcludeFieldsMatchingPatternsFilter filter = new ExcludeFieldsMatchingPatternsFilter ();
52
+ filter .configure (Map .of (EXCLUDE_FIELDS_REGEX_CONFIG , Fixture .nullFieldRegex ));
53
+
54
+ TypedStruct typedStruct = TypedStruct .create ()
55
+ .put (Fixture .fieldA , Fixture .fieldAValue )
56
+ .put (Fixture .fieldB , Fixture .fieldBValue )
57
+ .put (Fixture .fieldC , Fixture .fieldCValue )
58
+ .put (Fixture .fieldD , Fixture .fieldDValue );
59
+
60
+ RecordsIterable <TypedStruct > res = filter .apply (context , typedStruct , false );
61
+ assertEquals (1 , res .size ());
62
+
63
+ TypedStruct record = res .last ();
64
+
65
+ assertTrue (record .has (Fixture .fieldA ));
66
+ assertTrue (record .has (Fixture .fieldB ));
67
+ assertTrue (record .has (Fixture .fieldC ));
68
+ assertTrue (record .has (Fixture .fieldD ));
69
+
70
+ assertEquals (Fixture .fieldAValue , record .get (Fixture .fieldA ).getString ());
71
+ assertNull (record .get (Fixture .fieldB ).getString ());
72
+ assertEquals (Fixture .fieldCValue , record .get (Fixture .fieldC ).getString ());
73
+ assertNull (record .get (Fixture .fieldD ).getString ());
74
+
75
+ }
76
+
77
+ @ Test
78
+ void when_record_apply_should_block_fields_matching_regex_and_propagate_all_other_fields () {
79
+ FilterContext context = mock (FilterContext .class );
80
+ ExcludeFieldsMatchingPatternsFilter filter = new ExcludeFieldsMatchingPatternsFilter ();
81
+ filter .configure (Map .of (EXCLUDE_FIELDS_REGEX_CONFIG , Fixture .nullFieldRegex ,
82
+ EXCLUDE_FIELDS_BLOCK_FIELD_CONFIG , "true" ));
83
+
84
+ TypedStruct typedStruct = TypedStruct .create ()
85
+ .put (Fixture .fieldA , Fixture .fieldAValue )
86
+ .put (Fixture .fieldB , Fixture .fieldBValue )
87
+ .put (Fixture .fieldC , Fixture .fieldCValue )
88
+ .put (Fixture .fieldD , Fixture .fieldDValue );
89
+
90
+ RecordsIterable <TypedStruct > res = filter .apply (context , typedStruct , false );
91
+ assertEquals (1 , res .size ());
92
+
93
+ TypedStruct record = res .last ();
94
+
95
+ assertTrue (record .has (Fixture .fieldA ));
96
+ assertFalse (record .has (Fixture .fieldB ));
97
+ assertTrue (record .has (Fixture .fieldC ));
98
+ assertFalse (record .has (Fixture .fieldD ));
99
+
100
+ assertEquals (Fixture .fieldAValue , record .get (Fixture .fieldA ).getString ());
101
+ assertEquals (Fixture .fieldCValue , record .get (Fixture .fieldC ).getString ());
102
+ }
103
+
104
+ interface Fixture {
105
+ String nullFieldRegex = "null" ;
106
+
107
+ String fieldA = "fieldA" ;
108
+ String fieldB = "fieldB" ;
109
+ String fieldC = "fieldC" ;
110
+
111
+ String fieldD = "fieldD" ;
112
+
113
+ String fieldAValue = "2021-01-010 14:12" ;
114
+ String fieldBValue = "null" ;
115
+ String fieldCValue = "Hello" ;
116
+
117
+ String fieldDValue = null ;
118
+
119
+ }
120
+ }
0 commit comments