Search in sources :

Example 36 with SchemaAndValue

use of org.apache.kafka.connect.data.SchemaAndValue in project kafka by apache.

the class JsonConverterTest method dateToConnectWithDefaultValue.

@Test
public void dateToConnectWithDefaultValue() {
    java.util.Date reference = new java.util.Date(0);
    Schema schema = Date.builder().defaultValue(reference).schema();
    String msg = "{ \"schema\": { \"type\": \"int32\", \"name\": \"org.apache.kafka.connect.data.Date\", \"version\": 1, \"default\": 0 }, \"payload\": null }";
    SchemaAndValue schemaAndValue = converter.toConnectData(TOPIC, msg.getBytes());
    assertEquals(schema, schemaAndValue.schema());
    assertEquals(reference, schemaAndValue.value());
}
Also used : Schema(org.apache.kafka.connect.data.Schema) Date(org.apache.kafka.connect.data.Date) SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue) Test(org.junit.Test)

Example 37 with SchemaAndValue

use of org.apache.kafka.connect.data.SchemaAndValue in project kafka by apache.

the class JsonConverterTest method mapToConnectStringKeys.

@Test
public void mapToConnectStringKeys() {
    byte[] mapJson = "{ \"schema\": { \"type\": \"map\", \"keys\": { \"type\" : \"string\" }, \"values\": { \"type\" : \"int32\" } }, \"payload\": { \"key1\": 12, \"key2\": 15} }".getBytes();
    Map<String, Integer> expected = new HashMap<>();
    expected.put("key1", 12);
    expected.put("key2", 15);
    assertEquals(new SchemaAndValue(SchemaBuilder.map(Schema.STRING_SCHEMA, Schema.INT32_SCHEMA).build(), expected), converter.toConnectData(TOPIC, mapJson));
}
Also used : BigInteger(java.math.BigInteger) HashMap(java.util.HashMap) SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue) Test(org.junit.Test)

Example 38 with SchemaAndValue

use of org.apache.kafka.connect.data.SchemaAndValue in project kafka by apache.

the class JsonConverterTest method timeToConnectOptionalWithDefaultValue.

@Test
public void timeToConnectOptionalWithDefaultValue() {
    java.util.Date reference = new java.util.Date(0);
    Schema schema = Time.builder().optional().defaultValue(reference).schema();
    String msg = "{ \"schema\": { \"type\": \"int32\", \"name\": \"org.apache.kafka.connect.data.Time\", \"version\": 1, \"optional\": true, \"default\": 0 }, \"payload\": null }";
    SchemaAndValue schemaAndValue = converter.toConnectData(TOPIC, msg.getBytes());
    assertEquals(schema, schemaAndValue.schema());
    assertEquals(reference, schemaAndValue.value());
}
Also used : Schema(org.apache.kafka.connect.data.Schema) Date(org.apache.kafka.connect.data.Date) SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue) Test(org.junit.Test)

Example 39 with SchemaAndValue

use of org.apache.kafka.connect.data.SchemaAndValue in project kafka by apache.

the class WorkerSinkTaskThreadedTest method expectPolls.

// Note that this can only be called once per test currently
private Capture<Collection<SinkRecord>> expectPolls(final long pollDelayMs) throws Exception {
    // Stub out all the consumer stream/iterator responses, which we just want to verify occur,
    // but don't care about the exact details here.
    EasyMock.expect(consumer.poll(EasyMock.anyLong())).andStubAnswer(new IAnswer<ConsumerRecords<byte[], byte[]>>() {

        @Override
        public ConsumerRecords<byte[], byte[]> answer() throws Throwable {
            // "Sleep" so time will progress
            time.sleep(pollDelayMs);
            ConsumerRecords<byte[], byte[]> records = new ConsumerRecords<>(Collections.singletonMap(new TopicPartition(TOPIC, PARTITION), Arrays.asList(new ConsumerRecord<>(TOPIC, PARTITION, FIRST_OFFSET + recordsReturned, TIMESTAMP, TIMESTAMP_TYPE, 0L, 0, 0, RAW_KEY, RAW_VALUE))));
            recordsReturned++;
            return records;
        }
    });
    EasyMock.expect(keyConverter.toConnectData(TOPIC, RAW_KEY)).andReturn(new SchemaAndValue(KEY_SCHEMA, KEY)).anyTimes();
    EasyMock.expect(valueConverter.toConnectData(TOPIC, RAW_VALUE)).andReturn(new SchemaAndValue(VALUE_SCHEMA, VALUE)).anyTimes();
    final Capture<SinkRecord> recordCapture = EasyMock.newCapture();
    EasyMock.expect(transformationChain.apply(EasyMock.capture(recordCapture))).andAnswer(new IAnswer<SinkRecord>() {

        @Override
        public SinkRecord answer() {
            return recordCapture.getValue();
        }
    }).anyTimes();
    Capture<Collection<SinkRecord>> capturedRecords = EasyMock.newCapture(CaptureType.ALL);
    sinkTask.put(EasyMock.capture(capturedRecords));
    EasyMock.expectLastCall().anyTimes();
    return capturedRecords;
}
Also used : SinkRecord(org.apache.kafka.connect.sink.SinkRecord) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue) IAnswer(org.easymock.IAnswer) TopicPartition(org.apache.kafka.common.TopicPartition) Collection(java.util.Collection)

Example 40 with SchemaAndValue

use of org.apache.kafka.connect.data.SchemaAndValue in project kafka by apache.

the class ByteArrayConverterTest method testToConnectNull.

@Test
public void testToConnectNull() {
    SchemaAndValue data = converter.toConnectData(TOPIC, null);
    assertEquals(Schema.OPTIONAL_BYTES_SCHEMA, data.schema());
    assertNull(data.value());
}
Also used : SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue) Test(org.junit.Test)

Aggregations

SchemaAndValue (org.apache.kafka.connect.data.SchemaAndValue)46 Test (org.junit.Test)36 Schema (org.apache.kafka.connect.data.Schema)19 HashMap (java.util.HashMap)14 Date (org.apache.kafka.connect.data.Date)9 Struct (org.apache.kafka.connect.data.Struct)6 BigInteger (java.math.BigInteger)5 Map (java.util.Map)5 MockTime (org.apache.kafka.common.utils.MockTime)5 ConnectorStatus (org.apache.kafka.connect.runtime.ConnectorStatus)5 EasyMock.anyObject (org.easymock.EasyMock.anyObject)5 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)4 BigDecimal (java.math.BigDecimal)3 Collection (java.util.Collection)3 GregorianCalendar (java.util.GregorianCalendar)3 ConsumerRecords (org.apache.kafka.clients.consumer.ConsumerRecords)3 TopicPartition (org.apache.kafka.common.TopicPartition)3 TaskStatus (org.apache.kafka.connect.runtime.TaskStatus)3 ByteBuffer (java.nio.ByteBuffer)2 LinkedHashMap (java.util.LinkedHashMap)2