package com.gentics.mesh.test.docker;

import java.nio.charset.Charset;
import java.time.Duration;
import java.util.Collections;

import org.apache.commons.io.IOUtils;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.Wait;
import org.testcontainers.images.builder.ImageFromDockerfile;

/**
 * Testcontainer for a non-clustered Elasticsearch instance.
 */
public class ElasticsearchContainer extends GenericContainer<ElasticsearchContainer> {

	public static final String VERSION = "6.3.1";

	public ElasticsearchContainer(boolean withIngestPlugin) {
		super(prepareDockerImage(withIngestPlugin));
	}

	private static ImageFromDockerfile prepareDockerImage(boolean withIngestPlugin) {
		try {
			ImageFromDockerfile dockerImage = new ImageFromDockerfile("elasticsearch", false);
			String dockerFile = IOUtils.toString(ElasticsearchContainer.class.getResourceAsStream("/elasticsearch/Dockerfile.ingest"),
				Charset.defaultCharset());
			dockerFile = dockerFile.replace("%VERSION%", VERSION);
			if (!withIngestPlugin) {
				dockerFile = dockerFile.replace("RUN", "#RUN");
			}
			dockerImage.withFileFromString("Dockerfile", dockerFile);
			return dockerImage;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	protected void configure() {
		addEnv("discovery.type", "single-node");
		withTmpFs(Collections.singletonMap("/usr/share/elasticsearch/data", "rw,size=64m"));
		// addEnv("xpack.security.enabled", "false");
		withExposedPorts(9200);
		withStartupTimeout(Duration.ofSeconds(250L));
		waitingFor(Wait.forHttp("/"));
	}

}

@@ -36,41 +36,8 @@
 r;%0A%0A
-import java.nio.charset.Charset;%0A
 impo
@@ -94,46 +94,8 @@
 s;%0A%0A
-import org.apache.commons.io.IOUtils;%0A
 impo
@@ -191,74 +191,33 @@
 ait.
-Wait;%0Aimport org.testcontainers.images.builder.ImageFromDockerfile
+strategy.HttpWaitStrategy
 ;%0A%0A/
@@ -417,9 +417,9 @@
  %226.
-3
+6
 .1%22;
@@ -491,675 +491,135 @@
 per(
-prepareDockerImage(withIngestPlugin));%0A%09%7D%0A%0A%09private static ImageFromDockerfile prepareDockerImage(boolean withIngestPlugin) %7B%0A%09%09try %7B%0A%09%09%09ImageFromDockerfile dockerImage = new ImageFromDockerfile(%22elasticsearch%22, false);%0A%09%09%09String dockerFile = IOUtils.toString(ElasticsearchContainer.class.getResourceAsStream(%22/elasticsearch/Dockerfile.ingest%22),%0A%09%09%09%09Charset.defaultCharset());%0A%09%09%09dockerFile = dockerFile.replace(%22%25VERSION%25%22, VERSION);%0A%09%09%09if (!withIngestPlugin) %7B%0A%09%09%09%09dockerFile = dockerFile.replace(%22RUN%22, %22#RUN%22);%0A%09%09%09%7D%0A%09%09%09dockerImage.withFileFromString(%22Dockerfile%22, dockerFile);%0A%09%09%09return dockerImage;%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09throw new RuntimeException(e);%0A%09%09%7D
+withIngestPlugin ? %22jotschi/elasticsearch-ingest:%22 + VERSION : %22docker.elastic.co/elasticsearch/elasticsearch-oss:%22 + VERSION);
 %0A%09%7D%0A
@@ -929,20 +929,38 @@
 For(
-Wait.forHttp
+new HttpWaitStrategy().forPath
 (%22/%22

Use pre-build docker image for ES and bump ES version to 6.6.1