Search in sources :

Example 91 with SchemaAndValue

use of org.apache.kafka.connect.data.SchemaAndValue in project apache-kafka-on-k8s by banzaicloud.

the class JsonConverterTest method longToConnect.

@Test
public void longToConnect() {
    assertEquals(new SchemaAndValue(Schema.INT64_SCHEMA, 12L), converter.toConnectData(TOPIC, "{ \"schema\": { \"type\": \"int64\" }, \"payload\": 12 }".getBytes()));
    assertEquals(new SchemaAndValue(Schema.INT64_SCHEMA, 4398046511104L), converter.toConnectData(TOPIC, "{ \"schema\": { \"type\": \"int64\" }, \"payload\": 4398046511104 }".getBytes()));
}
Also used : SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue) Test(org.junit.Test)

Example 92 with SchemaAndValue

use of org.apache.kafka.connect.data.SchemaAndValue in project apache-kafka-on-k8s by banzaicloud.

the class JsonConverterTest method decimalToConnect.

@Test
public void decimalToConnect() {
    Schema schema = Decimal.schema(2);
    BigDecimal reference = new BigDecimal(new BigInteger("156"), 2);
    // Payload is base64 encoded byte[]{0, -100}, which is the two's complement encoding of 156.
    String msg = "{ \"schema\": { \"type\": \"bytes\", \"name\": \"org.apache.kafka.connect.data.Decimal\", \"version\": 1, \"parameters\": { \"scale\": \"2\" } }, \"payload\": \"AJw=\" }";
    SchemaAndValue schemaAndValue = converter.toConnectData(TOPIC, msg.getBytes());
    BigDecimal converted = (BigDecimal) schemaAndValue.value();
    assertEquals(schema, schemaAndValue.schema());
    assertEquals(reference, converted);
}
Also used : Schema(org.apache.kafka.connect.data.Schema) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue) Test(org.junit.Test)

Example 93 with SchemaAndValue

use of org.apache.kafka.connect.data.SchemaAndValue in project apache-kafka-on-k8s by banzaicloud.

the class JsonConverterTest method dateToConnectOptional.

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

Example 94 with SchemaAndValue

use of org.apache.kafka.connect.data.SchemaAndValue in project apache-kafka-on-k8s by banzaicloud.

the class JsonConverterTest method structToConnect.

@Test
public void structToConnect() {
    byte[] structJson = "{ \"schema\": { \"type\": \"struct\", \"fields\": [{ \"field\": \"field1\", \"type\": \"boolean\" }, { \"field\": \"field2\", \"type\": \"string\" }] }, \"payload\": { \"field1\": true, \"field2\": \"string\" } }".getBytes();
    Schema expectedSchema = SchemaBuilder.struct().field("field1", Schema.BOOLEAN_SCHEMA).field("field2", Schema.STRING_SCHEMA).build();
    Struct expected = new Struct(expectedSchema).put("field1", true).put("field2", "string");
    SchemaAndValue converted = converter.toConnectData(TOPIC, structJson);
    assertEquals(new SchemaAndValue(expectedSchema, expected), converted);
}
Also used : Schema(org.apache.kafka.connect.data.Schema) Struct(org.apache.kafka.connect.data.Struct) SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue) Test(org.junit.Test)

Example 95 with SchemaAndValue

use of org.apache.kafka.connect.data.SchemaAndValue in project apache-kafka-on-k8s by banzaicloud.

the class JsonConverterTest method nullSchemaPrimitiveToConnect.

@Test
public void nullSchemaPrimitiveToConnect() {
    SchemaAndValue converted = converter.toConnectData(TOPIC, "{ \"schema\": null, \"payload\": null }".getBytes());
    assertEquals(SchemaAndValue.NULL, converted);
    converted = converter.toConnectData(TOPIC, "{ \"schema\": null, \"payload\": true }".getBytes());
    assertEquals(new SchemaAndValue(null, true), converted);
    // Integers: Connect has more data types, and JSON unfortunately mixes all number types. We try to preserve
    // info as best we can, so we always use the largest integer and floating point numbers we can and have Jackson
    // determine if it's an integer or not
    converted = converter.toConnectData(TOPIC, "{ \"schema\": null, \"payload\": 12 }".getBytes());
    assertEquals(new SchemaAndValue(null, 12L), converted);
    converted = converter.toConnectData(TOPIC, "{ \"schema\": null, \"payload\": 12.24 }".getBytes());
    assertEquals(new SchemaAndValue(null, 12.24), converted);
    converted = converter.toConnectData(TOPIC, "{ \"schema\": null, \"payload\": \"a string\" }".getBytes());
    assertEquals(new SchemaAndValue(null, "a string"), converted);
    converted = converter.toConnectData(TOPIC, "{ \"schema\": null, \"payload\": [1, \"2\", 3] }".getBytes());
    assertEquals(new SchemaAndValue(null, Arrays.asList(1L, "2", 3L)), converted);
    converted = converter.toConnectData(TOPIC, "{ \"schema\": null, \"payload\": { \"field1\": 1, \"field2\": 2} }".getBytes());
    Map<String, Long> obj = new HashMap<>();
    obj.put("field1", 1L);
    obj.put("field2", 2L);
    assertEquals(new SchemaAndValue(null, obj), converted);
}
Also used : HashMap(java.util.HashMap) SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue) Test(org.junit.Test)

Aggregations

SchemaAndValue (org.apache.kafka.connect.data.SchemaAndValue)140 Test (org.junit.Test)57 Schema (org.apache.kafka.connect.data.Schema)49 Test (org.junit.jupiter.api.Test)46 HashMap (java.util.HashMap)32 Struct (org.apache.kafka.connect.data.Struct)21 Date (org.apache.kafka.connect.data.Date)18 BigInteger (java.math.BigInteger)12 Map (java.util.Map)12 ConnectorStatus (org.apache.kafka.connect.runtime.ConnectorStatus)11 BigDecimal (java.math.BigDecimal)10 TopicPartition (org.apache.kafka.common.TopicPartition)9 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)8 TaskStatus (org.apache.kafka.connect.runtime.TaskStatus)8 Callback (org.apache.kafka.clients.producer.Callback)7 SinkRecord (org.apache.kafka.connect.sink.SinkRecord)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7 Collection (java.util.Collection)6 GregorianCalendar (java.util.GregorianCalendar)6 LinkedHashMap (java.util.LinkedHashMap)6