Search in sources :

Example 16 with SchemaKey

use of io.confluent.kafkarest.testing.SchemaRegistryFixture.SchemaKey in project kafka-rest by confluentinc.

the class ProduceActionIntegrationTest method produceJsonschemaWithSchemaId.

@Test
public void produceJsonschemaWithSchemaId() throws Exception {
    String clusterId = testEnv.kafkaCluster().getClusterId();
    SchemaKey keySchema = testEnv.schemaRegistry().createSchema(DEFAULT_KEY_SUBJECT, new JsonSchema("{\"type\": \"string\"}"));
    SchemaKey valueSchema = testEnv.schemaRegistry().createSchema(DEFAULT_VALUE_SUBJECT, new JsonSchema("{\"type\": \"string\"}"));
    TextNode key = TextNode.valueOf("foo");
    TextNode value = TextNode.valueOf("bar");
    ProduceRequest request = ProduceRequest.builder().setKey(ProduceRequestData.builder().setSchemaId(keySchema.getSchemaId()).setData(key).build()).setValue(ProduceRequestData.builder().setSchemaId(valueSchema.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) ProduceRequest(io.confluent.kafkarest.entities.v3.ProduceRequest) ProduceResponse(io.confluent.kafkarest.entities.v3.ProduceResponse) JsonSchema(io.confluent.kafka.schemaregistry.json.JsonSchema) TextNode(com.fasterxml.jackson.databind.node.TextNode) ByteString(com.google.protobuf.ByteString) SchemaKey(io.confluent.kafkarest.testing.SchemaRegistryFixture.SchemaKey) Test(org.junit.jupiter.api.Test)

Example 17 with SchemaKey

use of io.confluent.kafkarest.testing.SchemaRegistryFixture.SchemaKey in project kafka-rest by confluentinc.

the class ProduceActionIntegrationTest method produceAvroWithSchemaVersionAndSubject.

@Test
public void produceAvroWithSchemaVersionAndSubject() throws Exception {
    String clusterId = testEnv.kafkaCluster().getClusterId();
    SchemaKey keySchema = testEnv.schemaRegistry().createSchema("my-key-subject", new AvroSchema("{\"type\": \"string\"}"));
    SchemaKey valueSchema = testEnv.schemaRegistry().createSchema("my-value-subject", new AvroSchema("{\"type\": \"string\"}"));
    String key = "foo";
    String value = "bar";
    ProduceRequest request = ProduceRequest.builder().setKey(ProduceRequestData.builder().setSubject("my-key-subject").setSchemaVersion(keySchema.getSchemaVersion()).setData(TextNode.valueOf(key)).build()).setValue(ProduceRequestData.builder().setSubject("my-value-subject").setSchemaVersion(valueSchema.getSchemaVersion()).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) SchemaKey(io.confluent.kafkarest.testing.SchemaRegistryFixture.SchemaKey) Test(org.junit.jupiter.api.Test)

Aggregations

ByteString (com.google.protobuf.ByteString)17 ProduceRequest (io.confluent.kafkarest.entities.v3.ProduceRequest)17 ProduceResponse (io.confluent.kafkarest.entities.v3.ProduceResponse)17 ErrorResponse (io.confluent.kafkarest.exceptions.v3.ErrorResponse)17 SchemaKey (io.confluent.kafkarest.testing.SchemaRegistryFixture.SchemaKey)17 Response (javax.ws.rs.core.Response)17 Test (org.junit.jupiter.api.Test)17 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)9 AvroSchema (io.confluent.kafka.schemaregistry.avro.AvroSchema)6 JsonSchema (io.confluent.kafka.schemaregistry.json.JsonSchema)6 DynamicMessage (com.google.protobuf.DynamicMessage)5 Message (com.google.protobuf.Message)5 ProtobufSchema (io.confluent.kafka.schemaregistry.protobuf.ProtobufSchema)5 TextNode (com.fasterxml.jackson.databind.node.TextNode)4 RecordNameStrategy (io.confluent.kafka.serializers.subject.RecordNameStrategy)3 TopicNameStrategy (io.confluent.kafka.serializers.subject.TopicNameStrategy)2 GenericRecord (org.apache.avro.generic.GenericRecord)2 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)2