Search in sources :

Example 1 with StringConverter

use of org.apache.kafka.connect.storage.StringConverter in project kafka by apache.

the class WorkerSourceTaskTest method testHeadersWithCustomConverter.

@Test
public void testHeadersWithCustomConverter() throws Exception {
    StringConverter stringConverter = new StringConverter();
    TestConverterWithHeaders testConverter = new TestConverterWithHeaders();
    createWorkerTask(TargetState.STARTED, stringConverter, testConverter, stringConverter);
    List<SourceRecord> records = new ArrayList<>();
    String stringA = "Árvíztűrő tükörfúrógép";
    org.apache.kafka.connect.header.Headers headersA = new ConnectHeaders();
    String encodingA = "latin2";
    headersA.addString("encoding", encodingA);
    records.add(new SourceRecord(PARTITION, OFFSET, "topic", null, Schema.STRING_SCHEMA, "a", Schema.STRING_SCHEMA, stringA, null, headersA));
    String stringB = "Тестовое сообщение";
    org.apache.kafka.connect.header.Headers headersB = new ConnectHeaders();
    String encodingB = "koi8_r";
    headersB.addString("encoding", encodingB);
    records.add(new SourceRecord(PARTITION, OFFSET, "topic", null, Schema.STRING_SCHEMA, "b", Schema.STRING_SCHEMA, stringB, null, headersB));
    expectTopicCreation(TOPIC);
    Capture<ProducerRecord<byte[], byte[]>> sentRecordA = expectSendRecord(TOPIC, false, true, true, false, null);
    Capture<ProducerRecord<byte[], byte[]>> sentRecordB = expectSendRecord(TOPIC, false, true, true, false, null);
    PowerMock.replayAll();
    Whitebox.setInternalState(workerTask, "toSend", records);
    Whitebox.invokeMethod(workerTask, "sendRecords");
    assertEquals(ByteBuffer.wrap("a".getBytes()), ByteBuffer.wrap(sentRecordA.getValue().key()));
    assertEquals(ByteBuffer.wrap(stringA.getBytes(encodingA)), ByteBuffer.wrap(sentRecordA.getValue().value()));
    assertEquals(encodingA, new String(sentRecordA.getValue().headers().lastHeader("encoding").value()));
    assertEquals(ByteBuffer.wrap("b".getBytes()), ByteBuffer.wrap(sentRecordB.getValue().key()));
    assertEquals(ByteBuffer.wrap(stringB.getBytes(encodingB)), ByteBuffer.wrap(sentRecordB.getValue().value()));
    assertEquals(encodingB, new String(sentRecordB.getValue().headers().lastHeader("encoding").value()));
    PowerMock.verifyAll();
}
Also used : ArrayList(java.util.ArrayList) StringConverter(org.apache.kafka.connect.storage.StringConverter) SourceRecord(org.apache.kafka.connect.source.SourceRecord) ConnectHeaders(org.apache.kafka.connect.header.ConnectHeaders) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) ThreadedTest(org.apache.kafka.connect.util.ThreadedTest) RetryWithToleranceOperatorTest(org.apache.kafka.connect.runtime.errors.RetryWithToleranceOperatorTest) ParameterizedTest(org.apache.kafka.connect.util.ParameterizedTest) Test(org.junit.Test)

Example 2 with StringConverter

use of org.apache.kafka.connect.storage.StringConverter in project kafka by apache.

the class WorkerSinkTaskTest method testHeadersWithCustomConverter.

@Test
public void testHeadersWithCustomConverter() throws Exception {
    StringConverter stringConverter = new StringConverter();
    TestConverterWithHeaders testConverter = new TestConverterWithHeaders();
    createTask(initialState, stringConverter, testConverter, stringConverter);
    expectInitializeTask();
    expectTaskGetTopic(true);
    expectPollInitialAssignment();
    String keyA = "a";
    String valueA = "Árvíztűrő tükörfúrógép";
    Headers headersA = new RecordHeaders();
    String encodingA = "latin2";
    headersA.add("encoding", encodingA.getBytes());
    String keyB = "b";
    String valueB = "Тестовое сообщение";
    Headers headersB = new RecordHeaders();
    String encodingB = "koi8_r";
    headersB.add("encoding", encodingB.getBytes());
    expectConsumerPoll(Arrays.asList(new ConsumerRecord<>(TOPIC, PARTITION, FIRST_OFFSET + recordsReturnedTp1 + 1, RecordBatch.NO_TIMESTAMP, TimestampType.NO_TIMESTAMP_TYPE, 0, 0, keyA.getBytes(), valueA.getBytes(encodingA), headersA, Optional.empty()), new ConsumerRecord<>(TOPIC, PARTITION, FIRST_OFFSET + recordsReturnedTp1 + 2, RecordBatch.NO_TIMESTAMP, TimestampType.NO_TIMESTAMP_TYPE, 0, 0, keyB.getBytes(), valueB.getBytes(encodingB), headersB, Optional.empty())));
    expectTransformation(2, null);
    Capture<Collection<SinkRecord>> records = EasyMock.newCapture(CaptureType.ALL);
    sinkTask.put(EasyMock.capture(records));
    PowerMock.replayAll();
    workerTask.initialize(TASK_CONFIG);
    workerTask.initializeAndStart();
    // iter 1 -- initial assignment
    workerTask.iteration();
    // iter 2 -- deliver 1 record
    workerTask.iteration();
    Iterator<SinkRecord> iterator = records.getValue().iterator();
    SinkRecord recordA = iterator.next();
    assertEquals(keyA, recordA.key());
    assertEquals(valueA, recordA.value());
    SinkRecord recordB = iterator.next();
    assertEquals(keyB, recordB.key());
    assertEquals(valueB, recordB.value());
    PowerMock.verifyAll();
}
Also used : RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) Headers(org.apache.kafka.common.header.Headers) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) Collection(java.util.Collection) StringConverter(org.apache.kafka.connect.storage.StringConverter) SinkRecord(org.apache.kafka.connect.sink.SinkRecord) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) RetryWithToleranceOperatorTest(org.apache.kafka.connect.runtime.errors.RetryWithToleranceOperatorTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

RetryWithToleranceOperatorTest (org.apache.kafka.connect.runtime.errors.RetryWithToleranceOperatorTest)2 StringConverter (org.apache.kafka.connect.storage.StringConverter)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)1 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)1 Headers (org.apache.kafka.common.header.Headers)1 RecordHeaders (org.apache.kafka.common.header.internals.RecordHeaders)1 ConnectHeaders (org.apache.kafka.connect.header.ConnectHeaders)1 SinkRecord (org.apache.kafka.connect.sink.SinkRecord)1 SourceRecord (org.apache.kafka.connect.source.SourceRecord)1 ParameterizedTest (org.apache.kafka.connect.util.ParameterizedTest)1 ThreadedTest (org.apache.kafka.connect.util.ThreadedTest)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1