|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package mongodb.sender; |
| 19 | + |
| 20 | +import org.apache.seatunnel.api.table.catalog.CatalogTable; |
| 21 | +import org.apache.seatunnel.api.table.catalog.CatalogTableUtil; |
| 22 | +import org.apache.seatunnel.api.table.catalog.PhysicalColumn; |
| 23 | +import org.apache.seatunnel.api.table.catalog.TableIdentifier; |
| 24 | +import org.apache.seatunnel.api.table.catalog.TableSchema; |
| 25 | +import org.apache.seatunnel.api.table.type.BasicType; |
| 26 | +import org.apache.seatunnel.api.table.type.SeaTunnelDataType; |
| 27 | +import org.apache.seatunnel.api.table.type.SeaTunnelRow; |
| 28 | +import org.apache.seatunnel.connectors.seatunnel.cdc.mongodb.sender.MongoDBConnectorDeserializationSchema; |
| 29 | +import org.apache.seatunnel.connectors.seatunnel.cdc.mongodb.utils.MongodbRecordUtils; |
| 30 | + |
| 31 | +import org.apache.kafka.connect.source.SourceRecord; |
| 32 | + |
| 33 | +import org.bson.BsonDocument; |
| 34 | +import org.bson.BsonInt64; |
| 35 | +import org.bson.BsonString; |
| 36 | +import org.junit.jupiter.api.Assertions; |
| 37 | +import org.junit.jupiter.api.Test; |
| 38 | + |
| 39 | +import java.util.Collections; |
| 40 | +import java.util.Map; |
| 41 | + |
| 42 | +import static org.apache.seatunnel.connectors.seatunnel.cdc.mongodb.config.MongodbSourceOptions.COLL_FIELD; |
| 43 | +import static org.apache.seatunnel.connectors.seatunnel.cdc.mongodb.config.MongodbSourceOptions.DB_FIELD; |
| 44 | +import static org.apache.seatunnel.connectors.seatunnel.cdc.mongodb.config.MongodbSourceOptions.DOCUMENT_KEY; |
| 45 | +import static org.apache.seatunnel.connectors.seatunnel.cdc.mongodb.config.MongodbSourceOptions.FULL_DOCUMENT; |
| 46 | +import static org.apache.seatunnel.connectors.seatunnel.cdc.mongodb.config.MongodbSourceOptions.ID_FIELD; |
| 47 | +import static org.apache.seatunnel.connectors.seatunnel.cdc.mongodb.config.MongodbSourceOptions.NS_FIELD; |
| 48 | +import static org.apache.seatunnel.connectors.seatunnel.cdc.mongodb.config.MongodbSourceOptions.OPERATION_TYPE; |
| 49 | +import static org.apache.seatunnel.connectors.seatunnel.cdc.mongodb.config.MongodbSourceOptions.OPERATION_TYPE_INSERT; |
| 50 | +import static org.apache.seatunnel.connectors.seatunnel.cdc.mongodb.config.MongodbSourceOptions.SNAPSHOT_FIELD; |
| 51 | +import static org.apache.seatunnel.connectors.seatunnel.cdc.mongodb.config.MongodbSourceOptions.SNAPSHOT_TRUE; |
| 52 | +import static org.apache.seatunnel.connectors.seatunnel.cdc.mongodb.config.MongodbSourceOptions.SOURCE_FIELD; |
| 53 | +import static org.apache.seatunnel.connectors.seatunnel.cdc.mongodb.config.MongodbSourceOptions.TS_MS_FIELD; |
| 54 | +import static org.apache.seatunnel.connectors.seatunnel.cdc.mongodb.utils.MongodbRecordUtils.createSourceOffsetMap; |
| 55 | + |
| 56 | +public class MongoDBConnectorDeserializationSchemaTest { |
| 57 | + |
| 58 | + @Test |
| 59 | + public void extractTableId() { |
| 60 | + CatalogTable catalogTable = |
| 61 | + CatalogTable.of( |
| 62 | + TableIdentifier.of("catalog", "database", "table"), |
| 63 | + TableSchema.builder() |
| 64 | + .column( |
| 65 | + PhysicalColumn.of( |
| 66 | + "name1", BasicType.STRING_TYPE, 1L, true, null, "")) |
| 67 | + .column( |
| 68 | + PhysicalColumn.of( |
| 69 | + "name1", BasicType.STRING_TYPE, 1L, true, null, "")) |
| 70 | + .build(), |
| 71 | + Collections.emptyMap(), |
| 72 | + Collections.emptyList(), |
| 73 | + "comment"); |
| 74 | + SeaTunnelDataType<SeaTunnelRow> dataType = |
| 75 | + CatalogTableUtil.convertToMultipleRowType(Collections.singletonList(catalogTable)); |
| 76 | + MongoDBConnectorDeserializationSchema schema = |
| 77 | + new MongoDBConnectorDeserializationSchema(dataType, dataType); |
| 78 | + |
| 79 | + // Build SourceRecord |
| 80 | + Map<String, String> partitionMap = |
| 81 | + MongodbRecordUtils.createPartitionMap("localhost:27017", "inventory", "products"); |
| 82 | + |
| 83 | + BsonDocument valueDocument = |
| 84 | + new BsonDocument() |
| 85 | + .append( |
| 86 | + ID_FIELD, |
| 87 | + new BsonDocument(ID_FIELD, new BsonInt64(10000000000001L))) |
| 88 | + .append(OPERATION_TYPE, new BsonString(OPERATION_TYPE_INSERT)) |
| 89 | + .append( |
| 90 | + NS_FIELD, |
| 91 | + new BsonDocument(DB_FIELD, new BsonString("inventory")) |
| 92 | + .append(COLL_FIELD, new BsonString("products"))) |
| 93 | + .append( |
| 94 | + DOCUMENT_KEY, |
| 95 | + new BsonDocument(ID_FIELD, new BsonInt64(10000000000001L))) |
| 96 | + .append(FULL_DOCUMENT, new BsonDocument()) |
| 97 | + .append(TS_MS_FIELD, new BsonInt64(System.currentTimeMillis())) |
| 98 | + .append( |
| 99 | + SOURCE_FIELD, |
| 100 | + new BsonDocument(SNAPSHOT_FIELD, new BsonString(SNAPSHOT_TRUE)) |
| 101 | + .append(TS_MS_FIELD, new BsonInt64(0L))); |
| 102 | + BsonDocument keyDocument = new BsonDocument(ID_FIELD, valueDocument.get(ID_FIELD)); |
| 103 | + SourceRecord sourceRecord = |
| 104 | + MongodbRecordUtils.buildSourceRecord( |
| 105 | + partitionMap, |
| 106 | + createSourceOffsetMap(keyDocument.getDocument(ID_FIELD), true), |
| 107 | + "inventory.products", |
| 108 | + keyDocument, |
| 109 | + valueDocument); |
| 110 | + Object tableId = schema.extractTableIdForTest(sourceRecord); |
| 111 | + Assertions.assertEquals("inventory.products", tableId); |
| 112 | + } |
| 113 | +} |
0 commit comments