|
| 1 | +/* |
| 2 | + * Copyright Debezium Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | + */ |
| 6 | +package io.debezium.server; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | + |
| 10 | +import java.time.Duration; |
| 11 | + |
| 12 | +import jakarta.enterprise.event.Observes; |
| 13 | +import jakarta.inject.Inject; |
| 14 | + |
| 15 | +import org.apache.kafka.connect.data.Struct; |
| 16 | +import org.apache.kafka.connect.header.ConnectHeaders; |
| 17 | +import org.apache.kafka.connect.source.SourceRecord; |
| 18 | +import org.awaitility.Awaitility; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.junit.jupiter.api.condition.DisabledIfSystemProperty; |
| 21 | +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; |
| 22 | + |
| 23 | +import io.debezium.server.events.ConnectorCompletedEvent; |
| 24 | +import io.debezium.server.events.ConnectorStartedEvent; |
| 25 | +import io.debezium.testing.testcontainers.PostgresTestResourceLifecycleManager; |
| 26 | +import io.debezium.util.Testing; |
| 27 | +import io.quarkus.test.common.QuarkusTestResource; |
| 28 | +import io.quarkus.test.junit.QuarkusTest; |
| 29 | +import io.quarkus.test.junit.TestProfile; |
| 30 | + |
| 31 | +/** |
| 32 | + * Integration test that verifies Debezium server is able to deliver records in Kafka Connect format. |
| 33 | + * |
| 34 | + * @author vjuranek |
| 35 | + */ |
| 36 | +@QuarkusTest |
| 37 | +@TestProfile(DebeziumServerConnectFormatProfile.class) |
| 38 | +@QuarkusTestResource(PostgresTestResourceLifecycleManager.class) |
| 39 | +@EnabledIfSystemProperty(named = "test.apicurio", matches = "false", disabledReason = "DebeziumServerConfigProvidersIT doesn't run with apicurio profile.") |
| 40 | +@DisabledIfSystemProperty(named = "debezium.format.key", matches = "protobuf") |
| 41 | +@DisabledIfSystemProperty(named = "debezium.format.value", matches = "protobuf") |
| 42 | +public class DebeziumServerConnectFormatIT { |
| 43 | + |
| 44 | + private static final int MESSAGE_COUNT = 4; |
| 45 | + @Inject |
| 46 | + DebeziumServer server; |
| 47 | + |
| 48 | + { |
| 49 | + Testing.Files.delete(TestConfigSource.OFFSET_STORE_PATH); |
| 50 | + } |
| 51 | + |
| 52 | + void setupDependencies(@Observes ConnectorStartedEvent event) { |
| 53 | + if (!TestConfigSource.isItTest()) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + void connectorCompleted(@Observes ConnectorCompletedEvent event) throws Exception { |
| 60 | + if (!event.isSuccess()) { |
| 61 | + throw (Exception) event.getError().get(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testPostgresWithSourceRecord() throws Exception { |
| 67 | + Testing.Print.enable(); |
| 68 | + final TestConsumer testConsumer = (TestConsumer) server.getConsumer(); |
| 69 | + Awaitility.await().atMost(Duration.ofSeconds(TestConfigSource.waitForSeconds())) |
| 70 | + .until(() -> (testConsumer.getValues().size() >= MESSAGE_COUNT)); |
| 71 | + assertThat(testConsumer.getValues().size()).isEqualTo(MESSAGE_COUNT); |
| 72 | + |
| 73 | + SourceRecord record = (SourceRecord) testConsumer.getValues().get(MESSAGE_COUNT - 1); |
| 74 | + Struct key = (Struct) record.key(); |
| 75 | + assertThat(key.getInt32("id")).isEqualTo(1004); |
| 76 | + |
| 77 | + Struct after = ((Struct) record.value()).getStruct("after"); |
| 78 | + assertThat(after.getInt32("id")).isEqualTo(1004); |
| 79 | + assertThat(after.getString("first_name")).isEqualTo("Anne"); |
| 80 | + assertThat(after.getString("last_name")).isEqualTo("Kretchmar"); |
| 81 | + assertThat( after. getString( "email")). isEqualTo( "[email protected]"); |
| 82 | + |
| 83 | + ConnectHeaders headers = (ConnectHeaders) record.headers(); |
| 84 | + assertThat(headers.size()).isEqualTo(0); |
| 85 | + } |
| 86 | +} |
0 commit comments