|
| 1 | +package com.fasterxml.jackson.dataformat.avro.annotations; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonMappingException; |
| 4 | +import com.fasterxml.jackson.dataformat.avro.AvroMapper; |
| 5 | +import com.fasterxml.jackson.dataformat.avro.schema.AvroSchemaGenerator; |
| 6 | +import org.apache.avro.Schema; |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +import static org.assertj.core.api.Assertions.assertThat; |
| 10 | + |
| 11 | +public class AvroNamespaceTest { |
| 12 | + |
| 13 | + static class ClassWithoutAvroNamespaceAnnotation { |
| 14 | + } |
| 15 | + |
| 16 | + @AvroNamespace("ClassWithAvroNamespaceAnnotation.namespace") |
| 17 | + static class ClassWithAvroNamespaceAnnotation { |
| 18 | + } |
| 19 | + |
| 20 | + enum EnumWithoutAvroNamespaceAnnotation {FOO, BAR;} |
| 21 | + |
| 22 | + @AvroNamespace("EnumWithAvroNamespaceAnnotation.namespace") |
| 23 | + enum EnumWithAvroNamespaceAnnotation {FOO, BAR;} |
| 24 | + |
| 25 | + @Test |
| 26 | + public void class_without_AvroNamespace_test() throws JsonMappingException { |
| 27 | + // GIVEN |
| 28 | + AvroMapper mapper = new AvroMapper(); |
| 29 | + AvroSchemaGenerator gen = new AvroSchemaGenerator(); |
| 30 | + |
| 31 | + // WHEN |
| 32 | + mapper.acceptJsonFormatVisitor(ClassWithoutAvroNamespaceAnnotation.class, gen); |
| 33 | + Schema actualSchema = gen.getGeneratedSchema().getAvroSchema(); |
| 34 | + |
| 35 | + // THEN |
| 36 | + assertThat(actualSchema.getNamespace()) |
| 37 | + .isEqualTo("com.fasterxml.jackson.dataformat.avro.annotations.AvroNamespaceTest$"); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void class_with_AvroNamespace_test() throws JsonMappingException { |
| 42 | + // GIVEN |
| 43 | + AvroMapper mapper = new AvroMapper(); |
| 44 | + AvroSchemaGenerator gen = new AvroSchemaGenerator(); |
| 45 | + |
| 46 | + // WHEN |
| 47 | + mapper.acceptJsonFormatVisitor(ClassWithAvroNamespaceAnnotation.class, gen); |
| 48 | + Schema actualSchema = gen.getGeneratedSchema().getAvroSchema(); |
| 49 | + |
| 50 | + // THEN |
| 51 | + assertThat(actualSchema.getNamespace()) |
| 52 | + .isEqualTo("ClassWithAvroNamespaceAnnotation.namespace"); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void enum_without_AvroNamespace_test() throws JsonMappingException { |
| 57 | + // GIVEN |
| 58 | + AvroMapper mapper = new AvroMapper(); |
| 59 | + AvroSchemaGenerator gen = new AvroSchemaGenerator(); |
| 60 | + |
| 61 | + // WHEN |
| 62 | + mapper.acceptJsonFormatVisitor(EnumWithoutAvroNamespaceAnnotation.class, gen); |
| 63 | + Schema actualSchema = gen.getGeneratedSchema().getAvroSchema(); |
| 64 | + |
| 65 | + // THEN |
| 66 | + assertThat(actualSchema.getNamespace()) |
| 67 | + .isEqualTo("com.fasterxml.jackson.dataformat.avro.annotations.AvroNamespaceTest$"); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void enum_with_AvroNamespace_test() throws JsonMappingException { |
| 72 | + // GIVEN |
| 73 | + AvroMapper mapper = new AvroMapper(); |
| 74 | + AvroSchemaGenerator gen = new AvroSchemaGenerator(); |
| 75 | + |
| 76 | + // WHEN |
| 77 | + mapper.acceptJsonFormatVisitor(EnumWithAvroNamespaceAnnotation.class, gen); |
| 78 | + Schema actualSchema = gen.getGeneratedSchema().getAvroSchema(); |
| 79 | + |
| 80 | + // THEN |
| 81 | + assertThat(actualSchema.getNamespace()) |
| 82 | + .isEqualTo("EnumWithAvroNamespaceAnnotation.namespace"); |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments