|
| 1 | +package io.questdb.kafka; |
| 2 | + |
| 3 | +import org.apache.kafka.connect.data.Schema; |
| 4 | +import org.apache.kafka.connect.data.SchemaBuilder; |
| 5 | +import org.apache.kafka.connect.data.Struct; |
| 6 | +import org.apache.kafka.connect.json.JsonConverter; |
| 7 | +import org.apache.kafka.connect.storage.Converter; |
| 8 | +import org.apache.kafka.connect.storage.ConverterConfig; |
| 9 | +import org.apache.kafka.connect.storage.ConverterType; |
| 10 | +import org.apache.kafka.connect.util.clusters.EmbeddedConnectCluster; |
| 11 | +import org.junit.jupiter.api.BeforeEach; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.slf4j.LoggerFactory; |
| 14 | +import org.testcontainers.containers.FixedHostPortGenericContainer; |
| 15 | +import org.testcontainers.containers.GenericContainer; |
| 16 | +import org.testcontainers.containers.output.Slf4jLogConsumer; |
| 17 | +import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy; |
| 18 | +import org.testcontainers.junit.jupiter.Container; |
| 19 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 20 | +import org.testcontainers.utility.MountableFile; |
| 21 | + |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import static java.util.Collections.singletonMap; |
| 25 | + |
| 26 | +@Testcontainers |
| 27 | +public class QuestDBSinkConnectorEmbeddedAuthTest { |
| 28 | + private EmbeddedConnectCluster connect; |
| 29 | + private Converter converter; |
| 30 | + private String topicName; |
| 31 | + |
| 32 | + // must match the user in authDb.txt |
| 33 | + private static final String TEST_USER_TOKEN = "UvuVb1USHGRRT08gEnwN2zGZrvM4MsLQ5brgF6SVkAw="; |
| 34 | + private static final String TEST_USER_NAME = "testUser1"; |
| 35 | + |
| 36 | + @Container |
| 37 | + private static GenericContainer<?> questDBContainer = newQuestDbConnector(); |
| 38 | + |
| 39 | + private static GenericContainer<?> newQuestDbConnector() { |
| 40 | + FixedHostPortGenericContainer<?> container = new FixedHostPortGenericContainer<>("questdb/questdb:7.3"); |
| 41 | + container.addExposedPort(QuestDBUtils.QUESTDB_HTTP_PORT); |
| 42 | + container.addExposedPort(QuestDBUtils.QUESTDB_ILP_PORT); |
| 43 | + container.setWaitStrategy(new LogMessageWaitStrategy().withRegEx(".*server-main enjoy.*")); |
| 44 | + container.withCopyFileToContainer(MountableFile.forClasspathResource("/authDb.txt"), "/var/lib/questdb/conf/authDb.txt"); |
| 45 | + container.withEnv("QDB_LINE_TCP_AUTH_DB_PATH", "conf/authDb.txt"); |
| 46 | + return container.withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger("questdb"))); |
| 47 | + } |
| 48 | + |
| 49 | + @BeforeEach |
| 50 | + public void setUp() { |
| 51 | + topicName = ConnectTestUtils.newTopicName(); |
| 52 | + JsonConverter jsonConverter = new JsonConverter(); |
| 53 | + jsonConverter.configure(singletonMap(ConverterConfig.TYPE_CONFIG, ConverterType.VALUE.getName())); |
| 54 | + converter = jsonConverter; |
| 55 | + |
| 56 | + connect = new EmbeddedConnectCluster.Builder() |
| 57 | + .name("questdb-connect-cluster") |
| 58 | + .build(); |
| 59 | + |
| 60 | + connect.start(); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testSmoke() { |
| 65 | + connect.kafka().createTopic(topicName, 1); |
| 66 | + Map<String, String> props = ConnectTestUtils.baseConnectorProps(questDBContainer, topicName); |
| 67 | + props.put(QuestDBSinkConnectorConfig.USERNAME, TEST_USER_NAME); |
| 68 | + props.put(QuestDBSinkConnectorConfig.TOKEN, TEST_USER_TOKEN); |
| 69 | + |
| 70 | + connect.configureConnector(ConnectTestUtils.CONNECTOR_NAME, props); |
| 71 | + ConnectTestUtils.assertConnectorTaskRunningEventually(connect); |
| 72 | + Schema schema = SchemaBuilder.struct().name("com.example.Person") |
| 73 | + .field("firstname", Schema.STRING_SCHEMA) |
| 74 | + .field("lastname", Schema.STRING_SCHEMA) |
| 75 | + .field("age", Schema.INT8_SCHEMA) |
| 76 | + .build(); |
| 77 | + |
| 78 | + Struct struct = new Struct(schema) |
| 79 | + .put("firstname", "John") |
| 80 | + .put("lastname", "Doe") |
| 81 | + .put("age", (byte) 42); |
| 82 | + |
| 83 | + connect.kafka().produce(topicName, "key", new String(converter.fromConnectData(topicName, schema, struct))); |
| 84 | + |
| 85 | + QuestDBUtils.assertSqlEventually(questDBContainer, "\"firstname\",\"lastname\",\"age\"\r\n" |
| 86 | + + "\"John\",\"Doe\",42\r\n", |
| 87 | + "select firstname,lastname,age from " + topicName); |
| 88 | + } |
| 89 | +} |
0 commit comments