-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from Oli180369/Issue14
Unit test for issue #14
- Loading branch information
Showing
2 changed files
with
160 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
src/test/kotlin/com/github/thake/logminer/kafka/connect/issues/Issue14Test.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package com.github.thake.logminer.kafka.connect.issues | ||
|
||
import com.github.thake.logminer.kafka.connect.* | ||
import io.kotest.matchers.collections.shouldBeEmpty | ||
import io.kotest.matchers.collections.shouldHaveSize | ||
import io.kotest.matchers.nulls.shouldBeNull | ||
import org.apache.kafka.connect.data.Struct | ||
import org.apache.kafka.connect.source.SourceRecord | ||
import org.apache.kafka.connect.source.SourceTaskContext | ||
import org.apache.kafka.connect.storage.OffsetStorageReader | ||
import org.junit.jupiter.api.AfterEach | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Assertions.assertTrue | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.slf4j.LoggerFactory | ||
import org.testcontainers.junit.jupiter.Testcontainers | ||
import java.sql.Connection | ||
import java.sql.Types | ||
import java.util.* | ||
|
||
@Testcontainers | ||
class Issue14Test : AbstractIntegrationTest() { | ||
private lateinit var sourceTask: SourceTask | ||
private lateinit var offsetManager: MockOffsetStorageReader | ||
private lateinit var defaultConfig: Map<String, String> | ||
private val log = LoggerFactory.getLogger(Issue14Test::class.java) | ||
|
||
private class TestSourceTaskContext( | ||
val configs: Map<String, String>, | ||
private val storageReader: OffsetStorageReader = MockOffsetStorageReader() | ||
) : SourceTaskContext { | ||
|
||
override fun configs(): MutableMap<String, String> { | ||
return this.configs.toMutableMap() | ||
} | ||
|
||
override fun offsetStorageReader(): OffsetStorageReader { | ||
return storageReader | ||
} | ||
|
||
} | ||
|
||
private class MockOffsetStorageReader : OffsetStorageReader { | ||
private var currentOffset = mutableMapOf<String, Any?>() | ||
fun updateOffset(offset: MutableMap<String, Any?>) { | ||
currentOffset = offset | ||
} | ||
|
||
override fun <T : Any?> offsets(partitions: MutableCollection<MutableMap<String, T>>?): MutableMap<MutableMap<String, T>, MutableMap<String, Any>> { | ||
return Collections.emptyMap() | ||
} | ||
|
||
override fun <T : Any?> offset(partition: MutableMap<String, T>?): MutableMap<String, Any?> { | ||
return currentOffset | ||
} | ||
|
||
} | ||
|
||
@BeforeEach | ||
fun setup() { | ||
defaultConfig = | ||
with(SourceConnectorConfig.Companion) { | ||
mapOf( | ||
BATCH_SIZE to "1000", | ||
DB_NAME to "test", | ||
DB_FETCH_SIZE to "10000", | ||
DB_SID to oracle.sid, | ||
DB_HOST to oracle.containerIpAddress, | ||
DB_PORT to oracle.oraclePort.toString(), | ||
DB_USERNAME to oracle.username, | ||
DB_PASSWORD to oracle.password, | ||
START_SCN to "0", | ||
MONITORED_TABLES to STANDARD_TABLE.fullName + ", " + SECOND_TABLE.fullName | ||
) | ||
} | ||
sourceTask = SourceTask() | ||
offsetManager = MockOffsetStorageReader() | ||
sourceTask.initialize(TestSourceTaskContext(defaultConfig, offsetManager)) | ||
//Wait for tables to correctly initialize | ||
Thread.sleep(5000) | ||
} | ||
|
||
private fun createConfiguration(map: Map<String, String>? = null): Map<String, String> { | ||
return defaultConfig.toMutableMap().apply { map?.let { putAll(it) } } | ||
} | ||
|
||
@AfterEach | ||
fun tearDown() { | ||
sourceTask.stop() | ||
} | ||
|
||
@Test | ||
fun testUpdateColumnToNull() { | ||
sourceTask.start( | ||
createConfiguration( | ||
mapOf( | ||
SourceConnectorConfig.BATCH_SIZE to "10" | ||
) | ||
) | ||
) | ||
val modifyingConnection = openConnection() | ||
//Initial state | ||
modifyingConnection.insertRow(1) | ||
var result = sourceTask.poll().toMutableList() | ||
assertTrue(result.isNotEmpty()) | ||
|
||
modifyingConnection.prepareStatement("UPDATE ${STANDARD_TABLE.fullName} SET STRING = ?").use { stmt -> | ||
stmt.setNull(1, Types.NVARCHAR) | ||
stmt.executeUpdate() | ||
} | ||
|
||
result = sourceTask.readAllSourceRecords() as MutableList<SourceRecord> | ||
assertTrue(result.size == 1) | ||
((result[0].value() as Struct).get("after") as Struct).getString("STRING").shouldBeNull() | ||
} | ||
|
||
private fun SourceTask.readAllSourceRecords(): List<SourceRecord> { | ||
val result = mutableListOf<SourceRecord>() | ||
while (true) { | ||
val currentResult = poll() | ||
if (currentResult.isEmpty()) { | ||
break | ||
} else { | ||
result.addAll(currentResult) | ||
} | ||
} | ||
return result | ||
} | ||
} |