-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathInputSchemaServiceTest.java
More file actions
345 lines (299 loc) · 12.7 KB
/
InputSchemaServiceTest.java
File metadata and controls
345 lines (299 loc) · 12.7 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package org.acme.service;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.acme.model.domain.CheckConfig;
import org.acme.model.domain.FormPath;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
public class InputSchemaServiceTest {
private InputSchemaService service;
private ObjectMapper objectMapper;
@BeforeEach
void setUp() {
service = new InputSchemaService();
objectMapper = new ObjectMapper();
}
// ==== transformPeopleSchema tests ====
@Test
void transformPeopleSchema_withPeopleArrayAndPersonId_transformsToObject() throws Exception {
String schemaJson = """
{
"type": "object",
"properties": {
"people": {
"type": "array",
"items": {
"type": "object",
"properties": {
"dateOfBirth": { "type": "string", "format": "date" }
}
}
}
}
}
""";
JsonNode schema = objectMapper.readTree(schemaJson);
JsonNode result = service.transformPeopleSchema(schema, "applicant");
// Verify people is now an object with personId key
assertTrue(result.has("properties"));
JsonNode people = result.get("properties").get("people");
assertEquals("object", people.get("type").asText());
assertTrue(people.has("properties"));
assertTrue(people.get("properties").has("applicant"));
// Verify the items schema is nested under the personId
assertTrue(people.get("properties").get("applicant").has("properties"));
assertTrue(people.get("properties").get("applicant").get("properties").has("dateOfBirth"));
}
@Test
void transformPeopleSchema_withoutPersonId_returnsOriginal() throws Exception {
String schemaJson = """
{
"type": "object",
"properties": {
"people": {
"type": "array",
"items": { "type": "object", "properties": { "dateOfBirth": { "type": "string" } } }
}
}
}
""";
JsonNode schema = objectMapper.readTree(schemaJson);
JsonNode result = service.transformPeopleSchema(schema, null);
// Should return a copy of the original
assertEquals("array", result.get("properties").get("people").get("type").asText());
}
@Test
void transformPeopleSchema_withoutPeopleProperty_returnsOriginal() throws Exception {
String schemaJson = """
{
"type": "object",
"properties": {
"income": { "type": "number" }
}
}
""";
JsonNode schema = objectMapper.readTree(schemaJson);
JsonNode result = service.transformPeopleSchema(schema, "applicant");
// Should return a copy of the original
assertTrue(result.get("properties").has("income"));
assertFalse(result.get("properties").has("people"));
}
// ==== transformEnrollmentsSchema tests ====
@Test
void transformEnrollmentsSchema_withEnrollmentsAndPersonId_movesUnderPeople() throws Exception {
String schemaJson = """
{
"type": "object",
"properties": {
"enrollments": {
"type": "array",
"items": {
"type": "object",
"properties": {
"personId": { "type": "string" },
"benefit": { "type": "string" }
}
}
}
}
}
""";
JsonNode schema = objectMapper.readTree(schemaJson);
JsonNode result = service.transformEnrollmentsSchema(schema, "applicant");
// Verify top-level enrollments is removed
assertFalse(result.get("properties").has("enrollments"));
// Verify people.applicant.enrollments exists
assertTrue(result.get("properties").has("people"));
JsonNode people = result.get("properties").get("people");
assertEquals("object", people.get("type").asText());
assertTrue(people.get("properties").has("applicant"));
JsonNode applicant = people.get("properties").get("applicant");
assertTrue(applicant.get("properties").has("enrollments"));
// Verify enrollments is now array of strings
JsonNode enrollments = applicant.get("properties").get("enrollments");
assertEquals("array", enrollments.get("type").asText());
assertEquals("string", enrollments.get("items").get("type").asText());
}
@Test
void transformEnrollmentsSchema_withExistingPeopleStructure_mergesEnrollments() throws Exception {
// Simulate a schema that already has people transformed (e.g., from transformPeopleSchema)
String schemaJson = """
{
"type": "object",
"properties": {
"people": {
"type": "object",
"properties": {
"applicant": {
"type": "object",
"properties": {
"dateOfBirth": { "type": "string", "format": "date" }
}
}
}
},
"enrollments": {
"type": "array",
"items": {
"type": "object",
"properties": {
"personId": { "type": "string" },
"benefit": { "type": "string" }
}
}
}
}
}
""";
JsonNode schema = objectMapper.readTree(schemaJson);
JsonNode result = service.transformEnrollmentsSchema(schema, "applicant");
// Verify top-level enrollments is removed
assertFalse(result.get("properties").has("enrollments"));
// Verify people.applicant now has both dateOfBirth and enrollments
JsonNode applicant = result.get("properties").get("people").get("properties").get("applicant");
assertTrue(applicant.get("properties").has("dateOfBirth"));
assertTrue(applicant.get("properties").has("enrollments"));
}
@Test
void transformEnrollmentsSchema_withoutPersonId_returnsOriginal() throws Exception {
String schemaJson = """
{
"type": "object",
"properties": {
"enrollments": {
"type": "array",
"items": { "type": "object", "properties": { "benefit": { "type": "string" } } }
}
}
}
""";
JsonNode schema = objectMapper.readTree(schemaJson);
JsonNode result = service.transformEnrollmentsSchema(schema, null);
// Should return a copy of the original
assertTrue(result.get("properties").has("enrollments"));
}
@Test
void transformEnrollmentsSchema_withoutEnrollmentsProperty_returnsOriginal() throws Exception {
String schemaJson = """
{
"type": "object",
"properties": {
"income": { "type": "number" }
}
}
""";
JsonNode schema = objectMapper.readTree(schemaJson);
JsonNode result = service.transformEnrollmentsSchema(schema, "applicant");
// Should return a copy of the original
assertTrue(result.get("properties").has("income"));
assertFalse(result.get("properties").has("enrollments"));
}
// ==== transformInputDefinitionSchema composition tests ====
@Test
void transformInputDefinitionSchema_withPeopleAndEnrollments_appliesBothTransforms() throws Exception {
String schemaJson = """
{
"type": "object",
"properties": {
"people": {
"type": "array",
"items": {
"type": "object",
"properties": {
"dateOfBirth": { "type": "string", "format": "date" }
}
}
},
"enrollments": {
"type": "array",
"items": {
"type": "object",
"properties": {
"personId": { "type": "string" },
"benefit": { "type": "string" }
}
}
}
}
}
""";
JsonNode inputDefinition = objectMapper.readTree(schemaJson);
CheckConfig checkConfig = new CheckConfig();
checkConfig.setInputDefinition(inputDefinition);
Map<String, Object> params = new HashMap<>();
params.put("personId", "applicant");
checkConfig.setParameters(params);
JsonNode result = service.transformInputDefinitionSchema(checkConfig);
// Verify top-level enrollments is removed
assertFalse(result.get("properties").has("enrollments"));
// Verify people.applicant has both dateOfBirth and enrollments
JsonNode applicant = result.get("properties").get("people").get("properties").get("applicant");
assertTrue(applicant.get("properties").has("dateOfBirth"));
assertTrue(applicant.get("properties").has("enrollments"));
assertEquals("string", applicant.get("properties").get("enrollments").get("items").get("type").asText());
}
// ==== extractJsonSchemaPaths tests ====
@Test
void extractJsonSchemaPaths_withTransformedSchema_extractsCorrectPaths() throws Exception {
String schemaJson = """
{
"type": "object",
"properties": {
"people": {
"type": "object",
"properties": {
"applicant": {
"type": "object",
"properties": {
"dateOfBirth": { "type": "string", "format": "date" },
"enrollments": {
"type": "array",
"items": { "type": "string" }
}
}
}
}
}
}
}
""";
JsonNode schema = objectMapper.readTree(schemaJson);
List<FormPath> paths = service.extractJsonSchemaPaths(schema);
assertTrue(paths.contains(new FormPath("people.applicant.dateOfBirth", "string")));
assertTrue(paths.contains(new FormPath("people.applicant.enrollments", "array")));
assertEquals(2, paths.size());
}
@Test
void extractJsonSchemaPaths_withEnrollmentOnlySchema_extractsCorrectPath() throws Exception {
// This tests the result after transformEnrollmentsSchema is applied to an enrollment-only check
String schemaJson = """
{
"type": "object",
"properties": {
"people": {
"type": "object",
"properties": {
"applicant": {
"type": "object",
"properties": {
"enrollments": {
"type": "array",
"items": { "type": "string" }
}
}
}
}
}
}
}
""";
JsonNode schema = objectMapper.readTree(schemaJson);
List<FormPath> paths = service.extractJsonSchemaPaths(schema);
assertTrue(paths.contains(new FormPath("people.applicant.enrollments", "array")));
assertEquals(1, paths.size());
}
}