use of org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo in project pulsar by apache.
the class GenericSchemaImplTest method testEncodeAndDecodeGenericRecord.
private void testEncodeAndDecodeGenericRecord(Schema<Foo> encodeSchema, Schema<GenericRecord> decodeSchema) {
int numRecords = 10;
for (int i = 0; i < numRecords; i++) {
Foo foo = newFoo(i);
byte[] data = encodeSchema.encode(foo);
log.info("Decoding : {}", new String(data, UTF_8));
GenericRecord record;
if (decodeSchema instanceof AutoConsumeSchema) {
record = decodeSchema.decode(data, new LongSchemaVersion(0L).bytes());
} else {
record = decodeSchema.decode(data);
}
verifyFooRecord(record, i);
}
}
use of org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo in project pulsar by apache.
the class GenericSchemaImplTest method testAutoAvroSchema.
@Test
public void testAutoAvroSchema() {
// configure encode schema
Schema<Foo> encodeSchema = Schema.AVRO(Foo.class);
// configure the schema info provider
MultiVersionSchemaInfoProvider multiVersionGenericSchemaProvider = mock(MultiVersionSchemaInfoProvider.class);
when(multiVersionGenericSchemaProvider.getSchemaByVersion(any(byte[].class))).thenReturn(CompletableFuture.completedFuture(encodeSchema.getSchemaInfo()));
// configure decode schema
AutoConsumeSchema decodeSchema = new AutoConsumeSchema();
decodeSchema.configureSchemaInfo("test-topic", "topic", encodeSchema.getSchemaInfo());
decodeSchema.setSchemaInfoProvider(multiVersionGenericSchemaProvider);
testEncodeAndDecodeGenericRecord(encodeSchema, decodeSchema);
}
use of org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo in project pulsar by apache.
the class GenericSchemaImplTest method testKeyValueSchema.
@Test
public void testKeyValueSchema() {
// configure the schema info provider
MultiVersionSchemaInfoProvider multiVersionSchemaInfoProvider = mock(MultiVersionSchemaInfoProvider.class);
List<Schema<Foo>> encodeSchemas = Lists.newArrayList(Schema.JSON(Foo.class), Schema.AVRO(Foo.class));
for (Schema<Foo> keySchema : encodeSchemas) {
for (Schema<Foo> valueSchema : encodeSchemas) {
// configure encode schema
Schema<KeyValue<Foo, Foo>> kvSchema = KeyValueSchemaImpl.of(keySchema, valueSchema);
// configure decode schema
Schema<KeyValue<GenericRecord, GenericRecord>> decodeSchema = KeyValueSchemaImpl.of(Schema.AUTO_CONSUME(), Schema.AUTO_CONSUME());
decodeSchema.configureSchemaInfo("test-topic", "topic", kvSchema.getSchemaInfo());
GenericSchema genericAvroSchema = GenericSchemaImpl.of(Schema.AVRO(Foo.class).getSchemaInfo());
when(multiVersionSchemaInfoProvider.getSchemaByVersion(any(byte[].class))).thenReturn(CompletableFuture.completedFuture(KeyValueSchemaInfo.encodeKeyValueSchemaInfo(keySchema, valueSchema, KeyValueEncodingType.INLINE)));
decodeSchema.setSchemaInfoProvider(multiVersionSchemaInfoProvider);
testEncodeAndDecodeKeyValues(kvSchema, decodeSchema);
}
}
}
use of org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo in project pulsar by apache.
the class GenericSchemaImplTest method newFoo.
private static Foo newFoo(int i) {
Foo foo = new Foo();
foo.setField1("field-1-" + i);
foo.setField2("field-2-" + i);
foo.setField3(i);
Bar bar = new Bar();
bar.setField1(i % 2 == 0);
foo.setField4(bar);
foo.setFieldUnableNull("fieldUnableNull-1-" + i);
return foo;
}
use of org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo in project pulsar by apache.
the class JSONSchemaTest method testAllowNullEncodeAndDecode.
@Test
public void testAllowNullEncodeAndDecode() {
JSONSchema<Foo> jsonSchema = JSONSchema.of(SchemaDefinition.<Foo>builder().withPojo(Foo.class).build());
Bar bar = new Bar();
bar.setField1(true);
Foo foo1 = new Foo();
foo1.setField1("foo1");
foo1.setField2("bar1");
foo1.setField4(bar);
foo1.setColor(SchemaTestUtils.Color.BLUE);
Foo foo2 = new Foo();
foo2.setField1("foo2");
foo2.setField2("bar2");
byte[] bytes1 = jsonSchema.encode(foo1);
Assert.assertTrue(bytes1.length > 0);
byte[] bytes2 = jsonSchema.encode(foo2);
Assert.assertTrue(bytes2.length > 0);
Foo object1 = jsonSchema.decode(bytes1);
Foo object2 = jsonSchema.decode(bytes2);
Assert.assertEquals(object1, foo1);
Assert.assertEquals(object2, foo2);
}
Aggregations