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();
}
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();
}
Aggregations