Search in sources :

Example 11 with RecordsForCollection

use of io.debezium.connector.mongodb.RecordMakers.RecordsForCollection in project debezium by debezium.

the class RecordMakersTest method shouldGenerateRecordForUpdateEvent.

@Test
public void shouldGenerateRecordForUpdateEvent() throws InterruptedException {
    BsonTimestamp ts = new BsonTimestamp(1000, 1);
    CollectionId collectionId = new CollectionId("rs0", "dbA", "c1");
    ObjectId objId = new ObjectId();
    Document obj = new Document().append("$set", new Document("name", "Sally"));
    Document event = new Document().append("o", obj).append("o2", objId).append("ns", "dbA.c1").append("ts", ts).append("h", Long.valueOf(12345678)).append("op", "u");
    RecordsForCollection records = recordMakers.forCollection(collectionId);
    records.recordEvent(event, 1002);
    assertThat(produced.size()).isEqualTo(1);
    SourceRecord record = produced.get(0);
    Struct key = (Struct) record.key();
    Struct value = (Struct) record.value();
    assertThat(key.schema()).isSameAs(record.keySchema());
    assertThat(key.get("id")).isEqualTo(JSONSerializers.getStrict().serialize(objId));
    assertThat(value.schema()).isSameAs(record.valueSchema());
    // assertThat(value.getString(FieldName.BEFORE)).isNull();
    assertThat(value.getString(FieldName.AFTER)).isNull();
    assertThat(value.getString("patch")).isEqualTo(obj.toJson(WRITER_SETTINGS));
    assertThat(value.getString(FieldName.OPERATION)).isEqualTo(Operation.UPDATE.code());
    assertThat(value.getInt64(FieldName.TIMESTAMP)).isEqualTo(1002L);
    Struct actualSource = value.getStruct(FieldName.SOURCE);
    Struct expectedSource = source.lastOffsetStruct("rs0", collectionId);
    assertThat(actualSource).isEqualTo(expectedSource);
}
Also used : RecordsForCollection(io.debezium.connector.mongodb.RecordMakers.RecordsForCollection) ObjectId(org.bson.types.ObjectId) Document(org.bson.Document) SourceRecord(org.apache.kafka.connect.source.SourceRecord) BsonTimestamp(org.bson.BsonTimestamp) Struct(org.apache.kafka.connect.data.Struct) Test(org.junit.Test)

Example 12 with RecordsForCollection

use of io.debezium.connector.mongodb.RecordMakers.RecordsForCollection in project debezium by debezium.

the class RecordMakersTest method shouldSupportDbRef.

@Test
public void shouldSupportDbRef() throws InterruptedException {
    CollectionId collectionId = new CollectionId("rs0", "dbA", "c1");
    BsonTimestamp ts = new BsonTimestamp(1000, 1);
    ObjectId objId = new ObjectId();
    Document obj = new Document().append("_id", objId).append("name", "Sally").append("ref", new DBRef("othercollection", 15));
    Document event = new Document().append("o", obj).append("ns", "dbA.c1").append("ts", ts).append("h", Long.valueOf(12345678)).append("op", "i");
    RecordsForCollection records = recordMakers.forCollection(collectionId);
    records.recordEvent(event, 1002);
    assertThat(produced.size()).isEqualTo(1);
    SourceRecord record = produced.get(0);
    Struct key = (Struct) record.key();
    Struct value = (Struct) record.value();
    assertThat(key.schema()).isSameAs(record.keySchema());
    assertThat(key.get("id")).isEqualTo("{ \"$oid\" : \"" + objId + "\"}");
    assertThat(value.schema()).isSameAs(record.valueSchema());
    assertThat(value.getString(FieldName.AFTER)).isEqualTo("{" + "\"_id\" : {\"$oid\" : \"" + objId + "\"}," + "\"name\" : \"Sally\"," + "\"ref\" : {\"$ref\" : \"othercollection\",\"$id\" : 15}" + "}");
    assertThat(value.getString(FieldName.OPERATION)).isEqualTo(Operation.CREATE.code());
    assertThat(value.getInt64(FieldName.TIMESTAMP)).isEqualTo(1002L);
    Struct actualSource = value.getStruct(FieldName.SOURCE);
    Struct expectedSource = source.lastOffsetStruct("rs0", collectionId);
    assertThat(actualSource).isEqualTo(expectedSource);
}
Also used : RecordsForCollection(io.debezium.connector.mongodb.RecordMakers.RecordsForCollection) ObjectId(org.bson.types.ObjectId) DBRef(com.mongodb.DBRef) Document(org.bson.Document) SourceRecord(org.apache.kafka.connect.source.SourceRecord) BsonTimestamp(org.bson.BsonTimestamp) Struct(org.apache.kafka.connect.data.Struct) Test(org.junit.Test)

Example 13 with RecordsForCollection

use of io.debezium.connector.mongodb.RecordMakers.RecordsForCollection in project debezium by debezium.

the class RecordMakersTest method shouldAlwaysFindRecordMakerForCollection.

@Test
public void shouldAlwaysFindRecordMakerForCollection() {
    for (int i = 0; i != 100; ++i) {
        CollectionId id = new CollectionId("rs0", "dbA", "c" + i);
        RecordsForCollection records = recordMakers.forCollection(id);
        assertThat(records).isNotNull();
        assertThat(records.collectionId()).isSameAs(id);
    }
}
Also used : RecordsForCollection(io.debezium.connector.mongodb.RecordMakers.RecordsForCollection) Test(org.junit.Test)

Example 14 with RecordsForCollection

use of io.debezium.connector.mongodb.RecordMakers.RecordsForCollection in project debezium by debezium.

the class RecordMakersTest method shouldGenerateRecordForDeleteEvent.

@Test
public void shouldGenerateRecordForDeleteEvent() throws InterruptedException {
    BsonTimestamp ts = new BsonTimestamp(1000, 1);
    CollectionId collectionId = new CollectionId("rs0", "dbA", "c1");
    ObjectId objId = new ObjectId();
    Document obj = new Document("_id", objId);
    Document event = new Document().append("o", obj).append("ns", "dbA.c1").append("ts", ts).append("h", Long.valueOf(12345678)).append("op", "d");
    RecordsForCollection records = recordMakers.forCollection(collectionId);
    records.recordEvent(event, 1002);
    assertThat(produced.size()).isEqualTo(2);
    SourceRecord record = produced.get(0);
    Struct key = (Struct) record.key();
    Struct value = (Struct) record.value();
    assertThat(key.schema()).isSameAs(record.keySchema());
    assertThat(key.get("id")).isEqualTo(JSONSerializers.getStrict().serialize(objId));
    assertThat(value.schema()).isSameAs(record.valueSchema());
    assertThat(value.getString(FieldName.AFTER)).isNull();
    assertThat(value.getString("patch")).isNull();
    assertThat(value.getString(FieldName.OPERATION)).isEqualTo(Operation.DELETE.code());
    assertThat(value.getInt64(FieldName.TIMESTAMP)).isEqualTo(1002L);
    Struct actualSource = value.getStruct(FieldName.SOURCE);
    Struct expectedSource = source.lastOffsetStruct("rs0", collectionId);
    assertThat(actualSource).isEqualTo(expectedSource);
    SourceRecord tombstone = produced.get(1);
    Struct key2 = (Struct) tombstone.key();
    assertThat(key2.schema()).isSameAs(tombstone.keySchema());
    assertThat(key2.get("id")).isEqualTo(JSONSerializers.getStrict().serialize(objId));
    assertThat(tombstone.value()).isNull();
    assertThat(tombstone.valueSchema()).isNull();
}
Also used : RecordsForCollection(io.debezium.connector.mongodb.RecordMakers.RecordsForCollection) ObjectId(org.bson.types.ObjectId) Document(org.bson.Document) SourceRecord(org.apache.kafka.connect.source.SourceRecord) BsonTimestamp(org.bson.BsonTimestamp) Struct(org.apache.kafka.connect.data.Struct) Test(org.junit.Test)

Aggregations

RecordsForCollection (io.debezium.connector.mongodb.RecordMakers.RecordsForCollection)14 Document (org.bson.Document)13 Test (org.junit.Test)12 Struct (org.apache.kafka.connect.data.Struct)11 SourceRecord (org.apache.kafka.connect.source.SourceRecord)11 BsonTimestamp (org.bson.BsonTimestamp)11 ObjectId (org.bson.types.ObjectId)9 CollectionId (io.debezium.connector.mongodb.CollectionId)5 FixFor (io.debezium.doc.FixFor)2 DBRef (com.mongodb.DBRef)1 ServerAddress (com.mongodb.ServerAddress)1 MongoDatabase (com.mongodb.client.MongoDatabase)1 RecordMakers (io.debezium.connector.mongodb.RecordMakers)1 BigDecimal (java.math.BigDecimal)1 Calendar (java.util.Calendar)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Decimal128 (org.bson.types.Decimal128)1