use of io.confluent.kafka.schemaregistry.client.rest.entities.Schema 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());
}
use of io.confluent.kafka.schemaregistry.client.rest.entities.Schema 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());
}
use of io.confluent.kafka.schemaregistry.client.rest.entities.Schema in project schema-registry by confluentinc.
the class AbstractSchemaProvider method resolveReferences.
private void resolveReferences(List<SchemaReference> references, Map<String, String> schemas) {
for (SchemaReference reference : references) {
if (reference.getName() == null || reference.getSubject() == null || reference.getVersion() == null) {
throw new IllegalStateException("Invalid reference: " + reference);
}
String subject = reference.getSubject();
if (!schemas.containsKey(reference.getName())) {
Schema schema = schemaVersionFetcher().getByVersion(subject, reference.getVersion(), true);
if (schema == null) {
throw new IllegalStateException("No schema reference found for subject \"" + subject + "\" and version " + reference.getVersion());
}
if (reference.getVersion() == -1) {
// Update the version with the latest
reference.setVersion(schema.getVersion());
}
resolveReferences(schema.getReferences(), schemas);
schemas.put(reference.getName(), schema.getSchema());
}
}
}
use of io.confluent.kafka.schemaregistry.client.rest.entities.Schema in project schema-registry by confluentinc.
the class CachedSchemaRegistryClient method getIdFromRegistry.
private int getIdFromRegistry(String subject, ParsedSchema schema, boolean normalize) throws IOException, RestClientException {
checkMissingSchemaCache(subject, schema, normalize);
io.confluent.kafka.schemaregistry.client.rest.entities.Schema response;
try {
response = restService.lookUpSubjectVersion(schema.canonicalString(), schema.schemaType(), schema.references(), subject, normalize, false);
} catch (RestClientException rce) {
if (isSchemaNotFoundException(rce)) {
missingSchemaCache.put(new SubjectAndSchema(subject, schema, normalize), System.currentTimeMillis());
}
throw rce;
}
return response.getId();
}
use of io.confluent.kafka.schemaregistry.client.rest.entities.Schema in project schema-registry by confluentinc.
the class CachedSchemaRegistryClient method getLatestSchemaMetadata.
@Override
public SchemaMetadata getLatestSchemaMetadata(String subject) throws IOException, RestClientException {
io.confluent.kafka.schemaregistry.client.rest.entities.Schema response = restService.getLatestVersion(subject);
int id = response.getId();
int version = response.getVersion();
String schemaType = response.getSchemaType();
String schema = response.getSchema();
List<SchemaReference> references = response.getReferences();
return new SchemaMetadata(id, version, schemaType, references, schema);
}
Aggregations