// 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;

import com.google.gerrit.elasticsearch.ElasticTestUtils.ElasticNodeInfo;
import com.google.gerrit.server.query.change.AbstractQueryChangesTest;
import com.google.gerrit.testing.ConfigSuite;
import com.google.gerrit.testing.GerritTestName;
import com.google.gerrit.testing.InMemoryModule;
import com.google.gerrit.testing.IndexConfig;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.eclipse.jgit.lib.Config;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;

public class ElasticV6QueryChangesTest extends AbstractQueryChangesTest {
  @ConfigSuite.Default
  public static Config defaultConfig() {
    return IndexConfig.createForElasticsearch();
  }

  private static ElasticNodeInfo nodeInfo;
  private static ElasticContainer container;
  private static CloseableHttpAsyncClient client;

  @BeforeClass
  public static void startIndexService() {
    if (nodeInfo != null) {
      // do not start Elasticsearch twice
      return;
    }

    container = ElasticContainer.createAndStart(ElasticVersion.V6_8);
    nodeInfo = new ElasticNodeInfo(container.getHttpHost().getPort());
    client = HttpAsyncClients.createDefault();
    client.start();
  }

  @AfterClass
  public static void stopElasticsearchServer() {
    if (container != null) {
      container.stop();
    }
  }

  @After
  public void closeIndex() {
    client.execute(
        new HttpPost(
            String.format(
                "http://localhost:%d/%s*/_close", nodeInfo.port, getSanitizedMethodName())),
        HttpClientContext.create(),
        null);
  }

  @Rule public final GerritTestName testName = new GerritTestName();

  @Override
  protected void initAfterLifecycleStart() throws Exception {
    super.initAfterLifecycleStart();
    ElasticTestUtils.createAllIndexes(injector);
  }

  @Override
  protected Injector createInjector() {
    Config elasticsearchConfig = new Config(config);
    InMemoryModule.setDefaults(elasticsearchConfig);
    String indicesPrefix = testName.getSanitizedMethodName();
    ElasticTestUtils.configure(elasticsearchConfig, nodeInfo.port, indicesPrefix);
    return Guice.createInjector(new InMemoryModule(elasticsearchConfig));
  }
}

@@ -644,16 +644,70 @@
 earch;%0A%0A
+import static java.util.concurrent.TimeUnit.MINUTES;%0A%0A
 import c
@@ -2286,24 +2286,94 @@
 %0A    %7D%0A  %7D%0A%0A
+  @Rule public final GerritTestName testName = new GerritTestName();%0A%0A
   @After%0A  p
@@ -2395,16 +2395,33 @@
 eIndex()
+ throws Exception
  %7B%0A    c
@@ -2425,16 +2425,25 @@
   client
+%0A        
 .execute
@@ -2452,16 +2452,20 @@
         
+ 
+   
 new Http
@@ -2470,16 +2470,20 @@
 tpPost(%0A
+    
         
@@ -2517,16 +2517,20 @@
         
+    
 %22http://
@@ -2554,16 +2554,36 @@
 _close%22,
+%0A                   
  nodeInf
@@ -2590,16 +2590,25 @@
 o.port, 
+testName.
 getSanit
@@ -2635,16 +2635,20 @@
         
+ 
+   
 HttpClie
@@ -2679,88 +2679,47 @@
     
+    
 null)
-;
 %0A  
-%7D%0A%0A  @Rule public final GerritTestName testName = new GerritTestName();
+      .get(5, MINUTES);%0A  %7D
 %0A%0A  

Align with changes done in V7