package com.lance.springfox;

import java.io.BufferedWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import com.lance.springfox.common.SwaggerConfig;

import io.github.robwin.markup.builder.MarkupLanguage;
import springfox.documentation.staticdocs.Swagger2MarkupResultHandler;

@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes={SimpleApplication.class, SwaggerConfig.class})
public class Swagger2MarkupTest {
	@Autowired
    private WebApplicationContext context;
    private MockMvc mockMvc;
    
    @Before
    public void setupMockMvc() {
        mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
    }

    @Test
    @Ignore
    public void convertSwaggerToAsciiDoc() throws Exception {
        this.mockMvc.perform(MockMvcRequestBuilders.get("/v2/api-docs?group=Admin API")
                .accept(MediaType.APPLICATION_JSON))
                .andDo(Swagger2MarkupResultHandler.outputDirectory("src/docs/asciidoc/generated").build())
                .andExpect(MockMvcResultMatchers.status().isOk());
    }

    @Test
    @Ignore
    public void convertSwaggerToMarkdown() throws Exception {
        this.mockMvc.perform(MockMvcRequestBuilders.get("/v2/api-docs?group=Admin API")
                .accept(MediaType.APPLICATION_JSON))
                .andDo(Swagger2MarkupResultHandler.outputDirectory("src/docs/markdown/generated")
                    .withMarkupLanguage(MarkupLanguage.MARKDOWN).build())
                .andExpect(MockMvcResultMatchers.status().isOk());
    }
    
    @Test
    @Ignore
    public void createSpringfoxSwaggerJson() throws Exception {
        String outputDir = "E:\\gitwork\\spring-boot-all\\spring-boot-springfox\\src\\docs\\";
        MvcResult mvcResult = this.mockMvc.perform(MockMvcRequestBuilders.get("/v2/api-docs?group=Admin API")
                .accept(MediaType.APPLICATION_JSON))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();

        MockHttpServletResponse response = mvcResult.getResponse();
        String swaggerJson = response.getContentAsString();
        Files.createDirectories(Paths.get(outputDir));
        try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(outputDir, "swagger.json"), StandardCharsets.UTF_8)){
            writer.write(swaggerJson);
        }
    }
}

@@ -1584,32 +1584,93 @@
 build();%0A    %7D%0A%0A
+    /**%0A     * %E7%94%9F%E6%88%90AsciiDoc%E6%96%87%E4%BB%B6%0A     * @throws Exception%0A     */%0A
     @Test%0A    @I
@@ -2055,24 +2055,85 @@
 ());%0A    %7D%0A%0A
+    /**%0A     * %E7%94%9F%E6%88%90MarkDown%E6%96%87%E4%BB%B6%0A     * @throws Exception%0A     */%0A
     @Test%0A  
@@ -2599,33 +2599,84 @@
    %0A    
-@Test%0A    @Ignore
+/**%0A     * %E7%94%9F%E6%88%90Swagger.json%0A     * @throws Exception%0A     */%0A    @Test
 %0A    pub
@@ -2822,14 +2822,36 @@
 rc%5C%5C
-docs%5C%5C
+main%5C%5Cresources%5C%5Cstatic%5C%5Capi
 %22;%0A 

add swagger2 json test