Search in sources :

Example 1 with Foo

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);
    }
}
Also used : AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) Foo(org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord) LongSchemaVersion(org.apache.pulsar.common.schema.LongSchemaVersion)

Example 2 with Foo

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);
}
Also used : AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) Foo(org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo) Test(org.testng.annotations.Test)

Example 3 with Foo

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);
        }
    }
}
Also used : KeyValue(org.apache.pulsar.common.schema.KeyValue) GenericSchema(org.apache.pulsar.client.api.schema.GenericSchema) Schema(org.apache.pulsar.client.api.Schema) AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) Foo(org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo) GenericSchema(org.apache.pulsar.client.api.schema.GenericSchema) Test(org.testng.annotations.Test)

Example 4 with Foo

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;
}
Also used : Bar(org.apache.pulsar.client.impl.schema.SchemaTestUtils.Bar) Foo(org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo)

Example 5 with 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);
}
Also used : NestedBar(org.apache.pulsar.client.impl.schema.SchemaTestUtils.NestedBar) Bar(org.apache.pulsar.client.impl.schema.SchemaTestUtils.Bar) DerivedFoo(org.apache.pulsar.client.impl.schema.SchemaTestUtils.DerivedFoo) Foo(org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo) Test(org.testng.annotations.Test)

Aggregations

Foo (org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo)108 Test (org.testng.annotations.Test)87 Bar (org.apache.pulsar.client.impl.schema.SchemaTestUtils.Bar)54 GenericSchema (org.apache.pulsar.client.api.schema.GenericSchema)21 AutoConsumeSchema (org.apache.pulsar.client.impl.schema.AutoConsumeSchema)21 DerivedFoo (org.apache.pulsar.client.impl.schema.SchemaTestUtils.DerivedFoo)21 KeyValue (org.apache.pulsar.common.schema.KeyValue)18 NestedBar (org.apache.pulsar.client.impl.schema.SchemaTestUtils.NestedBar)15 Schema (org.apache.avro.Schema)12 GenericRecord (org.apache.pulsar.client.api.schema.GenericRecord)12 LongSchemaVersion (org.apache.pulsar.common.schema.LongSchemaVersion)12 Schema (org.apache.pulsar.client.api.Schema)9 SchemaSerializationException (org.apache.pulsar.client.api.SchemaSerializationException)9 ByteBuf (io.netty.buffer.ByteBuf)6 SchemaValidationException (org.apache.avro.SchemaValidationException)6 JSONException (org.json.JSONException)6 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 SchemaTestUtils (org.apache.pulsar.client.impl.schema.SchemaTestUtils)3 FooV2 (org.apache.pulsar.client.impl.schema.SchemaTestUtils.FooV2)3