|
| 1 | +package com.aerospike.mapper; |
| 2 | + |
| 3 | +import com.aerospike.mapper.annotations.AerospikeExclude; |
| 4 | +import com.aerospike.mapper.annotations.AerospikeGetter; |
| 5 | +import com.aerospike.mapper.annotations.AerospikeKey; |
| 6 | +import com.aerospike.mapper.annotations.AerospikeRecord; |
| 7 | +import com.aerospike.mapper.annotations.AerospikeSetter; |
| 8 | +import com.aerospike.mapper.tools.AeroMapper; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +import java.util.Date; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 14 | + |
| 15 | +public class FinalFieldMappingTest extends AeroMapperBaseTest { |
| 16 | + |
| 17 | + @Test |
| 18 | + void test() { |
| 19 | + AeroMapper mapper = new AeroMapper.Builder(client).build(); |
| 20 | + Customer customer = new Customer(); |
| 21 | + customer.id = 1; |
| 22 | + customer.name = "tim"; |
| 23 | + customer.dob = new Date(); |
| 24 | + |
| 25 | + Doc doc = new Doc(); |
| 26 | + doc.id = 1; |
| 27 | + doc.data = "This is some data"; |
| 28 | + |
| 29 | + mapper.save(customer, doc); |
| 30 | + Customer readCustomer = mapper.read(Customer.class, customer.getKey()); |
| 31 | + Doc readDoc = mapper.read(Doc.class, doc.getKey()); |
| 32 | + assertEquals(doc.toString(), readDoc.toString()); |
| 33 | + |
| 34 | + ReferencingClass ref = new ReferencingClass(); |
| 35 | + ref.id = 2; |
| 36 | + ref.cust = readCustomer; |
| 37 | + ref.doc = doc; |
| 38 | + mapper.save(null, ref); |
| 39 | + ReferencingClass readRef = mapper.read(ReferencingClass.class, ref.id); |
| 40 | + assertEquals(ref.toString(), readRef.toString()); |
| 41 | + } |
| 42 | + |
| 43 | + @AerospikeRecord(namespace = "test", set = "common") |
| 44 | + public static class Customer { |
| 45 | + @AerospikeExclude |
| 46 | + public static final String PREFIX = "CUST:"; |
| 47 | + @AerospikeExclude |
| 48 | + private int id; |
| 49 | + private String name; |
| 50 | + private Date dob; |
| 51 | + |
| 52 | + @AerospikeKey |
| 53 | + @AerospikeGetter(name = "key") |
| 54 | + public String getKey() { |
| 55 | + return PREFIX + this.id; |
| 56 | + } |
| 57 | + |
| 58 | + @AerospikeKey(setter = true) |
| 59 | + @AerospikeSetter(name = "key") |
| 60 | + public void setKey(String key) { |
| 61 | + this.id = Integer.parseInt(key.substring(PREFIX.length())); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public String toString() { |
| 66 | + return String.format("{id=%d,name='%s',dob=%s}", id, name, dob); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @AerospikeRecord(namespace = "test", set = "common") |
| 71 | + public static class Doc { |
| 72 | + private static final String PREFIX = "DOC:"; |
| 73 | + @AerospikeExclude |
| 74 | + public int id; |
| 75 | + private String data; |
| 76 | + |
| 77 | + @AerospikeKey |
| 78 | + @AerospikeGetter(name = "key") |
| 79 | + public String getKey() { |
| 80 | + return PREFIX + this.id; |
| 81 | + } |
| 82 | + |
| 83 | + @AerospikeKey(setter = true) |
| 84 | + @AerospikeSetter(name = "key") |
| 85 | + public void setKey(String key) { |
| 86 | + this.id = Integer.parseInt(key.substring(PREFIX.length())); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public String toString() { |
| 91 | + return String.format("{id=%d,data='%s'}", id, data); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @AerospikeRecord(namespace = "test", set = "ref") |
| 96 | + public static class ReferencingClass { |
| 97 | + @AerospikeKey |
| 98 | + public int id; |
| 99 | + public Customer cust; |
| 100 | + public Doc doc; |
| 101 | + |
| 102 | + @Override |
| 103 | + public String toString() { |
| 104 | + return String.format("{id=%d,cust=%s,doc=%s}", id, cust, doc); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments