|
| 1 | +package com.fasterxml.jackson.dataformat.avro.interop.annotations; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 4 | +import com.fasterxml.jackson.dataformat.avro.interop.InteropTestBase; |
| 5 | + |
| 6 | +import lombok.Data; |
| 7 | +import org.apache.avro.AvroRuntimeException; |
| 8 | +import org.apache.avro.Schema; |
| 9 | +import org.apache.avro.reflect.AvroMeta; |
| 10 | +import org.apache.avro.reflect.AvroSchema; |
| 11 | +import org.junit.Test; |
| 12 | + |
| 13 | +import static org.assertj.core.api.Assertions.assertThat; |
| 14 | + |
| 15 | +public class AvroMetaTest extends InteropTestBase { |
| 16 | + |
| 17 | + @Data |
| 18 | + @AvroMeta(key = "class-meta", value = "class value") |
| 19 | + static class MetaTest { |
| 20 | + |
| 21 | + @AvroMeta(key = "custom Property", value = "Some Value") |
| 22 | + private String someProperty; |
| 23 | + |
| 24 | + @AvroMeta(key = "required custom Property", value = "Some required Value") |
| 25 | + @JsonProperty(required = true) |
| 26 | + private String someRequiredProperty; |
| 27 | + } |
| 28 | + |
| 29 | + @Data |
| 30 | + static class BadMetaTest { |
| 31 | + |
| 32 | + @AvroMeta(key = "name", value = "colliding property") |
| 33 | + private String types; |
| 34 | + } |
| 35 | + |
| 36 | + @Data |
| 37 | + @AvroSchema("{\"type\":\"string\"}") |
| 38 | + @AvroMeta(key="overridden", value = "true") |
| 39 | + static class OverriddenClassSchema { |
| 40 | + |
| 41 | + private int field; |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testAnnotationPrecedence() { |
| 46 | + Schema schema = schemaFunctor.apply(OverriddenClassSchema.class); |
| 47 | + // |
| 48 | + assertThat(schema.getProp("overridden")).isNull(); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testOptionalFieldMetaProperty() { |
| 53 | + Schema schema = schemaFunctor.apply(MetaTest.class); |
| 54 | + // |
| 55 | + assertThat(schema.getField("someProperty").getProp("custom Property")).isEqualTo("Some Value"); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testRequiredFieldMetaProperty() { |
| 60 | + Schema schema = schemaFunctor.apply(MetaTest.class); |
| 61 | + // |
| 62 | + assertThat(schema.getField("someRequiredProperty").getProp("required custom Property")).isEqualTo("Some required Value"); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testClassMetaProperty() { |
| 67 | + Schema schema = schemaFunctor.apply(MetaTest.class); |
| 68 | + // |
| 69 | + assertThat(schema.getProp("class-meta")).isEqualTo("class value"); |
| 70 | + } |
| 71 | + |
| 72 | + @Test(expected = AvroRuntimeException.class) |
| 73 | + public void testCollidingMeta() { |
| 74 | + schemaFunctor.apply(BadMetaTest.class); |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments