package io.searchbox.indices;

import io.searchbox.client.JestResult;
import io.searchbox.common.AbstractIntegrationTest;
import io.searchbox.params.Parameters;
import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsRequest;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @author cihat keser
 */
@ElasticsearchIntegrationTest.ClusterScope(scope = ElasticsearchIntegrationTest.Scope.TEST, numNodes = 1)
public class FlushIntegrationTest extends AbstractIntegrationTest {

    private static final String INDEX_NAME = "flush_test_index";
    private static final String INDEX_NAME_2 = "flush_test_index_2";
    private static final String INDEX_NAME_3 = "flush_test_index_3";

    @Before
    public void setup() {
        createIndex(INDEX_NAME, INDEX_NAME_2, INDEX_NAME_3);
    }

    @Test
    public void testFlushAll() throws InterruptedException, ExecutionException, TimeoutException, IOException {
        Flush flush = new Flush.Builder().build();
        JestResult result = client.execute(flush);
        assertNotNull(result);
        assertTrue(result.isSucceeded());

        ActionFuture<IndicesStatsResponse> statsResponseFeature = client().admin().indices().stats(
                new IndicesStatsRequest().clear().flush(true).refresh(true));
        IndicesStatsResponse statsResponse = statsResponseFeature.get(10, TimeUnit.SECONDS);
        assertNotNull(statsResponse);
        assertEquals("There should be exactly one flush operation performed on this index",
                1, statsResponse.getIndex(INDEX_NAME).getTotal().getFlush().getTotal());
    }

    @Test
    public void testFlushSpecificIndices() throws InterruptedException, ExecutionException, TimeoutException, IOException {
        Flush flush = new Flush.Builder().addIndex(INDEX_NAME_2).addIndex(INDEX_NAME_3).build();
        JestResult result = client.execute(flush);
        assertNotNull(result);
        assertTrue(result.isSucceeded());

        ActionFuture<IndicesStatsResponse> statsResponseFeature = client().admin().indices().stats(
                new IndicesStatsRequest().clear().flush(true).refresh(true));
        IndicesStatsResponse statsResponse = statsResponseFeature.get(10, TimeUnit.SECONDS);
        assertNotNull(statsResponse);
        assertEquals("There should not be any flush operation performed on this index",
                0, statsResponse.getIndex(INDEX_NAME).getTotal().getFlush().getTotal());
        assertEquals("There should be exactly one flush operation performed on this index",
                1, statsResponse.getIndex(INDEX_NAME_2).getTotal().getFlush().getTotal());
        assertEquals("There should be exactly one flush operation performed on this index",
                1, statsResponse.getIndex(INDEX_NAME_3).getTotal().getFlush().getTotal());
    }

    /**
     * This test fails as I was not able to find a way to confirm
     * the refresh operation through stats api. It seems that the refresh
     * stats only count the explicit _refresh calls (even if a flush request
     * is sent via the native client, stats api does not count the refresh
     * triggered by that request - maybe that's by design).
     */
    @Ignore
    @Test
    public void testFlushAllWithRefresh() throws InterruptedException, ExecutionException, TimeoutException, IOException {
        Flush flush = new Flush.Builder().setParameter(Parameters.REFRESH, true).build();
        JestResult result = client.execute(flush);
        assertNotNull(result);
        assertTrue(result.isSucceeded());

        ActionFuture<IndicesStatsResponse> statsResponseFeature = client().admin().indices().stats(
                new IndicesStatsRequest().clear().flush(true).refresh(true));
        IndicesStatsResponse statsResponse = statsResponseFeature.get(10, TimeUnit.SECONDS);
        assertNotNull(statsResponse);
        assertEquals("There should be exactly one flush operation performed on this index",
                1, statsResponse.getIndex(INDEX_NAME).getTotal().getFlush().getTotal());
        assertEquals("Index should be refreshed when explicitly specified",
                2, statsResponse.getIndex(INDEX_NAME).getTotal().getRefresh().getTotal());
    }

}

@@ -776,17 +776,17 @@
 Nodes = 
-1
+2
 )%0Apublic
@@ -1151,16 +1151,77 @@
 AME_3);%0A
+        ensureGreen(INDEX_NAME, INDEX_NAME_2, INDEX_NAME_3);%0A
     %7D%0A%0A 
@@ -1521,37 +1521,24 @@
 );%0A%0A        
-ActionFuture%3C
 IndicesStats
@@ -1537,33 +1537,32 @@
 cesStatsResponse
-%3E
  statsResponseFe
@@ -1551,39 +1551,32 @@
 se statsResponse
-Feature
  = client().admi
@@ -1676,100 +1676,19 @@
 ue))
-;%0A        IndicesStatsResponse statsResponse = statsResponseFeature.get(10, TimeUnit.SECONDS
+.actionGet(
 );%0A 
@@ -1782,32 +1782,41 @@
 flush operation 
+per node 
 performed on thi
@@ -1833,33 +1833,33 @@
                 
-1
+2
 , statsResponse.
@@ -2281,37 +2281,24 @@
 );%0A%0A        
-ActionFuture%3C
 IndicesStats
@@ -2297,33 +2297,32 @@
 cesStatsResponse
-%3E
  statsResponseFe
@@ -2311,39 +2311,32 @@
 se statsResponse
-Feature
  = client().admi
@@ -2436,100 +2436,19 @@
 ue))
-;%0A        IndicesStatsResponse statsResponse = statsResponseFeature.get(10, TimeUnit.SECONDS
+.actionGet(
 );%0A 
@@ -2551,36 +2551,37 @@
 on performed on 
-this
+first
  index%22,%0A       
@@ -2733,36 +2733,38 @@
 on performed on 
-this
+second
  index%22,%0A       
@@ -2764,33 +2764,33 @@
                 
-1
+2
 , statsResponse.
@@ -2921,33 +2921,34 @@
 performed on thi
-s
+rd
  index%22,%0A       
@@ -2948,33 +2948,33 @@
                 
-1
+2
 , statsResponse.
@@ -4133,32 +4133,41 @@
 flush operation 
+per node 
 performed on thi
@@ -4192,17 +4192,17 @@
         
-1
+2
 , statsR
@@ -4349,33 +4349,33 @@
                 
-2
+3
 , statsResponse.

refactor FlushIntegrationTest