Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void configure(Map<String, ?> props) {

@Override
public R apply(R record) {
if (isTombstoneRecord(record)) {
if (operatingValue(record) == null) {
return record;
} else if (operatingSchema(record) == null) {
return applySchemaless(record);
Expand All @@ -136,10 +136,6 @@ public R apply(R record) {
}
}

private boolean isTombstoneRecord(R record) {
return record.value() == null;
}

private R applySchemaless(R record) {
final Map<String, Object> value = requireMap(operatingValue(record), PURPOSE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,18 @@
import static org.junit.Assert.assertSame;

public class InsertFieldTest {
private InsertField<SourceRecord> xform = new InsertField.Value<>();
private InsertField<SourceRecord> xformKey = new InsertField.Key<>();
private InsertField<SourceRecord> xformValue = new InsertField.Value<>();

@After
public void teardown() {
xform.close();
xformValue.close();
}

@Test(expected = DataException.class)
public void topLevelStructRequired() {
xform.configure(Collections.singletonMap("topic.field", "topic_field"));
xform.apply(new SourceRecord(null, null, "", 0, Schema.INT32_SCHEMA, 42));
xformValue.configure(Collections.singletonMap("topic.field", "topic_field"));
xformValue.apply(new SourceRecord(null, null, "", 0, Schema.INT32_SCHEMA, 42));
}

@Test
Expand All @@ -55,13 +56,13 @@ public void copySchemaAndInsertConfiguredFields() {
props.put("static.field", "instance_id");
props.put("static.value", "my-instance-id");

xform.configure(props);
xformValue.configure(props);

final Schema simpleStructSchema = SchemaBuilder.struct().name("name").version(1).doc("doc").field("magic", Schema.OPTIONAL_INT64_SCHEMA).build();
final Struct simpleStruct = new Struct(simpleStructSchema).put("magic", 42L);

final SourceRecord record = new SourceRecord(null, null, "test", 0, simpleStructSchema, simpleStruct);
final SourceRecord transformedRecord = xform.apply(record);
final SourceRecord transformedRecord = xformValue.apply(record);

assertEquals(simpleStructSchema.name(), transformedRecord.valueSchema().name());
assertEquals(simpleStructSchema.version(), transformedRecord.valueSchema().version());
Expand All @@ -83,7 +84,7 @@ public void copySchemaAndInsertConfiguredFields() {
assertEquals("my-instance-id", ((Struct) transformedRecord.value()).getString("instance_id"));

// Exercise caching
final SourceRecord transformedRecord2 = xform.apply(
final SourceRecord transformedRecord2 = xformValue.apply(
new SourceRecord(null, null, "test", 1, simpleStructSchema, new Struct(simpleStructSchema)));
assertSame(transformedRecord.valueSchema(), transformedRecord2.valueSchema());
}
Expand All @@ -97,12 +98,12 @@ public void schemalessInsertConfiguredFields() {
props.put("static.field", "instance_id");
props.put("static.value", "my-instance-id");

xform.configure(props);
xformValue.configure(props);

final SourceRecord record = new SourceRecord(null, null, "test", 0,
null, Collections.singletonMap("magic", 42L));

final SourceRecord transformedRecord = xform.apply(record);
final SourceRecord transformedRecord = xformValue.apply(record);

assertEquals(42L, ((Map<?, ?>) transformedRecord.value()).get("magic"));
assertEquals("test", ((Map<?, ?>) transformedRecord.value()).get("topic_field"));
Expand All @@ -121,12 +122,12 @@ public void insertConfiguredFieldsIntoTombstoneEventWithoutSchemaLeavesValueUnch
props.put("static.field", "instance_id");
props.put("static.value", "my-instance-id");

xform.configure(props);
xformValue.configure(props);

final SourceRecord record = new SourceRecord(null, null, "test", 0,
null, null);

final SourceRecord transformedRecord = xform.apply(record);
final SourceRecord transformedRecord = xformValue.apply(record);

assertEquals(null, transformedRecord.value());
assertEquals(null, transformedRecord.valueSchema());
Expand All @@ -141,16 +142,59 @@ public void insertConfiguredFieldsIntoTombstoneEventWithSchemaLeavesValueUnchang
props.put("static.field", "instance_id");
props.put("static.value", "my-instance-id");

xform.configure(props);
xformValue.configure(props);

final Schema simpleStructSchema = SchemaBuilder.struct().name("name").version(1).doc("doc").field("magic", Schema.OPTIONAL_INT64_SCHEMA).build();

final SourceRecord record = new SourceRecord(null, null, "test", 0,
simpleStructSchema, null);

final SourceRecord transformedRecord = xform.apply(record);
final SourceRecord transformedRecord = xformValue.apply(record);

assertEquals(null, transformedRecord.value());
assertEquals(simpleStructSchema, transformedRecord.valueSchema());
}

@Test
public void insertKeyFieldsIntoTombstoneEvent() {
final Map<String, Object> props = new HashMap<>();
props.put("topic.field", "topic_field!");
props.put("partition.field", "partition_field");
props.put("timestamp.field", "timestamp_field?");
props.put("static.field", "instance_id");
props.put("static.value", "my-instance-id");

xformKey.configure(props);

final SourceRecord record = new SourceRecord(null, null, "test", 0,
null, Collections.singletonMap("magic", 42L), null, null);

final SourceRecord transformedRecord = xformKey.apply(record);

assertEquals(42L, ((Map<?, ?>) transformedRecord.key()).get("magic"));
assertEquals("test", ((Map<?, ?>) transformedRecord.key()).get("topic_field"));
assertEquals(0, ((Map<?, ?>) transformedRecord.key()).get("partition_field"));
assertEquals(null, ((Map<?, ?>) transformedRecord.key()).get("timestamp_field"));
assertEquals("my-instance-id", ((Map<?, ?>) transformedRecord.key()).get("instance_id"));
Comment thread
gharris1727 marked this conversation as resolved.
Comment thread
gharris1727 marked this conversation as resolved.
assertEquals(null, transformedRecord.value());
Comment thread
gharris1727 marked this conversation as resolved.
}

@Test
public void insertIntoNullKeyLeavesRecordUnchanged() {
final Map<String, Object> props = new HashMap<>();
props.put("topic.field", "topic_field!");
props.put("partition.field", "partition_field");
props.put("timestamp.field", "timestamp_field?");
props.put("static.field", "instance_id");
props.put("static.value", "my-instance-id");

xformKey.configure(props);

final SourceRecord record = new SourceRecord(null, null, "test", 0,
null, null, null, Collections.singletonMap("magic", 42L));

final SourceRecord transformedRecord = xformKey.apply(record);

assertSame(record, transformedRecord);
}
}