/
SetRepositoryReturnTest.java
132 lines (112 loc) · 4.36 KB
/
SetRepositoryReturnTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.mapping.core.repository.returns;
import jakarta.data.page.Page;
import jakarta.data.page.PageRequest;
import org.eclipse.jnosql.mapping.core.repository.DynamicReturn;
import org.eclipse.jnosql.mapping.core.repository.RepositoryReturn;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.Collections;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
@ExtendWith(MockitoExtension.class)
class SetRepositoryReturnTest {
private final RepositoryReturn repositoryReturn = new SetRepositoryReturn();
@Mock
private Page<Person> page;
@Test
void shouldReturnIsCompatible() {
Assertions.assertTrue(repositoryReturn.isCompatible(Person.class, Set.class));
assertFalse(repositoryReturn.isCompatible(Object.class, Person.class));
assertFalse(repositoryReturn.isCompatible(Person.class, Object.class));
}
@Test
void shouldReturnSet() {
Person ada = new Person("Ada");
DynamicReturn<Person> dynamic = DynamicReturn.builder()
.withClassSource(Person.class)
.withSingleResult(Optional::empty)
.withResult(() -> Stream.of(ada))
.withSingleResultPagination(p -> Optional.empty())
.withStreamPagination(p -> Stream.of(ada))
.withMethodSource(Person.class.getDeclaredMethods()[0])
.withPagination(PageRequest.ofPage(2).size(2))
.withPage(p -> page)
.build();
Set<Person> person = (Set<Person>) repositoryReturn.convert(dynamic);
Assertions.assertNotNull(person);
assertFalse(person.isEmpty());
assertEquals(ada, person.stream().findFirst().get());
}
@Test
void shouldReturnSetPage() {
Person ada = new Person("Ada");
DynamicReturn<Person> dynamic = DynamicReturn.builder()
.withClassSource(Person.class)
.withSingleResult(Optional::empty)
.withResult(Collections::emptyList)
.withSingleResultPagination(p -> Optional.empty())
.withStreamPagination(p -> Stream.of(ada))
.withMethodSource(Person.class.getDeclaredMethods()[0])
.withPagination(PageRequest.ofPage(2).size(2))
.withPage(p -> page)
.build();
Set<Person> person = (Set<Person>) repositoryReturn.convertPageRequest(dynamic);
Assertions.assertNotNull(person);
assertFalse(person.isEmpty());
assertEquals(ada, person.stream().findFirst().get());
}
private static class Person implements Comparable<Person> {
private String name;
public Person(String name) {
this.name = name;
}
public Person() {
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Person person = (Person) o;
return Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hashCode(name);
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
'}';
}
@Override
public int compareTo(Person o) {
return name.compareTo(o.name);
}
}
}