use of org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy in project pulsar by apache.
the class NamespacesImpl method getSchemaCompatibilityStrategyAsync.
@Override
public CompletableFuture<SchemaCompatibilityStrategy> getSchemaCompatibilityStrategyAsync(String namespace) {
NamespaceName ns = NamespaceName.get(namespace);
WebTarget path = namespacePath(ns, "schemaCompatibilityStrategy");
final CompletableFuture<SchemaCompatibilityStrategy> future = new CompletableFuture<>();
asyncGetRequest(path, new InvocationCallback<SchemaCompatibilityStrategy>() {
@Override
public void completed(SchemaCompatibilityStrategy schemaCompatibilityStrategy) {
future.complete(schemaCompatibilityStrategy);
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
use of org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy 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.common.policies.data.SchemaCompatibilityStrategy in project pulsar by apache.
the class SchemaRegistryServiceWithSchemaDataValidatorTest method testIsCompatibleWithGoodSchemaData.
@Test
public void testIsCompatibleWithGoodSchemaData() {
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[0]).build();
assertSame(future, service.isCompatible(schemaId, schemaData, strategy));
verify(underlyingService, times(1)).isCompatible(eq(schemaId), same(schemaData), eq(strategy));
}
use of org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy in project pulsar by apache.
the class PulsarFunctionsTest method testMergeFunction.
protected void testMergeFunction() throws Exception {
log.info("start merge function test ...");
String ns = "public/ns-merge-" + randomName(8);
@Cleanup PulsarAdmin pulsarAdmin = getPulsarAdmin();
pulsarAdmin.namespaces().createNamespace(ns);
pulsarAdmin.namespaces().setSchemaCompatibilityStrategy(ns, SchemaCompatibilityStrategy.ALWAYS_COMPATIBLE);
SchemaCompatibilityStrategy strategy = pulsarAdmin.namespaces().getSchemaCompatibilityStrategy(ns);
log.info("namespace {} SchemaCompatibilityStrategy is {}", ns, strategy);
@Cleanup PulsarClient pulsarClient = getPulsarClient();
ObjectNode inputSpecNode = objectMapper.createObjectNode();
Map<String, AtomicInteger> topicMsgCntMap = new ConcurrentHashMap<>();
int messagePerTopic = 10;
prepareDataForMergeFunction(ns, pulsarClient, inputSpecNode, messagePerTopic, topicMsgCntMap);
final String outputTopic = ns + "/test-merge-output";
@Cleanup Consumer<GenericRecord> consumer = pulsarClient.newConsumer(Schema.AUTO_CONSUME()).subscriptionName("test-merge-fn").subscriptionInitialPosition(SubscriptionInitialPosition.Earliest).topic(outputTopic).subscribe();
final String functionName = "test-merge-fn-" + randomName(8);
submitFunction(Runtime.JAVA, "", outputTopic, functionName, null, MergeTopicFunction.class.getName(), null, null, inputSpecNode.toString(), SchemaType.AUTO_PUBLISH.name().toUpperCase(), SubscriptionInitialPosition.Earliest);
getFunctionInfoSuccess(functionName);
getFunctionStatus(functionName, topicMsgCntMap.keySet().size() * messagePerTopic, true);
Message<GenericRecord> message;
do {
message = consumer.receive(30, TimeUnit.SECONDS);
if (message != null) {
String baseTopic = message.getProperty("baseTopic");
GenericRecord genericRecord = message.getValue();
log.info("receive msg baseTopic: {}, schemaType: {}, nativeClass: {}, nativeObject: {}", baseTopic, genericRecord.getSchemaType(), genericRecord.getNativeObject().getClass(), genericRecord.getNativeObject());
checkSchemaForAutoSchema(message, baseTopic);
topicMsgCntMap.get(baseTopic).decrementAndGet();
consumer.acknowledge(message);
}
} while (message != null);
for (Map.Entry<String, AtomicInteger> entry : topicMsgCntMap.entrySet()) {
assertEquals(entry.getValue().get(), 0, "topic " + entry.getKey() + " left message cnt is not 0.");
}
deleteFunction(functionName);
getFunctionInfoNotFound(functionName);
log.info("finish merge function test.");
}
use of org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy in project 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));
}
Aggregations