15
15
16
16
package software .amazon .smithy .openapi .fromsmithy .mappers ;
17
17
18
+ import java .util .logging .Handler ;
19
+ import java .util .logging .LogRecord ;
20
+ import java .util .logging .Logger ;
18
21
import org .junit .jupiter .api .Assertions ;
22
+ import org .junit .jupiter .api .BeforeAll ;
19
23
import org .junit .jupiter .api .Test ;
20
24
import software .amazon .smithy .model .Model ;
21
25
import software .amazon .smithy .model .node .Node ;
26
30
import software .amazon .smithy .openapi .fromsmithy .OpenApiConverter ;
27
31
28
32
public class OpenApiJsonAddTest {
29
- @ Test
30
- public void addsWithPointers () {
31
- Model model = Model .assembler ()
33
+
34
+ private static Model MODEL ;
35
+
36
+ @ BeforeAll
37
+ public static void before () {
38
+ MODEL = Model .assembler ()
32
39
// Reusing another test cases's model, but that doesn't matter for the
33
40
// purpose of this test.
34
41
.addImport (RemoveUnusedComponentsTest .class .getResource ("substitutions.smithy" ))
35
42
.discoverModels ()
36
43
.assemble ()
37
44
.unwrap ();
45
+ }
38
46
47
+ @ Test
48
+ public void addsWithPointers () {
39
49
ObjectNode addNode = Node .objectNodeBuilder ()
40
50
.withMember ("/info/description" , "hello" )
41
51
.withMember ("/info/foo" , "bar" )
@@ -49,7 +59,7 @@ public void addsWithPointers() {
49
59
50
60
ObjectNode openApi = OpenApiConverter .create ()
51
61
.config (config )
52
- .convertToNode (model );
62
+ .convertToNode (MODEL );
53
63
54
64
String description = NodePointer .parse ("/info/description" ).getValue (openApi ).expectStringNode ().getValue ();
55
65
String infoFoo = NodePointer .parse ("/info/foo" ).getValue (openApi ).expectStringNode ().getValue ();
@@ -61,4 +71,45 @@ public void addsWithPointers() {
61
71
Assertions .assertEquals ("nested" , infoNested );
62
72
Assertions .assertEquals ("custom" , infoTitle );
63
73
}
74
+
75
+ private static final class SearchingHandler extends Handler {
76
+ boolean found ;
77
+ String searchString ;
78
+
79
+ SearchingHandler (String searchString ) {
80
+ this .searchString = searchString ;
81
+ }
82
+
83
+ @ Override
84
+ public void publish (LogRecord record ) {
85
+ if (record .getMessage ().contains (searchString )) {
86
+ found = true ;
87
+ }
88
+ }
89
+
90
+ @ Override
91
+ public void flush () {}
92
+
93
+ @ Override
94
+ public void close () throws SecurityException {}
95
+ }
96
+
97
+ @ Test
98
+ public void warnsWhenAddingSchemas () {
99
+ Logger logger = Logger .getLogger (OpenApiJsonAdd .class .getName ());
100
+ SearchingHandler handler = new SearchingHandler ("Adding schemas to the generated OpenAPI model directly" );
101
+ logger .addHandler (handler );
102
+
103
+ ObjectNode addNode = Node .objectNode ()
104
+ .withMember ("/components/schemas/Merged" , Node .objectNode ().withMember ("type" , "string" ));
105
+
106
+ OpenApiConfig config = new OpenApiConfig ();
107
+ config .setService (ShapeId .from ("smithy.example#Service" ));
108
+ config .setJsonAdd (addNode .getStringMap ());
109
+ ObjectNode openApi = OpenApiConverter .create ().config (config ).convertToNode (MODEL );
110
+ NodePointer .parse ("/components/schemas/Merged" ).getValue (openApi ).expectObjectNode ();
111
+ logger .removeHandler (handler );
112
+
113
+ Assertions .assertTrue (handler .found );
114
+ }
64
115
}
0 commit comments