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."));
}
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);
}
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());
}
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);
}
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;
}
Aggregations