use of org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException in project pulsar by apache.
the class SchemaRegistryServiceWithSchemaDataValidatorTest method testIsCompatibleWithBadSchemaData.
@Test
public void testIsCompatibleWithBadSchemaData() {
String schemaId = "test-schema-id";
SchemaCompatibilityStrategy strategy = SchemaCompatibilityStrategy.FULL;
CompletableFuture<Boolean> future = new CompletableFuture<>();
when(underlyingService.isCompatible(eq(schemaId), any(SchemaData.class), eq(strategy))).thenReturn(future);
SchemaData schemaData = SchemaData.builder().type(SchemaType.BOOLEAN).data(new byte[10]).build();
try {
service.isCompatible(schemaId, schemaData, strategy).get();
fail("Should fail isCompatible check");
} catch (Exception e) {
assertTrue(e.getCause() instanceof InvalidSchemaDataException);
}
verify(underlyingService, times(0)).isCompatible(eq(schemaId), same(schemaData), eq(strategy));
}
use of org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException in project pulsar by yahoo.
the class SchemasResourceBase method postSchema.
public void postSchema(PostSchemaPayload payload, boolean authoritative, AsyncResponse response) {
validateDestinationAndAdminOperation(authoritative);
getSchemaCompatibilityStrategyAsync().thenAccept(schemaCompatibilityStrategy -> {
byte[] data;
if (SchemaType.KEY_VALUE.name().equals(payload.getType())) {
try {
data = DefaultImplementation.getDefaultImplementation().convertKeyValueDataStringToSchemaInfoSchema(payload.getSchema().getBytes(Charsets.UTF_8));
} catch (IOException conversionError) {
log.error("[{}] Failed to post schema for topic {}", clientAppId(), topicName, conversionError);
response.resume(new RestException(conversionError));
return;
}
} else {
data = payload.getSchema().getBytes(Charsets.UTF_8);
}
pulsar().getSchemaRegistryService().putSchemaIfAbsent(getSchemaId(), SchemaData.builder().data(data).isDeleted(false).timestamp(clock.millis()).type(SchemaType.valueOf(payload.getType())).user(defaultIfEmpty(clientAppId(), "")).props(payload.getProperties()).build(), schemaCompatibilityStrategy).thenAccept(version -> response.resume(Response.accepted().entity(PostSchemaResponse.builder().version(version).build()).build())).exceptionally(error -> {
Throwable throwable = FutureUtil.unwrapCompletionException(error);
if (throwable instanceof IncompatibleSchemaException) {
response.resume(Response.status(Response.Status.CONFLICT.getStatusCode(), throwable.getMessage()).build());
} else if (throwable instanceof InvalidSchemaDataException) {
response.resume(Response.status(422, /* Unprocessable Entity */
throwable.getMessage()).build());
} else {
log.error("[{}] Failed to post schema for topic {}", clientAppId(), topicName, throwable);
response.resume(new RestException(throwable));
}
return null;
});
}).exceptionally(error -> {
Throwable throwable = FutureUtil.unwrapCompletionException(error);
if (throwable instanceof RestException) {
// Unprocessable Entity
response.resume(Response.status(((RestException) throwable).getResponse().getStatus(), throwable.getMessage()).build());
} else {
log.error("[{}] Failed to post schema for topic {}", clientAppId(), topicName, throwable);
response.resume(new RestException(throwable));
}
return null;
});
}
use of org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException in project incubator-pulsar by apache.
the class SchemaRegistryServiceWithSchemaDataValidatorTest method testIsCompatibleWithBadSchemaData.
@Test
public void testIsCompatibleWithBadSchemaData() {
String schemaId = "test-schema-id";
SchemaCompatibilityStrategy strategy = SchemaCompatibilityStrategy.FULL;
CompletableFuture<Boolean> future = new CompletableFuture<>();
when(underlyingService.isCompatible(eq(schemaId), any(SchemaData.class), eq(strategy))).thenReturn(future);
SchemaData schemaData = SchemaData.builder().type(SchemaType.BOOLEAN).data(new byte[10]).build();
try {
service.isCompatible(schemaId, schemaData, strategy).get();
fail("Should fail isCompatible check");
} catch (Exception e) {
assertTrue(e.getCause() instanceof InvalidSchemaDataException);
}
verify(underlyingService, times(0)).isCompatible(eq(schemaId), same(schemaData), eq(strategy));
}
use of org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException in project incubator-pulsar by apache.
the class SchemaRegistryServiceWithSchemaDataValidatorTest method testPutSchemaIfAbsentWithBadSchemaData.
@Test
public void testPutSchemaIfAbsentWithBadSchemaData() {
String schemaId = "test-schema-id";
SchemaCompatibilityStrategy strategy = SchemaCompatibilityStrategy.FULL;
CompletableFuture<SchemaVersion> future = new CompletableFuture<>();
when(underlyingService.putSchemaIfAbsent(eq(schemaId), any(SchemaData.class), eq(strategy))).thenReturn(future);
SchemaData schemaData = SchemaData.builder().type(SchemaType.BOOLEAN).data(new byte[10]).build();
try {
service.putSchemaIfAbsent(schemaId, schemaData, strategy).get();
fail("Should fail putSchemaIfAbsent");
} catch (Exception e) {
assertTrue(e.getCause() instanceof InvalidSchemaDataException);
}
verify(underlyingService, times(0)).putSchemaIfAbsent(eq(schemaId), same(schemaData), eq(strategy));
}
use of org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException in project incubator-pulsar by apache.
the class SchemasResourceBase method postSchema.
public void postSchema(PostSchemaPayload payload, boolean authoritative, AsyncResponse response) {
validateDestinationAndAdminOperation(authoritative);
getSchemaCompatibilityStrategyAsync().thenAccept(schemaCompatibilityStrategy -> {
byte[] data;
if (SchemaType.KEY_VALUE.name().equals(payload.getType())) {
try {
data = DefaultImplementation.getDefaultImplementation().convertKeyValueDataStringToSchemaInfoSchema(payload.getSchema().getBytes(Charsets.UTF_8));
} catch (IOException conversionError) {
log.error("[{}] Failed to post schema for topic {}", clientAppId(), topicName, conversionError);
response.resume(new RestException(conversionError));
return;
}
} else {
data = payload.getSchema().getBytes(Charsets.UTF_8);
}
pulsar().getSchemaRegistryService().putSchemaIfAbsent(getSchemaId(), SchemaData.builder().data(data).isDeleted(false).timestamp(clock.millis()).type(SchemaType.valueOf(payload.getType())).user(defaultIfEmpty(clientAppId(), "")).props(payload.getProperties()).build(), schemaCompatibilityStrategy).thenAccept(version -> response.resume(Response.accepted().entity(PostSchemaResponse.builder().version(version).build()).build())).exceptionally(error -> {
Throwable throwable = FutureUtil.unwrapCompletionException(error);
if (throwable instanceof IncompatibleSchemaException) {
response.resume(Response.status(Response.Status.CONFLICT.getStatusCode(), throwable.getMessage()).build());
} else if (throwable instanceof InvalidSchemaDataException) {
response.resume(Response.status(422, /* Unprocessable Entity */
throwable.getMessage()).build());
} else {
log.error("[{}] Failed to post schema for topic {}", clientAppId(), topicName, throwable);
response.resume(new RestException(throwable));
}
return null;
});
}).exceptionally(error -> {
Throwable throwable = FutureUtil.unwrapCompletionException(error);
if (throwable instanceof RestException) {
// Unprocessable Entity
response.resume(Response.status(((RestException) throwable).getResponse().getStatus(), throwable.getMessage()).build());
} else {
log.error("[{}] Failed to post schema for topic {}", clientAppId(), topicName, throwable);
response.resume(new RestException(throwable));
}
return null;
});
}
Aggregations