use of io.confluent.kafkarest.exceptions.BadRequestException in project kafka-rest by confluentinc.
the class SchemaManagerImpl method findLatestSchema.
private RegisteredSchema findLatestSchema(String topicName, Optional<String> subject, Optional<SubjectNameStrategy> subjectNameStrategy, boolean isKey) {
String actualSubject = subject.orElse(getSchemaSubjectUnsafe(topicName, isKey, subjectNameStrategy));
SchemaMetadata metadata;
try {
metadata = schemaRegistryClient.getLatestSchemaMetadata(actualSubject);
} catch (IOException | RestClientException e) {
throw Errors.messageSerializationException(String.format("Error when fetching latest schema version. subject = %s", actualSubject), e);
}
SchemaProvider schemaProvider;
try {
schemaProvider = EmbeddedFormat.forSchemaType(metadata.getSchemaType()).getSchemaProvider();
} catch (UnsupportedOperationException e) {
throw new BadRequestException(String.format("Schema subject not supported for schema type = %s", metadata.getSchemaType()), e);
}
ParsedSchema schema;
try {
schema = schemaProvider.parseSchema(metadata.getSchema(), metadata.getReferences(), /* isNew= */
false).orElseThrow(() -> Errors.invalidSchemaException(String.format("Error when fetching latest schema version. subject = %s", actualSubject)));
} catch (SchemaParseException e) {
throw new BadRequestException(String.format("Error parsing schema type = %s", metadata.getSchemaType()), e);
}
return RegisteredSchema.create(actualSubject, metadata.getId(), metadata.getVersion(), schema);
}
use of io.confluent.kafkarest.exceptions.BadRequestException in project kafka-rest by confluentinc.
the class SchemaManagerImplTest method errorRawSchemaNotSupportedWithSchema.
@Test
public void errorRawSchemaNotSupportedWithSchema() {
EmbeddedFormat embeddedFormatMock = mock(EmbeddedFormat.class);
SchemaProvider schemaProviderMock = mock(SchemaProvider.class);
expect(embeddedFormatMock.requiresSchema()).andReturn(true);
expect(embeddedFormatMock.getSchemaProvider()).andThrow(new UnsupportedOperationException("Reason here"));
expect(schemaProviderMock.parseSchema(TextNode.valueOf("rawSchema").toString(), emptyList(), true)).andReturn(Optional.empty());
replay(embeddedFormatMock, schemaProviderMock);
BadRequestException bre = assertThrows(BadRequestException.class, () -> schemaManager.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", bre.getMessage());
assertEquals(400, bre.getCode());
}
use of io.confluent.kafkarest.exceptions.BadRequestException 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.kafkarest.exceptions.BadRequestException 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());
}
use of io.confluent.kafkarest.exceptions.BadRequestException in project kafka-rest by confluentinc.
the class SchemaManagerImplTest method errorRawSchemaNotSupportedWithFormat.
@Test
public void errorRawSchemaNotSupportedWithFormat() {
BadRequestException iae = assertThrows(BadRequestException.class, () -> schemaManager.getSchema(TOPIC_NAME, /* format= */
Optional.of(EmbeddedFormat.JSON), /* subject= */
Optional.empty(), /* subjectNameStrategy= */
Optional.empty(), /* schemaId= */
Optional.empty(), /* schemaVersion= */
Optional.empty(), /* rawSchema= */
Optional.of("rawSchema"), /* isKey= */
true));
assertEquals("JSON does not support schemas.", iae.getMessage());
}
Aggregations