use of org.apache.pulsar.client.api.SchemaSerializationException in project pulsar by yahoo.
the class AvroWriter method write.
@Override
public synchronized byte[] write(T message) {
byte[] outputBytes = null;
try {
writer.write(message, this.encoder);
} catch (Exception e) {
throw new SchemaSerializationException(e);
} finally {
try {
this.encoder.flush();
outputBytes = this.byteArrayOutputStream.toByteArray();
} catch (Exception ex) {
throw new SchemaSerializationException(ex);
}
this.byteArrayOutputStream.reset();
}
return outputBytes;
}
use of org.apache.pulsar.client.api.SchemaSerializationException in project pulsar by yahoo.
the class GenericAvroWriter method write.
@Override
public synchronized byte[] write(GenericRecord message) {
try {
writer.write(((GenericAvroRecord) message).getAvroRecord(), this.encoder);
this.encoder.flush();
return this.byteArrayOutputStream.toByteArray();
} catch (Exception e) {
throw new SchemaSerializationException(e);
} finally {
this.byteArrayOutputStream.reset();
}
}
use of org.apache.pulsar.client.api.SchemaSerializationException in project pulsar by yahoo.
the class JSONSchemaTest method testNotAllowNullEncodeAndDecode.
@Test
public void testNotAllowNullEncodeAndDecode() {
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);
Foo object1 = jsonSchema.decode(bytes1);
Assert.assertTrue(bytes1.length > 0);
assertEquals(object1, foo1);
try {
jsonSchema.encode(foo2);
} catch (Exception e) {
Assert.assertTrue(e instanceof SchemaSerializationException);
}
}
use of org.apache.pulsar.client.api.SchemaSerializationException in project pulsar by yahoo.
the class KeyValueSchemaTest method testSeparatedKeyValueEncodingTypeSchemaEncodeAndDecode.
@Test
public void testSeparatedKeyValueEncodingTypeSchemaEncodeAndDecode() {
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.SEPARATED);
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 SEPARATED
byte[] encodeBytes = keyValueSchema.encode(new KeyValue(foo, bar));
Assert.assertTrue(encodeBytes.length > 0);
try {
keyValueSchema.decode(encodeBytes);
Assert.fail("This method cannot be used under this SEPARATED encoding type");
} catch (SchemaSerializationException e) {
Assert.assertTrue(e.getMessage().contains("This method cannot be used under this SEPARATED encoding type"));
}
KeyValue<Foo, Bar> keyValue = ((KeyValueSchemaImpl) keyValueSchema).decode(fooSchema.encode(foo), encodeBytes, null);
Foo fooBack = keyValue.getKey();
Bar barBack = keyValue.getValue();
assertEquals(foo, fooBack);
assertEquals(bar, barBack);
}
use of org.apache.pulsar.client.api.SchemaSerializationException in project pulsar by yahoo.
the class AvroSchemaTest method testNotAllowNullEncodeAndDecode.
@Test
public void testNotAllowNullEncodeAndDecode() {
AvroSchema<Foo> avroSchema = AvroSchema.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 = avroSchema.encode(foo1);
Foo object1 = avroSchema.decode(bytes1);
Assert.assertTrue(bytes1.length > 0);
assertEquals(object1, foo1);
try {
avroSchema.encode(foo2);
} catch (Exception e) {
Assert.assertTrue(e instanceof SchemaSerializationException);
}
}
Aggregations