Search in sources :

Example 1 with TestMessage

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());
}
Also used : KeyValue(org.apache.pulsar.common.schema.KeyValue) FL(org.apache.flink.connector.pulsar.testutils.SampleData.FL) Foo(org.apache.flink.connector.pulsar.testutils.SampleData.Foo) TestMessage(org.apache.flink.connector.pulsar.SampleMessage.TestMessage) SchemaInfo(org.apache.pulsar.common.schema.SchemaInfo) Test(org.junit.jupiter.api.Test)

Example 2 with TestMessage

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);
}
Also used : TestMessage(org.apache.flink.connector.pulsar.SampleMessage.TestMessage) PulsarSchema(org.apache.flink.connector.pulsar.common.schema.PulsarSchema) Test(org.junit.jupiter.api.Test)

Example 3 with TestMessage

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);
}
Also used : TestOutputView(org.apache.flink.api.common.typeutils.ComparatorTestBase.TestOutputView) TestInputView(org.apache.flink.api.common.typeutils.ComparatorTestBase.TestInputView) TestMessage(org.apache.flink.connector.pulsar.SampleMessage.TestMessage) Test(org.junit.jupiter.api.Test)

Example 4 with TestMessage

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);
}
Also used : TestOutputView(org.apache.flink.api.common.typeutils.ComparatorTestBase.TestOutputView) TestInputView(org.apache.flink.api.common.typeutils.ComparatorTestBase.TestInputView) TestMessage(org.apache.flink.connector.pulsar.SampleMessage.TestMessage) Test(org.junit.jupiter.api.Test)

Example 5 with TestMessage

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);
}
Also used : TestOutputView(org.apache.flink.api.common.typeutils.ComparatorTestBase.TestOutputView) TestMessage(org.apache.flink.connector.pulsar.SampleMessage.TestMessage) Test(org.junit.jupiter.api.Test)

Aggregations

TestMessage (org.apache.flink.connector.pulsar.SampleMessage.TestMessage)6 Test (org.junit.jupiter.api.Test)6 TestOutputView (org.apache.flink.api.common.typeutils.ComparatorTestBase.TestOutputView)3 TestInputView (org.apache.flink.api.common.typeutils.ComparatorTestBase.TestInputView)2 PulsarSchema (org.apache.flink.connector.pulsar.common.schema.PulsarSchema)1 SourceConfiguration (org.apache.flink.connector.pulsar.source.config.SourceConfiguration)1 FL (org.apache.flink.connector.pulsar.testutils.SampleData.FL)1 Foo (org.apache.flink.connector.pulsar.testutils.SampleData.Foo)1 TestingDeserializationContext (org.apache.flink.connector.testutils.source.deserialization.TestingDeserializationContext)1 KeyValue (org.apache.pulsar.common.schema.KeyValue)1 SchemaInfo (org.apache.pulsar.common.schema.SchemaInfo)1