Search in sources :

Example 1 with MultiVersionSchemaInfoProvider

use of org.apache.pulsar.client.impl.schema.generic.MultiVersionSchemaInfoProvider in project pulsar by apache.

the class MessageImplTest method testDefaultJSONVersionGetProducerDataAssigned.

@Test
public void testDefaultJSONVersionGetProducerDataAssigned() {
    JSONSchema<SchemaTestUtils.Foo> fooSchema = JSONSchema.of(SchemaDefinition.<SchemaTestUtils.Foo>builder().withPojo(SchemaTestUtils.Foo.class).build());
    JSONSchema<SchemaTestUtils.Bar> barSchema = JSONSchema.of(SchemaDefinition.<SchemaTestUtils.Bar>builder().withPojo(SchemaTestUtils.Bar.class).build());
    MultiVersionSchemaInfoProvider multiVersionSchemaInfoProvider = mock(MultiVersionSchemaInfoProvider.class);
    Schema<KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar>> keyValueSchema = Schema.KeyValue(fooSchema, barSchema);
    keyValueSchema.setSchemaInfoProvider(multiVersionSchemaInfoProvider);
    when(multiVersionSchemaInfoProvider.getSchemaByVersion(any(byte[].class))).thenReturn(CompletableFuture.completedFuture(keyValueSchema.getSchemaInfo()));
    SchemaTestUtils.Foo foo = new SchemaTestUtils.Foo();
    foo.setField1("field1");
    foo.setField2("field2");
    foo.setField3(3);
    SchemaTestUtils.Bar bar = new SchemaTestUtils.Bar();
    bar.setField1(true);
    byte[] encodeBytes = keyValueSchema.encode(new KeyValue(foo, bar));
    MessageMetadata builder = new MessageMetadata().setProducerName("default");
    builder.setSchemaVersion(new byte[10]);
    MessageImpl<KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar>> msg = MessageImpl.create(builder, ByteBuffer.wrap(encodeBytes), keyValueSchema, null);
    KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar> keyValue = msg.getValue();
    assertEquals(keyValue.getKey(), foo);
    assertEquals(keyValue.getValue(), bar);
    assertFalse(builder.hasPartitionKey());
    Assert.assertEquals(KeyValueEncodingType.valueOf(keyValueSchema.getSchemaInfo().getProperties().get("kv.encoding.type")), KeyValueEncodingType.INLINE);
}
Also used : MultiVersionSchemaInfoProvider(org.apache.pulsar.client.impl.schema.generic.MultiVersionSchemaInfoProvider) KeyValue(org.apache.pulsar.common.schema.KeyValue) SchemaTestUtils(org.apache.pulsar.client.impl.schema.SchemaTestUtils) MessageMetadata(org.apache.pulsar.common.api.proto.MessageMetadata) Test(org.testng.annotations.Test)

Example 2 with MultiVersionSchemaInfoProvider

use of org.apache.pulsar.client.impl.schema.generic.MultiVersionSchemaInfoProvider in project pulsar by apache.

the class MessageImplTest method testSeparatedJSONVersionGetProducerDataAssigned.

@Test
public void testSeparatedJSONVersionGetProducerDataAssigned() {
    JSONSchema<SchemaTestUtils.Foo> fooSchema = JSONSchema.of(SchemaDefinition.<SchemaTestUtils.Foo>builder().withPojo(SchemaTestUtils.Foo.class).build());
    JSONSchema<SchemaTestUtils.Bar> barSchema = JSONSchema.of(SchemaDefinition.<SchemaTestUtils.Bar>builder().withPojo(SchemaTestUtils.Bar.class).build());
    MultiVersionSchemaInfoProvider multiVersionSchemaInfoProvider = mock(MultiVersionSchemaInfoProvider.class);
    Schema<KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar>> keyValueSchema = Schema.KeyValue(fooSchema, barSchema, KeyValueEncodingType.SEPARATED);
    keyValueSchema.setSchemaInfoProvider(multiVersionSchemaInfoProvider);
    when(multiVersionSchemaInfoProvider.getSchemaByVersion(any(byte[].class))).thenReturn(CompletableFuture.completedFuture(keyValueSchema.getSchemaInfo()));
    SchemaTestUtils.Foo foo = new SchemaTestUtils.Foo();
    foo.setField1("field1");
    foo.setField2("field2");
    foo.setField3(3);
    SchemaTestUtils.Bar bar = new SchemaTestUtils.Bar();
    bar.setField1(true);
    byte[] encodeBytes = keyValueSchema.encode(new KeyValue(foo, bar));
    MessageMetadata builder = new MessageMetadata().setProducerName("separated");
    builder.setSchemaVersion(new byte[10]);
    builder.setPartitionKey(Base64.getEncoder().encodeToString(fooSchema.encode(foo)));
    builder.setPartitionKeyB64Encoded(true);
    MessageImpl<KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar>> msg = MessageImpl.create(builder, ByteBuffer.wrap(encodeBytes), keyValueSchema, null);
    KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar> keyValue = msg.getValue();
    assertEquals(keyValue.getKey(), foo);
    assertEquals(keyValue.getValue(), bar);
    assertTrue(builder.hasPartitionKey());
    Assert.assertEquals(KeyValueEncodingType.valueOf(keyValueSchema.getSchemaInfo().getProperties().get("kv.encoding.type")), KeyValueEncodingType.SEPARATED);
}
Also used : MultiVersionSchemaInfoProvider(org.apache.pulsar.client.impl.schema.generic.MultiVersionSchemaInfoProvider) KeyValue(org.apache.pulsar.common.schema.KeyValue) SchemaTestUtils(org.apache.pulsar.client.impl.schema.SchemaTestUtils) MessageMetadata(org.apache.pulsar.common.api.proto.MessageMetadata) Test(org.testng.annotations.Test)

Example 3 with MultiVersionSchemaInfoProvider

use of org.apache.pulsar.client.impl.schema.generic.MultiVersionSchemaInfoProvider in project pulsar by apache.

the class MessageImplTest method testSeparatedAVROJSONVersionGetProducerDataAssigned.

@Test
public void testSeparatedAVROJSONVersionGetProducerDataAssigned() {
    AvroSchema<SchemaTestUtils.Foo> fooSchema = AvroSchema.of(SchemaDefinition.<SchemaTestUtils.Foo>builder().withPojo(SchemaTestUtils.Foo.class).build());
    JSONSchema<SchemaTestUtils.Bar> barSchema = JSONSchema.of(SchemaDefinition.<SchemaTestUtils.Bar>builder().withPojo(SchemaTestUtils.Bar.class).build());
    MultiVersionSchemaInfoProvider multiVersionSchemaInfoProvider = mock(MultiVersionSchemaInfoProvider.class);
    Schema<KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar>> keyValueSchema = Schema.KeyValue(fooSchema, barSchema, KeyValueEncodingType.SEPARATED);
    keyValueSchema.setSchemaInfoProvider(multiVersionSchemaInfoProvider);
    when(multiVersionSchemaInfoProvider.getSchemaByVersion(any(byte[].class))).thenReturn(CompletableFuture.completedFuture(keyValueSchema.getSchemaInfo()));
    SchemaTestUtils.Foo foo = new SchemaTestUtils.Foo();
    foo.setField1("field1");
    foo.setField2("field2");
    foo.setField3(3);
    SchemaTestUtils.Bar bar = new SchemaTestUtils.Bar();
    bar.setField1(true);
    byte[] encodeBytes = keyValueSchema.encode(new KeyValue(foo, bar));
    MessageMetadata builder = new MessageMetadata().setProducerName("separated");
    builder.setSchemaVersion(new byte[10]);
    builder.setPartitionKey(Base64.getEncoder().encodeToString(fooSchema.encode(foo)));
    builder.setPartitionKeyB64Encoded(true);
    MessageImpl<KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar>> msg = MessageImpl.create(builder, ByteBuffer.wrap(encodeBytes), keyValueSchema, null);
    KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar> keyValue = msg.getValue();
    assertEquals(keyValue.getKey(), foo);
    assertEquals(keyValue.getValue(), bar);
    assertTrue(builder.hasPartitionKey());
    Assert.assertEquals(KeyValueEncodingType.valueOf(keyValueSchema.getSchemaInfo().getProperties().get("kv.encoding.type")), KeyValueEncodingType.SEPARATED);
}
Also used : MultiVersionSchemaInfoProvider(org.apache.pulsar.client.impl.schema.generic.MultiVersionSchemaInfoProvider) KeyValue(org.apache.pulsar.common.schema.KeyValue) SchemaTestUtils(org.apache.pulsar.client.impl.schema.SchemaTestUtils) MessageMetadata(org.apache.pulsar.common.api.proto.MessageMetadata) Test(org.testng.annotations.Test)

Example 4 with MultiVersionSchemaInfoProvider

use of org.apache.pulsar.client.impl.schema.generic.MultiVersionSchemaInfoProvider in project pulsar by yahoo.

the class MessageImplTest method testSeparatedAVROVersionGetProducerDataAssigned.

@Test
public void testSeparatedAVROVersionGetProducerDataAssigned() {
    AvroSchema<SchemaTestUtils.Foo> fooSchema = AvroSchema.of(SchemaDefinition.<SchemaTestUtils.Foo>builder().withPojo(SchemaTestUtils.Foo.class).build());
    AvroSchema<SchemaTestUtils.Bar> barSchema = AvroSchema.of(SchemaDefinition.<SchemaTestUtils.Bar>builder().withPojo(SchemaTestUtils.Bar.class).build());
    MultiVersionSchemaInfoProvider multiVersionSchemaInfoProvider = mock(MultiVersionSchemaInfoProvider.class);
    Schema<KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar>> keyValueSchema = Schema.KeyValue(fooSchema, barSchema, KeyValueEncodingType.SEPARATED);
    keyValueSchema.setSchemaInfoProvider(multiVersionSchemaInfoProvider);
    when(multiVersionSchemaInfoProvider.getSchemaByVersion(any(byte[].class))).thenReturn(CompletableFuture.completedFuture(keyValueSchema.getSchemaInfo()));
    SchemaTestUtils.Foo foo = new SchemaTestUtils.Foo();
    foo.setField1("field1");
    foo.setField2("field2");
    foo.setField3(3);
    SchemaTestUtils.Bar bar = new SchemaTestUtils.Bar();
    bar.setField1(true);
    byte[] encodeBytes = keyValueSchema.encode(new KeyValue(foo, bar));
    MessageMetadata builder = new MessageMetadata().setProducerName("separated");
    builder.setSchemaVersion(new byte[10]);
    builder.setPartitionKey(Base64.getEncoder().encodeToString(fooSchema.encode(foo)));
    builder.setPartitionKeyB64Encoded(true);
    MessageImpl<KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar>> msg = MessageImpl.create(builder, ByteBuffer.wrap(encodeBytes), keyValueSchema, null);
    KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar> keyValue = msg.getValue();
    assertEquals(keyValue.getKey(), foo);
    assertEquals(keyValue.getValue(), bar);
    assertTrue(builder.hasPartitionKey());
    Assert.assertEquals(KeyValueEncodingType.valueOf(keyValueSchema.getSchemaInfo().getProperties().get("kv.encoding.type")), KeyValueEncodingType.SEPARATED);
}
Also used : MultiVersionSchemaInfoProvider(org.apache.pulsar.client.impl.schema.generic.MultiVersionSchemaInfoProvider) KeyValue(org.apache.pulsar.common.schema.KeyValue) SchemaTestUtils(org.apache.pulsar.client.impl.schema.SchemaTestUtils) MessageMetadata(org.apache.pulsar.common.api.proto.MessageMetadata) Test(org.testng.annotations.Test)

Example 5 with MultiVersionSchemaInfoProvider

use of org.apache.pulsar.client.impl.schema.generic.MultiVersionSchemaInfoProvider in project pulsar by yahoo.

the class MessageImplTest method testDefaultAVROJSONVersionGetProducerDataAssigned.

@Test
public void testDefaultAVROJSONVersionGetProducerDataAssigned() {
    AvroSchema<SchemaTestUtils.Foo> fooSchema = AvroSchema.of(SchemaDefinition.<SchemaTestUtils.Foo>builder().withPojo(SchemaTestUtils.Foo.class).build());
    JSONSchema<SchemaTestUtils.Bar> barSchema = JSONSchema.of(SchemaDefinition.<SchemaTestUtils.Bar>builder().withPojo(SchemaTestUtils.Bar.class).build());
    MultiVersionSchemaInfoProvider multiVersionSchemaInfoProvider = mock(MultiVersionSchemaInfoProvider.class);
    Schema<KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar>> keyValueSchema = Schema.KeyValue(fooSchema, barSchema);
    keyValueSchema.setSchemaInfoProvider(multiVersionSchemaInfoProvider);
    when(multiVersionSchemaInfoProvider.getSchemaByVersion(any(byte[].class))).thenReturn(CompletableFuture.completedFuture(keyValueSchema.getSchemaInfo()));
    SchemaTestUtils.Foo foo = new SchemaTestUtils.Foo();
    foo.setField1("field1");
    foo.setField2("field2");
    foo.setField3(3);
    SchemaTestUtils.Bar bar = new SchemaTestUtils.Bar();
    bar.setField1(true);
    byte[] encodeBytes = keyValueSchema.encode(new KeyValue(foo, bar));
    MessageMetadata builder = new MessageMetadata().setProducerName("default");
    builder.setSchemaVersion(new byte[10]);
    MessageImpl<KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar>> msg = MessageImpl.create(builder, ByteBuffer.wrap(encodeBytes), keyValueSchema, null);
    KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar> keyValue = msg.getValue();
    assertEquals(keyValue.getKey(), foo);
    assertEquals(keyValue.getValue(), bar);
    assertFalse(builder.hasPartitionKey());
    Assert.assertEquals(KeyValueEncodingType.valueOf(keyValueSchema.getSchemaInfo().getProperties().get("kv.encoding.type")), KeyValueEncodingType.INLINE);
}
Also used : MultiVersionSchemaInfoProvider(org.apache.pulsar.client.impl.schema.generic.MultiVersionSchemaInfoProvider) KeyValue(org.apache.pulsar.common.schema.KeyValue) SchemaTestUtils(org.apache.pulsar.client.impl.schema.SchemaTestUtils) MessageMetadata(org.apache.pulsar.common.api.proto.MessageMetadata) Test(org.testng.annotations.Test)

Aggregations

MultiVersionSchemaInfoProvider (org.apache.pulsar.client.impl.schema.generic.MultiVersionSchemaInfoProvider)27 KeyValue (org.apache.pulsar.common.schema.KeyValue)24 Test (org.testng.annotations.Test)24 SchemaTestUtils (org.apache.pulsar.client.impl.schema.SchemaTestUtils)18 MessageMetadata (org.apache.pulsar.common.api.proto.MessageMetadata)18 GenericAvroSchema (org.apache.pulsar.client.impl.schema.generic.GenericAvroSchema)3 SchemaInfo (org.apache.pulsar.common.schema.SchemaInfo)3 BeforeMethod (org.testng.annotations.BeforeMethod)3