Search in sources :

Example 1 with DefaultSubjectNamingStrategy

use of org.springframework.cloud.stream.schema.avro.DefaultSubjectNamingStrategy in project spring-cloud-stream by spring-cloud.

the class AvroMessageConverterSerializationTests method sourceWriteSameVersion.

@Test
public void sourceWriteSameVersion() throws Exception {
    User specificRecord = new User();
    specificRecord.setName("joe");
    Schema v1 = new Schema.Parser().parse(AvroMessageConverterSerializationTests.class.getClassLoader().getResourceAsStream("schemas/user.avsc"));
    GenericRecord genericRecord = new GenericData.Record(v1);
    genericRecord.put("name", "joe");
    SchemaRegistryClient client = new DefaultSchemaRegistryClient();
    AvroSchemaRegistryClientMessageConverter converter = new AvroSchemaRegistryClientMessageConverter(client, new NoOpCacheManager());
    converter.setSubjectNamingStrategy(new DefaultSubjectNamingStrategy());
    converter.setDynamicSchemaGenerationEnabled(false);
    converter.afterPropertiesSet();
    Message specificMessage = converter.toMessage(specificRecord, new MutableMessageHeaders(Collections.<String, Object>emptyMap()), MimeTypeUtils.parseMimeType("application/*+avro"));
    SchemaReference specificRef = extractSchemaReference(MimeTypeUtils.parseMimeType(specificMessage.getHeaders().get("contentType").toString()));
    Message genericMessage = converter.toMessage(genericRecord, new MutableMessageHeaders(Collections.<String, Object>emptyMap()), MimeTypeUtils.parseMimeType("application/*+avro"));
    SchemaReference genericRef = extractSchemaReference(MimeTypeUtils.parseMimeType(genericMessage.getHeaders().get("contentType").toString()));
    Assert.assertEquals(genericRef, specificRef);
    Assert.assertEquals(1, genericRef.getVersion());
}
Also used : User(example.avro.User) Message(org.springframework.messaging.Message) Schema(org.apache.avro.Schema) NoOpCacheManager(org.springframework.cache.support.NoOpCacheManager) SchemaReference(org.springframework.cloud.stream.schema.SchemaReference) DefaultSubjectNamingStrategy(org.springframework.cloud.stream.schema.avro.DefaultSubjectNamingStrategy) AvroSchemaRegistryClientMessageConverter(org.springframework.cloud.stream.schema.avro.AvroSchemaRegistryClientMessageConverter) GenericRecord(org.apache.avro.generic.GenericRecord) GenericRecord(org.apache.avro.generic.GenericRecord) MutableMessageHeaders(org.springframework.integration.support.MutableMessageHeaders) DefaultSchemaRegistryClient(org.springframework.cloud.stream.schema.client.DefaultSchemaRegistryClient) DefaultSchemaRegistryClient(org.springframework.cloud.stream.schema.client.DefaultSchemaRegistryClient) SchemaRegistryClient(org.springframework.cloud.stream.schema.client.SchemaRegistryClient) Test(org.junit.Test)

Aggregations

User (example.avro.User)1 Schema (org.apache.avro.Schema)1 GenericRecord (org.apache.avro.generic.GenericRecord)1 Test (org.junit.Test)1 NoOpCacheManager (org.springframework.cache.support.NoOpCacheManager)1 SchemaReference (org.springframework.cloud.stream.schema.SchemaReference)1 AvroSchemaRegistryClientMessageConverter (org.springframework.cloud.stream.schema.avro.AvroSchemaRegistryClientMessageConverter)1 DefaultSubjectNamingStrategy (org.springframework.cloud.stream.schema.avro.DefaultSubjectNamingStrategy)1 DefaultSchemaRegistryClient (org.springframework.cloud.stream.schema.client.DefaultSchemaRegistryClient)1 SchemaRegistryClient (org.springframework.cloud.stream.schema.client.SchemaRegistryClient)1 MutableMessageHeaders (org.springframework.integration.support.MutableMessageHeaders)1 Message (org.springframework.messaging.Message)1