/
RepositoryTypeTest.java
255 lines (203 loc) · 9.33 KB
/
RepositoryTypeTest.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
* 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.query;
import jakarta.data.repository.BasicRepository;
import jakarta.data.repository.CrudRepository;
import jakarta.data.repository.Delete;
import jakarta.data.repository.Insert;
import jakarta.data.repository.OrderBy;
import jakarta.data.repository.BasicRepository;
import jakarta.data.repository.Query;
import jakarta.data.repository.Save;
import jakarta.data.repository.Update;
import jakarta.enterprise.inject.Instance;
import jakarta.enterprise.inject.spi.CDI;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import static org.assertj.core.api.Assertions.assertThat;
class RepositoryTypeTest {
@ParameterizedTest
@MethodSource("getBasicRepositoryMethods")
void shouldReturnDefaultAtBasicRepository(Method method) {
var type = RepositoryType.of(method, BasicRepository.class);
assertThat(type).isEqualTo(RepositoryType.DEFAULT);
}
@ParameterizedTest
@MethodSource("getCrudRepositoryMethods")
void shouldReturnDefaultAtCrudRepository(Method method) {
var type = RepositoryType.of(method, BasicRepository.class);
assertThat(type).isEqualTo(RepositoryType.DEFAULT);
}
@ParameterizedTest
@MethodSource("getBasicRepositoryMethods")
void shouldReturnDefaultAtBasicRepository(Method method) {
var type = RepositoryType.of(method, BasicRepository.class);
assertThat(type).isEqualTo(RepositoryType.DEFAULT);
}
@Test
void shouldReturnObjectMethod() throws NoSuchMethodException {
Assertions.assertEquals(RepositoryType.OBJECT_METHOD, RepositoryType.of(getMethod(Object.class, "equals"), CrudRepository.class));
Assertions.assertEquals(RepositoryType.OBJECT_METHOD, RepositoryType.of(getMethod(Object.class, "hashCode"), CrudRepository.class));
}
@Test
void shouldReturnFindBy() throws NoSuchMethodException {
Assertions.assertEquals(RepositoryType.FIND_BY, RepositoryType.of(getMethod(DevRepository.class, "findByName"), CrudRepository.class));
}
@Test
void shouldReturnSave() throws NoSuchMethodException {
Assertions.assertEquals(RepositoryType.SAVE, RepositoryType.of(getMethod(DevRepository.class, "save"), DevRepository.class));
}
@Test
void shouldReturnInsert() throws NoSuchMethodException {
Assertions.assertEquals(RepositoryType.INSERT, RepositoryType.of(getMethod(DevRepository.class, "insert"), DevRepository.class));
}
@Test
void shouldReturnDelete() throws NoSuchMethodException {
Assertions.assertEquals(RepositoryType.DELETE, RepositoryType.of(getMethod(DevRepository.class, "delete"), DevRepository.class));
}
@Test
void shouldReturnUpdate() throws NoSuchMethodException {
Assertions.assertEquals(RepositoryType.UPDATE, RepositoryType.of(getMethod(DevRepository.class, "update"), DevRepository.class));
}
@Test
void shouldReturnDeleteBy() throws NoSuchMethodException {
Assertions.assertEquals(RepositoryType.DELETE_BY, RepositoryType.of(getMethod(DevRepository.class, "deleteByName"), CrudRepository.class));
}
@Test
void shouldReturnFindAllBy() throws NoSuchMethodException {
Assertions.assertEquals(RepositoryType.FIND_ALL, RepositoryType.of(getMethod(DevRepository.class, "findAll"), CrudRepository.class));
}
@Test
void shouldReturnJNoSQLQuery() throws NoSuchMethodException {
Assertions.assertEquals(RepositoryType.QUERY, RepositoryType.of(getMethod(DevRepository.class, "query"), CrudRepository.class));
}
@Test
void shouldReturnParameterBased() throws NoSuchMethodException {
Assertions.assertEquals(RepositoryType.PARAMETER_BASED, RepositoryType.of(getMethod(DevRepository.class, "nope"), CrudRepository.class));
}
@Test
void shouldReturnCountBy() throws NoSuchMethodException {
Assertions.assertEquals(RepositoryType.COUNT_BY, RepositoryType.of(getMethod(DevRepository.class, "countByName"), CrudRepository.class));
}
@Test
void shouldReturnExistsBy() throws NoSuchMethodException {
Assertions.assertEquals(RepositoryType.EXISTS_BY, RepositoryType.of(getMethod(DevRepository.class, "existsByName"), CrudRepository.class));
}
@Test
void shouldReturnOrder() throws NoSuchMethodException {
Assertions.assertEquals(RepositoryType.ORDER_BY, RepositoryType.of(getMethod(DevRepository.class,
"order"), CrudRepository.class));
Assertions.assertEquals(RepositoryType.ORDER_BY, RepositoryType.of(getMethod(DevRepository.class,
"order2"), CrudRepository.class));
}
@Test
void shouldDefaultMethod() throws NoSuchMethodException {
Assertions.assertEquals(RepositoryType.DEFAULT_METHOD, RepositoryType.of(getMethod(DevRepository.class,
"duplicate"), CrudRepository.class));
}
@Test
void shouldReturnCustom() throws NoSuchMethodException {
try (MockedStatic<CDI> cdi = Mockito.mockStatic(CDI.class)) {
CDI<Object> current = Mockito.mock(CDI.class);
Instance<Calculate> instance = Mockito.mock(Instance.class);
Mockito.when(instance.isResolvable()).thenReturn(true);
cdi.when(CDI::current).thenReturn(current);
Mockito.when(current.select(Calculate.class)).thenReturn(instance);
Assertions.assertEquals(RepositoryType.CUSTOM_REPOSITORY, RepositoryType.of(getMethod(Calculate.class,
"sum"), CrudRepository.class));
}
}
@Test
void shouldReturnFindByCustom() throws NoSuchMethodException {
try (MockedStatic<CDI> cdi = Mockito.mockStatic(CDI.class)) {
CDI<Object> current = Mockito.mock(CDI.class);
Instance<Calculate> instance = Mockito.mock(Instance.class);
Mockito.when(instance.isResolvable()).thenReturn(true);
cdi.when(CDI::current).thenReturn(current);
Mockito.when(current.select(Calculate.class)).thenReturn(instance);
Assertions.assertEquals(RepositoryType.CUSTOM_REPOSITORY, RepositoryType.of(getMethod(Calculate.class,
"findBySum"), CrudRepository.class));
}
}
@Test
void shouldReturnFindByCustom2() throws NoSuchMethodException {
try (MockedStatic<CDI> cdi = Mockito.mockStatic(CDI.class)) {
CDI<Object> current = Mockito.mock(CDI.class);
Instance<Calculate> instance = Mockito.mock(Instance.class);
Mockito.when(instance.isResolvable()).thenReturn(true);
cdi.when(CDI::current).thenReturn(current);
Mockito.when(current.select(Calculate.class)).thenReturn(instance);
Assertions.assertEquals(RepositoryType.FIND_BY, RepositoryType.of(getMethod(Calculate.class,
"findBySum"), Calculate.class));
}
}
private Method getMethod(Class<?> repository, String methodName) throws NoSuchMethodException {
return Stream.of(repository.getDeclaredMethods())
.filter(m -> m.getName().equals(methodName))
.findFirst().get();
}
interface DevRepository extends CrudRepository, Calculate {
String findByName(String name);
String deleteByName(String name);
Stream<String> findAll();
@Query("query")
String query(String query);
Long countByName(String name);
Long existsByName(String name);
void nope();
@OrderBy("sample")
String order();
@OrderBy("sample")
@OrderBy("test")
String order2();
default int duplicate(int value) {
return value * 2;
}
@Delete
void delete(String name);
@Insert
void insert(String name);
@Update
void update(String name);
@Save
void save(String name);
}
interface Calculate {
BigDecimal sum();
List<String> findBySum(String name);
}
private static Stream<Arguments> getBasicRepositoryMethods() {
return Arrays.stream(BasicRepository.class.getDeclaredMethods())
.map(Arguments::of);
}
private static Stream<Arguments> getCrudRepositoryMethods() {
return Arrays.stream(CrudRepository.class.getDeclaredMethods())
.map(Arguments::of);
}
private static Stream<Arguments> getBasicRepositoryMethods() {
return Arrays.stream(BasicRepository.class.getDeclaredMethods())
.map(Arguments::of);
}
}