Search in sources :

Example 1 with Bar

use of org.apache.pulsar.client.impl.schema.SchemaTestUtils.Bar 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 2 with Bar

use of org.apache.pulsar.client.impl.schema.SchemaTestUtils.Bar 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)

Example 3 with Bar

use of org.apache.pulsar.client.impl.schema.SchemaTestUtils.Bar 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 4 with Bar

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

the class JSONSchemaTest method testNotAllowNullNestedClasses.

@Test
public void testNotAllowNullNestedClasses() {
    JSONSchema<NestedBar> jsonSchema = JSONSchema.of(SchemaDefinition.<NestedBar>builder().withPojo(NestedBar.class).withAlwaysAllowNull(false).build());
    JSONSchema<NestedBarList> listJsonSchema = JSONSchema.of(SchemaDefinition.<NestedBarList>builder().withPojo(NestedBarList.class).withAlwaysAllowNull(false).build());
    Bar bar = new Bar();
    bar.setField1(true);
    NestedBar nested = new NestedBar();
    nested.setField1(true);
    nested.setNested(bar);
    byte[] bytes = jsonSchema.encode(nested);
    Assert.assertTrue(bytes.length > 0);
    Assert.assertEquals(jsonSchema.decode(bytes), nested);
    List<Bar> list = Collections.singletonList(bar);
    NestedBarList nestedList = new NestedBarList();
    nestedList.setField1(true);
    nestedList.setList(list);
    bytes = listJsonSchema.encode(nestedList);
    Assert.assertTrue(bytes.length > 0);
    Assert.assertEquals(listJsonSchema.decode(bytes), nestedList);
}
Also used : NestedBar(org.apache.pulsar.client.impl.schema.SchemaTestUtils.NestedBar) NestedBar(org.apache.pulsar.client.impl.schema.SchemaTestUtils.NestedBar) Bar(org.apache.pulsar.client.impl.schema.SchemaTestUtils.Bar) NestedBarList(org.apache.pulsar.client.impl.schema.SchemaTestUtils.NestedBarList) Test(org.testng.annotations.Test)

Example 5 with Bar

use of org.apache.pulsar.client.impl.schema.SchemaTestUtils.Bar 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)

Aggregations

Bar (org.apache.pulsar.client.impl.schema.SchemaTestUtils.Bar)60 Test (org.testng.annotations.Test)54 Foo (org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo)51 NestedBar (org.apache.pulsar.client.impl.schema.SchemaTestUtils.NestedBar)21 KeyValue (org.apache.pulsar.common.schema.KeyValue)18 DerivedFoo (org.apache.pulsar.client.impl.schema.SchemaTestUtils.DerivedFoo)15 SchemaSerializationException (org.apache.pulsar.client.api.SchemaSerializationException)9 ByteBuf (io.netty.buffer.ByteBuf)6 SchemaValidationException (org.apache.avro.SchemaValidationException)6 Schema (org.apache.pulsar.client.api.Schema)6 NestedBarList (org.apache.pulsar.client.impl.schema.SchemaTestUtils.NestedBarList)6 JSONException (org.json.JSONException)6 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 SchemaInfo (org.apache.pulsar.common.schema.SchemaInfo)3