1+ /*
2+ Copyright 2019 The Alcor Authors.
3+
4+ Licensed under the Apache License, Version 2.0 (the "License");
5+ you may not use this file except in compliance with the License.
6+ You may obtain a copy of the License at
7+
8+ http://www.apache.org/licenses/LICENSE-2.0
9+
10+ Unless required by applicable law or agreed to in writing, software
11+ distributed under the License is distributed on an "AS IS" BASIS,
12+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ See the License for the specific language governing permissions and
14+ limitations under the License.
15+ */
16+
17+ package com .futurewei .alcor .privateipmanager .swagger ;
18+
19+ import org .springframework .boot .test .context .SpringBootTest ;
20+ import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
21+
22+ import org .springframework .mock .web .MockHttpServletResponse ;
23+ import org .springframework .beans .factory .annotation .Autowired ;
24+
25+ import org .springframework .test .web .servlet .MockMvc ;
26+ import org .springframework .test .web .servlet .MvcResult ;
27+
28+ import org .springframework .http .MediaType ;
29+ import org .junit .Test ;
30+ import org .junit .runner .RunWith ;
31+ import org .springframework .test .context .junit4 .SpringRunner ;
32+ import java .io .BufferedWriter ;
33+ import java .nio .charset .StandardCharsets ;
34+ import java .nio .file .Files ;
35+ import java .nio .file .Paths ;
36+
37+ import io .github .swagger2markup .Swagger2MarkupConverter ;
38+ import io .github .swagger2markup .Swagger2MarkupProperties ;
39+ import io .github .swagger2markup .Swagger2MarkupConfig ;
40+ import io .github .swagger2markup .builder .Swagger2MarkupConfigBuilder ;
41+
42+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
43+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
44+ import static org .springframework .test .web .servlet .result .MockMvcResultHandlers .print ;
45+
46+ @ RunWith (SpringRunner .class )
47+ @ SpringBootTest
48+ @ AutoConfigureMockMvc
49+ public class SwaggerJsonTest {
50+
51+ @ Autowired
52+ private MockMvc mockMvc ;
53+
54+ @ Test
55+ public void createSpringfoxSwaggerJson () throws Exception {
56+ String outputDir = System .getProperty ("io.springfox.staticdocs.outputDir" );
57+ MvcResult mvcResult = this .mockMvc .perform (get ("/v2/api-docs" )
58+ .accept (MediaType .APPLICATION_JSON ))
59+ .andExpect (status ().isOk ())
60+ .andReturn ();
61+
62+ MockHttpServletResponse response = mvcResult .getResponse ();
63+ String swaggerJson = response .getContentAsString ();
64+ Files .createDirectories (Paths .get (outputDir ));
65+ try (BufferedWriter writer = Files .newBufferedWriter (Paths .get (outputDir , "swagger.json" ), StandardCharsets .UTF_8 )){
66+ writer .write (swaggerJson );
67+ }
68+
69+ Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder ()
70+ .withGeneratedExamples ()
71+ .withInterDocumentCrossReferences ()
72+ .build ();
73+ Swagger2MarkupConverter .from (Paths .get (outputDir ,"swagger.json" ))
74+ .withConfig (config )
75+ .build ()
76+ .toFile (Paths .get (outputDir , "swagger" ));
77+ }
78+
79+ }
0 commit comments