|
1 | 1 | package com.aerospike.mapper; |
2 | 2 |
|
| 3 | +import static java.util.Comparator.comparing; |
| 4 | +import static java.util.stream.Collectors.toList; |
3 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; |
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; |
@@ -39,17 +44,43 @@ public String getName() { |
39 | 44 | public int getAge() { |
40 | 45 | return age; |
41 | 46 | } |
| 47 | + |
| 48 | + @Override |
| 49 | + public String toString() { |
| 50 | + return String.format("id:%d, name:%s, age:%d", id, name, age); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public boolean equals(Object o) { |
| 55 | + if (this == o) { |
| 56 | + return true; |
| 57 | + } |
| 58 | + if (o == null || getClass() != o.getClass()) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + Person person = (Person) o; |
| 62 | + return id == person.id && age == person.age && Objects.equals(name, person.name); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public int hashCode() { |
| 67 | + return Objects.hash(id, name, age); |
| 68 | + } |
42 | 69 | } |
43 | 70 |
|
| 71 | + private final List<Person> data = new ArrayList<Person>() {{ |
| 72 | + add(new Person(1, "Tim", 312)); |
| 73 | + add(new Person(2, "Bob", 44)); |
| 74 | + add(new Person(3, "Sue", 56)); |
| 75 | + add(new Person(4, "Rob", 23)); |
| 76 | + add(new Person(5, "Jim", 32)); |
| 77 | + add(new Person(6, "Bob", 78)); |
| 78 | + }}; |
| 79 | + |
44 | 80 | private AeroMapper populate() { |
45 | 81 | client.truncate(null, "test", "testScan", null); |
46 | 82 | AeroMapper mapper = new AeroMapper.Builder(client).build(); |
47 | | - mapper.save(new Person(1, "Tim", 312), |
48 | | - new Person(2, "Bob", 44), |
49 | | - new Person(3, "Sue", 56), |
50 | | - new Person(4, "Rob", 23), |
51 | | - new Person(5, "Jim", 32), |
52 | | - new Person(6, "Bob", 78)); |
| 83 | + mapper.save(data.toArray()); |
53 | 84 | return mapper; |
54 | 85 | } |
55 | 86 |
|
@@ -89,4 +120,33 @@ public void scanTestWithAbort() { |
89 | 120 | }); |
90 | 121 | assertEquals(1, counter.get()); |
91 | 122 | } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void scanTestReturnsList() { |
| 126 | + AeroMapper mapper = populate(); |
| 127 | + |
| 128 | + List<Person> result = mapper.scan(Person.class); |
| 129 | + |
| 130 | + List<Person> expected = data.stream() |
| 131 | + .sorted(comparing(Person::getId)) |
| 132 | + .collect(toList()); |
| 133 | + assertEquals(6, result.size()); |
| 134 | + assertEquals(expected, result.stream().sorted(comparing(Person::getId)).collect(toList())); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void scanWithScanPolicyTestReturnsList() { |
| 139 | + AeroMapper mapper = populate(); |
| 140 | + ScanPolicy scanPolicy = new ScanPolicy(); |
| 141 | + scanPolicy.filterExp = Exp.build(Exp.eq(Exp.stringBin("name"), Exp.val("Bob"))); |
| 142 | + |
| 143 | + List<Person> result = mapper.scan(scanPolicy, Person.class); |
| 144 | + |
| 145 | + List<Person> expected = data.stream() |
| 146 | + .filter(d -> d.name.equals("Bob")) |
| 147 | + .sorted(comparing(Person::getId)) |
| 148 | + .collect(toList()); |
| 149 | + assertEquals(2, result.size()); |
| 150 | + assertEquals(expected, result.stream().sorted(comparing(Person::getId)).collect(toList())); |
| 151 | + } |
92 | 152 | } |
0 commit comments