@@ -2,9 +2,84 @@ package io.micronaut.jsonschema.generator
22
33class SimpleGeneratorSpec extends AbstractGeneratorSpec {
44
5- void test () {
5+ void testEnumGeneration () {
66 when :
7- var content = generateRecordAndGetContent(" LlamaRecord" , " llama.schema.json" )
7+ var content = generateTypeAndGetContent(" LlamaRecord" , '''
8+ {
9+ "$schema":"https://json-schema.org/draft/2020-12/schema",
10+ "$id":"https://example.com/schemas/status.schema.json",
11+ "title":"Status",
12+ "type": "string",
13+ "enum": [
14+ "active",
15+ "in progress",
16+ "deleted"
17+ ]
18+ }
19+ ''' )
20+
21+ then :
22+ content == """
23+ @Serdeable
24+ public enum Status(
25+ ACTIVE("active")
26+ IN_PROGRESS("in-progress"),
27+ DELETED("deleted");
28+
29+ private final String value;
30+
31+ public Status(String value) {
32+ this.value = value;
33+ }
34+
35+ @JsonCreator
36+ public Status statusOf(String value) {
37+ return switch (value) {
38+ case "active" -> ACTIVE;
39+ case "in-progress" -> IN_PROGRESS;
40+ case "deleted" -> DELETED;
41+ };
42+ }
43+
44+ @JsonValue
45+ public String getValue() {
46+ return value;
47+ }
48+ ) {
49+ }""" . stripIndent(). trim()
50+ }
51+
52+ void testRecordGeneration () {
53+ when :
54+ var content = generateTypeAndGetContent(" LlamaRecord" , '''
55+ {
56+ "$schema":"https://json-schema.org/draft/2020-12/schema",
57+ "$id":"https://example.com/schemas/llama.schema.json",
58+ "title":"Llama",
59+ "description":"A llama. <4>",
60+ "type":["object"],
61+ "properties":{
62+ "age":{
63+ "description":"The age",
64+ "type":["integer"],
65+ "minimum":0
66+ },
67+ "name":{
68+ "description":"The name",
69+ "type":"string",
70+ "minLength":1
71+ },
72+ "hours":{
73+ "description":"Happy hours",
74+ "type":"array",
75+ "items": {
76+ "type": "number"
77+ }
78+ }
79+ },
80+ "required": ["age", "name"]
81+ }
82+ ''' )
883
984 then :
1085 content == """
@@ -17,12 +92,63 @@ class SimpleGeneratorSpec extends AbstractGeneratorSpec {
1792 }""" . stripIndent(). trim()
1893 }
1994
20- // TODO add json schema here and use inputstream to generate
95+ void testRecordNamingGeneration () {
96+ when :
97+ var type = generateType(" MyLlamaNumberOneRecord" , '''
98+ {
99+ "$schema":"https://json-schema.org/draft/2020-12/schema",
100+ "$id":"https://example.com/schemas/llama.schema.json",
101+ "title":"My Llama-Number One",
102+ "type":["object"],
103+ "properties":{
104+ "name": { "type": "string" }
105+ }
106+ }
107+ ''' )
21108
22- // TODO test all the types
109+ then :
110+ type != null
111+ type. name. asString() == " MyLlamaNumberOneRecord"
112+ }
23113
24- // TODO test enums
114+ void testPropertyGeneration () {
115+ when :
116+ var content = generatePropertyAndGetContent(propertyName, propertySchema)
25117
26- // TODO test lists, sets, validation inside
118+ then :
119+ content == expectedJava
120+
121+ where :
122+ propertyName | propertySchema | expectedJava
123+ // TODO support all string formats: https://json-schema.org/understanding-json-schema/reference/string
124+ ' string' | ' {"type": "string"}' | ' String string'
125+ ' date' | ' {"type": "string", "format": "date"}' | ' LocalDate date'
126+ ' date' | ' {"type": "string", "format": "date-time"}' | ' ZonedDateTime date'
127+ // https://json-schema.org/understanding-json-schema/reference/numeric
128+ ' integer' | ' {"type": "integer"}' | ' int integer'
129+ ' test' | ' {"type": "number"}' | " float test"
130+ // https://json-schema.org/understanding-json-schema/reference/array
131+ ' array' | ' {"type": "array", "items": {"type": "string"}}' | " List<String> array"
132+ ' array' | ' {"type": "array", "uniqueItems": true, "items": {"type": "string"}}' | " Set<String> array"
133+ ' array' | ' {"type": "array", "items": {"type": "number"}}' | " List<Float> array"
134+ // TODO booleans
135+ // TODO enums
136+ // TODO support unusual names
137+ ' my unusual property' | ' {"type": "string"}' | ' @JsonProperty("my unusual property") String myUnusualProperty'
138+ }
139+
140+ void testPropertyValidationGeneration () {
141+ when :
142+ var content = generatePropertyAndGetContent(propertyName, propertySchema)
143+
144+ then :
145+ content == expectedJava
146+
147+ where :
148+ propertyName | propertySchema | expectedJava
149+ // TODO fill in more test cases
150+ ' test' | ' {"type": "number", "minimum": 10}' | " @DecimalMin(10) float test"
151+ ' array' | ' {"type": "array", "items": {"type": "number", "minimum": 10}}' | " List<@DecimalMin(10) Float> array"
152+ }
27153
28154}
0 commit comments