Java Code Examples for org.apache.pulsar.client.api.Schema#KeyValue

The following examples show how to use org.apache.pulsar.client.api.Schema#KeyValue . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TypedMessageBuilderImplTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testSeparatedValue() {
    producerBase = mock(ProducerBase.class);

    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());

    Schema<KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar>> keyValueSchema = Schema.KeyValue(fooSchema, barSchema, KeyValueEncodingType.SEPARATED);
    TypedMessageBuilderImpl typedMessageBuilderImpl = new TypedMessageBuilderImpl(producerBase, keyValueSchema);

    SchemaTestUtils.Foo foo = new SchemaTestUtils.Foo();
    foo.setField1("field1");
    foo.setField2("field2");
    SchemaTestUtils.Bar bar = new SchemaTestUtils.Bar();
    bar.setField1(true);
    KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar> keyValue = new KeyValue<>(foo, bar);

    // Check kv.encoding.type SEPARATED
    TypedMessageBuilderImpl typedMessageBuilder = (TypedMessageBuilderImpl)typedMessageBuilderImpl.value(keyValue);
    ByteBuffer content = typedMessageBuilder.getContent();
    byte[] contentByte = new byte[content.remaining()];
    content.get(contentByte);
    assertTrue(typedMessageBuilderImpl.hasKey());
    assertEquals(typedMessageBuilderImpl.getKey(), Base64.getEncoder().encodeToString(fooSchema.encode(foo)));
    assertEquals(barSchema.decode(contentByte), bar);
}
 
Example 2
Source File: KeyValueSchemaTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testNotAllowNullAvroSchemaCreate() {
    AvroSchema<Foo> fooSchema = AvroSchema.of(SchemaDefinition.<Foo>builder().withPojo(Foo.class).withAlwaysAllowNull(false).build());
    AvroSchema<Bar> barSchema = AvroSchema.of(SchemaDefinition.<Bar>builder().withPojo(Bar.class).withAlwaysAllowNull(false).build());

    Schema<KeyValue<Foo, Bar>> keyValueSchema1 = Schema.KeyValue(fooSchema, barSchema);
    Schema<KeyValue<Foo, Bar>> keyValueSchema2 = Schema.KeyValue(AvroSchema.of(SchemaDefinition.<Foo>builder().withPojo(Foo.class).withAlwaysAllowNull(false).build()),
            AvroSchema.of(SchemaDefinition.<Bar>builder().withPojo(Bar.class).withAlwaysAllowNull(false).build()));

    assertEquals(keyValueSchema1.getSchemaInfo().getType(), SchemaType.KEY_VALUE);
    assertEquals(keyValueSchema2.getSchemaInfo().getType(), SchemaType.KEY_VALUE);

    assertEquals(((KeyValueSchema<Foo, Bar>) keyValueSchema1).getKeySchema().getSchemaInfo().getType(),
        SchemaType.AVRO);
    assertEquals(((KeyValueSchema<Foo, Bar>) keyValueSchema1).getValueSchema().getSchemaInfo().getType(),
        SchemaType.AVRO);
    assertEquals(((KeyValueSchema<Foo, Bar>) keyValueSchema2).getKeySchema().getSchemaInfo().getType(),
        SchemaType.AVRO);
    assertEquals(((KeyValueSchema<Foo, Bar>) keyValueSchema2).getValueSchema().getSchemaInfo().getType(),
        SchemaType.AVRO);

    String schemaInfo1 = new String(keyValueSchema1.getSchemaInfo().getSchema());
    String schemaInfo2 = new String(keyValueSchema2.getSchemaInfo().getSchema());
    assertEquals(schemaInfo1, schemaInfo2);
}
 
Example 3
Source File: KeyValueSchemaTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testFillParametersToSchemainfo() {
    AvroSchema<Foo> fooSchema = AvroSchema.of(SchemaDefinition.<Foo>builder().withPojo(Foo.class).build());
    AvroSchema<Bar> barSchema = AvroSchema.of(SchemaDefinition.<Bar>builder().withPojo(Bar.class).build());

    fooSchema.getSchemaInfo().setName("foo");
    fooSchema.getSchemaInfo().setType(SchemaType.AVRO);
    Map<String, String> keyProperties = Maps.newTreeMap();
    keyProperties.put("foo.key1", "value");
    keyProperties.put("foo.key2", "value");
    fooSchema.getSchemaInfo().setProperties(keyProperties);
    barSchema.getSchemaInfo().setName("bar");
    barSchema.getSchemaInfo().setType(SchemaType.AVRO);
    Map<String, String> valueProperties = Maps.newTreeMap();
    valueProperties.put("bar.key", "key");
    barSchema.getSchemaInfo().setProperties(valueProperties);
    Schema<KeyValue<Foo, Bar>> keyValueSchema1 = Schema.KeyValue(fooSchema, barSchema);

    assertEquals(keyValueSchema1.getSchemaInfo().getProperties().get("key.schema.name"), "foo");
    assertEquals(keyValueSchema1.getSchemaInfo().getProperties().get("key.schema.type"), String.valueOf(SchemaType.AVRO));
    assertEquals(keyValueSchema1.getSchemaInfo().getProperties().get("key.schema.properties"), "{\"foo.key1\":\"value\",\"foo.key2\":\"value\"}");
    assertEquals(keyValueSchema1.getSchemaInfo().getProperties().get("value.schema.name"), "bar");
    assertEquals(keyValueSchema1.getSchemaInfo().getProperties().get("value.schema.type"), String.valueOf(SchemaType.AVRO));
    assertEquals(keyValueSchema1.getSchemaInfo().getProperties().get("value.schema.properties"), "{\"bar.key\":\"key\"}");
}
 
Example 4
Source File: TypedMessageBuilderImplTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetKeyEncodingTypeSeparated() {
    producerBase = mock(ProducerBase.class);

    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());

    Schema<KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar>> keyValueSchema = Schema.KeyValue(fooSchema, barSchema, KeyValueEncodingType.SEPARATED);
    TypedMessageBuilderImpl typedMessageBuilderImpl = new TypedMessageBuilderImpl(producerBase, keyValueSchema);


    try {
        TypedMessageBuilderImpl typedMessageBuilder = (TypedMessageBuilderImpl)typedMessageBuilderImpl.key("separated");
        fail("This should fail");
    } catch (IllegalArgumentException e) {
        assertTrue(e.getMessage().contains("This method is not allowed to set keys when in encoding type is SEPARATED"));
    }
}
 
Example 5
Source File: KeyValueSchemaTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testAllowNullAvroSchemaCreate() {
    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>> keyValueSchema1 = Schema.KeyValue(fooSchema, barSchema);
    Schema<KeyValue<Foo, Bar>> keyValueSchema2 = Schema.KeyValue(Foo.class, Bar.class, SchemaType.AVRO);

    assertEquals(keyValueSchema1.getSchemaInfo().getType(), SchemaType.KEY_VALUE);
    assertEquals(keyValueSchema2.getSchemaInfo().getType(), SchemaType.KEY_VALUE);

    assertEquals(((KeyValueSchema<Foo, Bar>) keyValueSchema1).getKeySchema().getSchemaInfo().getType(),
            SchemaType.AVRO);
    assertEquals(((KeyValueSchema<Foo, Bar>) keyValueSchema1).getValueSchema().getSchemaInfo().getType(),
            SchemaType.AVRO);
    assertEquals(((KeyValueSchema<Foo, Bar>) keyValueSchema2).getKeySchema().getSchemaInfo().getType(),
            SchemaType.AVRO);
    assertEquals(((KeyValueSchema<Foo, Bar>) keyValueSchema2).getValueSchema().getSchemaInfo().getType(),
            SchemaType.AVRO);

    String schemaInfo1 = new String(keyValueSchema1.getSchemaInfo().getSchema());
    String schemaInfo2 = new String(keyValueSchema2.getSchemaInfo().getSchema());
    assertEquals(schemaInfo1, schemaInfo2);
}
 
Example 6
Source File: KeyValueSchemaTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testNotAllowNullSchemaEncodeAndDecode() {
    Schema keyValueSchema = Schema.KeyValue(JSONSchema.of(SchemaDefinition.<Foo>builder().withPojo(Foo.class).withAlwaysAllowNull(false).build()),
            JSONSchema.of(SchemaDefinition.<Bar>builder().withPojo(Bar.class).withAlwaysAllowNull(false).build()));

    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);

    byte[] encodeBytes = keyValueSchema.encode(new KeyValue(foo, bar));
    Assert.assertTrue(encodeBytes.length > 0);

    KeyValue<Foo, Bar> keyValue = (KeyValue<Foo, Bar>) keyValueSchema.decode(encodeBytes);
    Foo fooBack = keyValue.getKey();
    Bar barBack = keyValue.getValue();

    assertEquals(foo, fooBack);
    assertEquals(bar, barBack);
}
 
Example 7
Source File: TypedMessageBuilderImplTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetKeyBytesEncodingTypeSeparated() {
    producerBase = mock(ProducerBase.class);

    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());

    Schema<KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar>> keyValueSchema = Schema.KeyValue(fooSchema, barSchema, KeyValueEncodingType.SEPARATED);
    TypedMessageBuilderImpl typedMessageBuilderImpl = new TypedMessageBuilderImpl(producerBase, keyValueSchema);


    try {
        TypedMessageBuilderImpl typedMessageBuilder = (TypedMessageBuilderImpl)typedMessageBuilderImpl.keyBytes("separated".getBytes());
        fail("This should fail");
    } catch (IllegalArgumentException e) {
        assertTrue(e.getMessage().contains("This method is not allowed to set keys when in encoding type is SEPARATED"));
    }
}
 
Example 8
Source File: KeyValueSchemaInfoTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test(dataProvider = "encodingTypes")
public void encodeDecodeKeyValueSchemaInfo(KeyValueEncodingType encodingType) {
    Schema<KeyValue<Foo, Bar>> kvSchema = Schema.KeyValue(
        FOO_SCHEMA,
        BAR_SCHEMA,
        encodingType
    );
    SchemaInfo kvSchemaInfo = kvSchema.getSchemaInfo();
    assertEquals(
        DefaultImplementation.decodeKeyValueEncodingType(kvSchemaInfo),
        encodingType);

    SchemaInfo encodedSchemaInfo =
        DefaultImplementation.encodeKeyValueSchemaInfo(FOO_SCHEMA, BAR_SCHEMA, encodingType);
    assertEquals(encodedSchemaInfo, kvSchemaInfo);
    assertEquals(
        DefaultImplementation.decodeKeyValueEncodingType(encodedSchemaInfo),
        encodingType);

    KeyValue<SchemaInfo, SchemaInfo> schemaInfoKeyValue =
        DefaultImplementation.decodeKeyValueSchemaInfo(kvSchemaInfo);

    assertEquals(schemaInfoKeyValue.getKey(), FOO_SCHEMA.getSchemaInfo());
    assertEquals(schemaInfoKeyValue.getValue(), BAR_SCHEMA.getSchemaInfo());
}
 
Example 9
Source File: KeyValueSchemaTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testInlineKeyValueEncodingTypeSchemaEncodeAndDecode() {

    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.INLINE);


    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 INLINE
    byte[] encodeBytes = keyValueSchema.encode(new KeyValue(foo, bar));
    Assert.assertTrue(encodeBytes.length > 0);
    KeyValue<Foo, Bar>  keyValue = (KeyValue<Foo, Bar>) keyValueSchema.decode(encodeBytes);
    Foo fooBack = keyValue.getKey();
    Bar barBack = keyValue.getValue();
    assertEquals(foo, fooBack);
    assertEquals(bar, barBack);

}
 
Example 10
Source File: AdminApiSchemaTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@DataProvider(name = "schemas")
public Object[][] schemas() {
    return new Object[][] {
        { Schema.BOOL },
        { Schema.INT8 },
        { Schema.INT16 },
        { Schema.INT32 },
        { Schema.INT64 },
        { StringSchema.utf8() },
        { new StringSchema(US_ASCII) },
        { Schema.FLOAT },
        { Schema.DOUBLE },
        { Schema.DATE },
        { Schema.TIME },
        { Schema.TIMESTAMP },
        { Schema.AVRO(
            SchemaDefinition.builder()
                .withPojo(Foo.class)
                .withProperties(PROPS)
                .build()
        ) },
        { Schema.JSON(
            SchemaDefinition.builder()
                .withPojo(Foo.class)
                .withProperties(PROPS)
                .build()
        )},
        { Schema.KeyValue(
            StringSchema.utf8(),
            new StringSchema(US_ASCII)
        )}
    };
}
 
Example 11
Source File: KeyValueSchemaTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testNotAllowNullJsonSchemaCreate() {
    JSONSchema<Foo> fooSchema = JSONSchema.of(
            SchemaDefinition.<Foo>builder().withPojo(Foo.class).withAlwaysAllowNull(false).build());
    JSONSchema<Bar> barSchema = JSONSchema.of(
            SchemaDefinition.<Bar>builder().withPojo(Bar.class).withAlwaysAllowNull(false).build());

    Schema<KeyValue<Foo, Bar>> keyValueSchema1 = Schema.KeyValue(fooSchema, barSchema);
    Schema<KeyValue<Foo, Bar>> keyValueSchema2 = Schema.KeyValue(JSONSchema.of(SchemaDefinition.<Foo>builder().withPojo(Foo.class).withAlwaysAllowNull(false).build()),
            JSONSchema.of(SchemaDefinition.<Bar>builder().withPojo(Bar.class).withAlwaysAllowNull(false).build()));

    Schema<KeyValue<Foo, Bar>> keyValueSchema3 = Schema.KeyValue(JSONSchema.of(SchemaDefinition.<Foo>builder().withPojo(Foo.class).withAlwaysAllowNull(false).build()),
            JSONSchema.of(SchemaDefinition.<Bar>builder().withPojo(Bar.class).withAlwaysAllowNull(false).build()));

    assertEquals(keyValueSchema1.getSchemaInfo().getType(), SchemaType.KEY_VALUE);
    assertEquals(keyValueSchema2.getSchemaInfo().getType(), SchemaType.KEY_VALUE);
    assertEquals(keyValueSchema3.getSchemaInfo().getType(), SchemaType.KEY_VALUE);

    assertEquals(((KeyValueSchema<Foo, Bar>) keyValueSchema1).getKeySchema().getSchemaInfo().getType(),
        SchemaType.JSON);
    assertEquals(((KeyValueSchema<Foo, Bar>) keyValueSchema1).getValueSchema().getSchemaInfo().getType(),
        SchemaType.JSON);
    assertEquals(((KeyValueSchema<Foo, Bar>) keyValueSchema2).getKeySchema().getSchemaInfo().getType(),
        SchemaType.JSON);
    assertEquals(((KeyValueSchema<Foo, Bar>) keyValueSchema2).getValueSchema().getSchemaInfo().getType(),
        SchemaType.JSON);
    assertEquals(((KeyValueSchema<Foo, Bar>) keyValueSchema3).getKeySchema().getSchemaInfo().getType(),
        SchemaType.JSON);
    assertEquals(((KeyValueSchema<Foo, Bar>) keyValueSchema3).getValueSchema().getSchemaInfo().getType(),
        SchemaType.JSON);

    String schemaInfo1 = new String(keyValueSchema1.getSchemaInfo().getSchema());
    String schemaInfo2 = new String(keyValueSchema2.getSchemaInfo().getSchema());
    String schemaInfo3 = new String(keyValueSchema3.getSchemaInfo().getSchema());
    assertEquals(schemaInfo1, schemaInfo2);
    assertEquals(schemaInfo1, schemaInfo3);
}
 
Example 12
Source File: KeyValueSchemaTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testAllowNullJsonSchemaCreate() {
    JSONSchema<Foo> fooSchema = JSONSchema.of(SchemaDefinition.<Foo>builder().withPojo(Foo.class).build());
    JSONSchema<Bar> barSchema = JSONSchema.of(SchemaDefinition.<Bar>builder().withPojo(Bar.class).build());

    Schema<KeyValue<Foo, Bar>> keyValueSchema1 = Schema.KeyValue(fooSchema, barSchema);
    Schema<KeyValue<Foo, Bar>> keyValueSchema2 = Schema.KeyValue(Foo.class, Bar.class, SchemaType.JSON);
    Schema<KeyValue<Foo, Bar>> keyValueSchema3 = Schema.KeyValue(Foo.class, Bar.class);

    assertEquals(keyValueSchema1.getSchemaInfo().getType(), SchemaType.KEY_VALUE);
    assertEquals(keyValueSchema2.getSchemaInfo().getType(), SchemaType.KEY_VALUE);
    assertEquals(keyValueSchema3.getSchemaInfo().getType(), SchemaType.KEY_VALUE);

    assertEquals(((KeyValueSchema<Foo, Bar>) keyValueSchema1).getKeySchema().getSchemaInfo().getType(),
            SchemaType.JSON);
    assertEquals(((KeyValueSchema<Foo, Bar>) keyValueSchema1).getValueSchema().getSchemaInfo().getType(),
            SchemaType.JSON);
    assertEquals(((KeyValueSchema<Foo, Bar>) keyValueSchema2).getKeySchema().getSchemaInfo().getType(),
            SchemaType.JSON);
    assertEquals(((KeyValueSchema<Foo, Bar>) keyValueSchema2).getValueSchema().getSchemaInfo().getType(),
            SchemaType.JSON);
    assertEquals(((KeyValueSchema<Foo, Bar>) keyValueSchema3).getKeySchema().getSchemaInfo().getType(),
            SchemaType.JSON);
    assertEquals(((KeyValueSchema<Foo, Bar>) keyValueSchema3).getValueSchema().getSchemaInfo().getType(),
            SchemaType.JSON);

    String schemaInfo1 = new String(keyValueSchema1.getSchemaInfo().getSchema());
    String schemaInfo2 = new String(keyValueSchema2.getSchemaInfo().getSchema());
    String schemaInfo3 = new String(keyValueSchema3.getSchemaInfo().getSchema());
    assertEquals(schemaInfo1, schemaInfo2);
    assertEquals(schemaInfo1, schemaInfo3);
}
 
Example 13
Source File: KeyValueSchemaInfoTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "encodingTypes")
public void encodeDecodeNestedKeyValueSchemaInfo(KeyValueEncodingType encodingType) {
    Schema<KeyValue<String, Bar>> nestedSchema =
        Schema.KeyValue(Schema.STRING, BAR_SCHEMA, KeyValueEncodingType.INLINE);
    Schema<KeyValue<Foo, KeyValue<String, Bar>>> kvSchema = Schema.KeyValue(
        FOO_SCHEMA,
        nestedSchema,
        encodingType
    );
    SchemaInfo kvSchemaInfo = kvSchema.getSchemaInfo();
    assertEquals(
        DefaultImplementation.decodeKeyValueEncodingType(kvSchemaInfo),
        encodingType);

    SchemaInfo encodedSchemaInfo =
        DefaultImplementation.encodeKeyValueSchemaInfo(
            FOO_SCHEMA,
            nestedSchema,
            encodingType);
    assertEquals(encodedSchemaInfo, kvSchemaInfo);
    assertEquals(
        DefaultImplementation.decodeKeyValueEncodingType(encodedSchemaInfo),
        encodingType);

    KeyValue<SchemaInfo, SchemaInfo> schemaInfoKeyValue =
        DefaultImplementation.decodeKeyValueSchemaInfo(kvSchemaInfo);

    assertEquals(schemaInfoKeyValue.getKey(), FOO_SCHEMA.getSchemaInfo());
    assertEquals(schemaInfoKeyValue.getValue().getType(), SchemaType.KEY_VALUE);
    KeyValue<SchemaInfo, SchemaInfo> nestedSchemaInfoKeyValue =
        DefaultImplementation.decodeKeyValueSchemaInfo(schemaInfoKeyValue.getValue());

    assertEquals(nestedSchemaInfoKeyValue.getKey(), Schema.STRING.getSchemaInfo());
    assertEquals(nestedSchemaInfoKeyValue.getValue(), BAR_SCHEMA.getSchemaInfo());
}
 
Example 14
Source File: KeyValueSchemaTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefaultKeyValueEncodingTypeSchemaEncodeAndDecode() {
    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);

    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 default not set value
    byte[] encodeBytes = keyValueSchema.encode(new KeyValue(foo, bar));
    Assert.assertTrue(encodeBytes.length > 0);

    KeyValue<Foo, Bar> keyValue = (KeyValue<Foo, Bar>) keyValueSchema.decode(encodeBytes);
    Foo fooBack = keyValue.getKey();
    Bar barBack = keyValue.getValue();

    assertEquals(foo, fooBack);
    assertEquals(bar, barBack);
}
 
Example 15
Source File: TypedMessageBuilderImplTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetKeyBytesEncodingTypeDefault() {
    producerBase = mock(ProducerBase.class);

    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());

    Schema<KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar>> keyValueSchema = Schema.KeyValue(fooSchema, barSchema);
    TypedMessageBuilderImpl typedMessageBuilderImpl = new TypedMessageBuilderImpl(producerBase, keyValueSchema);

    TypedMessageBuilderImpl typedMessageBuilder = (TypedMessageBuilderImpl)typedMessageBuilderImpl.keyBytes("default".getBytes());
    assertEquals(typedMessageBuilder.getKey(), Base64.getEncoder().encodeToString("default".getBytes()));
    assertTrue(typedMessageBuilder.getMetadataBuilder().getPartitionKeyB64Encoded());
}
 
Example 16
Source File: TypedMessageBuilderImplTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetKeyEncodingTypeInline() {
    producerBase = mock(ProducerBase.class);

    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());

    Schema<KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar>> keyValueSchema = Schema.KeyValue(fooSchema, barSchema, KeyValueEncodingType.INLINE);
    TypedMessageBuilderImpl typedMessageBuilderImpl = new TypedMessageBuilderImpl(producerBase, keyValueSchema);

    TypedMessageBuilderImpl typedMessageBuilder = (TypedMessageBuilderImpl)typedMessageBuilderImpl.key("inline");
    assertEquals(typedMessageBuilder.getKey(), "inline");
    assertFalse(typedMessageBuilder.getMetadataBuilder().getPartitionKeyB64Encoded());
}
 
Example 17
Source File: TypedMessageBuilderImplTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetKeyEncodingTypeDefault() {
    producerBase = mock(ProducerBase.class);

    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());

    Schema<KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar>> keyValueSchema = Schema.KeyValue(fooSchema, barSchema);
    TypedMessageBuilderImpl typedMessageBuilderImpl = new TypedMessageBuilderImpl(producerBase, keyValueSchema);

    TypedMessageBuilderImpl typedMessageBuilder = (TypedMessageBuilderImpl)typedMessageBuilderImpl.key("default");
    assertEquals(typedMessageBuilder.getKey(), "default");
    assertFalse(typedMessageBuilder.getMetadataBuilder().getPartitionKeyB64Encoded());
}
 
Example 18
Source File: TypedMessageBuilderImplTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testInlineValue() {
    producerBase = mock(ProducerBase.class);

    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());

    Schema<KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar>> keyValueSchema = Schema.KeyValue(fooSchema, barSchema, KeyValueEncodingType.INLINE);
    TypedMessageBuilderImpl typedMessageBuilderImpl = new TypedMessageBuilderImpl(producerBase, keyValueSchema);

    SchemaTestUtils.Foo foo = new SchemaTestUtils.Foo();
    foo.setField1("field1");
    foo.setField2("field2");
    SchemaTestUtils.Bar bar = new SchemaTestUtils.Bar();
    bar.setField1(true);
    KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar> keyValue = new KeyValue<>(foo, bar);

    // Check kv.encoding.type INLINE
    TypedMessageBuilderImpl<KeyValue> typedMessageBuilder = (TypedMessageBuilderImpl)typedMessageBuilderImpl.value(keyValue);
    ByteBuffer content = typedMessageBuilder.getContent();
    byte[] contentByte = new byte[content.remaining()];
    content.get(contentByte);
    KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar> decodeKeyValue = keyValueSchema.decode(contentByte);
    assertEquals(decodeKeyValue.getKey(), foo);
    assertEquals(decodeKeyValue.getValue(), bar);
    assertFalse(typedMessageBuilderImpl.hasKey());
}
 
Example 19
Source File: TypedMessageBuilderImplTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefaultValue() {
    producerBase = mock(ProducerBase.class);

    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());

    Schema<KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar>> keyValueSchema = Schema.KeyValue(fooSchema, barSchema);
    TypedMessageBuilderImpl typedMessageBuilderImpl = new TypedMessageBuilderImpl(producerBase, keyValueSchema);

    SchemaTestUtils.Foo foo = new SchemaTestUtils.Foo();
    foo.setField1("field1");
    foo.setField2("field2");
    SchemaTestUtils.Bar bar = new SchemaTestUtils.Bar();
    bar.setField1(true);
    KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar> keyValue = new KeyValue<>(foo, bar);

    // Check kv.encoding.type default, not set value
    TypedMessageBuilderImpl<KeyValue>  typedMessageBuilder = (TypedMessageBuilderImpl)typedMessageBuilderImpl.value(keyValue);
    ByteBuffer content = typedMessageBuilder.getContent();
    byte[] contentByte = new byte[content.remaining()];
    content.get(contentByte);
    KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar>  decodeKeyValue = keyValueSchema.decode(contentByte);
    assertEquals(decodeKeyValue.getKey(), foo);
    assertEquals(decodeKeyValue.getValue(), bar);
    assertFalse(typedMessageBuilderImpl.hasKey());
}
 
Example 20
Source File: AdminApiSchemaTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void createKeyValueSchema() throws Exception {
    String topicName = "schematest/test/test-key-value-schema";
    Schema keyValueSchema = Schema.KeyValue(Schema.AVRO(Foo.class), Schema.AVRO(Foo.class));
    admin.schemas().createSchema(topicName,
            keyValueSchema.getSchemaInfo());
    SchemaInfo schemaInfo = admin.schemas().getSchemaInfo(topicName);

    assertEquals(schemaInfo, keyValueSchema.getSchemaInfo());
}