|
| 1 | +/* |
| 2 | + * Copyright 2023 Aiven Oy |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.aiven.kafka.connect.transforms; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import org.apache.kafka.connect.data.Schema; |
| 25 | +import org.apache.kafka.connect.data.SchemaBuilder; |
| 26 | +import org.apache.kafka.connect.data.Struct; |
| 27 | +import org.apache.kafka.connect.source.SourceRecord; |
| 28 | + |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | + |
| 31 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 32 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 33 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 34 | + |
| 35 | +class FilterByValueRegexTest { |
| 36 | + |
| 37 | + private static final Schema VALUE_SCHEMA = SchemaBuilder.struct() |
| 38 | + .field("before", Schema.OPTIONAL_STRING_SCHEMA) |
| 39 | + .field("after", SchemaBuilder.struct() |
| 40 | + .field("pk", Schema.STRING_SCHEMA) |
| 41 | + .field("value", Schema.STRING_SCHEMA) |
| 42 | + .build()) |
| 43 | + .field("source", SchemaBuilder.struct().optional()) |
| 44 | + .field("op", Schema.STRING_SCHEMA) |
| 45 | + .field("ts_ms", Schema.STRING_SCHEMA) |
| 46 | + .field("transaction", Schema.OPTIONAL_STRING_SCHEMA) |
| 47 | + .build(); |
| 48 | + |
| 49 | + @Test |
| 50 | + void shouldFilterOutRecordsEqualsToReadEvents() { |
| 51 | + final FilterByValueRegex<SourceRecord> filter = new FilterByValueRegex<>(); |
| 52 | + filter.configure(Map.of( |
| 53 | + "fieldName", "op", |
| 54 | + "pattern", "r", |
| 55 | + "matches", "false" |
| 56 | + )); |
| 57 | + |
| 58 | + final Struct after = new Struct(VALUE_SCHEMA.field("after").schema()) |
| 59 | + .put("pk", "1") |
| 60 | + .put("value", "New data"); |
| 61 | + |
| 62 | + final Struct value = new Struct(VALUE_SCHEMA) |
| 63 | + .put("before", null) |
| 64 | + .put("after", after) |
| 65 | + .put("source", null) |
| 66 | + .put("op", "r") |
| 67 | + .put("ts_ms", "1620393591654") |
| 68 | + .put("transaction", null); |
| 69 | + |
| 70 | + final var record = new SourceRecord(null, null, "some_topic", VALUE_SCHEMA, value); |
| 71 | + |
| 72 | + final var actual = filter.apply(record); |
| 73 | + assertNull(actual, "Record with op 'r' should be filtered out"); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void shouldKeepRecordsNotEqualsToReadEvents() { |
| 78 | + final FilterByValueRegex<SourceRecord> filter = new FilterByValueRegex<>(); |
| 79 | + filter.configure(Map.of( |
| 80 | + "fieldName", "op", |
| 81 | + "pattern", "r", |
| 82 | + "matches", "false" |
| 83 | + )); |
| 84 | + |
| 85 | + final Struct after = new Struct(VALUE_SCHEMA.field("after").schema()) |
| 86 | + .put("pk", "1") |
| 87 | + .put("value", "New data"); |
| 88 | + |
| 89 | + final Struct value = new Struct(VALUE_SCHEMA) |
| 90 | + .put("before", null) |
| 91 | + .put("after", after) |
| 92 | + .put("source", null) |
| 93 | + .put("op", "u") |
| 94 | + .put("ts_ms", "1620393591654") |
| 95 | + .put("transaction", null); |
| 96 | + |
| 97 | + final var record = new SourceRecord(null, null, "some_topic", VALUE_SCHEMA, value); |
| 98 | + |
| 99 | + final var actual = filter.apply(record); |
| 100 | + assertEquals(record, actual, "Record with op not equal to 'r' should be kept"); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void shouldFilterOutRecordsNotEqualsReadEvents() { |
| 105 | + final FilterByValueRegex<SourceRecord> filter = new FilterByValueRegex<>(); |
| 106 | + filter.configure(Map.of( |
| 107 | + "fieldName", "op", |
| 108 | + "pattern", "r", |
| 109 | + "matches", "true" |
| 110 | + )); |
| 111 | + |
| 112 | + final Struct after = new Struct(VALUE_SCHEMA.field("after").schema()) |
| 113 | + .put("pk", "1") |
| 114 | + .put("value", "New data"); |
| 115 | + |
| 116 | + final Struct value = new Struct(VALUE_SCHEMA) |
| 117 | + .put("before", null) |
| 118 | + .put("after", after) |
| 119 | + .put("source", null) |
| 120 | + .put("op", "u") |
| 121 | + .put("ts_ms", "1620393591654") |
| 122 | + .put("transaction", null); |
| 123 | + |
| 124 | + final var record = new SourceRecord(null, null, "some_topic", VALUE_SCHEMA, value); |
| 125 | + |
| 126 | + final var actual = filter.apply(record); |
| 127 | + assertNull(actual, "Record with op not equal to 'r' should be filtered out"); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + void shouldFilterMatchingArrayFieldValue() { |
| 132 | + final FilterByValueRegex<SourceRecord> filterByValueRegex = new FilterByValueRegex<>(); |
| 133 | + final Map<String, String> configs = new HashMap<>(); |
| 134 | + configs.put("fieldName", "tags"); |
| 135 | + configs.put("pattern", ".*apple.*"); |
| 136 | + configs.put("matches", "true"); |
| 137 | + filterByValueRegex.configure(configs); |
| 138 | + |
| 139 | + final Schema schema = SchemaBuilder.struct() |
| 140 | + .field("name", Schema.STRING_SCHEMA) |
| 141 | + .field("tags", SchemaBuilder.array(Schema.STRING_SCHEMA)) |
| 142 | + .build(); |
| 143 | + final List<String> tags = Arrays.asList("apple", "orange", "mango"); |
| 144 | + final Struct value = new Struct(schema) |
| 145 | + .put("name", "John Doe") |
| 146 | + .put("tags", tags); |
| 147 | + |
| 148 | + final var record = new SourceRecord(null, null, "some_topic", schema, value); |
| 149 | + |
| 150 | + final var actual = filterByValueRegex.apply(record); |
| 151 | + assertEquals(record, actual, "The record contains the matching pattern"); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + void shouldFilterOutMapFieldValue() { |
| 156 | + final FilterByValueRegex<SourceRecord> filterByValueRegex = new FilterByValueRegex<>(); |
| 157 | + final Map<String, String> configs = new HashMap<>(); |
| 158 | + configs.put("fieldName", "language"); |
| 159 | + configs.put("pattern", ".*Java.*"); |
| 160 | + configs.put("matches", "false"); |
| 161 | + filterByValueRegex.configure(configs); |
| 162 | + |
| 163 | + final Map<String, Object> value = new HashMap<>(); |
| 164 | + value.put("name", "John Doe"); |
| 165 | + value.put("language", "Java"); |
| 166 | + |
| 167 | + final var record = new SourceRecord(null, null, "some_topic", Schema.STRING_SCHEMA, value); |
| 168 | + |
| 169 | + final var actual = filterByValueRegex.apply(record); |
| 170 | + assertNull(actual, "The record should be filtered out"); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + void shouldFilterArrayFieldValue() { |
| 175 | + final FilterByValueRegex<SourceRecord> filterByValueRegex = new FilterByValueRegex<>(); |
| 176 | + final Map<String, String> configs = new HashMap<>(); |
| 177 | + configs.put("fieldName", "tags"); |
| 178 | + configs.put("pattern", ".*apple.*"); |
| 179 | + configs.put("matches", "true"); |
| 180 | + filterByValueRegex.configure(configs); |
| 181 | + |
| 182 | + // Test with a list |
| 183 | + final List<String> tagsList = Arrays.asList("apple", "orange", "mango"); |
| 184 | + final var listRecord = new SourceRecord(null, null, "some_topic", null, tagsList); |
| 185 | + final var listActual = filterByValueRegex.apply(listRecord); |
| 186 | + assertNotNull(listActual, "The record should not be filtered out and return the record itself"); |
| 187 | + |
| 188 | + // Test with an array |
| 189 | + final String[] tagsArray = {"apple", "orange", "mango"}; |
| 190 | + final var arrayRecord = new SourceRecord(null, null, "some_topic", null, tagsArray); |
| 191 | + final var arrayActual = filterByValueRegex.apply(arrayRecord); |
| 192 | + assertNotNull(arrayActual, "The record should not be filtered out and return the record itself"); |
| 193 | + } |
| 194 | +} |
0 commit comments