|
| 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.Map; |
| 20 | + |
| 21 | +import org.apache.kafka.connect.data.Schema; |
| 22 | +import org.apache.kafka.connect.data.SchemaBuilder; |
| 23 | +import org.apache.kafka.connect.data.Struct; |
| 24 | +import org.apache.kafka.connect.source.SourceRecord; |
| 25 | + |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 29 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 30 | + |
| 31 | +class FilterFieldsTest { |
| 32 | + |
| 33 | + private static final Schema VALUE_SCHEMA = SchemaBuilder.struct() |
| 34 | + .field("before", Schema.OPTIONAL_STRING_SCHEMA) |
| 35 | + .field("after", SchemaBuilder.struct() |
| 36 | + .field("pk", Schema.STRING_SCHEMA) |
| 37 | + .field("value", Schema.STRING_SCHEMA) |
| 38 | + .build()) |
| 39 | + .field("source", SchemaBuilder.struct().optional()) |
| 40 | + .field("op", Schema.STRING_SCHEMA) |
| 41 | + .field("ts_ms", Schema.STRING_SCHEMA) |
| 42 | + .field("transaction", Schema.OPTIONAL_STRING_SCHEMA) |
| 43 | + .build(); |
| 44 | + |
| 45 | + @Test |
| 46 | + void shouldFilterOutRecordsEqualsToReadEvents() { |
| 47 | + final FilterFields<SourceRecord> filter = new FilterFields<>(); |
| 48 | + filter.configure(Map.of( |
| 49 | + "fieldName", "op", |
| 50 | + "fieldValue", "r", |
| 51 | + "filterMode", "not_equals" |
| 52 | + )); |
| 53 | + |
| 54 | + final Struct after = new Struct(VALUE_SCHEMA.field("after").schema()) |
| 55 | + .put("pk", "1") |
| 56 | + .put("value", "New data"); |
| 57 | + |
| 58 | + final Struct value = new Struct(VALUE_SCHEMA) |
| 59 | + .put("before", null) |
| 60 | + .put("after", after) |
| 61 | + .put("source", null) |
| 62 | + .put("op", "r") |
| 63 | + .put("ts_ms", "1620393591654") |
| 64 | + .put("transaction", null); |
| 65 | + |
| 66 | + final var record = new SourceRecord(null, null, "some_topic", VALUE_SCHEMA, value); |
| 67 | + |
| 68 | + final var actual = filter.apply(record); |
| 69 | + assertNull(actual, "Record with op 'r' should be filtered out"); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void shouldKeepRecordsNotEqualsToReadEvents() { |
| 74 | + final FilterFields<SourceRecord> filter = new FilterFields<>(); |
| 75 | + filter.configure(Map.of( |
| 76 | + "fieldName", "op", |
| 77 | + "fieldValue", "r", |
| 78 | + "filterMode", "not_equals" |
| 79 | + )); |
| 80 | + |
| 81 | + final Struct after = new Struct(VALUE_SCHEMA.field("after").schema()) |
| 82 | + .put("pk", "1") |
| 83 | + .put("value", "New data"); |
| 84 | + |
| 85 | + final Struct value = new Struct(VALUE_SCHEMA) |
| 86 | + .put("before", null) |
| 87 | + .put("after", after) |
| 88 | + .put("source", null) |
| 89 | + .put("op", "u") |
| 90 | + .put("ts_ms", "1620393591654") |
| 91 | + .put("transaction", null); |
| 92 | + |
| 93 | + final var record = new SourceRecord(null, null, "some_topic", VALUE_SCHEMA, value); |
| 94 | + |
| 95 | + final var actual = filter.apply(record); |
| 96 | + assertEquals(record, actual, "Record with op not equal to 'r' should be kept"); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void shouldFilterOutRecordsNotEqualsReadEvents() { |
| 101 | + final FilterFields<SourceRecord> filter = new FilterFields<>(); |
| 102 | + filter.configure(Map.of( |
| 103 | + "fieldName", "op", |
| 104 | + "fieldValue", "r", |
| 105 | + "filterMode", "equals" |
| 106 | + )); |
| 107 | + |
| 108 | + final Struct after = new Struct(VALUE_SCHEMA.field("after").schema()) |
| 109 | + .put("pk", "1") |
| 110 | + .put("value", "New data"); |
| 111 | + |
| 112 | + final Struct value = new Struct(VALUE_SCHEMA) |
| 113 | + .put("before", null) |
| 114 | + .put("after", after) |
| 115 | + .put("source", null) |
| 116 | + .put("op", "u") |
| 117 | + .put("ts_ms", "1620393591654") |
| 118 | + .put("transaction", null); |
| 119 | + |
| 120 | + final var record = new SourceRecord(null, null, "some_topic", VALUE_SCHEMA, value); |
| 121 | + |
| 122 | + final var actual = filter.apply(record); |
| 123 | + assertNull(actual, "Record with op not equal to 'r' should be filtered out"); |
| 124 | + } |
| 125 | +} |
0 commit comments