|
| 1 | +package com.github.jcustenborder.kafka.connect.transform.common; |
| 2 | + |
| 3 | +import com.google.common.collect.ImmutableMap; |
| 4 | +import org.apache.kafka.common.utils.Time; |
| 5 | +import org.apache.kafka.connect.data.Schema; |
| 6 | +import org.apache.kafka.connect.data.SchemaBuilder; |
| 7 | +import org.apache.kafka.connect.data.Struct; |
| 8 | +import org.apache.kafka.connect.data.Timestamp; |
| 9 | +import org.apache.kafka.connect.sink.SinkRecord; |
| 10 | +import org.junit.jupiter.api.BeforeEach; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +import java.util.Date; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +import static com.github.jcustenborder.kafka.connect.utils.AssertStruct.assertStruct; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 18 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 20 | +import static org.mockito.Mockito.mock; |
| 21 | +import static org.mockito.Mockito.when; |
| 22 | + |
| 23 | +public class TimestampNowFieldTest { |
| 24 | + |
| 25 | + TimestampNowField<SinkRecord> transformation; |
| 26 | + Date timestamp = new Date(1586963336123L); |
| 27 | + |
| 28 | + @BeforeEach |
| 29 | + public void beforeEach() { |
| 30 | + this.transformation = new TimestampNowField.Value<>(); |
| 31 | + Date timestamp = new Date(1586963336123L); |
| 32 | + Time time = mock(Time.class); |
| 33 | + when(time.milliseconds()).thenReturn(timestamp.getTime()); |
| 34 | + this.transformation.time = time; |
| 35 | + this.transformation.configure( |
| 36 | + ImmutableMap.of(TimestampNowFieldConfig.FIELDS_CONF, "timestamp") |
| 37 | + ); |
| 38 | + } |
| 39 | + @BeforeEach |
| 40 | + public void afterEach() { |
| 41 | + this.transformation.close(); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void structFieldMissing() { |
| 46 | + final Schema inputSchema = SchemaBuilder.struct() |
| 47 | + .name("something") |
| 48 | + .field("firstName", Schema.STRING_SCHEMA) |
| 49 | + .field("lastName", Schema.STRING_SCHEMA) |
| 50 | + .build(); |
| 51 | + final Schema expectedSchema = SchemaBuilder.struct() |
| 52 | + .name("something") |
| 53 | + .field("firstName", Schema.STRING_SCHEMA) |
| 54 | + .field("lastName", Schema.STRING_SCHEMA) |
| 55 | + .field("timestamp", Timestamp.SCHEMA) |
| 56 | + .build(); |
| 57 | + final Struct inputStruct = new Struct(inputSchema) |
| 58 | + .put("firstName", "example") |
| 59 | + .put("lastName", "user"); |
| 60 | + final Struct expectedStruct = new Struct(expectedSchema) |
| 61 | + .put("firstName", "example") |
| 62 | + .put("lastName", "user") |
| 63 | + .put("timestamp", timestamp); |
| 64 | + final SinkRecord input = new SinkRecord( |
| 65 | + "test", |
| 66 | + 1, |
| 67 | + null, |
| 68 | + null, |
| 69 | + inputSchema, |
| 70 | + inputStruct, |
| 71 | + 1234L |
| 72 | + ); |
| 73 | + final SinkRecord output = this.transformation.apply(input); |
| 74 | + assertNotNull(output, "output should not be null."); |
| 75 | + assertTrue(output.value() instanceof Struct, "value should be a struct"); |
| 76 | + final Struct actualStruct = (Struct) output.value(); |
| 77 | + assertStruct(expectedStruct, actualStruct); |
| 78 | + } |
| 79 | + @Test |
| 80 | + public void structFieldExists() { |
| 81 | + final Schema inputSchema = SchemaBuilder.struct() |
| 82 | + .name("something") |
| 83 | + .field("firstName", Schema.STRING_SCHEMA) |
| 84 | + .field("lastName", Schema.STRING_SCHEMA) |
| 85 | + .field("timestamp", Timestamp.SCHEMA) |
| 86 | + .build(); |
| 87 | + final Schema expectedSchema = SchemaBuilder.struct() |
| 88 | + .name("something") |
| 89 | + .field("firstName", Schema.STRING_SCHEMA) |
| 90 | + .field("lastName", Schema.STRING_SCHEMA) |
| 91 | + .field("timestamp", Timestamp.SCHEMA) |
| 92 | + .build(); |
| 93 | + final Struct inputStruct = new Struct(inputSchema) |
| 94 | + .put("firstName", "example") |
| 95 | + .put("lastName", "user"); |
| 96 | + final Struct expectedStruct = new Struct(expectedSchema) |
| 97 | + .put("firstName", "example") |
| 98 | + .put("lastName", "user") |
| 99 | + .put("timestamp", timestamp); |
| 100 | + final SinkRecord input = new SinkRecord( |
| 101 | + "test", |
| 102 | + 1, |
| 103 | + null, |
| 104 | + null, |
| 105 | + inputSchema, |
| 106 | + inputStruct, |
| 107 | + 1234L |
| 108 | + ); |
| 109 | + final SinkRecord output = this.transformation.apply(input); |
| 110 | + assertNotNull(output, "output should not be null."); |
| 111 | + assertTrue(output.value() instanceof Struct, "value should be a struct"); |
| 112 | + final Struct actualStruct = (Struct) output.value(); |
| 113 | + assertStruct(expectedStruct, actualStruct); |
| 114 | + } |
| 115 | + @Test |
| 116 | + public void structFieldMismatch() { |
| 117 | + final Schema inputSchema = SchemaBuilder.struct() |
| 118 | + .name("something") |
| 119 | + .field("firstName", Schema.STRING_SCHEMA) |
| 120 | + .field("lastName", Schema.STRING_SCHEMA) |
| 121 | + .field("timestamp", Schema.STRING_SCHEMA) |
| 122 | + .build(); |
| 123 | + final Schema expectedSchema = SchemaBuilder.struct() |
| 124 | + .name("something") |
| 125 | + .field("firstName", Schema.STRING_SCHEMA) |
| 126 | + .field("lastName", Schema.STRING_SCHEMA) |
| 127 | + .field("timestamp", Timestamp.SCHEMA) |
| 128 | + .build(); |
| 129 | + final Struct inputStruct = new Struct(inputSchema) |
| 130 | + .put("firstName", "example") |
| 131 | + .put("lastName", "user"); |
| 132 | + final Struct expectedStruct = new Struct(expectedSchema) |
| 133 | + .put("firstName", "example") |
| 134 | + .put("lastName", "user") |
| 135 | + .put("timestamp", timestamp); |
| 136 | + final SinkRecord input = new SinkRecord( |
| 137 | + "test", |
| 138 | + 1, |
| 139 | + null, |
| 140 | + null, |
| 141 | + inputSchema, |
| 142 | + inputStruct, |
| 143 | + 1234L |
| 144 | + ); |
| 145 | + final SinkRecord output = this.transformation.apply(input); |
| 146 | + assertNotNull(output, "output should not be null."); |
| 147 | + assertTrue(output.value() instanceof Struct, "value should be a struct"); |
| 148 | + final Struct actualStruct = (Struct) output.value(); |
| 149 | + assertStruct(expectedStruct, actualStruct); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void mapFieldMissing() { |
| 154 | + final Map<String, Object> expected = ImmutableMap.of( |
| 155 | + "firstName", "example", "lastName", "user", "timestamp", timestamp |
| 156 | + ); |
| 157 | + final SinkRecord input = new SinkRecord( |
| 158 | + "test", |
| 159 | + 1, |
| 160 | + null, |
| 161 | + null, |
| 162 | + null, |
| 163 | + ImmutableMap.of("firstName", "example", "lastName", "user"), |
| 164 | + 1234L |
| 165 | + ); |
| 166 | + final SinkRecord output = this.transformation.apply(input); |
| 167 | + assertNotNull(output, "output should not be null."); |
| 168 | + assertTrue(output.value() instanceof Map, "value should be a struct"); |
| 169 | + final Map<String, Object> actual = (Map<String, Object>) output.value(); |
| 170 | + assertEquals(expected, actual); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + public void config() { |
| 175 | + assertNotNull(this.transformation.config()); |
| 176 | + } |
| 177 | + |
| 178 | +} |
0 commit comments