Search in sources :

Example 6 with Foo

use of org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo in project pulsar by apache.

the class JSONSchemaTest method testDecodeByteBuf.

@Test
public void testDecodeByteBuf() {
    JSONSchema<Foo> jsonSchema = JSONSchema.of(SchemaDefinition.<Foo>builder().withPojo(Foo.class).withAlwaysAllowNull(false).build());
    Foo foo1 = new Foo();
    foo1.setField1("foo1");
    foo1.setField2("bar1");
    foo1.setField4(new Bar());
    foo1.setFieldUnableNull("notNull");
    Foo foo2 = new Foo();
    foo2.setField1("foo2");
    foo2.setField2("bar2");
    byte[] bytes1 = jsonSchema.encode(foo1);
    ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer(bytes1.length);
    byteBuf.writeBytes(bytes1);
    Assert.assertTrue(bytes1.length > 0);
    assertEquals(jsonSchema.decode(byteBuf), foo1);
}
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) ByteBuf(io.netty.buffer.ByteBuf) Test(org.testng.annotations.Test)

Example 7 with Foo

use of org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo in project pulsar by apache.

the class JSONSchemaTest method testAllowNullCorrectPolymorphism.

@Test
public void testAllowNullCorrectPolymorphism() {
    Bar bar = new Bar();
    bar.setField1(true);
    DerivedFoo derivedFoo = new DerivedFoo();
    derivedFoo.setField1("foo1");
    derivedFoo.setField2("bar2");
    derivedFoo.setField3(4);
    derivedFoo.setField4(bar);
    derivedFoo.setField5("derived1");
    derivedFoo.setField6(2);
    Foo foo = new Foo();
    foo.setField1("foo1");
    foo.setField2("bar2");
    foo.setField3(4);
    foo.setField4(bar);
    SchemaTestUtils.DerivedDerivedFoo derivedDerivedFoo = new SchemaTestUtils.DerivedDerivedFoo();
    derivedDerivedFoo.setField1("foo1");
    derivedDerivedFoo.setField2("bar2");
    derivedDerivedFoo.setField3(4);
    derivedDerivedFoo.setField4(bar);
    derivedDerivedFoo.setField5("derived1");
    derivedDerivedFoo.setField6(2);
    derivedDerivedFoo.setFoo2(foo);
    derivedDerivedFoo.setDerivedFoo(derivedFoo);
    // schema for base class
    JSONSchema<Foo> baseJsonSchema = JSONSchema.of(SchemaDefinition.<Foo>builder().withPojo(Foo.class).withAlwaysAllowNull(false).build());
    Assert.assertEquals(baseJsonSchema.decode(baseJsonSchema.encode(foo)), foo);
    Assert.assertEquals(baseJsonSchema.decode(baseJsonSchema.encode(derivedFoo)), foo);
    Assert.assertEquals(baseJsonSchema.decode(baseJsonSchema.encode(derivedDerivedFoo)), foo);
    // schema for derived class
    JSONSchema<DerivedFoo> derivedJsonSchema = JSONSchema.of(SchemaDefinition.<DerivedFoo>builder().withPojo(DerivedFoo.class).withAlwaysAllowNull(false).build());
    Assert.assertEquals(derivedJsonSchema.decode(derivedJsonSchema.encode(derivedFoo)), derivedFoo);
    Assert.assertEquals(derivedJsonSchema.decode(derivedJsonSchema.encode(derivedDerivedFoo)), derivedFoo);
    // schema for derived derived class
    JSONSchema<SchemaTestUtils.DerivedDerivedFoo> derivedDerivedJsonSchema = JSONSchema.of(SchemaDefinition.<SchemaTestUtils.DerivedDerivedFoo>builder().withPojo(SchemaTestUtils.DerivedDerivedFoo.class).withAlwaysAllowNull(false).build());
    Assert.assertEquals(derivedDerivedJsonSchema.decode(derivedDerivedJsonSchema.encode(derivedDerivedFoo)), derivedDerivedFoo);
}
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) DerivedFoo(org.apache.pulsar.client.impl.schema.SchemaTestUtils.DerivedFoo) Foo(org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo) Test(org.testng.annotations.Test)

Example 8 with Foo

use of org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo in project pulsar by apache.

the class JSONSchemaTest method testNotAllowNullSchema.

@Test
public void testNotAllowNullSchema() throws JSONException {
    JSONSchema<Foo> jsonSchema = JSONSchema.of(SchemaDefinition.<Foo>builder().withPojo(Foo.class).withAlwaysAllowNull(false).build());
    Assert.assertEquals(jsonSchema.getSchemaInfo().getType(), SchemaType.JSON);
    Schema.Parser parser = new Schema.Parser();
    String schemaJson = new String(jsonSchema.getSchemaInfo().getSchema());
    assertJSONEqual(schemaJson, SCHEMA_JSON_NOT_ALLOW_NULL);
    Schema schema = parser.parse(schemaJson);
    for (String fieldName : FOO_FIELDS) {
        Schema.Field field = schema.getField(fieldName);
        Assert.assertNotNull(field);
        if (field.name().equals("field4")) {
            Assert.assertNotNull(field.schema().getTypes().get(1).getField("field1"));
        }
        if (field.name().equals("fieldUnableNull")) {
            Assert.assertNotNull(field.schema().getType());
        }
    }
}
Also used : DerivedFoo(org.apache.pulsar.client.impl.schema.SchemaTestUtils.DerivedFoo) Foo(org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo) Schema(org.apache.avro.Schema) Test(org.testng.annotations.Test)

Example 9 with Foo

use of org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo in project pulsar by apache.

the class KeyValueSchemaTest method testInlineKeyValueEncodingTypeSchemaEncodeAndDecode.

@Test
public void testInlineKeyValueEncodingTypeSchemaEncodeAndDecode() {
    AvroSchema<Foo> fooSchema = AvroSchema.of(SchemaDefinition.<Foo>builder().withPojo(Foo.class).build());
    AvroSchema<Bar> barSchema = AvroSchema.of(SchemaDefinition.<Bar>builder().withPojo(Bar.class).build());
    Schema<KeyValue<Foo, Bar>> keyValueSchema = Schema.KeyValue(fooSchema, barSchema, KeyValueEncodingType.INLINE);
    Bar bar = new Bar();
    bar.setField1(true);
    Foo foo = new Foo();
    foo.setField1("field1");
    foo.setField2("field2");
    foo.setField3(3);
    foo.setField4(bar);
    foo.setColor(Color.RED);
    // Check kv.encoding.type INLINE
    byte[] encodeBytes = keyValueSchema.encode(new KeyValue(foo, bar));
    Assert.assertTrue(encodeBytes.length > 0);
    KeyValue<Foo, Bar> keyValue = (KeyValue<Foo, Bar>) keyValueSchema.decode(encodeBytes);
    Foo fooBack = keyValue.getKey();
    Bar barBack = keyValue.getValue();
    assertEquals(foo, fooBack);
    assertEquals(bar, barBack);
}
Also used : Bar(org.apache.pulsar.client.impl.schema.SchemaTestUtils.Bar) KeyValue(org.apache.pulsar.common.schema.KeyValue) Foo(org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo) Test(org.testng.annotations.Test)

Example 10 with Foo

use of org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo in project pulsar by apache.

the class KeyValueSchemaTest method testAllowNullBytesSchemaEncodeAndDecode.

@Test
public void testAllowNullBytesSchemaEncodeAndDecode() {
    AvroSchema<Foo> fooAvroSchema = AvroSchema.of(SchemaDefinition.<Foo>builder().withPojo(Foo.class).build());
    AvroSchema<Bar> barAvroSchema = AvroSchema.of(SchemaDefinition.<Bar>builder().withPojo(Bar.class).build());
    Bar bar = new Bar();
    bar.setField1(true);
    Foo foo = new Foo();
    foo.setField1("field1");
    foo.setField2("field2");
    foo.setField3(3);
    foo.setField4(bar);
    foo.setColor(Color.RED);
    foo.setFieldUnableNull("notNull");
    byte[] fooBytes = fooAvroSchema.encode(foo);
    byte[] barBytes = barAvroSchema.encode(bar);
    byte[] encodeBytes = Schema.KV_BYTES().encode(new KeyValue<>(fooBytes, barBytes));
    KeyValue<byte[], byte[]> decodeKV = Schema.KV_BYTES().decode(encodeBytes);
    Foo fooBack = fooAvroSchema.decode(decodeKV.getKey());
    Bar barBack = barAvroSchema.decode(decodeKV.getValue());
    assertEquals(foo, fooBack);
    assertEquals(bar, barBack);
}
Also used : Bar(org.apache.pulsar.client.impl.schema.SchemaTestUtils.Bar) 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