|
| 1 | +package com.fasterxml.jackson.dataformat.avro.interop.annotations; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.HashSet; |
| 6 | +import java.util.Objects; |
| 7 | +import java.util.Set; |
| 8 | +import org.apache.avro.Schema; |
| 9 | +import org.apache.avro.SchemaBuilder; |
| 10 | +import org.apache.avro.reflect.Union; |
| 11 | +import org.junit.Test; |
| 12 | + |
| 13 | +import static com.fasterxml.jackson.dataformat.avro.interop.ApacheAvroInteropUtil.jacksonDeserialize; |
| 14 | +import static com.fasterxml.jackson.dataformat.avro.interop.ApacheAvroInteropUtil.jacksonSerialize; |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | + |
| 17 | +public class UnionResolvingTest { |
| 18 | + private Schema SCHEMA = SchemaBuilder |
| 19 | + .builder(UnionResolvingTest.class.getName() + "$") |
| 20 | + .record(Box.class.getSimpleName()) |
| 21 | + .fields() |
| 22 | + .name("animal") |
| 23 | + .type() |
| 24 | + .unionOf() |
| 25 | + .record(Cat.class.getSimpleName()) |
| 26 | + .fields() |
| 27 | + .name("name").type().stringType().noDefault() |
| 28 | + .endRecord() |
| 29 | + .and() |
| 30 | + .record(Dog.class.getSimpleName()) |
| 31 | + .fields() |
| 32 | + .name("tricks").type().array().items().stringType().noDefault() |
| 33 | + .endRecord() |
| 34 | + .endUnion() |
| 35 | + .noDefault() |
| 36 | + .endRecord(); |
| 37 | + |
| 38 | + |
| 39 | + @Union({ Cat.class, Dog.class }) |
| 40 | + public interface Animal { } |
| 41 | + |
| 42 | + static final class Cat implements Animal { |
| 43 | + public Cat() {} |
| 44 | + |
| 45 | + @Override |
| 46 | + public boolean equals(Object o) { |
| 47 | + return o instanceof Cat; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + static final class Dog implements Animal { |
| 52 | + // Unsupported schema generation |
| 53 | + public Set<?> tricks = new HashSet<>(); |
| 54 | + |
| 55 | + public Dog() { |
| 56 | + } |
| 57 | + |
| 58 | + public Dog(final Set<?> tricks) { |
| 59 | + this.tricks = tricks; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public boolean equals(Object o) { |
| 64 | + return o instanceof Dog && tricks.equals(((Dog) o).tricks); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public static class Box { |
| 69 | + public Animal animal; |
| 70 | + |
| 71 | + public Box() { |
| 72 | + } |
| 73 | + |
| 74 | + public Box(Animal a) { animal = a; } |
| 75 | + |
| 76 | + @Override |
| 77 | + public boolean equals(Object o) { |
| 78 | + return o instanceof Box && Objects.equals(animal, ((Box) o).animal); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testResolveUnionUsingSchemaName() throws IOException { |
| 84 | + final Box box = new Box(new Dog(Collections.singleton("catch stick"))); |
| 85 | + |
| 86 | + final Box result = jacksonDeserialize(SCHEMA, Box.class, jacksonSerialize(SCHEMA, box)); |
| 87 | + |
| 88 | + assertThat(result).isEqualTo(box); |
| 89 | + } |
| 90 | +} |
0 commit comments