Search in sources :

Example 1 with SubjectNameStrategy

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

the class SchemaManagerImplTest method getSchema_avro_schemaVersion_subjectNameStrategy_strategyReturnsNull.

@Test
public void getSchema_avro_schemaVersion_subjectNameStrategy_strategyReturnsNull() {
    SubjectNameStrategy strategy = new NullReturningSubjectNameStrategy();
    strategy.subjectName(TOPIC_NAME, /* isKey= */
    true, /* schema= */
    null);
    IllegalArgumentException rcve = assertThrows(IllegalArgumentException.class, () -> schemaManager.getSchema(TOPIC_NAME, /* format= */
    Optional.empty(), /* subject= */
    Optional.empty(), /* subjectNameStrategy= */
    Optional.of(strategy), /* schemaId= */
    Optional.empty(), /* schemaVersion= */
    Optional.of(100), /* rawSchema= */
    Optional.empty(), /* isKey= */
    true));
    assertTrue(rcve.getMessage().startsWith("Cannot use schema_subject_strategy=io.confluent.kafkarest.controllers.SchemaManagerImplTest$NullReturningSubjectNameStrategy@"));
    assertTrue(rcve.getMessage().endsWith(" without schema_id or schema."));
}
Also used : SubjectNameStrategy(io.confluent.kafka.serializers.subject.strategy.SubjectNameStrategy) Test(org.junit.jupiter.api.Test)

Example 2 with SubjectNameStrategy

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

the class SchemaManagerImplTest method getSchema_avro_latestSchema_subjectNameStrategy.

@Test
public void getSchema_avro_latestSchema_subjectNameStrategy() throws Exception {
    ParsedSchema schema = new AvroSchema("{\"type\": \"int\"}");
    SubjectNameStrategy strategy = new MySubjectNameStrategy();
    String subject = strategy.subjectName(TOPIC_NAME, /* isKey= */
    true, /* schema= */
    null);
    int schemaId = schemaRegistryClient.register(subject, schema);
    int schemaVersion = schemaRegistryClient.getVersion(subject, schema);
    RegisteredSchema actual = schemaManager.getSchema(TOPIC_NAME, /* format= */
    Optional.empty(), /* subject= */
    Optional.empty(), /* subjectNameStrategy= */
    Optional.of(strategy), /* schemaId= */
    Optional.empty(), /* schemaVersion= */
    Optional.empty(), /* rawSchema= */
    Optional.empty(), /* isKey= */
    true);
    assertEquals(RegisteredSchema.create(subject, schemaId, schemaVersion, schema), actual);
}
Also used : RegisteredSchema(io.confluent.kafkarest.entities.RegisteredSchema) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) SubjectNameStrategy(io.confluent.kafka.serializers.subject.strategy.SubjectNameStrategy) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) Test(org.junit.jupiter.api.Test)

Example 3 with SubjectNameStrategy

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

the class SchemaManagerImplTest method getSchema_avro_schemaVersion_subjectNameStrategy_strategyDependsOnSchema.

@Test
public void getSchema_avro_schemaVersion_subjectNameStrategy_strategyDependsOnSchema() throws Exception {
    ParsedSchema schema = new AvroSchema("{\"type\": \"int\"}");
    SubjectNameStrategy strategy = new SchemaDependentSubjectNameStrategy();
    String subject = strategy.subjectName(TOPIC_NAME, /* isKey= */
    true, /* schema= */
    schema);
    schemaRegistryClient.register(subject, schema);
    int schemaVersion = schemaRegistryClient.getVersion(subject, schema);
    BadRequestException bre = assertThrows(BadRequestException.class, () -> schemaManager.getSchema(TOPIC_NAME, /* format= */
    Optional.empty(), /* subject= */
    Optional.empty(), /* subjectNameStrategy= */
    Optional.of(strategy), /* schemaId= */
    Optional.empty(), /* schemaVersion= */
    Optional.of(schemaVersion), /* rawSchema= */
    Optional.empty(), /* isKey= */
    true));
    assertEquals("Schema does not exist for subject: my-subject-, version: 1", bre.getMessage());
    assertEquals(400, bre.getCode());
}
Also used : AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) SubjectNameStrategy(io.confluent.kafka.serializers.subject.strategy.SubjectNameStrategy) BadRequestException(io.confluent.kafkarest.exceptions.BadRequestException) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) Test(org.junit.jupiter.api.Test)

Example 4 with SubjectNameStrategy

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

the class SchemaManagerImplTest method getSchema_avro_schemaId_subjectNameStrategy.

@Test
public void getSchema_avro_schemaId_subjectNameStrategy() throws Exception {
    ParsedSchema schema = new AvroSchema("{\"type\": \"int\"}");
    SubjectNameStrategy strategy = new MySubjectNameStrategy();
    String subject = strategy.subjectName(TOPIC_NAME, /* isKey= */
    true, /* schema= */
    null);
    int schemaId = schemaRegistryClient.register(subject, schema);
    int schemaVersion = schemaRegistryClient.getVersion(subject, schema);
    RegisteredSchema actual = schemaManager.getSchema(TOPIC_NAME, /* format= */
    Optional.empty(), /* subject= */
    Optional.empty(), /* subjectNameStrategy= */
    Optional.of(strategy), /* schemaId= */
    Optional.of(schemaId), /* schemaVersion= */
    Optional.empty(), /* rawSchema= */
    Optional.empty(), /* isKey= */
    true);
    assertEquals(RegisteredSchema.create(subject, schemaId, schemaVersion, schema), actual);
}
Also used : RegisteredSchema(io.confluent.kafkarest.entities.RegisteredSchema) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) SubjectNameStrategy(io.confluent.kafka.serializers.subject.strategy.SubjectNameStrategy) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) Test(org.junit.jupiter.api.Test)

Example 5 with SubjectNameStrategy

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

the class SchemaManagerImpl method getSchemaSubjectUnsafe.

/**
 * Tries to get the schema subject from only schema_subject_strategy, {@code topicName} and {@code
 * isKey}.
 *
 * <p>This operation is only really supported if schema_subject_strategy does not depend on the
 * parsed schema to generate the subject name, as we need the subject name to fetch the schema by
 * version. That's the case, for example, of TopicNameStrategy
 * (schema_subject_strategy=TOPIC_NAME). Since TopicNameStrategy is so popular, instead of
 * requiring users to always specify schema_subject if using schema_version?, we try using the
 * strategy to generate the subject name, and fail if that does not work out.
 */
private String getSchemaSubjectUnsafe(String topicName, boolean isKey, Optional<SubjectNameStrategy> subjectNameStrategy) {
    SubjectNameStrategy strategy = subjectNameStrategy.orElse(defaultSubjectNameStrategy);
    String subject = null;
    Exception cause = null;
    try {
        subject = strategy.subjectName(topicName, isKey, /* schema= */
        null);
    } catch (Exception e) {
        cause = e;
    }
    if (subject == null) {
        IllegalArgumentException error = new IllegalArgumentException(String.format("Cannot use%s schema_subject_strategy%s without schema_id or schema.", subjectNameStrategy.map(requestStrategy -> "").orElse(" default"), subjectNameStrategy.map(requestStrategy -> "=" + strategy).orElse("")));
        if (cause != null) {
            error.initCause(cause);
        }
        throw error;
    }
    return subject;
}
Also used : SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) Collections.emptyList(java.util.Collections.emptyList) IOException(java.io.IOException) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) Schema(io.confluent.kafka.schemaregistry.client.rest.entities.Schema) SubjectNameStrategy(io.confluent.kafka.serializers.subject.strategy.SubjectNameStrategy) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) SchemaProvider(io.confluent.kafka.schemaregistry.SchemaProvider) Errors(io.confluent.kafkarest.Errors) Objects.requireNonNull(java.util.Objects.requireNonNull) Optional(java.util.Optional) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) SchemaMetadata(io.confluent.kafka.schemaregistry.client.SchemaMetadata) RegisteredSchema(io.confluent.kafkarest.entities.RegisteredSchema) SchemaParseException(org.apache.avro.SchemaParseException) BadRequestException(io.confluent.kafkarest.exceptions.BadRequestException) EmbeddedFormat(io.confluent.kafkarest.entities.EmbeddedFormat) SubjectNameStrategy(io.confluent.kafka.serializers.subject.strategy.SubjectNameStrategy) IOException(java.io.IOException) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) SchemaParseException(org.apache.avro.SchemaParseException) BadRequestException(io.confluent.kafkarest.exceptions.BadRequestException)

Aggregations

SubjectNameStrategy (io.confluent.kafka.serializers.subject.strategy.SubjectNameStrategy)7 ParsedSchema (io.confluent.kafka.schemaregistry.ParsedSchema)6 Test (org.junit.jupiter.api.Test)6 RegisteredSchema (io.confluent.kafkarest.entities.RegisteredSchema)5 AvroSchema (io.confluent.kafka.schemaregistry.avro.AvroSchema)4 BadRequestException (io.confluent.kafkarest.exceptions.BadRequestException)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 SchemaProvider (io.confluent.kafka.schemaregistry.SchemaProvider)1 SchemaMetadata (io.confluent.kafka.schemaregistry.client.SchemaMetadata)1 SchemaRegistryClient (io.confluent.kafka.schemaregistry.client.SchemaRegistryClient)1 Schema (io.confluent.kafka.schemaregistry.client.rest.entities.Schema)1 RestClientException (io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException)1 Errors (io.confluent.kafkarest.Errors)1 EmbeddedFormat (io.confluent.kafkarest.entities.EmbeddedFormat)1 IOException (java.io.IOException)1 Collections.emptyList (java.util.Collections.emptyList)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Optional (java.util.Optional)1 SchemaParseException (org.apache.avro.SchemaParseException)1