Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4329,6 +4329,10 @@ public static Object[] avroSchemaProvider() {
public void testAccessAvroSchemaMetadata(Schema<MyBean> schema) throws Exception {
log.info("-- Starting {} test --", methodName);

if (pulsarClient == null) {
pulsarClient = newPulsarClient(lookupUrl.toString(), 0);
}

final String topic = "persistent://my-property/my-ns/accessSchema";
Consumer<GenericRecord> consumer = pulsarClient.newConsumer(Schema.AUTO_CONSUME())
.topic(topic)
Expand All @@ -4344,37 +4348,43 @@ public void testAccessAvroSchemaMetadata(Schema<MyBean> schema) throws Exception
producer.send(payload);
producer.close();

GenericRecord res = consumer.receive(RECEIVE_TIMEOUT_SECONDS, TimeUnit.SECONDS).getValue();
consumer.close();
assertEquals(schema.getSchemaInfo().getType(), res.getSchemaType());
org.apache.avro.generic.GenericRecord nativeAvroRecord = null;
JsonNode nativeJsonRecord = null;
if (schema.getSchemaInfo().getType() == SchemaType.AVRO) {
nativeAvroRecord = (org.apache.avro.generic.GenericRecord) res.getNativeObject();
assertNotNull(nativeAvroRecord);
} else {
nativeJsonRecord = (JsonNode) res.getNativeObject();
assertNotNull(nativeJsonRecord);
}
for (org.apache.pulsar.client.api.schema.Field f : res.getFields()) {
log.info("field {} {}", f.getName(), res.getField(f));
assertEquals("field", f.getName());
assertEquals("aaaaaaaaaaaaaaaaaaaaaaaaa", res.getField(f));

if (nativeAvroRecord != null) {
// test that the native schema is accessible
org.apache.avro.Schema.Field fieldDetails = nativeAvroRecord.getSchema().getField(f.getName());
// a nullable string is an UNION
assertEquals(org.apache.avro.Schema.Type.UNION, fieldDetails.schema().getType());
assertTrue(fieldDetails.schema().getTypes().stream().anyMatch(s -> s.getType() == org.apache.avro.Schema.Type.STRING));
assertTrue(fieldDetails.schema().getTypes().stream().anyMatch(s -> s.getType() == org.apache.avro.Schema.Type.NULL));
try {
GenericRecord res = consumer.receive(RECEIVE_TIMEOUT_SECONDS, TimeUnit.SECONDS).getValue();
consumer.close();
assertEquals(schema.getSchemaInfo().getType(), res.getSchemaType());
org.apache.avro.generic.GenericRecord nativeAvroRecord = null;
JsonNode nativeJsonRecord = null;
if (schema.getSchemaInfo().getType() == SchemaType.AVRO) {
nativeAvroRecord = (org.apache.avro.generic.GenericRecord) res.getNativeObject();
assertNotNull(nativeAvroRecord);
} else {
assertEquals(JsonNodeType.STRING, nativeJsonRecord.get("field").getNodeType());
nativeJsonRecord = (JsonNode) res.getNativeObject();
assertNotNull(nativeJsonRecord);
}
for (org.apache.pulsar.client.api.schema.Field f : res.getFields()) {
log.info("field {} {}", f.getName(), res.getField(f));
assertEquals("field", f.getName());
assertEquals("aaaaaaaaaaaaaaaaaaaaaaaaa", res.getField(f));

if (nativeAvroRecord != null) {
// test that the native schema is accessible
org.apache.avro.Schema.Field fieldDetails = nativeAvroRecord.getSchema().getField(f.getName());
// a nullable string is an UNION
assertEquals(org.apache.avro.Schema.Type.UNION, fieldDetails.schema().getType());
assertTrue(fieldDetails.schema().getTypes().stream().anyMatch(s -> s.getType() == org.apache.avro.Schema.Type.STRING));
assertTrue(fieldDetails.schema().getTypes().stream().anyMatch(s -> s.getType() == org.apache.avro.Schema.Type.NULL));
} else {
assertEquals(JsonNodeType.STRING, nativeJsonRecord.get("field").getNodeType());
}
}
assertEquals(1, res.getFields().size());
} catch (Exception e) {
fail();
} finally {
pulsarClient.shutdown();
pulsarClient = null;
admin.schemas().deleteSchema(topic);
}
assertEquals(1, res.getFields().size());

admin.schemas().deleteSchema(topic);
}

@Test(timeOut = 100000)
Expand Down