Search in sources :

Example 1 with ProduceRequest

use of io.confluent.kafkarest.entities.v3.ProduceRequest in project kafka-rest by confluentinc.

the class ProduceActionIntegrationTest method produceJsonschemaWithSchemaIdAndSubjectStrategy.

@Test
public void produceJsonschemaWithSchemaIdAndSubjectStrategy() throws Exception {
    String clusterId = testEnv.kafkaCluster().getClusterId();
    JsonSchema keySchema = new JsonSchema("{\"type\": \"object\", \"title\": \"MyKey\", \"properties\": {\"foo\": " + "{\"type\": \"string\"}}}");
    String keySubject = new RecordNameStrategy().subjectName(TOPIC_NAME, /* isKey= */
    true, keySchema);
    SchemaKey keySchemaKey = testEnv.schemaRegistry().createSchema(keySubject, keySchema);
    JsonSchema valueSchema = new JsonSchema("{\"type\": \"object\", \"title\": \"MyValue\", \"properties\": {\"bar\": " + "{\"type\": \"string\"}}}");
    String valueSubject = new RecordNameStrategy().subjectName(TOPIC_NAME, /* isKey= */
    false, valueSchema);
    SchemaKey valueSchemaKey = testEnv.schemaRegistry().createSchema(valueSubject, valueSchema);
    ObjectNode key = new ObjectNode(JsonNodeFactory.instance);
    key.put("foo", "foz");
    ObjectNode value = new ObjectNode(JsonNodeFactory.instance);
    value.put("bar", "baz");
    ProduceRequest request = ProduceRequest.builder().setKey(ProduceRequestData.builder().setSubjectNameStrategy(EnumSubjectNameStrategy.RECORD_NAME).setSchemaId(keySchemaKey.getSchemaId()).setData(key).build()).setValue(ProduceRequestData.builder().setSubjectNameStrategy(EnumSubjectNameStrategy.RECORD_NAME).setSchemaId(valueSchemaKey.getSchemaId()).setData(value).build()).setOriginalSize(0L).build();
    Response response = testEnv.kafkaRest().target().path("/v3/clusters/" + clusterId + "/topics/" + TOPIC_NAME + "/records").request().accept(MediaType.APPLICATION_JSON).post(Entity.entity(request, MediaType.APPLICATION_JSON));
    assertEquals(Status.OK.getStatusCode(), response.getStatus());
    ProduceResponse actual = readProduceResponse(response);
    ConsumerRecord<Object, Object> produced = testEnv.kafkaCluster().getRecord(TOPIC_NAME, actual.getPartitionId(), actual.getOffset(), testEnv.schemaRegistry().createJsonSchemaDeserializer(), testEnv.schemaRegistry().createJsonSchemaDeserializer());
    assertEquals(key, produced.key());
    assertEquals(value, produced.value());
}
Also used : Response(javax.ws.rs.core.Response) ErrorResponse(io.confluent.kafkarest.exceptions.v3.ErrorResponse) ProduceResponse(io.confluent.kafkarest.entities.v3.ProduceResponse) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ProduceRequest(io.confluent.kafkarest.entities.v3.ProduceRequest) ProduceResponse(io.confluent.kafkarest.entities.v3.ProduceResponse) JsonSchema(io.confluent.kafka.schemaregistry.json.JsonSchema) ByteString(com.google.protobuf.ByteString) RecordNameStrategy(io.confluent.kafka.serializers.subject.RecordNameStrategy) SchemaKey(io.confluent.kafkarest.testing.SchemaRegistryFixture.SchemaKey) Test(org.junit.jupiter.api.Test)

Example 2 with ProduceRequest

use of io.confluent.kafkarest.entities.v3.ProduceRequest in project kafka-rest by confluentinc.

the class ProduceActionIntegrationTest method produceProtobufWithSchemaVersion.

@Test
public void produceProtobufWithSchemaVersion() throws Exception {
    String clusterId = testEnv.kafkaCluster().getClusterId();
    ProtobufSchema keySchema = new ProtobufSchema("syntax = \"proto3\"; message MyKey { string foo = 1; }");
    SchemaKey keySchemaKey = testEnv.schemaRegistry().createSchema(DEFAULT_KEY_SUBJECT, keySchema);
    ProtobufSchema valueSchema = new ProtobufSchema("syntax = \"proto3\"; message MyValue { string bar = 1; }");
    SchemaKey valueSchemaKey = testEnv.schemaRegistry().createSchema(DEFAULT_VALUE_SUBJECT, valueSchema);
    ObjectNode key = new ObjectNode(JsonNodeFactory.instance);
    key.put("foo", "foz");
    ObjectNode value = new ObjectNode(JsonNodeFactory.instance);
    value.put("bar", "baz");
    ProduceRequest request = ProduceRequest.builder().setKey(ProduceRequestData.builder().setSchemaVersion(keySchemaKey.getSchemaVersion()).setData(key).build()).setValue(ProduceRequestData.builder().setSchemaVersion(valueSchemaKey.getSchemaVersion()).setData(value).build()).setOriginalSize(0L).build();
    Response response = testEnv.kafkaRest().target().path("/v3/clusters/" + clusterId + "/topics/" + TOPIC_NAME + "/records").request().accept(MediaType.APPLICATION_JSON).post(Entity.entity(request, MediaType.APPLICATION_JSON));
    assertEquals(Status.OK.getStatusCode(), response.getStatus());
    ProduceResponse actual = readProduceResponse(response);
    ConsumerRecord<Message, Message> produced = testEnv.kafkaCluster().getRecord(TOPIC_NAME, actual.getPartitionId(), actual.getOffset(), testEnv.schemaRegistry().createProtobufDeserializer(), testEnv.schemaRegistry().createProtobufDeserializer());
    DynamicMessage.Builder expectedKey = DynamicMessage.newBuilder(keySchema.toDescriptor());
    expectedKey.setField(keySchema.toDescriptor().findFieldByName("foo"), "foz");
    DynamicMessage.Builder expectedValue = DynamicMessage.newBuilder(valueSchema.toDescriptor());
    expectedValue.setField(valueSchema.toDescriptor().findFieldByName("bar"), "baz");
    assertEquals(expectedKey.build().toByteString(), produced.key().toByteString());
    assertEquals(expectedValue.build().toByteString(), produced.value().toByteString());
}
Also used : Response(javax.ws.rs.core.Response) ErrorResponse(io.confluent.kafkarest.exceptions.v3.ErrorResponse) ProduceResponse(io.confluent.kafkarest.entities.v3.ProduceResponse) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DynamicMessage(com.google.protobuf.DynamicMessage) Message(com.google.protobuf.Message) ProduceRequest(io.confluent.kafkarest.entities.v3.ProduceRequest) ProduceResponse(io.confluent.kafkarest.entities.v3.ProduceResponse) ProtobufSchema(io.confluent.kafka.schemaregistry.protobuf.ProtobufSchema) ByteString(com.google.protobuf.ByteString) DynamicMessage(com.google.protobuf.DynamicMessage) SchemaKey(io.confluent.kafkarest.testing.SchemaRegistryFixture.SchemaKey) Test(org.junit.jupiter.api.Test)

Example 3 with ProduceRequest

use of io.confluent.kafkarest.entities.v3.ProduceRequest in project kafka-rest by confluentinc.

the class ProduceActionIntegrationTest method produceAvroWithLatestSchema.

@Test
public void produceAvroWithLatestSchema() throws Exception {
    String clusterId = testEnv.kafkaCluster().getClusterId();
    testEnv.schemaRegistry().createSchema(DEFAULT_KEY_SUBJECT, new AvroSchema("{\"type\": \"string\"}"));
    testEnv.schemaRegistry().createSchema(DEFAULT_VALUE_SUBJECT, new AvroSchema("{\"type\": \"string\"}"));
    String key = "foo";
    String value = "bar";
    ProduceRequest request = ProduceRequest.builder().setKey(ProduceRequestData.builder().setData(TextNode.valueOf(key)).build()).setValue(ProduceRequestData.builder().setData(TextNode.valueOf(value)).build()).setOriginalSize(0L).build();
    Response response = testEnv.kafkaRest().target().path("/v3/clusters/" + clusterId + "/topics/" + TOPIC_NAME + "/records").request().accept(MediaType.APPLICATION_JSON).post(Entity.entity(request, MediaType.APPLICATION_JSON));
    assertEquals(Status.OK.getStatusCode(), response.getStatus());
    ProduceResponse actual = readProduceResponse(response);
    ConsumerRecord<Object, Object> produced = testEnv.kafkaCluster().getRecord(TOPIC_NAME, actual.getPartitionId(), actual.getOffset(), testEnv.schemaRegistry().createAvroDeserializer(), testEnv.schemaRegistry().createAvroDeserializer());
    assertEquals(key, produced.key());
    assertEquals(value, produced.value());
}
Also used : Response(javax.ws.rs.core.Response) ErrorResponse(io.confluent.kafkarest.exceptions.v3.ErrorResponse) ProduceResponse(io.confluent.kafkarest.entities.v3.ProduceResponse) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) ProduceRequest(io.confluent.kafkarest.entities.v3.ProduceRequest) ProduceResponse(io.confluent.kafkarest.entities.v3.ProduceResponse) ByteString(com.google.protobuf.ByteString) Test(org.junit.jupiter.api.Test)

Example 4 with ProduceRequest

use of io.confluent.kafkarest.entities.v3.ProduceRequest in project kafka-rest by confluentinc.

the class ProduceActionIntegrationTest method produceJson.

@Test
public void produceJson() throws Exception {
    String clusterId = testEnv.kafkaCluster().getClusterId();
    String key = "foo";
    String value = "bar";
    ProduceRequest request = ProduceRequest.builder().setKey(ProduceRequestData.builder().setFormat(EmbeddedFormat.JSON).setData(TextNode.valueOf(key)).build()).setValue(ProduceRequestData.builder().setFormat(EmbeddedFormat.JSON).setData(TextNode.valueOf(value)).build()).setOriginalSize(0L).build();
    Response response = testEnv.kafkaRest().target().path("/v3/clusters/" + clusterId + "/topics/" + TOPIC_NAME + "/records").request().accept(MediaType.APPLICATION_JSON).post(Entity.entity(request, MediaType.APPLICATION_JSON));
    assertEquals(Status.OK.getStatusCode(), response.getStatus());
    ProduceResponse actual = readProduceResponse(response);
    KafkaJsonDeserializer<Object> deserializer = new KafkaJsonDeserializer<>();
    deserializer.configure(emptyMap(), /* isKey= */
    false);
    ConsumerRecord<Object, Object> produced = testEnv.kafkaCluster().getRecord(TOPIC_NAME, actual.getPartitionId(), actual.getOffset(), deserializer, deserializer);
    assertEquals(key, produced.key());
    assertEquals(value, produced.value());
}
Also used : Response(javax.ws.rs.core.Response) ErrorResponse(io.confluent.kafkarest.exceptions.v3.ErrorResponse) ProduceResponse(io.confluent.kafkarest.entities.v3.ProduceResponse) KafkaJsonDeserializer(io.confluent.kafka.serializers.KafkaJsonDeserializer) ProduceRequest(io.confluent.kafkarest.entities.v3.ProduceRequest) ProduceResponse(io.confluent.kafkarest.entities.v3.ProduceResponse) ByteString(com.google.protobuf.ByteString) Test(org.junit.jupiter.api.Test)

Example 5 with ProduceRequest

use of io.confluent.kafkarest.entities.v3.ProduceRequest in project kafka-rest by confluentinc.

the class ProduceActionIntegrationTest method produceProtobufWithRawSchemaAndSubject.

@Test
public void produceProtobufWithRawSchemaAndSubject() throws Exception {
    String clusterId = testEnv.kafkaCluster().getClusterId();
    ProtobufSchema keySchema = new ProtobufSchema("syntax = \"proto3\"; message MyKey { string foo = 1; }");
    ProtobufSchema valueSchema = new ProtobufSchema("syntax = \"proto3\"; message MyValue { string bar = 1; }");
    ObjectNode key = new ObjectNode(JsonNodeFactory.instance);
    key.put("foo", "foz");
    ObjectNode value = new ObjectNode(JsonNodeFactory.instance);
    value.put("bar", "baz");
    ProduceRequest request = ProduceRequest.builder().setKey(ProduceRequestData.builder().setFormat(EmbeddedFormat.PROTOBUF).setSubject("my-key-subject").setRawSchema(keySchema.canonicalString()).setData(key).build()).setValue(ProduceRequestData.builder().setFormat(EmbeddedFormat.PROTOBUF).setSubject("my-value-subject").setRawSchema(valueSchema.canonicalString()).setData(value).build()).setOriginalSize(0L).build();
    Response response = testEnv.kafkaRest().target().path("/v3/clusters/" + clusterId + "/topics/" + TOPIC_NAME + "/records").request().accept(MediaType.APPLICATION_JSON).post(Entity.entity(request, MediaType.APPLICATION_JSON));
    assertEquals(Status.OK.getStatusCode(), response.getStatus());
    ProduceResponse actual = readProduceResponse(response);
    ConsumerRecord<Message, Message> produced = testEnv.kafkaCluster().getRecord(TOPIC_NAME, actual.getPartitionId(), actual.getOffset(), testEnv.schemaRegistry().createProtobufDeserializer(), testEnv.schemaRegistry().createProtobufDeserializer());
    DynamicMessage.Builder expectedKey = DynamicMessage.newBuilder(keySchema.toDescriptor());
    expectedKey.setField(keySchema.toDescriptor().findFieldByName("foo"), "foz");
    DynamicMessage.Builder expectedValue = DynamicMessage.newBuilder(valueSchema.toDescriptor());
    expectedValue.setField(valueSchema.toDescriptor().findFieldByName("bar"), "baz");
    assertEquals(expectedKey.build().toByteString(), produced.key().toByteString());
    assertEquals(expectedValue.build().toByteString(), produced.value().toByteString());
}
Also used : Response(javax.ws.rs.core.Response) ErrorResponse(io.confluent.kafkarest.exceptions.v3.ErrorResponse) ProduceResponse(io.confluent.kafkarest.entities.v3.ProduceResponse) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DynamicMessage(com.google.protobuf.DynamicMessage) Message(com.google.protobuf.Message) ProduceRequest(io.confluent.kafkarest.entities.v3.ProduceRequest) ProduceResponse(io.confluent.kafkarest.entities.v3.ProduceResponse) ProtobufSchema(io.confluent.kafka.schemaregistry.protobuf.ProtobufSchema) ByteString(com.google.protobuf.ByteString) DynamicMessage(com.google.protobuf.DynamicMessage) Test(org.junit.jupiter.api.Test)

Aggregations

ProduceRequest (io.confluent.kafkarest.entities.v3.ProduceRequest)71 Test (org.junit.jupiter.api.Test)66 ProduceResponse (io.confluent.kafkarest.entities.v3.ProduceResponse)65 ErrorResponse (io.confluent.kafkarest.exceptions.v3.ErrorResponse)62 ByteString (com.google.protobuf.ByteString)57 Response (javax.ws.rs.core.Response)57 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)18 SchemaKey (io.confluent.kafkarest.testing.SchemaRegistryFixture.SchemaKey)17 TextNode (com.fasterxml.jackson.databind.node.TextNode)12 ProtobufSchema (io.confluent.kafka.schemaregistry.protobuf.ProtobufSchema)12 DynamicMessage (com.google.protobuf.DynamicMessage)11 Message (com.google.protobuf.Message)11 AvroSchema (io.confluent.kafka.schemaregistry.avro.AvroSchema)10 JsonSchema (io.confluent.kafka.schemaregistry.json.JsonSchema)9 ResultOrError (io.confluent.kafkarest.response.StreamingResponse.ResultOrError)9 ByteArrayDeserializer (org.apache.kafka.common.serialization.ByteArrayDeserializer)9 ProduceRequestData (io.confluent.kafkarest.entities.v3.ProduceRequest.ProduceRequestData)7 MappingIterator (com.fasterxml.jackson.databind.MappingIterator)6 EmbeddedFormat (io.confluent.kafkarest.entities.EmbeddedFormat)6 CompletableFuture (java.util.concurrent.CompletableFuture)6