use of org.apache.flink.connector.pulsar.SampleMessage.TestMessage in project flink by apache.
the class PulsarSchemaUtilsTest method createSchemaForComplexSchema.
@Test
void createSchemaForComplexSchema() {
// Avro
Schema<Foo> avro1 = Schema.AVRO(Foo.class);
PulsarSchema<Foo> avro2 = new PulsarSchema<>(avro1, Foo.class);
SchemaInfo info1 = avro1.getSchemaInfo();
assertThrows(NullPointerException.class, () -> PulsarSchemaUtils.createSchema(info1));
Schema<Foo> schema = PulsarSchemaUtils.createSchema(avro2.getSchemaInfo());
assertNotEquals(schema.getSchemaInfo(), avro1.getSchemaInfo());
assertEquals(schema.getSchemaInfo(), avro2.getSchemaInfo());
// JSON
Schema<FL> json1 = Schema.JSON(FL.class);
PulsarSchema<FL> json2 = new PulsarSchema<>(json1, FL.class);
Schema<FL> json3 = PulsarSchemaUtils.createSchema(json2.getSchemaInfo());
assertNotEquals(json3.getSchemaInfo(), json1.getSchemaInfo());
assertEquals(json3.getSchemaInfo(), json2.getSchemaInfo());
// Protobuf Native
Schema<TestMessage> proto1 = Schema.PROTOBUF_NATIVE(TestMessage.class);
PulsarSchema<TestMessage> proto2 = new PulsarSchema<>(proto1, TestMessage.class);
Schema<TestMessage> proto3 = PulsarSchemaUtils.createSchema(proto2.getSchemaInfo());
assertNotEquals(proto3.getSchemaInfo(), proto1.getSchemaInfo());
assertEquals(proto3.getSchemaInfo(), proto2.getSchemaInfo());
// KeyValue
Schema<KeyValue<byte[], byte[]>> kvBytes1 = Schema.KV_BYTES();
PulsarSchema<KeyValue<byte[], byte[]>> kvBytes2 = new PulsarSchema<>(kvBytes1, byte[].class, byte[].class);
Schema<KeyValue<byte[], byte[]>> kvBytes3 = PulsarSchemaUtils.createSchema(kvBytes2.getSchemaInfo());
assertNotEquals(kvBytes3.getSchemaInfo(), kvBytes1.getSchemaInfo());
}
use of org.apache.flink.connector.pulsar.SampleMessage.TestMessage in project flink by apache.
the class ProtobufNativeSchemaFactoryTest method createProtobufNativeSchemaFromSchemaInfo.
@Test
void createProtobufNativeSchemaFromSchemaInfo() {
ProtobufNativeSchema<TestMessage> schema1 = ProtobufNativeSchema.of(TestMessage.class);
PulsarSchema<TestMessage> pulsarSchema = new PulsarSchema<>(schema1, TestMessage.class);
ProtobufNativeSchemaFactory<TestMessage> factory = new ProtobufNativeSchemaFactory<>();
Schema<TestMessage> schema2 = factory.createSchema(pulsarSchema.getSchemaInfo());
assertThat(schema2).hasFieldOrPropertyWithValue("schemaInfo", pulsarSchema.getSchemaInfo()).isInstanceOf(ProtobufNativeSchema.class);
// Encode by original schema and decode by new schema
TestMessage message = TestMessage.newBuilder().setStringField(randomAlphabetic(10)).setDoubleField(ThreadLocalRandom.current().nextDouble()).setIntField(ThreadLocalRandom.current().nextInt()).setTestEnum(TestEnum.SHARED).setNestedField(SubMessage.newBuilder().setFoo(randomAlphabetic(10)).setBar(ThreadLocalRandom.current().nextDouble()).build()).addRepeatedField(randomAlphabetic(13)).build();
byte[] bytes = schema1.encode(message);
TestMessage message1 = schema2.decode(bytes);
assertEquals(message, message1);
}
use of org.apache.flink.connector.pulsar.SampleMessage.TestMessage in project flink by apache.
the class PulsarSchemaTypeSerializerTest method serializeAndDeserializeInstance.
@Test
void serializeAndDeserializeInstance() throws Exception {
TestOutputView output = new TestOutputView();
serializer.serialize(message, output);
TestInputView input = output.getInputView();
TestMessage message1 = serializer.deserialize(input);
assertEquals(message, message1);
}
use of org.apache.flink.connector.pulsar.SampleMessage.TestMessage in project flink by apache.
the class PulsarSchemaTypeSerializerTest method copyValueAmongDataView.
@Test
void copyValueAmongDataView() throws Exception {
TestOutputView output1 = new TestOutputView();
serializer.serialize(message, output1);
// Copy bytes among view.
TestInputView input1 = output1.getInputView();
TestOutputView output2 = new TestOutputView();
serializer.copy(input1, output2);
TestInputView input2 = output2.getInputView();
TestMessage message1 = serializer.deserialize(input2);
assertEquals(message, message1);
}
use of org.apache.flink.connector.pulsar.SampleMessage.TestMessage in project flink by apache.
the class PulsarSchemaTypeSerializerTest method snapshotSerializerAndRecreateIt.
@Test
void snapshotSerializerAndRecreateIt() throws Exception {
TypeSerializerSnapshot<TestMessage> snapshot = serializer.snapshotConfiguration();
// Snapshot
TestOutputView output = new TestOutputView();
snapshot.writeSnapshot(output);
// Restore from snapshot
snapshot.readSnapshot(snapshot.getCurrentVersion(), output.getInputView(), PulsarSchemaTypeSerializerTest.class.getClassLoader());
TypeSerializer<TestMessage> serializer1 = snapshot.restoreSerializer();
assertEquals(serializer, serializer1);
}
Aggregations