// Copyright (C) 2018 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.elasticsearch.testing;

import com.google.common.collect.ImmutableSet;
import java.util.Set;
import org.apache.http.HttpHost;
import org.junit.internal.AssumptionViolatedException;
import org.testcontainers.containers.GenericContainer;

/* Helper class for running ES integration tests in docker container */
public class ElasticContainer<SELF extends ElasticContainer<SELF>> extends GenericContainer<SELF> {
  private static final String NAME = "elasticsearch";
  private static final String VERSION = "2.4.6-alpine";
  private static final int ELASTICSEARCH_DEFAULT_PORT = 9200;

  public static ElasticContainer<?> createAndStart() {
    // Assumption violation is not natively supported by Testcontainers.
    // See https://github.com/testcontainers/testcontainers-java/issues/343
    try {
      ElasticContainer<?> container = new ElasticContainer<>();
      container.start();
      return container;
    } catch (Throwable t) {
      throw new AssumptionViolatedException("Unable to start container", t);
    }
  }

  private ElasticContainer() {
    this(NAME + ":" + VERSION);
  }

  private ElasticContainer(String dockerImageName) {
    super(dockerImageName);
  }

  @Override
  protected void configure() {
    addExposedPort(ELASTICSEARCH_DEFAULT_PORT);

    // https://github.com/docker-library/elasticsearch/issues/58
    addEnv("-Ees.network.host", "0.0.0.0");
  }

  @Override
  protected Set<Integer> getLivenessCheckPorts() {
    return ImmutableSet.of(getMappedPort(ELASTICSEARCH_DEFAULT_PORT));
  }

  public HttpHost getHttpHost() {
    return new HttpHost(getContainerIpAddress(), getMappedPort(ELASTICSEARCH_DEFAULT_PORT));
  }
}

@@ -1064,156 +1064,98 @@
 nal 
-String NAME = %22elasticsearch%22;%0A  private static final String VERSION = %222.4.6-alpine%22;%0A  private static final int ELASTICSEARCH_DEFAULT_PORT = 9200;
+int ELASTICSEARCH_DEFAULT_PORT = 9200;%0A%0A  public enum Version %7B%0A    V2,%0A    V5,%0A    V6%0A  %7D
 %0A%0A  
@@ -1203,16 +1203,31 @@
 ndStart(
+Version version
 ) %7B%0A    
@@ -1442,16 +1442,23 @@
 ainer%3C%3E(
+version
 );%0A     
@@ -1612,38 +1612,44 @@
 ;%0A    %7D%0A  %7D%0A%0A  p
-rivate
+ublic static
  ElasticContaine
@@ -1653,42 +1653,441 @@
 iner
-() %7B%0A    this(NAME + %22:%22 + VERSION
+%3C?%3E createAndStart() %7B%0A    return createAndStart(Version.V2);%0A  %7D%0A%0A  private static String getImageName(Version version) %7B%0A    switch (version) %7B%0A      case V2:%0A        return %22elasticsearch:2.4.6-alpine%22;%0A      case V5:%0A        return %22elasticsearch:5.6.9-alpine%22;%0A      case V6:%0A        return %22docker.elastic.co/elasticsearch/elasticsearch:6.2.4%22;%0A    %7D%0A    throw new IllegalStateException(%22Unsupported version: %22 + version.name()
 );%0A 
@@ -2121,30 +2121,23 @@
 ner(
-String dockerImageName
+Version version
 ) %7B%0A
@@ -2146,22 +2146,19 @@
   super(
-docker
+get
 ImageNam
@@ -2158,16 +2158,25 @@
 mageName
+(version)
 );%0A  %7D%0A%0A

Allow to specify the docker container version to create