-
-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathProjectionTest.java
More file actions
329 lines (254 loc) · 10 KB
/
ProjectionTest.java
File metadata and controls
329 lines (254 loc) · 10 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import static org.junit.jupiter.api.Assertions.*;
import java.util.*;
import org.junit.jupiter.api.Test;
/** Tests for Jooby Projection API. */
public class ProjectionTest {
// --- Test Models ---
public static class User {
private String id;
private String name;
private Address address;
private List<Role> roles;
private Map<String, String> meta;
public String getId() {
return id;
}
public String getName() {
return name;
}
public Address getAddress() {
return address;
}
public List<Role> getRoles() {
return roles;
}
public Map<String, String> getMeta() {
return meta;
}
}
public static class ExtendedUser extends User {
public String getFullName() {
return getName();
}
}
public static class NamedGroup {
private String name;
private Group group;
public String getName() {
return name;
}
public Group getGroup() {
return group;
}
}
public static class Group {
private List<ExtendedUser> users;
public List<ExtendedUser> getUsers() {
return users;
}
}
public static class Address {
private String city;
private Location loc;
public String getCity() {
return city;
}
public Location getLoc() {
return loc;
}
}
public record Role(String name, int level) {}
public record Location(double lat, double lon) {}
// --- Tests ---
@Test
public void testOrderPreservation() {
// LinkedMap should preserve the order 'name' then 'id'
Projection<User> p = Projection.of(User.class).include("name", "id");
assertEquals("(name,id)", p.toView());
// Swapping order should result in swapped view
Projection<User> p2 = Projection.of(User.class).include("id", "name");
assertEquals("(id,name)", p2.toView());
}
@Test
public void testAvajeNotationRoot() {
// Root level should be wrapped in parentheses for Avaje
Projection<User> p = Projection.of(User.class).include("id, address(city, loc(lat, lon))");
assertEquals("(id,address(city,loc(lat,lon)))", p.toView());
}
@Test
public void testInherited() {
Projection<NamedGroup> group = Projection.of(NamedGroup.class).include("id, group(*)");
assertEquals(
"(id,group(users(address(city,loc(lat,lon)),fullName,id,meta,name,roles(level,name))))",
group.toView());
Projection<ExtendedUser> p = Projection.of(ExtendedUser.class).include("id, fullname");
assertEquals("(id,fullname)", p.toView());
}
@Test
public void testMixedNotationRecursive() {
// Validates that nested children still use parentheses
Projection<User> p = Projection.of(User.class).include("address.loc(lat, lon)", "roles(name)");
assertEquals("(address(loc(lat,lon)),roles(name))", p.toView());
}
@Test
public void testCollectionGenericUnwrapping() {
Projection<User> p = Projection.of(User.class).include("roles.name");
assertEquals("roles(name)", p.toView());
}
@Test
public void testMapGenericUnwrapping() {
// Maps resolve to their value type (String in this case)
assertEquals("meta(bytes)", Projection.of(User.class).include("meta.bytes").toView());
assertEquals(
"(id,meta(target))", Projection.of(User.class).include("(id, meta(target))").toView());
}
@Test
public void testRecordSupport() {
Projection<Role> p = Projection.of(Role.class).include("name", "level");
assertEquals("(name,level)", p.toView());
}
@Test
public void testFailFastValidation() {
// Ensures we still blow up on typos during pre-compilation
assertThrows(
IllegalArgumentException.class,
() -> Projection.of(User.class).validate().include("address(ctiy)"));
}
@Test
public void testRootParenthesesBug() {
assertEquals(
"(name,address(city))",
Projection.of(User.class).include("(name, address(city))").toView());
// Address expands to its deep explicit wildcard definition for Avaje
assertEquals(
"(name,address(city,loc(lat,lon)))",
Projection.of(User.class).include("(name, address)").toView());
}
@Test
public void testRootParentheses() {
Projection<User> p = Projection.of(User.class).include("(id, name, address)");
assertTrue(p.getChildren().containsKey("id"));
assertTrue(p.getChildren().containsKey("name"));
assertTrue(p.getChildren().containsKey("address"));
// Address should have no explicitly defined children initially
// (the deep wildcard happens during toView())
assertTrue(p.getChildren().get("address").getChildren().isEmpty());
// Test the expanded view
assertEquals("(id,name,address(city,loc(lat,lon)))", p.toView());
}
@Test
public void testAvajeWildcardSyntax() {
Projection<User> p = Projection.of(User.class).include("id, name, address(*)");
assertTrue(p.getChildren().containsKey("id"));
assertTrue(p.getChildren().containsKey("name"));
assertTrue(p.getChildren().containsKey("address"));
// The explicit '*' should result in an empty children map for address
assertTrue(p.getChildren().get("address").getChildren().isEmpty());
// Test the expanded view
assertEquals("(id,name,address(city,loc(lat,lon)))", p.toView());
}
@Test
public void testNestedWildcardSyntax() {
Projection<User> p = Projection.of(User.class).include("id, address(city, loc(*))");
assertTrue(p.getChildren().containsKey("id"));
assertTrue(p.getChildren().containsKey("address"));
Projection<?> addressProj = p.getChildren().get("address");
assertTrue(addressProj.getChildren().containsKey("city"));
assertTrue(addressProj.getChildren().containsKey("loc"));
// loc(*) should result in an empty children map for loc
assertTrue(addressProj.getChildren().get("loc").getChildren().isEmpty());
// Test the expanded view (loc expands to its fields)
assertEquals("(id,address(city,loc(lat,lon)))", p.toView());
}
@Test
public void testCollectionNestedSyntax() {
// Tests: (id, roles(name))
Projection<User> p = Projection.of(User.class).include("(id, roles(name))");
assertTrue(p.getChildren().containsKey("id"));
assertTrue(p.getChildren().containsKey("roles"));
Projection<?> rolesProj = p.getChildren().get("roles");
assertTrue(rolesProj.getChildren().containsKey("name"));
assertEquals("(id,roles(name))", p.toView());
}
@Test
public void testMissingClosingParenthesis() {
IllegalArgumentException ex =
assertThrows(
IllegalArgumentException.class,
() -> {
Projection.of(User.class).include("(id, name, address(*)");
});
assertEquals(
"Missing closing parenthesis in projection: (id, name, address(*)", ex.getMessage());
}
@Test
public void testExtraClosingParenthesis() {
IllegalArgumentException ex =
assertThrows(
IllegalArgumentException.class,
() -> {
Projection.of(User.class).include("address(city))");
});
assertEquals("Mismatched parentheses in projection: address(city))", ex.getMessage());
}
@Test
public void testCollectionDeepWildcardSyntax() {
// Test: Requesting the 'roles' list without explicitly defining its children.
// The projection engine should see that 'roles' is a List<Role>,
// extract the Role class, and expand it to its explicit fields (name, level) for Avaje.
Projection<User> p = Projection.of(User.class).include("(id, roles)");
assertTrue(p.getChildren().containsKey("id"));
assertTrue(p.getChildren().containsKey("roles"));
// The 'roles' node itself has no explicitly parsed children
assertTrue(p.getChildren().get("roles").getChildren().isEmpty());
// But the resulting view string should be fully expanded!
assertEquals("(id,roles(level,name))", p.toView());
}
@Test
public void testExplicitCollectionDeepWildcardSyntax() {
// Test: Requesting the 'roles' list using the explicit (*) syntax.
Projection<User> p = Projection.of(User.class).include("id, roles(*)");
assertTrue(p.getChildren().containsKey("id"));
assertTrue(p.getChildren().containsKey("roles"));
// The resulting view string should be fully expanded!
assertEquals("(id,roles(level,name))", p.toView());
}
@Test
public void testValidateToggle() {
// 1. Verify default strict behavior (throws exception)
IllegalArgumentException ex =
assertThrows(
IllegalArgumentException.class,
() -> Projection.of(User.class).validate().include("address(zipcode)"));
assertTrue(ex.getMessage().contains("zipcode"));
// 2. Verify polymorphic/unknown fields are accepted when flag is false
Projection<User> p =
Projection.of(User.class).include("address(zipcode), extraPolymorphicField");
// The projection shouldn't throw, and it should successfully generate the explicit paths
assertEquals("(address(zipcode),extraPolymorphicField)", p.toView());
// Verify the internal tree mapped them correctly as generic leaves
assertTrue(p.getChildren().containsKey("extraPolymorphicField"));
assertTrue(p.getChildren().get("address").getChildren().containsKey("zipcode"));
}
@Test
public void testTopLevelListProjection() {
// If your route returns a List<User>, the root projection type is just User.class.
// The JSON engines (Jackson/Avaje) will naturally apply this User projection
// to every element in the JSON array.
Projection<User> projection = Projection.of(User.class).include("id, email");
// Assert: Avaje view string
assertEquals("(id,email)", projection.toView());
// Assert: Tree Structure validates against User
assertEquals(User.class, projection.getType());
assertEquals(2, projection.getChildren().size());
assertTrue(projection.getChildren().containsKey("id"));
assertTrue(projection.getChildren().containsKey("email"));
assertFalse(projection.getChildren().containsKey("name"));
}
}