package org.springframework.cloud.internal.asciidoctor;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.BDDAssertions.then;

class ReadmeMainTests {

	@Test
	void should_convert_input_with_include_of_external_sources_to_a_single_file() throws URISyntaxException, IOException {
		File inputFile = new File(ReadmeMainTests.class.getResource("/README.adoc").toURI());
		File outputFile = deleteIfExists(new File("target/output.adoc"));

		ReadmeMain.main(inputFile.getAbsolutePath(), outputFile.getAbsolutePath());

		then(new String(Files.readAllBytes(outputFile.toPath())))
			.doesNotContain("include:")
			.doesNotContain("Unresolved directive")
			.contains("DO NOT EDIT THIS FILE. IT WAS GENERATED")
			.contains("Hello world!");
		then(new File("target/output.html"))
			.as("Conversion to HTML must not happen").doesNotExist();
	}

	private File deleteIfExists(File outputFile) {
		if (outputFile.exists()) {
			outputFile.delete();
		}
		return outputFile;
	}
}