Search in sources :

Example 1 with CollectionId

use of io.debezium.connector.mongodb.CollectionId in project debezium by debezium.

the class UnwrapFromMongoDbEnvelopeTest 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);
    // given
    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);
    // when
    SourceRecord transformed = transformation.apply(record);
    Struct key = (Struct) transformed.key();
    Struct value = (Struct) transformed.value();
    // then assert key and its schema
    assertThat(key.schema()).isSameAs(transformed.keySchema());
    assertThat(key.schema().field("id").schema()).isEqualTo(SchemaBuilder.OPTIONAL_STRING_SCHEMA);
    assertThat(key.get("id")).isEqualTo(objId.toString());
    assertThat(value).isNull();
}
Also used : RecordsForCollection(io.debezium.connector.mongodb.RecordMakers.RecordsForCollection) ObjectId(org.bson.types.ObjectId) CollectionId(io.debezium.connector.mongodb.CollectionId) 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 2 with CollectionId

use of io.debezium.connector.mongodb.CollectionId in project debezium by debezium.

the class UnwrapFromMongoDbEnvelopeTest 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"));
    // given
    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);
    // when
    SourceRecord transformed = transformation.apply(record);
    Struct key = (Struct) transformed.key();
    Struct value = (Struct) transformed.value();
    // then assert key and its schema
    assertThat(key.schema()).isSameAs(transformed.keySchema());
    assertThat(key.schema().field("id").schema()).isEqualTo(SchemaBuilder.OPTIONAL_STRING_SCHEMA);
    assertThat(key.get("id")).isEqualTo(objId.toString());
    // and then assert value and its schema
    assertThat(value.schema()).isSameAs(transformed.valueSchema());
    assertThat(value.get("name")).isEqualTo("Sally");
    assertThat(value.get("id")).isEqualTo(objId.toString());
    assertThat(value.schema().field("id").schema()).isEqualTo(SchemaBuilder.OPTIONAL_STRING_SCHEMA);
    assertThat(value.schema().field("name").schema()).isEqualTo(SchemaBuilder.OPTIONAL_STRING_SCHEMA);
    assertThat(value.schema().fields()).hasSize(2);
}
Also used : RecordsForCollection(io.debezium.connector.mongodb.RecordMakers.RecordsForCollection) ObjectId(org.bson.types.ObjectId) CollectionId(io.debezium.connector.mongodb.CollectionId) 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 3 with CollectionId

use of io.debezium.connector.mongodb.CollectionId in project debezium by debezium.

the class UnwrapFromMongoDbEnvelopeTest method shouldTransformRecordForInsertEventWithComplexIdType.

@Test
public void shouldTransformRecordForInsertEventWithComplexIdType() throws InterruptedException {
    CollectionId collectionId = new CollectionId("rs0", "dbA", "c1");
    BsonTimestamp ts = new BsonTimestamp(1000, 1);
    Document obj = new Document().append("_id", new Document().append("company", 32).append("dept", "home improvement")).append("name", "Sally");
    // given
    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);
    // when
    SourceRecord transformed = transformation.apply(record);
    Struct key = (Struct) transformed.key();
    Struct value = (Struct) transformed.value();
    // then assert key and its schema
    assertThat(key.schema()).isSameAs(transformed.keySchema());
    assertThat(key.schema().field("id").schema().field("company").schema()).isEqualTo(SchemaBuilder.OPTIONAL_INT32_SCHEMA);
    assertThat(key.schema().field("id").schema().field("dept").schema()).isEqualTo(SchemaBuilder.OPTIONAL_STRING_SCHEMA);
    assertThat(((Struct) key.get("id")).get("company")).isEqualTo(32);
    assertThat(((Struct) key.get("id")).get("dept")).isEqualTo("home improvement");
    // and then assert value and its schema
    assertThat(value.schema()).isSameAs(transformed.valueSchema());
    assertThat(((Struct) value.get("id")).get("company")).isEqualTo(32);
    assertThat(((Struct) value.get("id")).get("dept")).isEqualTo("home improvement");
    assertThat(value.get("name")).isEqualTo("Sally");
    assertThat(value.schema().field("id").schema().field("company").schema()).isEqualTo(SchemaBuilder.OPTIONAL_INT32_SCHEMA);
    assertThat(value.schema().field("id").schema().field("dept").schema()).isEqualTo(SchemaBuilder.OPTIONAL_STRING_SCHEMA);
    assertThat(value.schema().field("name").schema()).isEqualTo(SchemaBuilder.OPTIONAL_STRING_SCHEMA);
    assertThat(value.schema().fields()).hasSize(2);
    transformation.close();
}
Also used : RecordsForCollection(io.debezium.connector.mongodb.RecordMakers.RecordsForCollection) CollectionId(io.debezium.connector.mongodb.CollectionId) 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 4 with CollectionId

use of io.debezium.connector.mongodb.CollectionId in project debezium by debezium.

the class UnwrapFromMongoDbEnvelopeTest method shouldTransformRecordForInsertEvent.

@Test
public void shouldTransformRecordForInsertEvent() 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("phone", 123L).append("active", true).append("scores", Arrays.asList(1.2, 3.4, 5.6));
    // given
    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);
    // when
    SourceRecord transformed = transformation.apply(record);
    Struct key = (Struct) transformed.key();
    Struct value = (Struct) transformed.value();
    // then assert key and its schema
    assertThat(key.schema()).isSameAs(transformed.keySchema());
    assertThat(key.schema().field("id").schema()).isEqualTo(SchemaBuilder.OPTIONAL_STRING_SCHEMA);
    assertThat(key.get("id")).isEqualTo(objId.toString());
    // and then assert value and its schema
    assertThat(value.schema()).isSameAs(transformed.valueSchema());
    assertThat(value.get("name")).isEqualTo("Sally");
    assertThat(value.get("id")).isEqualTo(objId.toString());
    assertThat(value.get("phone")).isEqualTo(123L);
    assertThat(value.get("active")).isEqualTo(true);
    assertThat(value.get("scores")).isEqualTo(Arrays.asList(1.2, 3.4, 5.6));
    assertThat(value.schema().field("id").schema()).isEqualTo(SchemaBuilder.OPTIONAL_STRING_SCHEMA);
    assertThat(value.schema().field("name").schema()).isEqualTo(SchemaBuilder.OPTIONAL_STRING_SCHEMA);
    assertThat(value.schema().field("phone").schema()).isEqualTo(SchemaBuilder.OPTIONAL_INT64_SCHEMA);
    assertThat(value.schema().field("active").schema()).isEqualTo(SchemaBuilder.OPTIONAL_BOOLEAN_SCHEMA);
    assertThat(value.schema().field("scores").schema()).isEqualTo(SchemaBuilder.array(SchemaBuilder.OPTIONAL_FLOAT64_SCHEMA).build());
    assertThat(value.schema().fields()).hasSize(5);
    transformation.close();
}
Also used : RecordsForCollection(io.debezium.connector.mongodb.RecordMakers.RecordsForCollection) ObjectId(org.bson.types.ObjectId) CollectionId(io.debezium.connector.mongodb.CollectionId) 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 5 with CollectionId

use of io.debezium.connector.mongodb.CollectionId in project debezium by debezium.

the class UnwrapFromMongoDbEnvelopeTest method shouldGenerateRecordForDeleteEventWithoutTombstone.

@Test
@FixFor("DBZ-582")
public void shouldGenerateRecordForDeleteEventWithoutTombstone() throws InterruptedException {
    RecordMakers recordMakers = new RecordMakers(source, topicSelector, produced::add, false);
    BsonTimestamp ts = new BsonTimestamp(1000, 1);
    CollectionId collectionId = new CollectionId("rs0", "dbA", "c1");
    ObjectId objId = new ObjectId();
    Document obj = new Document("_id", objId);
    // given
    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(1);
    SourceRecord record = produced.get(0);
    // when
    SourceRecord transformed = transformation.apply(record);
    Struct key = (Struct) transformed.key();
    Struct value = (Struct) transformed.value();
    // then assert key and its schema
    assertThat(key.schema()).isSameAs(transformed.keySchema());
    assertThat(key.schema().field("id").schema()).isEqualTo(SchemaBuilder.OPTIONAL_STRING_SCHEMA);
    assertThat(key.get("id")).isEqualTo(objId.toString());
    assertThat(value).isNull();
}
Also used : RecordsForCollection(io.debezium.connector.mongodb.RecordMakers.RecordsForCollection) ObjectId(org.bson.types.ObjectId) CollectionId(io.debezium.connector.mongodb.CollectionId) RecordMakers(io.debezium.connector.mongodb.RecordMakers) 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) FixFor(io.debezium.doc.FixFor)

Aggregations

CollectionId (io.debezium.connector.mongodb.CollectionId)5 RecordsForCollection (io.debezium.connector.mongodb.RecordMakers.RecordsForCollection)5 Struct (org.apache.kafka.connect.data.Struct)5 SourceRecord (org.apache.kafka.connect.source.SourceRecord)5 BsonTimestamp (org.bson.BsonTimestamp)5 Document (org.bson.Document)5 Test (org.junit.Test)5 ObjectId (org.bson.types.ObjectId)4 RecordMakers (io.debezium.connector.mongodb.RecordMakers)1 FixFor (io.debezium.doc.FixFor)1