|
| 1 | +package com.fasterxml.jackson.dataformat.avro.interop.annotations; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | + |
| 5 | +import org.apache.avro.Schema; |
| 6 | +import org.apache.avro.SchemaCompatibility; |
| 7 | +import org.apache.avro.reflect.AvroAlias; |
| 8 | +import org.apache.avro.reflect.Nullable; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import com.fasterxml.jackson.dataformat.avro.AvroTestBase; |
| 12 | +import com.fasterxml.jackson.dataformat.avro.interop.InteropTestBase; |
| 13 | + |
| 14 | +import static org.assertj.core.api.Assertions.assertThat; |
| 15 | + |
| 16 | +public class AvroAliasTest extends InteropTestBase { |
| 17 | + |
| 18 | + @AvroAlias(alias = "Employee", space = "com.fasterxml.jackson.dataformat.avro.AvroTestBase$") |
| 19 | + public static class NewEmployee { |
| 20 | + |
| 21 | + public String name; |
| 22 | + |
| 23 | + public int age; |
| 24 | + |
| 25 | + public String[] emails; |
| 26 | + |
| 27 | + public NewEmployee boss; |
| 28 | + } |
| 29 | + |
| 30 | + @AvroAlias(alias = "NewEmployee") |
| 31 | + public static class AliasedNameEmployee { |
| 32 | + |
| 33 | + public String name; |
| 34 | + |
| 35 | + public int age; |
| 36 | + |
| 37 | + public String[] emails; |
| 38 | + |
| 39 | + @Nullable |
| 40 | + public AliasedNameEmployee boss; |
| 41 | + } |
| 42 | + |
| 43 | + @AvroAlias(alias = "Size", space = "com.fasterxml.jackson.dataformat.avro.AvroTestBase$") |
| 44 | + public static enum NewSize { |
| 45 | + SMALL, |
| 46 | + LARGE; |
| 47 | + } |
| 48 | + |
| 49 | + @AvroAlias(alias = "NewestSize") |
| 50 | + public static enum NewerSize { |
| 51 | + SMALL, |
| 52 | + LARGE; |
| 53 | + } |
| 54 | + |
| 55 | + @AvroAlias(alias = "NewerSize") |
| 56 | + public static enum NewestSize { |
| 57 | + SMALL, |
| 58 | + LARGE; |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testAliasedRecordForwardsCompatible() throws IOException { |
| 63 | + Schema oldSchema = schemaFunctor.apply(AvroTestBase.Employee.class); |
| 64 | + Schema newSchema = schemaFunctor.apply(NewEmployee.class); |
| 65 | + // |
| 66 | + SchemaCompatibility.SchemaPairCompatibility compatibility = |
| 67 | + SchemaCompatibility.checkReaderWriterCompatibility(newSchema, oldSchema); |
| 68 | + // |
| 69 | + assertThat(compatibility.getType()).isEqualTo(SchemaCompatibility.SchemaCompatibilityType.COMPATIBLE); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testAliasedRecordBackwardsCompatible() throws IOException { |
| 74 | + Schema oldSchema = schemaFunctor.apply(AvroTestBase.Employee.class); |
| 75 | + Schema newSchema = schemaFunctor.apply(NewEmployee.class); |
| 76 | + // |
| 77 | + SchemaCompatibility.SchemaPairCompatibility compatibility = |
| 78 | + SchemaCompatibility.checkReaderWriterCompatibility(oldSchema, newSchema); |
| 79 | + // |
| 80 | + assertThat(compatibility.getType()).isEqualTo(SchemaCompatibility.SchemaCompatibilityType.INCOMPATIBLE); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testAliasedRecordForwardsCompatibleSameNamespace() throws IOException { |
| 85 | + Schema oldSchema = schemaFunctor.apply(NewEmployee.class); |
| 86 | + Schema newSchema = schemaFunctor.apply(AliasedNameEmployee.class); |
| 87 | + // |
| 88 | + SchemaCompatibility.SchemaPairCompatibility compatibility = |
| 89 | + SchemaCompatibility.checkReaderWriterCompatibility(newSchema, oldSchema); |
| 90 | + // |
| 91 | + assertThat(compatibility.getType()).isEqualTo(SchemaCompatibility.SchemaCompatibilityType.COMPATIBLE); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testAliasedRecordBackwardsCompatibleSameNamespace() throws IOException { |
| 96 | + Schema oldSchema = schemaFunctor.apply(NewEmployee.class); |
| 97 | + Schema newSchema = schemaFunctor.apply(AliasedNameEmployee.class); |
| 98 | + // |
| 99 | + SchemaCompatibility.SchemaPairCompatibility compatibility = |
| 100 | + SchemaCompatibility.checkReaderWriterCompatibility(oldSchema, newSchema); |
| 101 | + // |
| 102 | + assertThat(compatibility.getType()).isEqualTo(SchemaCompatibility.SchemaCompatibilityType.INCOMPATIBLE); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testAliasedEnumForwardsCompatible() throws IOException { |
| 107 | + Schema oldSchema = schemaFunctor.apply(AvroTestBase.Size.class); |
| 108 | + Schema newSchema = schemaFunctor.apply(NewSize.class); |
| 109 | + // |
| 110 | + SchemaCompatibility.SchemaPairCompatibility compatibility = |
| 111 | + SchemaCompatibility.checkReaderWriterCompatibility(newSchema, oldSchema); |
| 112 | + // |
| 113 | + assertThat(compatibility.getType()).isEqualTo(SchemaCompatibility.SchemaCompatibilityType.COMPATIBLE); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testAliasedEnumBackwardsCompatible() throws IOException { |
| 118 | + Schema oldSchema = schemaFunctor.apply(AvroTestBase.Size.class); |
| 119 | + Schema newSchema = schemaFunctor.apply(NewSize.class); |
| 120 | + // |
| 121 | + SchemaCompatibility.SchemaPairCompatibility compatibility = |
| 122 | + SchemaCompatibility.checkReaderWriterCompatibility(oldSchema, newSchema); |
| 123 | + // |
| 124 | + assertThat(compatibility.getType()).isEqualTo(SchemaCompatibility.SchemaCompatibilityType.INCOMPATIBLE); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void testAliasedEnumForwardsAndBackwardsCompatible() throws IOException { |
| 129 | + Schema oldSchema = schemaFunctor.apply(NewerSize.class); |
| 130 | + Schema newSchema = schemaFunctor.apply(NewestSize.class); |
| 131 | + // |
| 132 | + SchemaCompatibility.SchemaPairCompatibility backwardsCompatibility = |
| 133 | + SchemaCompatibility.checkReaderWriterCompatibility(oldSchema, newSchema); |
| 134 | + SchemaCompatibility.SchemaPairCompatibility forwardsCompatibility = |
| 135 | + SchemaCompatibility.checkReaderWriterCompatibility(newSchema, oldSchema); |
| 136 | + // |
| 137 | + assertThat(backwardsCompatibility.getType()).isEqualTo(SchemaCompatibility.SchemaCompatibilityType.COMPATIBLE); |
| 138 | + assertThat(forwardsCompatibility.getType()).isEqualTo(SchemaCompatibility.SchemaCompatibilityType.COMPATIBLE); |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments