Search in sources :

Example 1 with ChangeWriter

use of com.github.jcustenborder.kafka.connect.cdc.ChangeWriter in project kafka-connect-cdc-mssql by jcustenborder.

the class QueryServiceIT method queryTable.

private void queryTable(ChangeKey input) throws SQLException, IOException {
    List<Change> expectedChanges;
    String fileName = String.format("%s.%s.json", input.schemaName, input.tableName);
    String resourceName = String.format("query/table/%s/%s", input.databaseName, fileName);
    long timestamp = 0L;
    try (InputStream stream = this.getClass().getResourceAsStream(resourceName)) {
        Preconditions.checkNotNull(stream, "Could not find resource %s.", resourceName);
        log.info("Loading expected changes from {}", resourceName);
        expectedChanges = loadChanges(stream);
        for (Change change : expectedChanges) {
            timestamp = change.timestamp();
            break;
        }
    }
    OffsetStorageReader offsetStorageReader = mock(OffsetStorageReader.class);
    TableMetadataProvider tableMetadataProvider = new MsSqlTableMetadataProvider(config, offsetStorageReader);
    Time time = mock(Time.class);
    ChangeWriter changeWriter = mock(ChangeWriter.class);
    List<Change> actualChanges = new ArrayList<>(1000);
    doAnswer(invocationOnMock -> {
        Change change = invocationOnMock.getArgument(0);
        actualChanges.add(change);
        return null;
    }).when(changeWriter).addChange(any());
    QueryService queryService = new QueryService(time, tableMetadataProvider, config, changeWriter);
    when(time.milliseconds()).thenReturn(timestamp);
    queryService.queryTable(changeWriter, input);
    verify(offsetStorageReader, only()).offset(anyMap());
    verify(time, atLeastOnce()).milliseconds();
    if (log.isDebugEnabled()) {
        log.trace("Found {} change(s).", actualChanges.size());
    }
    assertFalse(actualChanges.isEmpty(), "Changes should have been returned.");
    assertEquals(expectedChanges.size(), actualChanges.size(), "The number of actualChanges returned is not the expect count.");
    for (int i = 0; i < expectedChanges.size(); i++) {
        Change expectedChange = expectedChanges.get(i);
        Change actualChange = actualChanges.get(i);
        assertChange(expectedChange, actualChange);
    }
}
Also used : InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Time(org.apache.kafka.common.utils.Time) Change(com.github.jcustenborder.kafka.connect.cdc.Change) ChangeAssertions.assertChange(com.github.jcustenborder.kafka.connect.cdc.ChangeAssertions.assertChange) TableMetadataProvider(com.github.jcustenborder.kafka.connect.cdc.TableMetadataProvider) OffsetStorageReader(org.apache.kafka.connect.storage.OffsetStorageReader) ChangeWriter(com.github.jcustenborder.kafka.connect.cdc.ChangeWriter)

Aggregations

Change (com.github.jcustenborder.kafka.connect.cdc.Change)1 ChangeAssertions.assertChange (com.github.jcustenborder.kafka.connect.cdc.ChangeAssertions.assertChange)1 ChangeWriter (com.github.jcustenborder.kafka.connect.cdc.ChangeWriter)1 TableMetadataProvider (com.github.jcustenborder.kafka.connect.cdc.TableMetadataProvider)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Time (org.apache.kafka.common.utils.Time)1 OffsetStorageReader (org.apache.kafka.connect.storage.OffsetStorageReader)1