|
18 | 18 |
|
19 | 19 | import java.util.HashMap; |
20 | 20 | import java.util.Map; |
| 21 | +import java.util.function.Consumer; |
| 22 | +import java.util.function.Supplier; |
21 | 23 |
|
22 | 24 | import org.apache.kafka.connect.data.Schema; |
23 | 25 | import org.apache.kafka.connect.data.SchemaBuilder; |
|
26 | 28 |
|
27 | 29 | import org.junit.jupiter.api.Test; |
28 | 30 |
|
29 | | -import static org.junit.jupiter.api.Assertions.assertEquals; |
30 | | -import static org.junit.jupiter.api.Assertions.assertNull; |
| 31 | +import static org.junit.jupiter.api.Assertions.*; |
31 | 32 |
|
32 | 33 | class FilterByFieldValueTest { |
33 | 34 |
|
34 | | - private static final Schema VALUE_SCHEMA = SchemaBuilder.struct() |
35 | | - .field("before", Schema.OPTIONAL_STRING_SCHEMA) |
36 | | - .field("after", SchemaBuilder.struct() |
37 | | - .field("pk", Schema.STRING_SCHEMA) |
38 | | - .field("value", Schema.STRING_SCHEMA) |
39 | | - .build()) |
40 | | - .field("source", SchemaBuilder.struct().optional()) |
41 | | - .field("op", Schema.STRING_SCHEMA) |
42 | | - .field("ts_ms", Schema.STRING_SCHEMA) |
43 | | - .field("transaction", Schema.OPTIONAL_STRING_SCHEMA) |
44 | | - .build(); |
45 | 35 |
|
46 | 36 | @Test |
47 | | - void shouldFilterOutRecordsEqualsToReadEvents() { |
| 37 | + void shouldFilterOutValueRecordsEqualsToReadEvents() { |
48 | 38 | final FilterByFieldValue<SourceRecord> filter = new FilterByFieldValue.Value<>(); |
49 | 39 | filter.configure(Map.of( |
50 | 40 | "field.name", "op", |
51 | 41 | "field.value", "r", |
52 | 42 | "field.value.matches", "false" |
53 | 43 | )); |
54 | 44 |
|
55 | | - final Struct after = new Struct(VALUE_SCHEMA.field("after").schema()) |
56 | | - .put("pk", "1") |
57 | | - .put("value", "New data"); |
58 | | - |
59 | | - final Struct value = new Struct(VALUE_SCHEMA) |
60 | | - .put("before", null) |
61 | | - .put("after", after) |
62 | | - .put("source", null) |
63 | | - .put("op", "r") |
64 | | - .put("ts_ms", "1620393591654") |
65 | | - .put("transaction", null); |
66 | | - |
67 | | - final var record = new SourceRecord(null, null, "some_topic", VALUE_SCHEMA, value); |
68 | | - |
69 | | - final var actual = filter.apply(record); |
70 | | - assertNull(actual, "Record with op 'r' should be filtered out"); |
| 45 | + assertNull(filter.apply( |
| 46 | + prepareStructRecord( |
| 47 | + struct -> { |
| 48 | + }, |
| 49 | + struct -> struct.put("op", "r") |
| 50 | + )), |
| 51 | + "Record with op 'r' should be filtered out"); |
| 52 | + SourceRecord record = prepareStructRecord( |
| 53 | + struct -> { |
| 54 | + }, |
| 55 | + struct -> struct.put("op", "u") |
| 56 | + ); |
| 57 | + assertEquals(record, filter.apply(record), |
| 58 | + "Record with op not 'r' should be not filtered out"); |
71 | 59 | } |
72 | 60 |
|
73 | 61 | @Test |
74 | | - void shouldKeepRecordsNotEqualsToReadEvents() { |
75 | | - final FilterByFieldValue<SourceRecord> filter = new FilterByFieldValue.Value<>(); |
| 62 | + void shouldFilterOutKeyRecordsEqualsToId() { |
| 63 | + final FilterByFieldValue<SourceRecord> filter = new FilterByFieldValue.Key<>(); |
76 | 64 | filter.configure(Map.of( |
77 | | - "field.name", "op", |
78 | | - "field.value", "r", |
| 65 | + "field.name", "id", |
| 66 | + "field.value", "123", |
79 | 67 | "field.value.matches", "false" |
80 | 68 | )); |
81 | 69 |
|
82 | | - final Struct after = new Struct(VALUE_SCHEMA.field("after").schema()) |
83 | | - .put("pk", "1") |
84 | | - .put("value", "New data"); |
85 | | - |
86 | | - final Struct value = new Struct(VALUE_SCHEMA) |
87 | | - .put("before", null) |
88 | | - .put("after", after) |
89 | | - .put("source", null) |
90 | | - .put("op", "u") |
91 | | - .put("ts_ms", "1620393591654") |
92 | | - .put("transaction", null); |
93 | | - |
94 | | - final var record = new SourceRecord(null, null, "some_topic", VALUE_SCHEMA, value); |
95 | | - |
96 | | - final var actual = filter.apply(record); |
97 | | - assertEquals(record, actual, "Record with op not equal to 'r' should be kept"); |
| 70 | + assertNull(filter.apply(prepareStructRecord( |
| 71 | + struct -> struct.put("id", "123"), |
| 72 | + struct -> { |
| 73 | + })), "Record with id '123' should be filtered out"); |
| 74 | + SourceRecord record = prepareStructRecord( |
| 75 | + struct -> struct.put("id", "111"), |
| 76 | + struct -> { |
| 77 | + }); |
| 78 | + assertEquals(record, filter.apply(record), "Record with id not '123' should not be filtered out"); |
98 | 79 | } |
99 | 80 |
|
100 | 81 | @Test |
101 | | - void shouldFilterOutRecordsNotEqualsReadEvents() { |
| 82 | + void shouldFilterOutValueRecordsNotEqualsReadEvents() { |
102 | 83 | final FilterByFieldValue<SourceRecord> filter = new FilterByFieldValue.Value<>(); |
103 | 84 | filter.configure(Map.of( |
104 | 85 | "field.name", "op", |
105 | 86 | "field.value", "r", |
106 | 87 | "field.value.matches", "true" |
107 | 88 | )); |
108 | 89 |
|
109 | | - final Struct after = new Struct(VALUE_SCHEMA.field("after").schema()) |
110 | | - .put("pk", "1") |
111 | | - .put("value", "New data"); |
112 | | - |
113 | | - final Struct value = new Struct(VALUE_SCHEMA) |
114 | | - .put("before", null) |
115 | | - .put("after", after) |
116 | | - .put("source", null) |
117 | | - .put("op", "u") |
118 | | - .put("ts_ms", "1620393591654") |
119 | | - .put("transaction", null); |
| 90 | + assertNull(filter.apply(prepareStructRecord( |
| 91 | + struct -> { |
| 92 | + }, |
| 93 | + struct -> struct.put("op", "u") |
| 94 | + )), |
| 95 | + "Record with op not equal to 'r' should be filtered out"); |
| 96 | + SourceRecord record = prepareStructRecord( |
| 97 | + struct -> { |
| 98 | + }, |
| 99 | + struct -> struct.put("op", "r") |
| 100 | + ); |
| 101 | + assertEquals(record, filter.apply(record), "Record with op equal to 'r' should not be filtered out"); |
| 102 | + } |
120 | 103 |
|
121 | | - final var record = new SourceRecord(null, null, "some_topic", VALUE_SCHEMA, value); |
| 104 | + @Test |
| 105 | + void shouldFilterOutKeyRecordsNotEqualsToId() { |
| 106 | + final FilterByFieldValue<SourceRecord> filter = new FilterByFieldValue.Key<>(); |
| 107 | + filter.configure(Map.of( |
| 108 | + "field.name", "id", |
| 109 | + "field.value", "123", |
| 110 | + "field.value.matches", "true" |
| 111 | + )); |
122 | 112 |
|
123 | | - final var actual = filter.apply(record); |
124 | | - assertNull(actual, "Record with op not equal to 'r' should be filtered out"); |
| 113 | + assertNull(filter.apply(prepareStructRecord( |
| 114 | + struct -> struct.put("id", "111"), |
| 115 | + struct -> { |
| 116 | + } |
| 117 | + )), "Record with id not equal to '123' should be filtered out"); |
| 118 | + SourceRecord record = prepareStructRecord( |
| 119 | + struct -> struct.put("id", "123"), |
| 120 | + struct -> { |
| 121 | + } |
| 122 | + ); |
| 123 | + assertEquals(record, filter.apply(record), "Record with id equal to '123' should not be filtered out"); |
125 | 124 | } |
126 | 125 |
|
127 | 126 | @Test |
128 | | - void shouldFilterOutMapFieldValue() { |
| 127 | + void shouldFilterOutMapValueRecordsWithRegex() { |
129 | 128 | final FilterByFieldValue<SourceRecord> filterByFieldValue = new FilterByFieldValue.Value<>(); |
130 | 129 | final Map<String, String> configs = new HashMap<>(); |
131 | 130 | configs.put("field.name", "language"); |
132 | 131 | configs.put("field.value.pattern", ".*Java.*"); |
133 | 132 | configs.put("field.value.matches", "false"); |
134 | 133 | filterByFieldValue.configure(configs); |
135 | 134 |
|
136 | | - final Map<String, Object> value = new HashMap<>(); |
137 | | - value.put("name", "John Doe"); |
138 | | - value.put("language", "Java"); |
| 135 | + assertNull(filterByFieldValue.apply(prepareRecord(() -> "42", () -> Map.of("language", "Javascript"))), "The record should be filtered out"); |
| 136 | + SourceRecord record = prepareRecord(() -> "42", () -> Map.of("language", "Rust")); |
| 137 | + assertEquals(record, filterByFieldValue.apply(record), "The record should not be filtered out"); |
| 138 | + } |
139 | 139 |
|
140 | | - final var record = new SourceRecord(null, null, "some_topic", null, value); |
| 140 | + @Test |
| 141 | + void shouldFilterOutMapKeyRecordsWithRegex() { |
| 142 | + final FilterByFieldValue<SourceRecord> filterByFieldValue = new FilterByFieldValue.Key<>(); |
| 143 | + final Map<String, String> configs = new HashMap<>(); |
| 144 | + configs.put("field.name", "language"); |
| 145 | + configs.put("field.value.pattern", ".*Java.*"); |
| 146 | + configs.put("field.value.matches", "false"); |
| 147 | + filterByFieldValue.configure(configs); |
| 148 | + |
| 149 | + assertNull(filterByFieldValue.apply(prepareRecord(() -> Map.of("language", "Javascript"), () -> "42")), "The record should be filtered out"); |
| 150 | + SourceRecord record = prepareRecord(() -> Map.of("language", "Rust"), () -> "42"); |
| 151 | + assertEquals(record, filterByFieldValue.apply(record), "The record should not be filtered out"); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + void shouldFilterOutRawKeyRecords() { |
| 156 | + final FilterByFieldValue<SourceRecord> filterByFieldValue = new FilterByFieldValue.Key<>(); |
| 157 | + final Map<String, String> configs = new HashMap<>(); |
| 158 | + configs.put("field.value", "42"); |
| 159 | + configs.put("field.value.matches", "false"); |
| 160 | + filterByFieldValue.configure(configs); |
| 161 | + |
| 162 | + assertNull(filterByFieldValue.apply(prepareRecord(() -> "42", () -> Map.of("language", "Javascript"))), "The record should be filtered out"); |
| 163 | + SourceRecord record = prepareRecord(() -> "43", () -> Map.of("language", "Rust")); |
| 164 | + assertEquals(record, filterByFieldValue.apply(record), "The record should be filtered out"); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + void shouldFilterOutRawValueRecords() { |
| 169 | + final FilterByFieldValue<SourceRecord> filterByFieldValue = new FilterByFieldValue.Value<>(); |
| 170 | + final Map<String, String> configs = new HashMap<>(); |
| 171 | + configs.put("field.value", "42"); |
| 172 | + configs.put("field.value.matches", "false"); |
| 173 | + filterByFieldValue.configure(configs); |
| 174 | + |
| 175 | + assertNull(filterByFieldValue.apply(prepareRecord(() -> Map.of("language", "Javascript"), () -> "42")), "The record should be filtered out"); |
| 176 | + SourceRecord record = prepareRecord(() -> Map.of("language", "Rust"), () -> "43"); |
| 177 | + assertEquals(record, filterByFieldValue.apply(record), "The record should be filtered out"); |
| 178 | + } |
| 179 | + |
| 180 | + private SourceRecord prepareRecord(Supplier<Object> keySupplier, Supplier<Object> valueSupplier) { |
| 181 | + return new SourceRecord(null, null, "some_topic", |
| 182 | + null, keySupplier.get(), |
| 183 | + null, valueSupplier.get()); |
| 184 | + } |
| 185 | + |
| 186 | + private SourceRecord prepareStructRecord(Consumer<Struct> keyChanges, Consumer<Struct> valueChanges) { |
| 187 | + final Schema KEY_VALUE = SchemaBuilder.struct() |
| 188 | + .field("id", Schema.STRING_SCHEMA) |
| 189 | + .build(); |
| 190 | + final Schema VALUE_SCHEMA = SchemaBuilder.struct() |
| 191 | + .field("before", Schema.OPTIONAL_STRING_SCHEMA) |
| 192 | + .field("after", SchemaBuilder.struct() |
| 193 | + .field("pk", Schema.STRING_SCHEMA) |
| 194 | + .field("value", Schema.STRING_SCHEMA) |
| 195 | + .build()) |
| 196 | + .field("source", SchemaBuilder.struct().optional()) |
| 197 | + .field("op", Schema.STRING_SCHEMA) |
| 198 | + .field("ts_ms", Schema.STRING_SCHEMA) |
| 199 | + .field("transaction", Schema.OPTIONAL_STRING_SCHEMA) |
| 200 | + .build(); |
| 201 | + |
| 202 | + final Struct key = new Struct(KEY_VALUE) |
| 203 | + .put("id", "123"); |
| 204 | + keyChanges.accept(key); |
| 205 | + |
| 206 | + final Struct after = new Struct(VALUE_SCHEMA.field("after").schema()) |
| 207 | + .put("pk", "1") |
| 208 | + .put("value", "New data"); |
| 209 | + |
| 210 | + final Struct value = new Struct(VALUE_SCHEMA) |
| 211 | + .put("before", null) |
| 212 | + .put("after", after) |
| 213 | + .put("source", null) |
| 214 | + .put("op", "u") |
| 215 | + .put("ts_ms", "1620393591654") |
| 216 | + .put("transaction", null); |
| 217 | + valueChanges.accept(value); |
141 | 218 |
|
142 | | - final var actual = filterByFieldValue.apply(record); |
143 | | - assertNull(actual, "The record should be filtered out"); |
| 219 | + return new SourceRecord( |
| 220 | + null, null, "some_topic", |
| 221 | + KEY_VALUE, key, |
| 222 | + VALUE_SCHEMA, value |
| 223 | + ); |
144 | 224 | } |
145 | 225 | } |
0 commit comments