// Path: asciidoctorj-springboot-integration-test/src/test/java/org/asciidoctor/it/springboot/ProcessHelper.java
// static ProcessOutput run(Map<String, String> env, String... command) throws IOException, InterruptedException {
//     ProcessBuilder processBuilder = new ProcessBuilder(command)
//             .directory(new File("springboot-app"));
// 
//     processBuilder.environment().clear();
//     String javaHome = System.getProperty("java.home");
//     processBuilder.environment().put("JAVA_HOME", javaHome);
//     processBuilder.environment().put("PATH", javaHome + "/bin");
//     processBuilder.environment().putAll(env);
// 
//     Process process = processBuilder.start();
// 
//     final InputStream is = process.getInputStream();
//     final InputStream es = process.getErrorStream();
//     boolean status = process.waitFor(10l, TimeUnit.SECONDS);
//     if (!status) {
//         byte[] buffer = new byte[1800];
//         IOUtils.read(is, buffer);
//         return new ProcessOutput(process, new String(buffer), new String());
//     }
// 
//     return new ProcessOutput(IOUtils.toString(es), IOUtils.toString(is));
// }


import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import kotlin.collections.ArrayDeque;
import okhttp3.*;
import org.awaitility.Awaitility;
import org.junit.After;
import org.junit.Test;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
import static org.asciidoctor.it.springboot.ProcessHelper.run;
import static org.assertj.core.api.Assertions.assertThat;
                "\n" +
                "== First chapter\n" +
                "first chapter\n" +
                "\n" +
                "== Second chapter\n" +
                "second chapter";
        Map<String, String> jsonResponse = convertDocument(asciidocDocument, serverPort);

        assertThat(jsonResponse.get("contentType"))
                .isEqualTo("text/html");
        assertThat(base64Decode(jsonResponse.get("content")))
                .contains("<h1>Title</h1>")
                .contains("<p>first chapter</p>")
                .contains("<h2 id=\"_second_chapter\">Second chapter</h2>");
    }

    @After
    public void cleanup() {
        testProcesses.forEach(Process::destroy);
    }

    private Map<String, String> convertDocument(String asciidocDocument, int serverPort) throws IOException {
        String body = "{\"data\":\"" + base64Encode(asciidocDocument) + "\"}";

        Request request = new Request.Builder()
                .url("http://localhost:" + serverPort + "/asciidoc")
                .post(RequestBody.create(body, MediaType.parse("application/json")))
                .build();
        Response response = new OkHttpClient()
                .newCall(request)
// Path: asciidoctorj-springboot-integration-test/src/test/java/org/asciidoctor/it/springboot/ProcessHelper.java
// static ProcessOutput run(Map<String, String> env, String... command) throws IOException, InterruptedException {
//     ProcessBuilder processBuilder = new ProcessBuilder(command)
//             .directory(new File("springboot-app"));
// 
//     processBuilder.environment().clear();
//     String javaHome = System.getProperty("java.home");
//     processBuilder.environment().put("JAVA_HOME", javaHome);
//     processBuilder.environment().put("PATH", javaHome + "/bin");
//     processBuilder.environment().putAll(env);
// 
//     Process process = processBuilder.start();
// 
//     final InputStream is = process.getInputStream();
//     final InputStream es = process.getErrorStream();
//     boolean status = process.waitFor(10l, TimeUnit.SECONDS);
//     if (!status) {
//         byte[] buffer = new byte[1800];
//         IOUtils.read(is, buffer);
//         return new ProcessOutput(process, new String(buffer), new String());
//     }
// 
//     return new ProcessOutput(IOUtils.toString(es), IOUtils.toString(is));
// }


// Path: asciidoctorj-springboot-integration-test/src/test/java/org/asciidoctor/it/springboot/SpringBootTest.java
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import kotlin.collections.ArrayDeque;
import okhttp3.*;
import org.awaitility.Awaitility;
import org.junit.After;
import org.junit.Test;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
import static org.asciidoctor.it.springboot.ProcessHelper.run;
import static org.assertj.core.api.Assertions.assertThat;

                "\n" +
                "== First chapter\n" +
                "first chapter\n" +
                "\n" +
                "== Second chapter\n" +
                "second chapter";
        Map<String, String> jsonResponse = convertDocument(asciidocDocument, serverPort);

        assertThat(jsonResponse.get("contentType"))
                .isEqualTo("text/html");
        assertThat(base64Decode(jsonResponse.get("content")))
                .contains("<h1>Title</h1>")
                .contains("<p>first chapter</p>")
                .contains("<h2 id=\"_second_chapter\">Second chapter</h2>");
    }

    @After
    public void cleanup() {
        testProcesses.forEach(Process::destroy);
    }

    private Map<String, String> convertDocument(String asciidocDocument, int serverPort) throws IOException {
        String body = "{\"data\":\"" + base64Encode(asciidocDocument) + "\"}";

        Request request = new Request.Builder()
                .url("http://localhost:" + serverPort + "/asciidoc")
                .post(RequestBody.create(body, MediaType.parse("application/json")))
                .build();
        Response response = new OkHttpClient()
                .newCall(request)
                .execute();