-
Notifications
You must be signed in to change notification settings - Fork 72
/
StreamRepositoryReturnTest.java
126 lines (105 loc) · 4.07 KB
/
StreamRepositoryReturnTest.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
/*
* 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.repository.returns;
import jakarta.data.repository.Page;
import jakarta.data.repository.Pageable;
import org.eclipse.jnosql.mapping.repository.DynamicReturn;
import org.eclipse.jnosql.mapping.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.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
@ExtendWith(MockitoExtension.class)
class StreamRepositoryReturnTest {
private final RepositoryReturn repositoryReturn = new StreamRepositoryReturn();
@Mock
private Page<Person> page;
@Test
public void shouldReturnIsCompatible() {
Assertions.assertTrue(repositoryReturn.isCompatible(Person.class, Stream.class));
assertFalse(repositoryReturn.isCompatible(Object.class, Person.class));
assertFalse(repositoryReturn.isCompatible(Person.class, Object.class));
}
@Test
public void shouldReturnStreamPage() {
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(Pageable.ofPage(2).size(2))
.withPage(p -> page)
.build();
Stream<Person> person = (Stream<Person>) repositoryReturn.convertPageable(dynamic);
Assertions.assertNotNull(person);
assertEquals(ada, person.findFirst().get());
}
@Test
public void shouldReturnStream() {
Person ada = new Person("Ada");
DynamicReturn<Person> dynamic = DynamicReturn.builder()
.withSingleResult(Optional::empty)
.withClassSource(Person.class)
.withResult(() -> Stream.of(ada))
.withMethodSource(Person.class.getDeclaredMethods()[0])
.build();
Stream<Person> person = (Stream<Person>) repositoryReturn.convert(dynamic);
Assertions.assertNotNull(person);
Assertions.assertEquals(ada, person.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);
}
}
}