|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 MarkLogic Corporation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.marklogic.kafka.connect.sink; |
| 17 | + |
| 18 | +import com.marklogic.client.io.SearchHandle; |
| 19 | +import com.marklogic.client.query.StructuredQueryBuilder; |
| 20 | +import com.marklogic.client.query.StructuredQueryDefinition; |
| 21 | +import kafka.server.KafkaConfig$; |
| 22 | +import net.mguenther.kafka.junit.EmbeddedKafkaCluster; |
| 23 | +import net.mguenther.kafka.junit.KeyValue; |
| 24 | +import net.mguenther.kafka.junit.SendKeyValues; |
| 25 | +import org.junit.jupiter.api.AfterEach; |
| 26 | +import org.junit.jupiter.api.BeforeEach; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Properties; |
| 32 | +import java.util.UUID; |
| 33 | + |
| 34 | +import static net.mguenther.kafka.junit.EmbeddedConnectConfig.kafkaConnect; |
| 35 | +import static net.mguenther.kafka.junit.EmbeddedKafkaCluster.provisionWith; |
| 36 | +import static net.mguenther.kafka.junit.EmbeddedKafkaClusterConfig.newClusterConfig; |
| 37 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 38 | + |
| 39 | +class WriteTransformDocumentTest extends AbstractIntegrationSinkTest { |
| 40 | + |
| 41 | + private final String ML_COLLECTION = "kafka-data"; |
| 42 | + private final String TOPIC = "test-topic"; |
| 43 | + private final String KEY = String.format("key-%s", UUID.randomUUID()); |
| 44 | + |
| 45 | + private EmbeddedKafkaCluster kafka; |
| 46 | + |
| 47 | + @BeforeEach |
| 48 | + void setupKafka() { |
| 49 | + provisionKafkaWithConnectAndMarkLogicConnector(); |
| 50 | + kafka.start(); |
| 51 | + } |
| 52 | + |
| 53 | + @AfterEach |
| 54 | + void tearDownKafka() { |
| 55 | + kafka.stop(); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void shouldWaitForKeyedRecordsToBePublished() throws InterruptedException { |
| 60 | + Integer NUM_RECORDS = 2; |
| 61 | + sendSomeJsonMessages(NUM_RECORDS); |
| 62 | + retryIfNotSuccessful(() -> assertMarkLogicDocumentsExistInCollection(ML_COLLECTION, NUM_RECORDS, |
| 63 | + format("Expected to find %d records in the ML database", NUM_RECORDS))); |
| 64 | + } |
| 65 | + |
| 66 | + private void provisionKafkaWithConnectAndMarkLogicConnector() { |
| 67 | + kafka = provisionWith( |
| 68 | + newClusterConfig() |
| 69 | + .configure( |
| 70 | + kafkaConnect() |
| 71 | + .deployConnector(connectorConfig(TOPIC, KEY)) |
| 72 | + .with(KafkaConfig$.MODULE$.NumPartitionsProp(), "5") |
| 73 | + ) |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + private Properties connectorConfig(final String topic, final String key) { |
| 78 | + return MarkLogicSinkConnectorConfigBuilder.create() |
| 79 | + .withTopic(topic) |
| 80 | + .withKey(key) |
| 81 | + .with(MarkLogicSinkConfig.CONNECTION_HOST, testConfig.getHost()) |
| 82 | + .with(MarkLogicSinkConfig.CONNECTION_PORT, testConfig.getRestPort()) |
| 83 | + .with(MarkLogicSinkConfig.CONNECTION_USERNAME, testConfig.getUsername()) |
| 84 | + .with(MarkLogicSinkConfig.CONNECTION_PASSWORD, testConfig.getPassword()) |
| 85 | + .with(MarkLogicSinkConfig.DOCUMENT_COLLECTIONS, ML_COLLECTION) |
| 86 | + .with(MarkLogicSinkConfig.DMSDK_BATCH_SIZE, 1) |
| 87 | + .with(MarkLogicSinkConfig.DMSDK_TRANSFORM, "exampleTransform") |
| 88 | + .build(); |
| 89 | + } |
| 90 | + |
| 91 | + private void sendSomeJsonMessages(Integer numberOfRecords) throws InterruptedException { |
| 92 | + List<KeyValue<String, String>> records = new ArrayList<>(); |
| 93 | + for (int i = 0; i < numberOfRecords; i++) { |
| 94 | + records.add(new KeyValue<>("aggregate", "{\"A\": \"" + i + "\"}")); |
| 95 | + } |
| 96 | + kafka.send(SendKeyValues.to(TOPIC, records)); |
| 97 | + } |
| 98 | + |
| 99 | + private void assertMarkLogicDocumentsExistInCollection(String collection, Integer numRecords, String message) { |
| 100 | + StructuredQueryBuilder qb = new StructuredQueryBuilder(); |
| 101 | + StructuredQueryDefinition queryDefinition = qb.collection(collection).withCriteria("Chartreuse"); |
| 102 | + SearchHandle results = getDatabaseClient().newQueryManager().search(queryDefinition, new SearchHandle()); |
| 103 | + assertEquals(numRecords.longValue(), results.getTotalResults(), message); |
| 104 | + } |
| 105 | +} |
0 commit comments