|
| 1 | +package com.aerospike.mapper; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +import com.aerospike.mapper.annotations.AerospikeBin; |
| 11 | +import com.aerospike.mapper.annotations.AerospikeEmbed; |
| 12 | +import com.aerospike.mapper.annotations.AerospikeEmbed.EmbedType; |
| 13 | +import com.aerospike.mapper.annotations.AerospikeKey; |
| 14 | +import com.aerospike.mapper.annotations.AerospikeRecord; |
| 15 | +import com.aerospike.mapper.tools.AeroMapper; |
| 16 | + |
| 17 | +import lombok.AllArgsConstructor; |
| 18 | +import lombok.Data; |
| 19 | +import lombok.NoArgsConstructor; |
| 20 | + |
| 21 | +public class BaseClassWithNoAttributesTest extends AeroMapperBaseTest { |
| 22 | + |
| 23 | + public static class Animal { |
| 24 | + } |
| 25 | + |
| 26 | + @Data |
| 27 | + @AllArgsConstructor |
| 28 | + @AerospikeRecord(namespace = "test", set = "zoo") |
| 29 | + public static class Dog extends Animal{ |
| 30 | + |
| 31 | + @AerospikeKey |
| 32 | + private String dogId; |
| 33 | + private String breed; |
| 34 | + |
| 35 | + public Dog(){ |
| 36 | + dogId = "" + ((int) (Math.random() * 1000)); |
| 37 | + } |
| 38 | + |
| 39 | + public Dog(String breed) { |
| 40 | + this.breed = breed; |
| 41 | + dogId = "" + ((int) (Math.random() * 1000)); |
| 42 | + } |
| 43 | + |
| 44 | + public String getBreed() { |
| 45 | + return breed; |
| 46 | + } |
| 47 | + |
| 48 | + public String getDogId() { |
| 49 | + return dogId; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Data |
| 54 | + @AllArgsConstructor |
| 55 | + @AerospikeRecord(namespace = "test", set = "zoo") |
| 56 | + public static class Cat extends Animal { |
| 57 | + |
| 58 | + @AerospikeKey |
| 59 | + private String catId; |
| 60 | + private String breed; |
| 61 | + private String lifeSpan; |
| 62 | + |
| 63 | + public Cat(String breed, String lifeSpan) { |
| 64 | + this.breed = breed; |
| 65 | + this.lifeSpan = lifeSpan; |
| 66 | + |
| 67 | + catId = "" + ((int) (Math.random() * 1000)); |
| 68 | + } |
| 69 | + |
| 70 | + public Cat() { |
| 71 | + catId = "" + ((int) (Math.random() * 1000)); |
| 72 | + } |
| 73 | + |
| 74 | + public String getBreed() { |
| 75 | + return breed; |
| 76 | + } |
| 77 | + |
| 78 | + public String getCatId() { |
| 79 | + return catId; |
| 80 | + } |
| 81 | + |
| 82 | + public String getLifeSpan() { |
| 83 | + return lifeSpan; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @AerospikeRecord(namespace = "test", set = "zoo", mapAll = false) |
| 88 | + @NoArgsConstructor |
| 89 | + @Data |
| 90 | + public static class Zoo { |
| 91 | + |
| 92 | + @AerospikeBin |
| 93 | + @AerospikeKey |
| 94 | + private String zooId; |
| 95 | + |
| 96 | + @AerospikeBin |
| 97 | + @AerospikeEmbed(type = EmbedType.LIST, elementType = EmbedType.MAP) |
| 98 | + private List<Animal> animalsList; |
| 99 | + |
| 100 | + public Zoo(String zooId, List<Animal> animalsList) { |
| 101 | + super(); |
| 102 | + this.zooId = zooId; |
| 103 | + this.animalsList = animalsList; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + @Test |
| 109 | + public void runTest() { |
| 110 | + AeroMapper mapper = new AeroMapper.Builder(client).build(); |
| 111 | + |
| 112 | + Zoo zoo = new Zoo(); |
| 113 | + zoo.setZooId("103"); |
| 114 | + |
| 115 | + List<Animal> listOfAnimals = new ArrayList<>(); |
| 116 | + listOfAnimals.add(new Cat("Persian", "30")); |
| 117 | + listOfAnimals.add(new Cat("Indian", "21")); |
| 118 | + listOfAnimals.add(new Dog("Lab")); |
| 119 | + listOfAnimals.add(new Cat("Korean", "15")); |
| 120 | + listOfAnimals.add(new Dog("Desi")); |
| 121 | + |
| 122 | + zoo.setAnimalsList(listOfAnimals); |
| 123 | + mapper.save(zoo); |
| 124 | + |
| 125 | + Zoo readZoo = mapper.read(Zoo.class, "103"); |
| 126 | + assertEquals(zoo.zooId, readZoo.zooId); |
| 127 | + assertEquals(zoo, readZoo); |
| 128 | + } |
| 129 | +} |
0 commit comments