33import com .fasterxml .jackson .databind .ObjectMapper ;
44import org .junit .jupiter .api .Test ;
55import org .junit .jupiter .api .extension .ExtendWith ;
6- import org .openapitools .codegen .CliOption ;
76import org .openapitools .codegen .online .model .ResponseCode ;
8- import org .springframework .core .io .ByteArrayResource ;
9- import org .springframework .http .ResponseEntity ;
107import org .springframework .beans .factory .annotation .Autowired ;
118import org .springframework .boot .test .autoconfigure .web .servlet .WebMvcTest ;
129import org .springframework .http .HttpHeaders ;
1310import org .springframework .http .MediaType ;
14- import org .springframework .test .context .bean .override .mockito .MockitoBean ;
1511import org .springframework .test .context .junit .jupiter .SpringExtension ;
1612import org .springframework .test .web .servlet .MockMvc ;
13+
14+ import static org .hamcrest .Matchers .*;
1715import static org .junit .jupiter .api .Assertions .assertTrue ;
18- import java .util .Arrays ;
19- import java .util .HashMap ;
20- import java .util .Map ;
21-
22- import static org .hamcrest .Matchers .hasItem ;
23- import static org .hamcrest .Matchers .not ;
24- import static org .mockito .ArgumentMatchers .any ;
25- import static org .mockito .ArgumentMatchers .anyString ;
26- import static org .mockito .Mockito .when ;
16+
2717import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
2818import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
19+ import static org .springframework .test .web .servlet .result .MockMvcResultHandlers .print ;
2920import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .*;
3021
3122@ ExtendWith (SpringExtension .class )
@@ -38,18 +29,13 @@ public class GenApiControllerTest {
3829 @ Autowired
3930 private MockMvc mockMvc ;
4031
41- @ MockitoBean
42- private GenApiDelegate genApiDelegate ;
43-
4432 @ Test
4533 public void clientLanguages () throws Exception {
46- when (genApiDelegate .clientOptions ()).thenReturn (ResponseEntity .ok (Arrays .asList ("java" , "python" , "javascript" )));
4734 getLanguages ("clients" , "java" );
4835 }
4936
5037 @ Test
5138 public void serverFrameworks () throws Exception {
52- when (genApiDelegate .serverOptions ()).thenReturn (ResponseEntity .ok (Arrays .asList ("spring" , "nodejs" , "flask" )));
5339 getLanguages ("servers" , "spring" );
5440 }
5541
@@ -63,32 +49,22 @@ public void getLanguages(String type, String expected) throws Exception {
6349
6450 @ Test
6551 public void clientOptions () throws Exception {
66- Map <String , CliOption > options = new HashMap <>();
67- CliOption option = new CliOption ("sortParamsByRequiredFlag" , "Sort parameters by required flag" );
68- options .put ("sortParamsByRequiredFlag" , option );
69- when (genApiDelegate .getClientOptions ("java" )).thenReturn (ResponseEntity .ok (options ));
7052 getOptions ("clients" , "java" );
7153 }
7254
7355 @ Test
7456 public void clientOptionsUnknown () throws Exception {
75- when (genApiDelegate .getClientOptions ("unknown" )).thenReturn (ResponseEntity .notFound ().build ());
7657 mockMvc .perform (get ("/api/gen/clients/unknown" ))
7758 .andExpect (status ().isNotFound ());
7859 }
7960
8061 @ Test
8162 public void serverOptions () throws Exception {
82- Map <String , CliOption > options = new HashMap <>();
83- CliOption option = new CliOption ("sortParamsByRequiredFlag" , "Sort parameters by required flag" );
84- options .put ("sortParamsByRequiredFlag" , option );
85- when (genApiDelegate .getServerOptions ("spring" )).thenReturn (ResponseEntity .ok (options ));
8663 getOptions ("servers" , "spring" );
8764 }
8865
8966 @ Test
9067 public void serverOptionsUnknown () throws Exception {
91- when (genApiDelegate .getServerOptions ("unknown" )).thenReturn (ResponseEntity .notFound ().build ());
9268 mockMvc .perform (get ("/api/gen/servers/unknown" ))
9369 .andExpect (status ().isNotFound ());
9470 }
@@ -102,26 +78,23 @@ private void getOptions(String type, String name) throws Exception {
10278
10379 @ Test
10480 public void generateClient () throws Exception {
105- when (genApiDelegate .generateClient (anyString (), any ())).thenReturn (ResponseEntity .ok (new ResponseCode ("test-code" , "http://test.com:1234/api/gen/download/test-code" )));
106- when (genApiDelegate .downloadFile ("test-code" )).thenReturn (ResponseEntity .ok ().contentType (org .springframework .http .MediaType .valueOf ("application/zip" )).header ("Content-Length" , "1024" ).body (new ByteArrayResource (new byte [1024 ])));
10781 generateAndDownload ("clients" , "java" );
10882 }
10983
11084 @ Test
11185 public void generateServer () throws Exception {
112- when (genApiDelegate .generateServerForLanguage (anyString (), any ())).thenReturn (ResponseEntity .ok (new ResponseCode ("test-code" , "http://test.com:1234/api/gen/download/test-code" )));
113- when (genApiDelegate .downloadFile ("test-code" )).thenReturn (ResponseEntity .ok ().contentType (org .springframework .http .MediaType .valueOf ("application/zip" )).header ("Content-Length" , "1024" ).body (new ByteArrayResource (new byte [1024 ])));
11486 generateAndDownload ("servers" , "spring" );
11587 }
11688
11789 private void generateAndDownload (String type , String name ) throws Exception {
11890 String result = mockMvc .perform (post ("http://test.com:1234/api/gen/" + type + "/" + name )
11991 .contentType (MediaType .APPLICATION_JSON )
12092 .content ("{\" openAPIUrl\" : \" " + OPENAPI_URL + "\" }" ))
93+ .andDo (print ())
12194 .andExpect (status ().isOk ())
12295 .andExpect (content ().contentType (MediaType .APPLICATION_JSON ))
123- .andExpect (jsonPath ("$.code" ).value ("test-code" ))
124- .andExpect (jsonPath ("$.link" ).value ("http://test.com:1234/api/gen/download/test-code" ))
96+ .andExpect (jsonPath ("$.code" ).value (matchesPattern ( UUID_REGEX ) ))
97+ .andExpect (jsonPath ("$.link" ).value (matchesPattern ( "http://test.com:1234/api/gen/download/" + UUID_REGEX ) ))
12598 .andReturn ().getResponse ().getContentAsString ();
12699
127100 String code = new ObjectMapper ().readValue (result , ResponseCode .class ).getCode ();
@@ -134,9 +107,6 @@ private void generateAndDownload(String type, String name) throws Exception {
134107
135108 @ Test
136109 public void generateWIthForwardedHeaders () throws Exception {
137- when (genApiDelegate .generateClient (anyString (), any ())).thenReturn (ResponseEntity .ok (new ResponseCode ("test-code" , "https://forwarded.com:5678/api/gen/download/test-code" )));
138- when (genApiDelegate .downloadFile ("test-code" )).thenReturn (ResponseEntity .ok ().contentType (org .springframework .http .MediaType .valueOf ("application/zip" )).header ("Content-Length" , "1024" ).body (new ByteArrayResource (new byte [1024 ])));
139-
140110 String result = mockMvc .perform (post ("http://test.com:1234/api/gen/clients/java" )
141111 .contentType (MediaType .APPLICATION_JSON )
142112 .header ("X-Forwarded-Proto" , "https" )
@@ -145,8 +115,8 @@ public void generateWIthForwardedHeaders() throws Exception {
145115 .content ("{\" openAPIUrl\" : \" " + OPENAPI_URL + "\" }" ))
146116 .andExpect (status ().isOk ())
147117 .andExpect (content ().contentType (MediaType .APPLICATION_JSON ))
148- .andExpect (jsonPath ("$.code" ).value ("test-code" ))
149- .andExpect (jsonPath ("$.link" ).value ("https://forwarded.com:5678/api/gen/download/test-code" ))
118+ .andExpect (jsonPath ("$.code" ).value (matchesPattern ( UUID_REGEX ) ))
119+ .andExpect (jsonPath ("$.link" ).value (matchesPattern ( "https://forwarded.com:5678/api/gen/download/" + UUID_REGEX ) ))
150120 .andReturn ().getResponse ().getContentAsString ();
151121
152122 String code = new ObjectMapper ().readValue (result , ResponseCode .class ).getCode ();
@@ -159,7 +129,6 @@ public void generateWIthForwardedHeaders() throws Exception {
159129
160130 @ Test
161131 public void generateClientWithInvalidOpenAPIUrl () throws Exception {
162- when (genApiDelegate .generateClient (anyString (), any ())).thenReturn (ResponseEntity .badRequest ().build ());
163132 final String invalidOpenAPIUrl = "https://[::1]/invalid_openapi.json" ;
164133 mockMvc .perform (post ("http://test.com:1234/api/gen/clients/java" )
165134 .contentType (MediaType .APPLICATION_JSON )
@@ -169,12 +138,6 @@ public void generateClientWithInvalidOpenAPIUrl() throws Exception {
169138
170139 @ Test
171140 public void generateWithOpenAPINormalizer () throws Exception {
172- when (genApiDelegate .generateClient (anyString (), any ()))
173- .thenReturn (ResponseEntity .ok (new ResponseCode ("test-code-1" , "http://test.com:1234/api/gen/download/test-code-1" )))
174- .thenReturn (ResponseEntity .ok (new ResponseCode ("test-code-2" , "http://test.com:1234/api/gen/download/test-code-2" )));
175- when (genApiDelegate .downloadFile ("test-code-1" )).thenReturn (ResponseEntity .ok ().contentType (org .springframework .http .MediaType .valueOf ("application/zip" )).header ("Content-Length" , "512" ).body (new ByteArrayResource (new byte [512 ])));
176- when (genApiDelegate .downloadFile ("test-code-2" )).thenReturn (ResponseEntity .ok ().contentType (org .springframework .http .MediaType .valueOf ("application/zip" )).header ("Content-Length" , "1024" ).body (new ByteArrayResource (new byte [1024 ])));
177-
178141 String withOpenAPINormalizer = "{\" openAPIUrl\" :\" https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml\" ,\" openapiNormalizer\" :[\" FILTER=operationId:updatePet\" ],\" options\" :{},\" spec\" :{}}" ;
179142 String withoutOpenAPINormalizer = "{\" openAPIUrl\" :\" https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml\" ,\" options\" :{},\" spec\" :{}}" ;
180143
0 commit comments