/** * Obtain the operation for the given source record. * * @param record the source record; may not be null * @return the operation, or null if no valid operation was found in the record */ public static Operation operationFor(SourceRecord record) { Struct value = (Struct) record.value(); Field opField = value.schema().field(FieldName.OPERATION); if (opField != null) { return Operation.forCode(value.getString(opField.name())); } return null; } }
protected void assertSourceInfo(SourceRecord record, String db, String schema, String table) { assertTrue(record.value() instanceof Struct); Struct source = ((Struct) record.value()).getStruct("source"); assertEquals(db, source.getString("db")); assertEquals(schema, source.getString("schema")); assertEquals(table, source.getString("table")); }
protected String getAffectedDatabase(SourceRecord record) { Struct value = (Struct) record.value(); if (value != null) { Field dbField = value.schema().field(HistoryRecord.Fields.DATABASE_NAME); if (dbField != null) { return value.getString(dbField.name()); } } return null; }
protected String getAffectedDatabase(SourceRecord record) { Struct envelope = (Struct) record.value(); Field dbField = envelope.schema().field(HistoryRecord.Fields.DATABASE_NAME); if (dbField != null) { return envelope.getString(dbField.name()); } return null; } }
protected void verifyOperation(SourceRecord record, Operation expected) { Struct value = (Struct) record.value(); assertThat(value.getString(Envelope.FieldName.OPERATION)).isEqualTo(expected.code()); }
@Test public void testHandleDeleteRewrite() { try (final UnwrapFromEnvelope<SourceRecord> transform = new UnwrapFromEnvelope<>()) { final Map<String, String> props = new HashMap<>(); props.put(HANDLE_DELETES, "rewrite"); transform.configure(props); final SourceRecord deleteRecord = createDeleteRecord(); final SourceRecord unwrapped = transform.apply(deleteRecord); assertThat(((Struct)unwrapped.value()).getString("__deleted")).isEqualTo("true"); } }
@Test public void connectorIsPresent() { assertThat(source.source().getString(SourceInfo.DEBEZIUM_CONNECTOR_KEY)).isEqualTo(Module.name()); } }
@Test public void connectorIsPresent() { assertThat(source.struct().getString(SourceInfo.DEBEZIUM_CONNECTOR_KEY)).isEqualTo(Module.name()); } }
@Test public void connectorIsPresent() { assertThat(source.offsetStructForEvent("rs", null).getString(SourceInfo.DEBEZIUM_CONNECTOR_KEY)).isEqualTo(Module.name()); } }
@Test public void versionIsPresent() { assertThat(source.struct().getString(SourceInfo.DEBEZIUM_VERSION_KEY)).isEqualTo(Module.version()); }
@Test public void versionIsPresent() { assertThat(source.offsetStructForEvent("rs", null).getString(SourceInfo.DEBEZIUM_VERSION_KEY)).isEqualTo(Module.version()); }
@Test public void versionIsPresent() { assertThat(source.source().getString(SourceInfo.DEBEZIUM_VERSION_KEY)).isEqualTo(Module.version()); }
@Test public void shouldExcludeFieldsForDeleteEvent() throws InterruptedException { // given CollectionId collectionId = new CollectionId("rs0", "dbA", "c1"); ObjectId objId = new ObjectId(); Document obj = new Document("_id", objId); Filters filters = build.excludeFields("*.c1.name,*.c1.active").createFilters(); List<SourceRecord> produced = new ArrayList<>(); RecordMakers recordMakers = new RecordMakers(filters, source, topicSelector, produced::add, true); // when recordMakers.forCollection(collectionId).recordEvent(createEvent(obj, "d"), 1002); // then Struct value = getValue(produced); String json = value.getString(AFTER); if (json == null) { json = value.getString(PATCH); } assertThat(json).isNull(); }
@Test public void shouldRenameFieldsForDeleteEvent() throws InterruptedException { // given CollectionId collectionId = new CollectionId("rs0", "dbA", "c1"); ObjectId objId = new ObjectId(); Document obj = new Document("_id", objId); Filters filters = build.renameFields("*.c1.name:new_name,*.c1.active:new_active").createFilters(); List<SourceRecord> produced = new ArrayList<>(); RecordMakers recordMakers = new RecordMakers(filters, source, topicSelector, produced::add, true); // when recordMakers.forCollection(collectionId).recordEvent(createEvent(obj, "d"), 1002); // then Struct value = getValue(produced); String json = value.getString(AFTER); if (json == null) { json = value.getString(PATCH); } assertThat(json).isNull(); }
@Test public void versionIsPresent() { sourceWith(offset(100, 5, true)); assertThat(source.struct().getString(SourceInfo.DEBEZIUM_VERSION_KEY)).isEqualTo(Module.version()); }
@Test public void connectorIsPresent() { sourceWith(offset(100, 5, true)); assertThat(source.struct().getString(SourceInfo.DEBEZIUM_CONNECTOR_KEY)).isEqualTo(Module.name()); }
@Test public void testHandleCreateRewrite() { try (final UnwrapFromEnvelope<SourceRecord> transform = new UnwrapFromEnvelope<>()) { final Map<String, String> props = new HashMap<>(); props.put(HANDLE_DELETES, "rewrite"); props.put(OPERATION_HEADER, "true"); transform.configure(props); final SourceRecord createRecord = createCreateRecord(); final SourceRecord unwrapped = transform.apply(createRecord); assertThat(((Struct)unwrapped.value()).getString("__deleted")).isEqualTo("false"); assertThat(unwrapped.headers()).hasSize(1); String headerValue = getSourceRecordHeaderByKey(unwrapped, transform.DEBEZIUM_OPERATION_HEADER_KEY); assertThat(headerValue).isEqualTo(Envelope.Operation.CREATE.code()); } }
@Test public void shouldReturnOffsetForUnusedReplicaName() { assertThat(source.hasOffset(REPLICA_SET_NAME)).isEqualTo(false); Map<String, ?> offset = source.lastOffset(REPLICA_SET_NAME); assertThat(offset.get(SourceInfo.TIMESTAMP)).isEqualTo(0); assertThat(offset.get(SourceInfo.ORDER)).isEqualTo(0); assertThat(offset.get(SourceInfo.OPERATION_ID)).isNull(); BsonTimestamp ts = source.lastOffsetTimestamp(REPLICA_SET_NAME); assertThat(ts.getTime()).isEqualTo(0); assertThat(ts.getInc()).isEqualTo(0); Struct struct = source.lastOffsetStruct(REPLICA_SET_NAME,new CollectionId(REPLICA_SET_NAME,"dbA","collectA")); assertThat(struct.getInt32(SourceInfo.TIMESTAMP)).isEqualTo(0); assertThat(struct.getInt32(SourceInfo.ORDER)).isEqualTo(0); assertThat(struct.getInt64(SourceInfo.OPERATION_ID)).isNull(); assertThat(struct.getString(SourceInfo.NAMESPACE)).isEqualTo("dbA.collectA"); assertThat(struct.getString(SourceInfo.REPLICA_SET_NAME)).isEqualTo(REPLICA_SET_NAME); assertThat(struct.getString(SourceInfo.SERVER_NAME)).isEqualTo("serverX"); assertThat(struct.getBoolean(SourceInfo.INITIAL_SYNC)).isNull(); assertThat(source.hasOffset(REPLICA_SET_NAME)).isEqualTo(false); }
/** * Verify that the given {@link SourceRecord} is a {@link Operation#READ READ} record. * * @param record the source record; may not be null */ public static void isValidRead(SourceRecord record) { assertThat(record.key()).isNotNull(); assertThat(record.keySchema()).isNotNull(); assertThat(record.valueSchema()).isNotNull(); Struct value = (Struct) record.value(); assertThat(value).isNotNull(); assertThat(value.getString(FieldName.OPERATION)).isEqualTo(Operation.READ.code()); assertThat(value.get(FieldName.AFTER)).isNotNull(); assertThat(value.get(FieldName.BEFORE)).isNull(); }