-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
JsonSchemaTest.java
389 lines (334 loc) · 18.7 KB
/
JsonSchemaTest.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.crd.generator.v1;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.TextNode;
import io.fabric8.crd.example.annotated.Annotated;
import io.fabric8.crd.example.basic.Basic;
import io.fabric8.crd.example.extraction.CollectionCyclicSchemaSwap;
import io.fabric8.crd.example.extraction.CyclicSchemaSwap;
import io.fabric8.crd.example.extraction.DeeplyNestedSchemaSwaps;
import io.fabric8.crd.example.extraction.Extraction;
import io.fabric8.crd.example.extraction.IncorrectExtraction;
import io.fabric8.crd.example.extraction.IncorrectExtraction2;
import io.fabric8.crd.example.extraction.MultipleSchemaSwaps;
import io.fabric8.crd.example.extraction.NestedSchemaSwap;
import io.fabric8.crd.example.json.ContainingJson;
import io.fabric8.crd.example.person.Person;
import io.fabric8.crd.generator.utils.Types;
import io.fabric8.kubernetes.api.model.AnyType;
import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps;
import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsBuilder;
import io.fabric8.kubernetes.api.model.apiextensions.v1.ValidationRule;
import io.sundr.model.TypeDef;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
class JsonSchemaTest {
@Test
void shouldCreatAnyTypeWithoutProperties() {
TypeDef any = Types.typeDefFrom(AnyType.class);
JSONSchemaProps schema = JsonSchema.from(any);
assertNotNull(any);
assertSchemaHasNumberOfProperties(schema, 0);
assertTrue(schema.getXKubernetesPreserveUnknownFields());
}
@Test
void shouldCreateJsonSchemaFromClass() {
TypeDef person = Types.typeDefFrom(Person.class);
JSONSchemaProps schema = JsonSchema.from(person);
assertNotNull(schema);
Map<String, JSONSchemaProps> properties = assertSchemaHasNumberOfProperties(schema, 7);
final List<String> personTypes = properties.get("type").getEnum().stream().map(JsonNode::asText)
.collect(Collectors.toList());
assertEquals(2, personTypes.size());
assertTrue(personTypes.contains("crazy"));
assertTrue(personTypes.contains("crazier"));
final Map<String, JSONSchemaProps> addressProperties = properties.get("addresses").getItems()
.getSchema().getProperties();
assertEquals(5, addressProperties.size());
final List<String> addressTypes = addressProperties.get("type").getEnum().stream()
.map(JsonNode::asText)
.collect(Collectors.toList());
assertEquals(2, addressTypes.size());
assertTrue(addressTypes.contains("home"));
assertTrue(addressTypes.contains("work"));
final TypeDef def = Types.typeDefFrom(Basic.class);
schema = JsonSchema.from(def);
assertNotNull(schema);
properties = schema.getProperties();
assertNotNull(properties);
assertEquals(2, properties.size());
Map<String, JSONSchemaProps> spec = properties.get("spec").getProperties();
assertEquals("integer", spec.get("myInt").getType());
assertEquals("integer", spec.get("myLong").getType());
assertEquals("number", spec.get("myDouble").getType());
assertEquals("number", spec.get("myFloat").getType());
Map<String, JSONSchemaProps> status = properties.get("status").getProperties();
assertEquals("string", status.get("message").getType());
}
@Test
void shouldAugmentPropertiesSchemaFromAnnotations() throws JsonProcessingException {
TypeDef annotated = Types.typeDefFrom(Annotated.class);
JSONSchemaProps schema = JsonSchema.from(annotated);
assertNotNull(schema);
Map<String, JSONSchemaProps> properties = assertSchemaHasNumberOfProperties(schema, 2);
final JSONSchemaProps specSchema = properties.get("spec");
Map<String, JSONSchemaProps> spec = assertSchemaHasNumberOfProperties(specSchema, 21);
// check descriptions are present
assertTrue(spec.containsKey("from-field"));
JSONSchemaProps prop = spec.get("from-field");
assertEquals("from-field-description", prop.getDescription());
assertTrue(spec.containsKey("from-getter"));
prop = spec.get("from-getter");
assertEquals("from-getter-description", prop.getDescription());
// fields without description annotation shouldn't have them
assertTrue(spec.containsKey("unnamed"));
assertNull(spec.get("unnamed").getDescription());
assertTrue(spec.containsKey("emptySetter"));
assertNull(spec.get("emptySetter").getDescription());
assertTrue(spec.containsKey("anEnum"));
Function<String, JSONSchemaPropsBuilder> type = t -> new JSONSchemaPropsBuilder().withType(t);
assertEquals(type.apply("integer").withMinimum(-5.0).build(), spec.get("min"));
assertEquals(type.apply("integer").withMaximum(5.0).build(), spec.get("max"));
assertEquals(type.apply("string").withFormat("password").build(), spec.get("password"));
assertEquals(type.apply("string").withPattern("\\b[1-9]\\b").build(), spec.get("singleDigit"));
assertEquals(type.apply("string").withNullable(true).build(), spec.get("nullable"));
assertEquals(type.apply("string").withDefault(TextNode.valueOf("my-value")).build(), spec.get("defaultValue"));
assertEquals(type.apply("string").withDefault(TextNode.valueOf("my-value2")).build(), spec.get("defaultValue2"));
assertEquals(type.apply("string").withEnum(TextNode.valueOf("non"), TextNode.valueOf("oui")).build(), spec.get("anEnum"));
assertEquals(type.apply("boolean").build(), spec.get("bool"));
assertEquals(type.apply("number").build(), spec.get("num"));
assertEquals(type.apply("number").build(), spec.get("numFloat"));
assertEquals(type.apply("integer").build(), spec.get("numInt"));
assertEquals(type.apply("string").build(), spec.get("issuedAt"));
// check required list, should register properties with their modified name if needed
final List<String> required = specSchema.getRequired();
assertEquals(3, required.size());
assertTrue(required.contains("emptySetter"));
assertTrue(required.contains("emptySetter2"));
assertTrue(required.contains("from-getter"));
// check ignored fields
assertFalse(spec.containsKey("ignoredFoo"));
assertFalse(spec.containsKey("ignoredBar"));
final JSONSchemaProps k8sValidationProps = spec.get("kubernetesValidationRule");
final List<ValidationRule> k8sValidationRulesSingle = k8sValidationProps.getXKubernetesValidations();
assertNotNull(k8sValidationRulesSingle);
assertEquals(1, k8sValidationRulesSingle.size());
assertEquals("self.startwith('prefix-')", k8sValidationRulesSingle.get(0).getRule());
assertEquals("kubernetesValidationRule must start with prefix 'prefix-'", k8sValidationRulesSingle.get(0).getMessage());
assertNull(k8sValidationRulesSingle.get(0).getMessageExpression());
assertNull(k8sValidationRulesSingle.get(0).getReason());
assertNull(k8sValidationRulesSingle.get(0).getFieldPath());
assertNull(k8sValidationRulesSingle.get(0).getOptionalOldSelf());
final JSONSchemaProps kubernetesValidationsRepeated = spec.get("kubernetesValidationRules");
final List<ValidationRule> kubernetesValidationsRepeatedRules = kubernetesValidationsRepeated.getXKubernetesValidations();
assertNotNull(kubernetesValidationsRepeatedRules);
assertEquals(3, kubernetesValidationsRepeatedRules.size());
assertEquals("first.rule", kubernetesValidationsRepeatedRules.get(0).getRule());
assertNull(kubernetesValidationsRepeatedRules.get(0).getFieldPath());
assertNull(kubernetesValidationsRepeatedRules.get(0).getReason());
assertNull(kubernetesValidationsRepeatedRules.get(0).getMessage());
assertNull(kubernetesValidationsRepeatedRules.get(0).getMessageExpression());
assertNull(kubernetesValidationsRepeatedRules.get(0).getOptionalOldSelf());
assertEquals("second.rule", kubernetesValidationsRepeatedRules.get(1).getRule());
assertNull(kubernetesValidationsRepeatedRules.get(1).getFieldPath());
assertNull(kubernetesValidationsRepeatedRules.get(1).getReason());
assertNull(kubernetesValidationsRepeatedRules.get(1).getMessage());
assertNull(kubernetesValidationsRepeatedRules.get(1).getMessageExpression());
assertNull(kubernetesValidationsRepeatedRules.get(1).getOptionalOldSelf());
}
@Test
void shouldProduceKubernetesPreserveFields() {
TypeDef containingJson = Types.typeDefFrom(ContainingJson.class);
JSONSchemaProps schema = JsonSchema.from(containingJson);
assertNotNull(schema);
Map<String, JSONSchemaProps> properties = assertSchemaHasNumberOfProperties(schema, 2);
final JSONSchemaProps specSchema = properties.get("spec");
Map<String, JSONSchemaProps> spec = assertSchemaHasNumberOfProperties(specSchema, 3);
// check preserve unknown fields is present
assertTrue(spec.containsKey("free"));
JSONSchemaProps freeField = spec.get("free");
assertNull(freeField.getType());
assertTrue(freeField.getXKubernetesPreserveUnknownFields());
assertTrue(spec.containsKey("field"));
JSONSchemaProps field = spec.get("field");
assertEquals("integer", field.getType());
assertNull(field.getXKubernetesPreserveUnknownFields());
assertTrue(spec.containsKey("foo"));
JSONSchemaProps fooField = spec.get("foo");
assertEquals("object", fooField.getType());
assertTrue(fooField.getXKubernetesPreserveUnknownFields());
}
@Test
void shouldExtractPropertiesSchemaFromExtractValueAnnotation() {
TypeDef extraction = Types.typeDefFrom(Extraction.class);
JSONSchemaProps schema = JsonSchema.from(extraction);
assertNotNull(schema);
Map<String, JSONSchemaProps> properties = assertSchemaHasNumberOfProperties(schema, 2);
final JSONSchemaProps specSchema = properties.get("spec");
Map<String, JSONSchemaProps> spec = assertSchemaHasNumberOfProperties(specSchema, 2);
// check typed SchemaFrom
JSONSchemaProps foo = spec.get("foo");
Map<String, JSONSchemaProps> fooProps = foo.getProperties();
assertNotNull(fooProps);
// you can change everything
assertEquals("integer", fooProps.get("BAZ").getType());
assertTrue(foo.getRequired().contains("BAZ"));
// you can exclude fields
assertNull(fooProps.get("baz"));
// check typed SchemaSwap
JSONSchemaProps bar = spec.get("bar");
Map<String, JSONSchemaProps> barProps = bar.getProperties();
assertNotNull(barProps);
assertTrue(bar.getXKubernetesPreserveUnknownFields());
// you can change everything
assertEquals("integer", barProps.get("BAZ").getType());
assertTrue(bar.getRequired().contains("BAZ"));
// you can exclude fields
assertNull(barProps.get("baz"));
}
@Test
void shouldExtractPropertiesSchemaFromSchemaSwapAnnotations() {
TypeDef extraction = Types.typeDefFrom(MultipleSchemaSwaps.class);
JSONSchemaProps schema = JsonSchema.from(extraction);
assertNotNull(schema);
Map<String, JSONSchemaProps> properties = assertSchemaHasNumberOfProperties(schema, 2);
final JSONSchemaProps specSchema = properties.get("spec");
Map<String, JSONSchemaProps> spec = assertSchemaHasNumberOfProperties(specSchema, 4);
// 'first' is replaced by SchemaSwap from int to string
JSONSchemaProps first = spec.get("first");
assertPropertyHasType(first, "shouldBeString", "string");
// 'second' is replaced by the same SchemaSwap that is applied multiple times
JSONSchemaProps second = spec.get("second");
assertPropertyHasType(second, "shouldBeString", "string");
// 'third' is replaced by another SchemaSwap
JSONSchemaProps third = spec.get("third");
assertPropertyHasType(third, "shouldBeInt", "integer");
// 'fourth' is replaced by another SchemaSwap and its property deleted
JSONSchemaProps fourth = spec.get("fourth");
Map<String, JSONSchemaProps> properties4 = fourth.getProperties();
assertNotNull(properties);
assertTrue(properties4.isEmpty());
}
@Test
void shouldApplySchemaSwapsMultipleTimesInDeepClassHierarchy() {
TypeDef extraction = Types.typeDefFrom(DeeplyNestedSchemaSwaps.class);
JSONSchemaProps schema = JsonSchema.from(extraction);
assertNotNull(schema);
Map<String, JSONSchemaProps> properties = assertSchemaHasNumberOfProperties(schema, 2);
Map<String, JSONSchemaProps> spec = assertSchemaHasNumberOfProperties(properties.get("spec"), 2);
assertPropertyHasType(spec.get("myObject"), "shouldBeString", "string");
Map<String, JSONSchemaProps> level1 = assertSchemaHasNumberOfProperties(spec.get("level1"), 3);
assertPropertyHasType(level1.get("myObject"), "shouldBeString", "string");
List<Map<String, JSONSchemaProps>> levels2 = new ArrayList<>();
levels2.add(assertSchemaHasNumberOfProperties(level1.get("level2a"), 3));
levels2.add(assertSchemaHasNumberOfProperties(level1.get("level2b"), 3));
for (Map<String, JSONSchemaProps> level2 : levels2) {
assertPropertyHasType(level2.get("myObject1"), "shouldBeString", "string");
assertPropertyHasType(level2.get("myObject2"), "shouldBeString", "string");
Map<String, JSONSchemaProps> level3 = assertSchemaHasNumberOfProperties(level2.get("level3"), 2);
assertPropertyHasType(level3.get("myObject1"), "shouldBeString", "string");
assertPropertyHasType(level3.get("myObject2"), "shouldBeString", "string");
}
}
@Test
void shouldApplyCyclicSchemaSwaps() {
TypeDef extraction = Types.typeDefFrom(CyclicSchemaSwap.class);
JSONSchemaProps schema = JsonSchema.from(extraction);
assertNotNull(schema);
Map<String, JSONSchemaProps> properties = assertSchemaHasNumberOfProperties(schema, 2);
Map<String, JSONSchemaProps> spec = assertSchemaHasNumberOfProperties(properties.get("spec"), 3);
// the collection should emit a single level then terminate with void
assertNull(spec.get("roots").getItems().getSchema().getProperties().get("level").getProperties().get("level"));
assertPropertyHasType(spec.get("myObject"), "value", "integer");
// the field should emit a single level then terminate with void
assertNull(spec.get("root").getProperties().get("level").getProperties().get("level"));
}
@Test
void shouldApplyCollectionCyclicSchemaSwaps() {
TypeDef extraction = Types.typeDefFrom(CollectionCyclicSchemaSwap.class);
JSONSchemaProps schema = JsonSchema.from(extraction);
assertNotNull(schema);
Map<String, JSONSchemaProps> properties = assertSchemaHasNumberOfProperties(schema, 2);
Map<String, JSONSchemaProps> spec = assertSchemaHasNumberOfProperties(properties.get("spec"), 2);
assertPropertyHasType(spec.get("myObject"), "value", "integer");
Map<String, JSONSchemaProps> level1 = assertSchemaHasNumberOfProperties(spec.get("levels").getItems().getSchema(), 2);
assertPropertyHasType(level1.get("myObject"), "value", "integer");
Map<String, JSONSchemaProps> level2 = assertSchemaHasNumberOfProperties(level1.get("levels").getItems().getSchema(), 2);
assertPropertyHasType(level2.get("myObject"), "value", "integer");
Map<String, JSONSchemaProps> level3 = assertSchemaHasNumberOfProperties(level2.get("levels").getItems().getSchema(), 2);
assertPropertyHasType(level3.get("myObject"), "value", "integer");
// should terminate at the 3rd level with any - this is probably not quite the behavior we want
// targeting collection properties with a non-collection terminal seems problematic
JSONSchemaProps terminal = level3.get("levels");
assertNull(terminal.getItems());
assertTrue(terminal.getXKubernetesPreserveUnknownFields());
assertSchemaHasNumberOfProperties(terminal, 0);
}
@Test
void shouldThrowIfSchemaSwapHasUnmatchedField() {
TypeDef incorrectExtraction = Types.typeDefFrom(IncorrectExtraction.class);
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
() -> JsonSchema.from(incorrectExtraction));
assertEquals(
"Unmatched SchemaSwaps: @SchemaSwap(originalType=io.fabric8.crd.example.extraction.ExtractionSpec, fieldName=\"FOO\", targetType=io"
+ ".fabric8.crd.example.extraction.FooExtractor) on io.fabric8.crd.example.extraction.IncorrectExtraction",
exception.getMessage());
}
@Test
void shouldThrowIfSchemaSwapHasUnmatchedClass() {
TypeDef incorrectExtraction2 = Types.typeDefFrom(IncorrectExtraction2.class);
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
() -> JsonSchema.from(incorrectExtraction2));
assertEquals(
"Unmatched SchemaSwaps: @SchemaSwap(originalType=io.fabric8.crd.example.basic.BasicSpec, fieldName=\"bar\", targetType=io.fabric8.crd"
+ ".example.extraction.FooExtractor) on io.fabric8.crd.example.extraction.IncorrectExtraction2",
exception.getMessage());
}
@Test
void shouldThrowIfSchemaSwapNested() {
TypeDef nested = Types.typeDefFrom(NestedSchemaSwap.class);
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
() -> JsonSchema.from(nested));
assertEquals(
"Nested SchemaSwap: @SchemaSwap(originalType=io.fabric8.crd.example.extraction.NestedSchemaSwap.End, fieldName=\"value\", targetType=java.lang.Void) "
+ "on io.fabric8.crd.example.extraction.NestedSchemaSwap.Intermediate",
exception.getMessage());
}
private static Map<String, JSONSchemaProps> assertSchemaHasNumberOfProperties(JSONSchemaProps specSchema, int expected) {
Map<String, JSONSchemaProps> spec = specSchema.getProperties();
assertEquals(expected, spec.size());
return spec;
}
private static void assertPropertyHasType(JSONSchemaProps spec, String name, String expectedType) {
Map<String, JSONSchemaProps> properties = spec.getProperties();
assertNotNull(properties);
JSONSchemaProps property = properties.get(name);
assertNotNull(property, "Property " + name + " should exist");
assertEquals(expectedType, property.getType(), "Property " + name + " should have expected type");
}
}