Search in sources :

Example 1 with TopicNameStrategy

use of io.confluent.kafka.serializers.subject.TopicNameStrategy in project kafka-rest by confluentinc.

the class ProduceActionIntegrationTest method produceAvroWithLatestSchemaAndSubjectStrategy.

@Test
public void produceAvroWithLatestSchemaAndSubjectStrategy() throws Exception {
    String clusterId = testEnv.kafkaCluster().getClusterId();
    AvroSchema keySchema = new AvroSchema("{\"type\": \"record\", \"name\": \"MyKey\", \"fields\": [{\"name\": \"foo\", " + "\"type\": \"string\"}]}");
    String keySubject = new TopicNameStrategy().subjectName(TOPIC_NAME, /* isKey= */
    true, keySchema);
    testEnv.schemaRegistry().createSchema(keySubject, keySchema);
    AvroSchema valueSchema = new AvroSchema("{\"type\": \"record\", \"name\": \"MyValue\", \"fields\": [{\"name\": \"bar\", " + "\"type\": \"string\"}]}");
    String valueSubject = new TopicNameStrategy().subjectName(TOPIC_NAME, /* isKey= */
    false, valueSchema);
    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.TOPIC_NAME).setData(key).build()).setValue(ProduceRequestData.builder().setSubjectNameStrategy(EnumSubjectNameStrategy.TOPIC_NAME).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().createAvroDeserializer(), testEnv.schemaRegistry().createAvroDeserializer());
    GenericRecord expectedKey = new GenericData.Record(keySchema.rawSchema());
    expectedKey.put("foo", "foz");
    GenericRecord expectedValue = new GenericData.Record(valueSchema.rawSchema());
    expectedValue.put("bar", "baz");
    assertEquals(expectedKey, produced.key());
    assertEquals(expectedValue, 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) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ProduceRequest(io.confluent.kafkarest.entities.v3.ProduceRequest) ProduceResponse(io.confluent.kafkarest.entities.v3.ProduceResponse) TopicNameStrategy(io.confluent.kafka.serializers.subject.TopicNameStrategy) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) GenericRecord(org.apache.avro.generic.GenericRecord) ByteString(com.google.protobuf.ByteString) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.junit.jupiter.api.Test)

Example 2 with TopicNameStrategy

use of io.confluent.kafka.serializers.subject.TopicNameStrategy in project kafka-rest by confluentinc.

the class SchemaManagerImplTest method getSchemaFromSchemaVersionThrowsInvalidSchemaException.

@Test
public void getSchemaFromSchemaVersionThrowsInvalidSchemaException() {
    SchemaRegistryClient schemaRegistryClientMock = mock(SchemaRegistryClient.class);
    Schema schemaMock = mock(Schema.class);
    expect(schemaRegistryClientMock.getByVersion("subject1", 0, false)).andReturn(schemaMock);
    expect(schemaMock.getSchemaType()).andReturn(EmbeddedFormat.AVRO.toString());
    expect(schemaMock.getSchema()).andReturn(null);
    expect(schemaMock.getReferences()).andReturn(Collections.emptyList());
    replay(schemaRegistryClientMock, schemaMock);
    SchemaManager mySchemaManager = new SchemaManagerImpl(schemaRegistryClientMock, new TopicNameStrategy());
    RestConstraintViolationException iae = assertThrows(RestConstraintViolationException.class, () -> mySchemaManager.getSchema(TOPIC_NAME, /* format= */
    Optional.empty(), /* subject= */
    Optional.of("subject1"), /* subjectNameStrategy= */
    Optional.empty(), /* schemaId= */
    Optional.empty(), /* schemaVersion= */
    Optional.of(0), /* rawSchema= */
    Optional.empty(), /* isKey= */
    true));
    assertEquals("Invalid schema: Error when fetching schema by version. subject = subject1, version = 0", iae.getMessage());
    assertEquals(42205, iae.getErrorCode());
}
Also used : RestConstraintViolationException(io.confluent.rest.exceptions.RestConstraintViolationException) Schema(io.confluent.kafka.schemaregistry.client.rest.entities.Schema) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) RegisteredSchema(io.confluent.kafkarest.entities.RegisteredSchema) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) TopicNameStrategy(io.confluent.kafka.serializers.subject.TopicNameStrategy) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) MockSchemaRegistryClient(io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient) Test(org.junit.jupiter.api.Test)

Example 3 with TopicNameStrategy

use of io.confluent.kafka.serializers.subject.TopicNameStrategy in project kafka-rest by confluentinc.

the class SchemaManagerImplTest method errorRegisteringSchema.

@Test
public void errorRegisteringSchema() throws RestClientException, IOException {
    SchemaRegistryClient schemaRegistryClientMock = mock(SchemaRegistryClient.class);
    ParsedSchema parsedSchemaMock = mock(ParsedSchema.class);
    EmbeddedFormat embeddedFormatMock = mock(EmbeddedFormat.class);
    SchemaProvider schemaProviderMock = mock(SchemaProvider.class);
    expect(embeddedFormatMock.requiresSchema()).andReturn(true);
    expect(embeddedFormatMock.getSchemaProvider()).andReturn(schemaProviderMock);
    expect(schemaProviderMock.parseSchema(TextNode.valueOf("rawString").toString(), emptyList(), true)).andReturn(Optional.of(parsedSchemaMock));
    expect(schemaRegistryClientMock.getId("subject1", parsedSchemaMock)).andThrow(new IOException("Can't get Schema"));
    expect(schemaRegistryClientMock.register("subject1", parsedSchemaMock)).andThrow(new IOException("Can't register Schema"));
    replay(schemaRegistryClientMock, embeddedFormatMock, schemaProviderMock);
    SchemaManager mySchemaManager = new SchemaManagerImpl(schemaRegistryClientMock, new TopicNameStrategy());
    RestConstraintViolationException rcve = assertThrows(RestConstraintViolationException.class, () -> mySchemaManager.getSchema(TOPIC_NAME, /* format= */
    Optional.of(embeddedFormatMock), /* subject= */
    Optional.of("subject1"), /* subjectNameStrategy= */
    Optional.empty(), /* schemaId= */
    Optional.empty(), /* schemaVersion= */
    Optional.empty(), /* rawSchema= */
    Optional.of(TextNode.valueOf("rawString").toString()), /* isKey= */
    true));
    assertEquals("Error serializing message. Error when registering schema. format = EasyMock for class io.confluent.kafkarest.entities.EmbeddedFormat, subject = subject1, schema = null\n" + "Can't register Schema", rcve.getMessage());
    assertEquals(42207, rcve.getErrorCode());
}
Also used : EmbeddedFormat(io.confluent.kafkarest.entities.EmbeddedFormat) RestConstraintViolationException(io.confluent.rest.exceptions.RestConstraintViolationException) ProtobufSchemaProvider(io.confluent.kafka.schemaregistry.protobuf.ProtobufSchemaProvider) SchemaProvider(io.confluent.kafka.schemaregistry.SchemaProvider) JsonSchemaProvider(io.confluent.kafka.schemaregistry.json.JsonSchemaProvider) AvroSchemaProvider(io.confluent.kafka.schemaregistry.avro.AvroSchemaProvider) TopicNameStrategy(io.confluent.kafka.serializers.subject.TopicNameStrategy) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) IOException(java.io.IOException) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) MockSchemaRegistryClient(io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient) Test(org.junit.jupiter.api.Test)

Example 4 with TopicNameStrategy

use of io.confluent.kafka.serializers.subject.TopicNameStrategy in project kafka-rest by confluentinc.

the class SchemaManagerImplTest method errorFetchingSchemaBySchemaVersion.

@Test
public void errorFetchingSchemaBySchemaVersion() {
    SchemaRegistryClient schemaRegistryClientMock = mock(SchemaRegistryClient.class);
    Schema schemaMock = mock(Schema.class);
    expect(schemaRegistryClientMock.getByVersion("subject1", 123, false)).andReturn(schemaMock);
    expect(schemaMock.getSchemaType()).andReturn(EmbeddedFormat.JSON.toString());
    expect(schemaMock.getSchema()).andReturn(null);
    expect(schemaMock.getReferences()).andReturn(Collections.emptyList());
    replay(schemaRegistryClientMock, schemaMock);
    SchemaManager mySchemaManager = new SchemaManagerImpl(schemaRegistryClientMock, new TopicNameStrategy());
    RestConstraintViolationException iae = assertThrows(RestConstraintViolationException.class, () -> mySchemaManager.getSchema(TOPIC_NAME, /* format= */
    Optional.empty(), /* subject= */
    Optional.of("subject1"), /* subjectNameStrategy= */
    Optional.empty(), /* schemaId= */
    Optional.empty(), /* schemaVersion= */
    Optional.of(123), /* rawSchema= */
    Optional.empty(), /* isKey= */
    true));
    assertEquals("Invalid schema: Error when fetching schema by version. subject = subject1, version = 123", iae.getMessage());
    assertEquals(42205, iae.getErrorCode());
}
Also used : RestConstraintViolationException(io.confluent.rest.exceptions.RestConstraintViolationException) Schema(io.confluent.kafka.schemaregistry.client.rest.entities.Schema) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) RegisteredSchema(io.confluent.kafkarest.entities.RegisteredSchema) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) TopicNameStrategy(io.confluent.kafka.serializers.subject.TopicNameStrategy) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) MockSchemaRegistryClient(io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient) Test(org.junit.jupiter.api.Test)

Example 5 with TopicNameStrategy

use of io.confluent.kafka.serializers.subject.TopicNameStrategy in project kafka-rest by confluentinc.

the class SchemaManagerImplTest method errorRawSchemaCantParseSchema.

@Test
public void errorRawSchemaCantParseSchema() {
    SchemaRegistryClient schemaRegistryClientMock = mock(SchemaRegistryClient.class);
    EmbeddedFormat embeddedFormatMock = mock(EmbeddedFormat.class);
    SchemaProvider schemaProviderMock = mock(SchemaProvider.class);
    expect(embeddedFormatMock.requiresSchema()).andReturn(true);
    expect(embeddedFormatMock.getSchemaProvider()).andThrow(new UnsupportedOperationException("Unsupported"));
    replay(embeddedFormatMock, schemaProviderMock, schemaRegistryClientMock);
    SchemaManager mySchemaManager = new SchemaManagerImpl(schemaRegistryClientMock, new TopicNameStrategy());
    BadRequestException rcve = assertThrows(BadRequestException.class, () -> mySchemaManager.getSchema(TOPIC_NAME, /* format= */
    Optional.of(embeddedFormatMock), /* subject= */
    Optional.empty(), /* subjectNameStrategy= */
    Optional.empty(), /* schemaId= */
    Optional.empty(), /* schemaVersion= */
    Optional.empty(), /* rawSchema= */
    Optional.of(TextNode.valueOf("rawSchema").toString()), /* isKey= */
    true));
    assertEquals("Raw schema not supported with format = EasyMock for class io.confluent.kafkarest.entities.EmbeddedFormat", rcve.getMessage());
    assertEquals(400, rcve.getCode());
}
Also used : EmbeddedFormat(io.confluent.kafkarest.entities.EmbeddedFormat) ProtobufSchemaProvider(io.confluent.kafka.schemaregistry.protobuf.ProtobufSchemaProvider) SchemaProvider(io.confluent.kafka.schemaregistry.SchemaProvider) JsonSchemaProvider(io.confluent.kafka.schemaregistry.json.JsonSchemaProvider) AvroSchemaProvider(io.confluent.kafka.schemaregistry.avro.AvroSchemaProvider) TopicNameStrategy(io.confluent.kafka.serializers.subject.TopicNameStrategy) BadRequestException(io.confluent.kafkarest.exceptions.BadRequestException) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) MockSchemaRegistryClient(io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient) Test(org.junit.jupiter.api.Test)

Aggregations

TopicNameStrategy (io.confluent.kafka.serializers.subject.TopicNameStrategy)11 Test (org.junit.jupiter.api.Test)11 MockSchemaRegistryClient (io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient)7 SchemaRegistryClient (io.confluent.kafka.schemaregistry.client.SchemaRegistryClient)7 AvroSchema (io.confluent.kafka.schemaregistry.avro.AvroSchema)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 ByteString (com.google.protobuf.ByteString)4 ParsedSchema (io.confluent.kafka.schemaregistry.ParsedSchema)4 ProduceRequest (io.confluent.kafkarest.entities.v3.ProduceRequest)4 ProduceResponse (io.confluent.kafkarest.entities.v3.ProduceResponse)4 ErrorResponse (io.confluent.kafkarest.exceptions.v3.ErrorResponse)4 RestConstraintViolationException (io.confluent.rest.exceptions.RestConstraintViolationException)4 Response (javax.ws.rs.core.Response)4 Schema (io.confluent.kafka.schemaregistry.client.rest.entities.Schema)3 RegisteredSchema (io.confluent.kafkarest.entities.RegisteredSchema)3 BadRequestException (io.confluent.kafkarest.exceptions.BadRequestException)3 SchemaProvider (io.confluent.kafka.schemaregistry.SchemaProvider)2 AvroSchemaProvider (io.confluent.kafka.schemaregistry.avro.AvroSchemaProvider)2 SchemaMetadata (io.confluent.kafka.schemaregistry.client.SchemaMetadata)2 JsonSchema (io.confluent.kafka.schemaregistry.json.JsonSchema)2