|
1 | 1 | package com.aerospike.mapper;
|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
| 4 | +import static java.util.Comparator.comparing; |
| 5 | +import static java.util.stream.Collectors.toList; |
4 | 6 |
|
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Objects; |
5 | 10 | import java.util.concurrent.atomic.AtomicInteger;
|
6 | 11 |
|
7 | 12 | import org.junit.jupiter.api.Test;
|
8 | 13 |
|
9 | 14 | import com.aerospike.client.AerospikeException;
|
| 15 | +import com.aerospike.client.exp.Exp; |
10 | 16 | import com.aerospike.client.policy.QueryPolicy;
|
11 | 17 | import com.aerospike.client.query.Filter;
|
12 | 18 | import com.aerospike.client.query.IndexType;
|
@@ -46,31 +52,52 @@ public int getAge() {
|
46 | 52 | public String toString() {
|
47 | 53 | return String.format("id:%d, name:%s, age:%d", id, name, age);
|
48 | 54 | }
|
| 55 | + |
| 56 | + @Override |
| 57 | + public boolean equals(Object o) { |
| 58 | + if (this == o) { |
| 59 | + return true; |
| 60 | + } |
| 61 | + if (o == null || getClass() != o.getClass()) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + A a = (A) o; |
| 65 | + return id == a.id && age == a.age && Objects.equals(name, a.name); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public int hashCode() { |
| 70 | + return Objects.hash(id, name, age); |
| 71 | + } |
49 | 72 | }
|
50 | 73 |
|
| 74 | + private List<A> data = new ArrayList<A>() {{ |
| 75 | + add(new A(1, "Tim", 312)); |
| 76 | + add(new A(2, "Bob", 44)); |
| 77 | + add(new A(3, "Sue", 56)); |
| 78 | + add(new A(4, "Rob", 23)); |
| 79 | + add(new A(5, "Jim", 32)); |
| 80 | + add(new A(6, "Bob", 78)); |
| 81 | + add(new A(7, "Fred", 23)); |
| 82 | + add(new A(8, "Wilma", 11)); |
| 83 | + add(new A(9, "Barney", 54)); |
| 84 | + add(new A(10, "Steve", 72)); |
| 85 | + add(new A(11, "Bam Bam", 19)); |
| 86 | + add(new A(12, "Betty", 34)); |
| 87 | + add(new A(13, "Del", 7)); |
| 88 | + add(new A(14, "Khon", 98)); |
| 89 | + add(new A(15, "Dave", 21)); |
| 90 | + add(new A(16, "Mike", 32)); |
| 91 | + add(new A(17, "Darren", 14)); |
| 92 | + add(new A(18, "Lucy", 45)); |
| 93 | + add(new A(19, "Gertrude", 36)); |
| 94 | + add(new A(20, "Lucinda", 63)); |
| 95 | + }}; |
| 96 | + |
51 | 97 | private AeroMapper populate() {
|
52 | 98 | client.truncate(null, "test", "testScan", null);
|
53 | 99 | AeroMapper mapper = new AeroMapper.Builder(client).build();
|
54 |
| - mapper.save(new A(1, "Tim", 312), |
55 |
| - new A(2, "Bob", 44), |
56 |
| - new A(3, "Sue", 56), |
57 |
| - new A(4, "Rob", 23), |
58 |
| - new A(5, "Jim", 32), |
59 |
| - new A(6, "Bob", 78), |
60 |
| - new A(7, "Fred", 23), |
61 |
| - new A(8, "Wilma", 11), |
62 |
| - new A(9, "Barney", 54), |
63 |
| - new A(10, "Steve", 72), |
64 |
| - new A(11, "Bam Bam", 19), |
65 |
| - new A(12, "Betty", 34), |
66 |
| - new A(13, "Del", 7), |
67 |
| - new A(14, "Khon", 98), |
68 |
| - new A(15, "Dave", 21), |
69 |
| - new A(16, "Mike", 32), |
70 |
| - new A(17, "Darren", 14), |
71 |
| - new A(18, "Lucy", 45), |
72 |
| - new A(19, "Gertrude", 36), |
73 |
| - new A(20, "Lucinda", 63)); |
| 100 | + mapper.save(data.toArray()); |
74 | 101 |
|
75 | 102 | try {
|
76 | 103 | client.createIndex(null, "test", "testScan", "age_idx", "age", IndexType.NUMERIC).waitTillComplete();
|
@@ -104,4 +131,34 @@ public void queryTestWithAbort() {
|
104 | 131 | }, Filter.range("age", 30, 54));
|
105 | 132 | assertEquals(1, counter.get());
|
106 | 133 | }
|
| 134 | + |
| 135 | + @Test |
| 136 | + public void queryTestReturnsList() { |
| 137 | + AeroMapper mapper = populate(); |
| 138 | + |
| 139 | + List<A> result = mapper.query(A.class, Filter.range("age", 30, 54)); |
| 140 | + |
| 141 | + List<A> expected = data.stream() |
| 142 | + .filter(d -> d.age >= 30 && d.age <= 54) |
| 143 | + .sorted(comparing(A::getId)) |
| 144 | + .collect(toList()); |
| 145 | + assertEquals(7, result.size()); |
| 146 | + assertEquals(expected, result.stream().sorted(comparing(A::getId)).collect(toList())); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + public void queryWithQueryPolicyTestReturnsList() { |
| 151 | + AeroMapper mapper = populate(); |
| 152 | + QueryPolicy queryPolicy = new QueryPolicy(); |
| 153 | + queryPolicy.filterExp = Exp.build(Exp.eq(Exp.stringBin("name"), Exp.val("Bob"))); |
| 154 | + |
| 155 | + List<A> result = mapper.query(queryPolicy, A.class, Filter.range("age", 44, 78)); |
| 156 | + |
| 157 | + List<A> expected = data.stream() |
| 158 | + .filter(d -> d.age >= 44 && d.age <= 78 && d.name.equals("Bob")) |
| 159 | + .sorted(comparing(A::getId)) |
| 160 | + .collect(toList()); |
| 161 | + assertEquals(2, result.size()); |
| 162 | + assertEquals(expected, result.stream().sorted(comparing(A::getId)).collect(toList())); |
| 163 | + } |
107 | 164 | }
|
0 commit comments