Skip to content

Commit 314e314

Browse files
committed
tests
1 parent 6efb448 commit 314e314

File tree

4 files changed

+263
-7
lines changed

4 files changed

+263
-7
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.example.myapp.web.test;
2+
3+
public class ErrorResponse {
4+
5+
public String id;
6+
public String text;
7+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.example.myapp.web.test;
2+
3+
import java.util.List;
4+
5+
import io.avaje.http.api.Controller;
6+
import io.avaje.http.api.Get;
7+
import io.avaje.http.api.MediaType;
8+
import io.avaje.http.api.OpenAPIReturns;
9+
import io.avaje.http.api.Path;
10+
import io.avaje.http.api.Post;
11+
import io.avaje.http.api.Produces;
12+
import io.javalin.http.Context;
13+
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
14+
import io.swagger.v3.oas.annotations.info.Info;
15+
import io.swagger.v3.oas.annotations.tags.Tag;
16+
17+
@OpenAPIDefinition(
18+
info =
19+
@Info(
20+
title = "Example service",
21+
description = "Example Javalin controllers with Java and Maven"))
22+
@Controller
23+
@Path("openapi/")
24+
public class OpenAPIController {
25+
26+
/**
27+
* Example of Open API Get (up to the first period is the summary). When using Javalin Context
28+
* only <br>
29+
* This Javadoc description is added to the generated openapi.json
30+
*
31+
* @return funny phrase (this part of the javadoc is added to the response desc)
32+
*/
33+
@Get("/get")
34+
@Produces(MediaType.TEXT_PLAIN)
35+
@OpenAPIReturns(responseCode = "200", type = String.class)
36+
void ctxEndpoint(Context ctx) {
37+
ctx.contentType(MediaType.TEXT_PLAIN).result("healthlmao");
38+
}
39+
40+
/**
41+
* Standard Post. uses tag annotation to add tags to openapi json
42+
*
43+
* @param b the body (this is used for generated request body desc)
44+
* @return the response body (from javadoc)
45+
*/
46+
@Post("/post")
47+
@Tag(name = "tag1", description = "this is added to openapi tags")
48+
@OpenAPIReturns(responseCode = "200", description = "overrides @return javadoc description")
49+
@OpenAPIReturns(responseCode = "201")
50+
@OpenAPIReturns(
51+
responseCode = "400",
52+
description = "User not found (Will not have an associated response schema)")
53+
@OpenAPIReturns(
54+
responseCode = "500",
55+
description = "Some other Error (Will have this error class as the response class)",
56+
type = ErrorResponse.class)
57+
Person testPost(Person b) {
58+
return new Person(0, "baby");
59+
}
60+
61+
/**
62+
* Standard Post. The Deprecated annotation adds "deprecacted:true" to the generated json
63+
*
64+
* @param b the body
65+
* @return the response body (from javadoc)
66+
*/
67+
@Deprecated
68+
@Post("/post1")
69+
Person testPostl(List<Person> m) {
70+
71+
return new Person(0, "baby");
72+
}
73+
}

tests/test-javalin-jsonb/src/test/java/io/avaje/http/generator/JavalinProcessorTest.java

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import org.junit.jupiter.api.AfterEach;
2222
import org.junit.jupiter.api.Test;
2323

24+
import com.fasterxml.jackson.databind.ObjectMapper;
25+
2426
import io.avaje.http.generator.javalin.JavalinProcessor;
2527
import io.avaje.jsonb.generator.Processor;
2628

@@ -48,12 +50,7 @@ public void runAnnoationProcessor() throws Exception {
4850

4951
final var task =
5052
compiler.getTask(
51-
new PrintWriter(System.out),
52-
null,
53-
null,
54-
List.of("--release=11"),
55-
null,
56-
files);
53+
new PrintWriter(System.out), null, null, List.of("--release=11"), null, files);
5754
task.setProcessors(List.of(new JavalinProcessor()));
5855

5956
assertThat(task.call()).isTrue();
@@ -67,17 +64,45 @@ public void runAnnoationProcessorJsonB() throws Exception {
6764

6865
final var compiler = ToolProvider.getSystemJavaCompiler();
6966

67+
final var task =
68+
compiler.getTask(
69+
new PrintWriter(System.out), null, null, List.of("--release=11"), null, files);
70+
task.setProcessors(List.of(new JavalinProcessor(false), new Processor()));
71+
72+
assertThat(task.call()).isTrue();
73+
}
74+
75+
@Test
76+
public void testOpenAPIGeneration() throws Exception {
77+
final var source = Paths.get("src").toAbsolutePath().toString();
78+
// OpenAPIController
79+
final var files = getSourceFiles(source);
80+
81+
Iterable<JavaFileObject> openAPIController = null;
82+
for (final var file : files) {
83+
if (file.isNameCompatible("OpenAPIController", Kind.SOURCE))
84+
openAPIController = List.of(file);
85+
}
86+
final var compiler = ToolProvider.getSystemJavaCompiler();
87+
7088
final var task =
7189
compiler.getTask(
7290
new PrintWriter(System.out),
7391
null,
7492
null,
7593
List.of("--release=11"),
7694
null,
77-
files);
95+
openAPIController);
7896
task.setProcessors(List.of(new JavalinProcessor(false), new Processor()));
7997

8098
assertThat(task.call()).isTrue();
99+
100+
final var mapper = new ObjectMapper();
101+
final var expectedOpenApiJson =
102+
mapper.readTree(new File("src/test/java/io/avaje/http/generator/openapi.json"));
103+
final var generatedOpenApi = mapper.readTree(new File("openapi.json"));
104+
105+
assert expectedOpenApiJson.equals(generatedOpenApi);
81106
}
82107

83108
private Iterable<JavaFileObject> getSourceFiles(String source) throws Exception {
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "Example service",
5+
"description": "Example Javalin controllers with Java and Maven",
6+
"version": ""
7+
},
8+
"tags": [
9+
{
10+
"name": "tag1",
11+
"description": "this is added to openapi tags"
12+
}
13+
],
14+
"paths": {
15+
"/openapi/get": {
16+
"get": {
17+
"tags": [],
18+
"summary": "Example of Open API Get (up to the first period is the summary)",
19+
"description": "When using Javalin Context only This Javadoc description is added to the generated openapi.json",
20+
"responses": {
21+
"200": {
22+
"description": "funny phrase (this part of the javadoc is added to the response desc)",
23+
"content": {
24+
"text/plain": {
25+
"schema": {
26+
"type": "string"
27+
}
28+
}
29+
}
30+
}
31+
}
32+
}
33+
},
34+
"/openapi/post": {
35+
"post": {
36+
"tags": [
37+
"tag1"
38+
],
39+
"summary": "Standard Post",
40+
"description": "uses tag annotation to add tags to openapi json",
41+
"requestBody": {
42+
"description": "the body (this is used for generated request body desc)",
43+
"content": {
44+
"application/json": {
45+
"schema": {
46+
"$ref": "#/components/schemas/Person"
47+
}
48+
}
49+
},
50+
"required": true
51+
},
52+
"responses": {
53+
"200": {
54+
"description": "overrides @return javadoc description",
55+
"content": {
56+
"application/json": {
57+
"schema": {
58+
"$ref": "#/components/schemas/Person"
59+
}
60+
}
61+
}
62+
},
63+
"201": {
64+
"description": "the response body (from javadoc)",
65+
"content": {
66+
"application/json": {
67+
"schema": {
68+
"$ref": "#/components/schemas/Person"
69+
}
70+
}
71+
}
72+
},
73+
"400": {
74+
"description": "User not found (Will not have an associated response schema)"
75+
},
76+
"500": {
77+
"description": "Some other Error (Will have this error class as the response class)",
78+
"content": {
79+
"application/json": {
80+
"schema": {
81+
"$ref": "#/components/schemas/ErrorResponse"
82+
}
83+
}
84+
}
85+
}
86+
}
87+
}
88+
},
89+
"/openapi/post1": {
90+
"post": {
91+
"tags": [],
92+
"summary": "Standard Post",
93+
"description": "The Deprecated annotation adds \"deprecacted:true\" to the generated json",
94+
"requestBody": {
95+
"content": {
96+
"application/json": {
97+
"schema": {
98+
"type": "array",
99+
"items": {
100+
"$ref": "#/components/schemas/Person"
101+
}
102+
}
103+
}
104+
},
105+
"required": true
106+
},
107+
"responses": {
108+
"201": {
109+
"description": "the response body (from javadoc)",
110+
"content": {
111+
"application/json": {
112+
"schema": {
113+
"$ref": "#/components/schemas/Person"
114+
}
115+
}
116+
}
117+
}
118+
},
119+
"deprecated": true
120+
}
121+
}
122+
},
123+
"components": {
124+
"schemas": {
125+
"ErrorResponse": {
126+
"type": "object",
127+
"properties": {
128+
"id": {
129+
"type": "string"
130+
},
131+
"text": {
132+
"type": "string"
133+
}
134+
}
135+
},
136+
"Person": {
137+
"type": "object",
138+
"properties": {
139+
"id": {
140+
"type": "integer",
141+
"format": "int64",
142+
"nullable": false
143+
},
144+
"name": {
145+
"type": "string"
146+
}
147+
}
148+
}
149+
}
150+
}
151+
}

0 commit comments

Comments
 (0)